Python Basics

Python Variables and Data Types (str, int, float, bool)

Give values a name and let Python work out their type. Meet the four types you will use in almost every program.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Creating and updating variables
  • str, int, float and bool
  • Checking a type with type() and converting with int() or str()
Variable → value → type
name"Ada" → str
age36 → int
height1.68 → float
is_adminFalse → bool
type(age) returns <class 'int'>.

A name that points to a value

The problem

A program has to remember things: a name typed by a user, a price, whether a box is ticked. Writing the same value again and again is slow and breaks as soon as the value changes. A variable stores it once under a name you can reuse everywhere.

Think of it like this

A variable is like a label stuck on a storage box. The label (the name) stays the same while you can put different things inside. The kind of thing in the box decides what you can do with it: you can add numbers, but not add a word to a number.

From labeled boxes to code
name = "Ada"🏷️ A box labeled name holding a word (str)
age = 36🔢 A box holding a whole number (int)
height = 1.68📏 A box holding a decimal number (float)
is_student = False✅ A yes-or-no switch (bool)
age = age + 1🔄 Take the value out, change it, put it back
The label stays; the content and its kind can change.
In one sentence

A variable is a name that refers to a value, and the value's data type (such as str, int, float or bool) decides what operations you can use on it.

Key points

  1. name = "Ada" creates a variable. You never write the type; Python works it out from the value.
  2. type(age) shows the type, which is why the second line prints <class 'int'> for age.
  3. age = age + 1 reads the old value, adds 1 and stores the result under the same name.
  4. int("42"), str(3.5) and float(7) convert between types, which you need when text must become a number.

When should you use it?

  • Storing anything the program needs again later, such as a user's name or a running total.
  • Giving a clear name to a value, so tax_rate explains itself where 0.1 would not.
  • Converting input from text to numbers before doing arithmetic with it.

When not to use it

  • Keeping many related values such as 30 scores: use a list instead of score1, score2, ...
  • Keeping several facts about one thing: use a dictionary or a class so they stay together.

Example code

Run code →
main.py
# A variable is a name that refers to a value. Python works out the type for you.
name = "Ada"          # str: text
age = 36              # int: whole number
height = 1.68         # float: decimal number
is_student = False    # bool: True or False

print(name, age, height, is_student)
print(type(name), type(age), type(height), type(is_student))

# Variables can be reassigned, even to a value of another type.
age = age + 1
print("Next year:", age)

# Convert between types with int(), float(), str() and bool().
print(int("42") + 1)
print(str(3.5) + " stars")
print(float(7) / 2)
Get a Run button for this example

Result of running the code

Ada 36 1.68 False
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
Next year: 37
43
3.5 stars
3.5

Code explanation

type() shows a value's type. int(), float() and str() convert between types, which you need when text such as "42" must become a number.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Python data types at a glance

age = 30int: whole numbers
price = 9.99float: decimal numbers
name = "Ada"str: text
active = Truebool: True or False
result = NoneNone: no value
[1, 2], (1, 2), {"a": 1}, {1, 2}list, tuple, dict and set
type(age), isinstance(age, int)Check a value's type
int("42"), float("3.5"), str(42)Convert between types
a, b = 1, 2Assign several variables at once
a, b = b, aSwap two variables

Common mistakes with Python variables and types

Joining a string and a number with +

Python does not convert numbers to text automatically. Use an f-string or str().

Problem
age = 30
print("Age: " + age)  # TypeError
Fix
age = 30
print(f"Age: {age}")

Forgetting that input() returns a string

input() always returns text, so convert it before doing math.

Problem
age = input("Age: ")
print(age + 1)  # TypeError
Fix
age = int(input("Age: "))
print(age + 1)

Comparing floats with ==

Decimal numbers are stored in binary, so 0.1 + 0.2 is not exactly 0.3. Use math.isclose() or round().

Problem
print(0.1 + 0.2 == 0.3)  # False
Fix
import math
print(math.isclose(0.1 + 0.2, 0.3))  # True

Frequently asked questions

What are the basic data types in Python?

The built-in types you use most are int, float, str, bool and None, plus the collections list, tuple, dict and set.

Do I need to declare a variable's type in Python?

No. Python is dynamically typed: a variable gets its type from the value you assign. You can add type hints such as age: int = 30 for readability and tools, but Python does not enforce them.

How do I check the type of a variable?

Use type(x) to see the type, or isinstance(x, int) to test it. isinstance is usually better because it also accepts subclasses.

What is the difference between mutable and immutable types?

Mutable values, such as lists, dicts and sets, can change in place. Immutable values, such as int, float, str and tuple, cannot; operations on them create new values.

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges