29
Lesson 3 of 44

Data types

Explore the fundamentals of data types in programming in-depth in this lesson. Data types are essential elements in program creation, as they determine how information is stored and manipulated. Enhance your programming skills and understanding of data types through a series of practical exercises designed to reinforce your knowledge.

  • 11 theory articles
  • 18 practice exercises
Start lesson

Fundamentals concepts 11

Variables

Variables are an essential part of any programming language. A variable is a name that refers to a value stored in the computer's…

Theory
Read more

Fundamentals of data types

A data type defines what kind of value something is and which operations can be done with it. Python is dynamically typed (the…

Theory
Read more

Basic data types

These are the basic types that represent simple values. Unlike other languages, in Python they are objects like any other, and…

Theory
Read more

Type conversion

Sometimes you need to turn a value of one type into another, for example to do arithmetic with a number that the user typed. This…

Theory
Read more

Strings: indexes and slices

A string ( str ) is a sequence of characters. Almost everything a program shows is text, so it is worth mastering how to create…

Theory
Read more

String methods

Strings come with many built-in tools, called methods, to transform and examine them. You call them by writing a dot after the…

Theory
Read more

Formatting the output

Showing a result is not only about being correct: how it looks matters too. Python offers formatted strings, or f-strings, to…

Theory
Read more

Comparisons and logical values

Comparing values is the basis for a program to make decisions. Every comparison results in a logical value ( bool ): True or…

Theory
Read more

Random numbers with random

Games, simulations and tests often need random values. Python provides them in the random module, which is part of the standard…

Theory
Read more

Mutability, identity and copies

Everything in Python is an object , and a variable is just a name that points to an object. Understanding which objects can be…

Theory
Read more

Truth value and safe conversions

Any object can be used as a condition. Python converts it with bool() : falsy values are None , False , zeros ( 0 , 0.0 ), empty…

Theory
Read more

Practice exercises 18

Reverse chars

Write a program to ask the user for three letters and display them in reverse order.

Exercise Easy
Solve it

Simple geometric calculator

Write a Python program that requests the width (x) and height (y) of a rectangle and calculate the perimeter, area and diagonal.

Exercise Easy
Solve it

Speed unit converter

Create a program Python to ask the user for a distance in meters and the time taken (hours, minutes, seconds).

Exercise Easy
Solve it

Generate a random number

Create a Python program that prompts the user for 2 integers and displays a random number on the screen between those values.

Exercise Easy
Solve it

Swap two variables

Create a Python program that reads two texts and swaps them using variables, without writing them directly in the output.

Exercise Easy
Solve it

A number in several types

Create a Python program that reads a decimal number and shows it converted to other types.

Exercise Easy
Solve it

Initials

Create a Python program that reads a first name and a surname and shows their initials in capital letters separated by dots.

Exercise Easy
Solve it

Text profile

Create a Python program that reads a sentence and shows four facts about it.

Exercise Easy
Solve it

Spaces to hyphens

Create a Python program that turns a sentence into a hyphen-joined string and counts its words.

Exercise Easy
Solve it

Slices of a word

Create a Python program that reads a word of at least four letters and shows several pieces of it.

Exercise Easy
Solve it

Is it a palindrome?

A palindrome is a word that reads the same forwards and backwards, like level . Create a Python program that checks it.

Exercise Medium
Solve it

Comparison table

Create a Python program that compares two integers with all the comparison operators.

Exercise Easy
Solve it

Invoice with format

Create a Python program that calculates the total of a purchase and shows it formatted as money.

Exercise Easy
Solve it

Digits of a number

Create a Python program that separates the digits of a three-digit number using only arithmetic operations.

Exercise Medium
Solve it

Spot the alias

The is operator checks identity (is it the same object?) and == checks equality (do they have the same value?). A copy is equal…

Exercise Medium
Solve it

Shallow versus deep copy

With nested lists, a shallow copy shares the inner lists with the original; a deep copy duplicates them too.

Exercise Hard
Solve it

Truthy or falsy

In Python every value has a truth value : None , False , zero, empty strings and empty collections are falsy; everything else is…

Exercise Medium
Solve it

Safe conversion to integer

int() raises ValueError if the text is not an integer. A function can catch that error and return None to signal that the…

Exercise Medium
Solve it