Python for Absolute Beginners: Learn to Code Without Losing Your Mind
Section 6 of 21

Python Data Types: Numbers, Strings, and Lists Explained

3 min read Updated

Variables hold values, but those values come in different types. This matters, because Python behaves differently depending on what kind of data it's dealing with.

Think of it like this: you can add two numbers together and get a third number. But what does it mean to "add" two photos together? Or to divide a person's name by 3? Different types of data support different operations, and Python needs to know what type it's dealing with to make sensible decisions.

The Four Basic Types

1. Integers (int)

Whole numbers. No decimal point. Can be positive or negative.

age = 17
temperature = -5
population = 8000000000
bananas_eaten_today = 0

Python integers can be arbitrarily large — unlike some other languages, Python doesn't cap out at some maximum number. You want to store a 100-digit number? Fine. Python doesn't care.

2. Floats (float)

Numbers with decimal points. Called "float" because of the way computers store them (the decimal point "floats" — it can be in different positions).

pi = 3.14159
price = 9.99
grade = 91.5
very_small = 0.0000001

A word of warning: floating-point arithmetic can sometimes give you slightly weird results due to how computers store decimals in binary. For example:

print(0.1 + 0.2)
# Might print: 0.30000000000000004

This isn't a bug in your code — it's a quirk of how all computers (not just Python) handle decimal numbers. For most purposes, you can ignore this. For things like financial calculations, there are special tools to handle it precisely. But for now, just know it's a thing that exists.

3. Strings (str)

Text. Any sequence of characters surrounded by quotes. Both single quotes and double quotes work.

name = "Alice"
message = 'Hello there!'
empty_string = ""
long_text = "This is a string. It can be quite long. It can have numbers like 42 in it."

We have a whole section on strings coming up, because they're incredibly important and incredibly useful.

4. Booleans (bool)

The simplest type. A boolean is either True or False. That's it. Two options. Named after George Boole, a 19th-century mathematician who invented the branch of algebra that makes all of computer science possible.

is_raining = True
has_umbrella = False
is_tired = True
wants_coffee = True  # Always.

Booleans might seem too simple to be useful, but they're absolutely everywhere in programming. Every time Python makes a decision (like "should I run this code or not?"), it's evaluating something to True or False.

Checking What Type Something Is

Python lets you check the type of a value with the type() function:

print(type(42))          # <class 'int'>
print(type(3.14))        # <class 'float'>
print(type("hello"))     # <class 'str'>
print(type(True))        # <class 'bool'>

You'll use this occasionally when you're confused about what type some variable is, which happens more often than you'd think.

Type Conversion: Changing Types

Sometimes you have a value of one type and you need it in another type. Python provides built-in functions for this:

# String to integer
age_text = "25"
age_number = int(age_text)  # Now it's the number 25
print(age_number + 1)       # Works! Prints 26

# Integer to string
score = 100
score_text = str(score)     # Now it's the text "100"
print("Your score: " + score_text)  # Works!

# String to float
price_text = "9.99"
price = float(price_text)
print(price * 2)            # Prints 19.98

# Integer to float
whole_number = 5
as_float = float(whole_number)  # 5.0

This comes up constantly when dealing with user input, because when a user types something, Python receives it as a string — even if they typed a number. More on this when we get to input().

What Happens When Types Collide?

Python is protective of types. It won't let you do operations that don't make sense:

age = 25
message = "I am " + age + " years old"  # ERROR!

This will crash with a TypeError, because Python doesn't know how to add a string and an integer. You need to convert explicitly:

age = 25
message = "I am " + str(age) + " years old"  # Works!
print(message)  # I am 25 years old

Or better yet, use f-strings (coming up next section!), which handle this automatically.

A Quick Visualization

Here's a useful mental picture of how the four basic types relate to each other:

mindmap
  root((Data Types))
    int
      Whole numbers
      No decimal point
      Example: 42, -7, 1000
    float
      Decimal numbers
      Example: 3.14, -0.5, 99.9
    str
      Text in quotes
      Example: "hello", "123"
      Can be empty ""
    bool
      Only True or False
      Used for decisions
      Named after George Boole