What Are Variables?

A variable is a named container that stores a value. Think of it as a labeled box where you put data that your program needs to remember and use later.

name = "Alice"
age = 25
height = 1.68

In Python, you create a variable simply by assigning a value to a name using the = sign. No special keywords or type declarations needed.

💡
Variable naming rules

Variable names must start with a letter or underscore, can contain letters, numbers, and underscores, and are case-sensitive (Name and name are different variables). Use descriptive names like user_age instead of x.

Strings

Strings are sequences of characters, used for text. They're enclosed in quotes — either single (') or double ("):

greeting = "Hello, World!"
name = 'Alice'
message = "It's a beautiful day"

String Operations

# Concatenation (joining strings)
first = "Hello"
second = "World"
combined = first + " " + second    # "Hello World"

# Repetition
laugh = "ha" * 3                   # "hahaha"

# Length
length = len("Python")             # 6

# Accessing characters (0-indexed)
word = "Python"
first_char = word[0]               # "P"
last_char = word[-1]               # "n"

f-Strings (Formatted Strings)

The modern way to embed variables inside strings:

name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
# Output: My name is Alice and I am 25 years old.

Numbers

Python has two main number types:

Integers (int)

Whole numbers without decimal points:

age = 25
year = 2026
negative = -10
big_number = 1_000_000    # underscores for readability

Floats (float)

Numbers with decimal points:

height = 1.68
temperature = -3.5
pi = 3.14159

Arithmetic Operations

a = 10
b = 3

print(a + b)     # 13    Addition
print(a - b)     # 7     Subtraction
print(a * b)     # 30    Multiplication
print(a / b)     # 3.333 Division (always returns float)
print(a // b)    # 3     Integer division (rounds down)
print(a % b)     # 1     Modulus (remainder)
print(a ** b)    # 1000  Exponentiation (power)
⚠️
Division always returns a float

10 / 2 returns 5.0 (not 5). Use // for integer division if you need a whole number result.

Booleans

Booleans represent truth values — either True or False:

is_student = True
is_admin = False

# Comparison operators return booleans
print(5 > 3)       # True
print(10 == 20)    # False
print(5 != 3)      # True
print(5 >= 5)     # True

Boolean Operations

a = True
b = False

print(a and b)     # False (both must be True)
print(a or b)      # True  (at least one must be True)
print(not a)       # False (inverts the value)

Type Checking and Conversion

Use type() to check what type a variable is:

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

Convert between types:

# String to integer
age_str = "25"
age_num = int(age_str)      # 25

# Integer to string
count = 42
count_str = str(count)      # "42"

# String to float
price = float("19.99")     # 19.99

# Float to integer (truncates, doesn't round)
whole = int(3.7)            # 3
⚠️
Invalid conversions cause errors

int("hello") will crash your program with a ValueError. Only convert strings that actually contain valid numbers.

User Input

Get input from the user with the input() function:

name = input("What is your name? ")
print(f"Hello, {name}!")

# input() always returns a string, so convert for numbers:
age = int(input("How old are you? "))
print(f"In 10 years you'll be {age + 10}.")

Summary

  • Variables store data using name = value syntax
  • Strings hold text: "hello"
  • Integers hold whole numbers: 42
  • Floats hold decimals: 3.14
  • Booleans hold True/False values
  • Use type() to check types and int(), str(), float() to convert
  • f-strings let you embed variables in text: f"Hello {name}"
🎉
Variables mastered!

You now understand how Python stores and manipulates data. Next up: control flow — making your programs make decisions with if, else, and loops.