Variables and Data Types
Learn how to store and manipulate data in Python.
Variables
Variables are containers for storing data values.
# Creating variables
name = "Alice"
age = 25
height = 1.65
is_student = True
print(f"{name} is {age} years old")
Naming Rules
- Must start with letter or underscore
- Can contain letters, numbers, underscores
- Case-sensitive (
name≠Name) - Cannot use Python keywords
Data Types
Numbers
# Integer
count = 42
# Float
price = 19.99
# Complex
z = 3 + 4j
# Arithmetic operations
result = count * 2 + price
Strings
# Single or double quotes
message = "Hello, World!"
name = 'Python'
# Multi-line strings
poem = """
Roses are red,
Violets are blue,
Python is awesome,
And so are you!
"""
# String operations
greeting = message + " Welcome to " + name
uppercase = message.upper()
length = len(message)
Boolean
is_valid = True
is_complete = False
# Comparison operators return boolean
result = 10 > 5 # True
check = "a" == "b" # False
None
# Represents absence of value
result = None
if result is None:
print("No result yet")
Type Conversion
# Convert between types
age_str = "25"
age_int = int(age_str)
price_float = 19.99
price_str = str(price_float)
# Check type
print(type(age_int)) # <class 'int'>
Practice Exercise
Create variables for: - Your name (string) - Your age (integer) - Your GPA or rating (float) - Whether you like Python (boolean)
Then print them all in a formatted string! 💻