3. Data types

Quiz: 0/6

Think of data types as different kinds of containers you use to hold specific types of information in Python. Just like you use a cup for water, a bowl for soup, and a plate for food, Python has different "containers" for different kinds of data.

Basic data types

Strings str

A string is a collection of characters arranged in a specific order, like letters, numbers, symbols, or even spaces. It's essentially how Python handles and stores text-based data. Think of it as a way to represent words, phrases, sentences, or any sequence of characters that you might use in a program.

 

In Python, strings are created by enclosing the characters within either single quotes ' or double quotes ". Both types of quotes work the same way, so you can use whichever you prefer. For example, "Hello" and 'Hello' are both strings. If your string contains one type of quote, you can use the other type to avoid confusion or errors.

Examples:

name = "Alice"
greeting = 'Hello, world!'

Use case: When you need to store text, names, or messages.

Integers int

An integer is a whole number, meaning it has no fractional or decimal part. Integers can be positive, negative, or even zero. They are used to represent quantities, counts, or values where only whole numbers make sense, like the number of apples in a basket or a person’s age in years.

 

Integers are written as plain numbers without any decimal point or special symbols. For example, 42, -15, and 0 are all integers. You don’t need to use quotes around them because Python recognizes them as numeric values automatically. They are one of the most common data types used in programming for mathematical calculations and logical comparisons.

Examples:

age = 25
year = 2024

Use case: When you want to count things or work with whole numbers.

Floats float

A float, short for "floating-point number," is a number that can represent values with a decimal point. Floats are used when you need to work with more precise quantities, such as measurements, prices, or scientific data, where fractions of a number are important. Floats can be both positive and negative, and they include any number that isn't a whole number.

 

A float is written with a decimal point. For example, 3.14, -7.5, and 0.99 are all floats. Floats can also be written in scientific notation (e.g., 1.5e2 for 150), but the key is that they always include a decimal point or are in a form that Python understands as a non-whole number.

Examples:

price = 19.99
temperature = 36.5

Use case: When you need to work with measurements, prices, or calculations that need decimals.

Booleans bool

A boolean is a data type that represents one of two possible values: True or False. It's used in Python to handle logical operations and to make decisions in your program. Booleans are often the result of comparisons (like checking if two numbers are equal) or conditions (like whether something is true or false), helping your program choose what actions to take.

 

Booleans are written as True or False, with the first letter capitalized. These values are not enclosed in quotes because they are special constants in Python. For example, is_sunny = True or is_raining = False are boolean variables. You'll often use booleans in conditional statements (like if and while) to control the flow of your program.

Examples:

is_raining = False
has_ticket = True

Use case: When you want to check if something is true or false, like for making decisions in your program.

Why these data types matter

Python needs to know the type of data you're working with so it can handle it correctly. For example:

You can add integers 2 + 35 but not a string and an integer "hello" + 5 → error!.

Hands-On Practise

Exercise: Data types in Python

In this exercise, you'll practice creating variables of different data types (string, integer, float, and boolean) and perform simple operations with them.

Instructions:

  1. Create a variable called name and store your name as a string. Then, print a greeting message using your name.
  2. Create a variable called age and store your age as an integer. Then, print a message saying "I am [your age] years old."
  3. Create a variable called price and store a price (e.g., 19.99) as a float. Then, print the message "The price is [price] dollars."
  4. Create a variable called is_student and store either True or False depending on whether you're a student. Then, print a message saying "I am a student: [True/False]."

Bonus (Optional):

Try performing some operations with your variables. For example:

  1. Add a number to your age.
  2. Multiply the price by 2.
  3. Combine two strings together.
Output:

Quizzes: 0/6

Question 1:

What is a string in Python?

  • A sequence of characters enclosed in quotes.
  • A whole number without a decimal point.
  • A number with a decimal point.
  • A value that is either True or False.

Question 2:

Which of the following is an example of an integer in Python?

  • "42"
  • 42.0
  • 42
  • True

Question 3:

What is a float in Python?

  • A number without a decimal point.
  • A boolean value like True or False.
  • A number with a decimal point or in scientific notation.
  • A sequence of characters enclosed in quotes.

Question 4:

Which of these statements correctly represents a boolean in Python?

  • is_raining = 'False'
  • is_sunny = True
  • has_ticket = "True"
  • status = 1

Question 5:

Why does the following operation result in an error? "hello" + 5

  • You cannot add two integers.
  • You cannot concatenate a string and an integer.
  • The operation is missing a space between the string and the integer.
  • Strings cannot be concatenated with anything.

Question 6:

Which of these data types is used for storing a decimal number?

  • str
  • int
  • float
  • bool

Feedback

Share your thoughts about this lesson

Discussion

Please log in to create a discussion.
Previous lessonVariables and AssignmentNext lessonType conversion