🌱BeginnerPython Fundamentals
Variables and Data Types
Creating variables, Python's core data types, and type conversion.
Overview
A variable in Python is a name that refers to a value stored in memory. Unlike many languages, you do not declare a type for a variable Python infers it from the value you assign. You can even reassign the same variable to a completely different type later (though doing so without good reason is generally considered bad practice).
Python has four fundamental built-in data types you will use in almost every program:
- **int** whole numbers: 0, 42, -7, 1_000_000 (underscores for readability)
- **float** decimal numbers: 3.14, -0.5, 1.0e10 (scientific notation)
- **str** text: "hello", 'world', """multi-line"""
- **bool** True or False (capital T and F)
Beyond these primitives, Python also has complex collection types (list, tuple, dict, set) which you will learn in later topics. The built-in functions type() and isinstance() let you inspect what type a value has at runtime.
Key Points
- •Assignment uses = : `x = 10` stores 10 and binds it to the name x
- •Python is dynamically typed: the type is attached to the value, not the variable name
- •int can be arbitrarily large in Python no overflow as in many other languages
- •float uses IEEE 754 double precision; be aware of rounding: 0.1 + 0.2 != 0.3 exactly
- •Strings can use single quotes, double quotes, or triple quotes (for multi-line text)
- •type() returns the type of a value; isinstance(x, int) checks if x is an int
- •int(), float(), str(), bool() are type-conversion functions
Examples
Creating variables of different types
age = 25 # int
price = 9.99 # float
name = "Alice" # str
is_active = True # bool
print(type(age)) # <class 'int'>
print(type(price)) # <class 'float'>
print(type(name)) # <class 'str'>
print(type(is_active)) # <class 'bool'>Multiple assignment and swapping
# Assign multiple variables on one line
x, y, z = 1, 2, 3
print(x, y, z) # 1 2 3
# Swap without a temp variable
a, b = 10, 20
a, b = b, a
print(a, b) # 20 10This tuple unpacking syntax is very Pythonic and widely used.
Type conversion (casting)
n = int("42") # str → int
f = float("3.14") # str → float
s = str(100) # int → str
b = bool(0) # 0 → False, any non-zero → True
print(n, type(n)) # 42 <class 'int'>
print(b) # False
print(bool("")) # False (empty string is falsy)
print(bool("hi")) # TrueCommon Mistakes
- Confusing = (assignment) with == (equality check): `if x = 5` is a SyntaxError in Python
- Integer division surprise: `7 / 2` gives 3.5 in Python 3 (true division); use `7 // 2` for 3
- Forgetting that bool is a subclass of int: True == 1 and False == 0 in Python
Note
Use descriptive variable names in snake_case (my_variable, total_price). Python style guide PEP 8 recommends this, and it makes your code far more readable than single-letter names.
Exercises
Write Python code to solve each exercise. Run it in your browser and check your output.
Loading editor…
Completed: 0 / 5 exercises
Knowledge Check
Question 1 of 4