beginner⏱30 minutesLesson 3 of 10
Variables and Data Types
Learn how to store data using variables and understand Python's core data types: int, float, string, and bool
Variables and Data Types
Variables are containers for storing data. Python is dynamically typed, meaning you don't need to declare a variable's type — Python infers it automatically.
Variables
python
name = "Alice" # String
age = 25 # Integer
height = 1.68 # Float
is_student = True # BooleanNaming Rules
- Must start with a letter or underscore
- Can contain letters, numbers, and underscores
- Case-sensitive (
age≠Age) - Cannot use Python keywords (like
if,for,while)
python
# Valid names
user_name = "Bob"
_user_id = 42
camelCase = "OK" # Allowed but not preferred
# Invalid names
2nd_place = "No" # Starts with number
my-var = "No" # Hyphen not allowed
class = "No" # Reserved keywordNaming Conventions (PEP 8)
python
# Use snake_case for variables and functions
user_age = 30
total_price = 99.99
# Use UPPER_CASE for constants
PI = 3.14159
MAX_SIZE = 100Note
Python follows PEP 8 style guide. Use snake_case for variables, not camelCase.
Numeric Types
Integers (int)
python
count = 10
negative = -5
big_number = 1_000_000 # Underscores improve readabilityFloats (float)
python
price = 19.99
pi = 3.14159
scientific = 1.5e-4 # 0.00015Type Conversion
python
# int to float
x = float(10) # 10.0
# float to int (truncates)
y = int(3.99) # 3
# string to int
z = int("42") # 42Strings (str)
python
# Single or double quotes
first = 'Hello'
second = "World"
# Multi-line strings
poem = """Roses are red,
Violets are blue,
Python is fun,
And so are you."""
# String concatenation
greeting = "Hello" + " " + "World"
# String interpolation (f-strings)
name = "Alice"
age = 25
message = f"{name} is {age} years old."
# String methods
text = " Python is FUN "
print(text.lower()) # " python is fun "
print(text.upper()) # " PYTHON IS FUN "
print(text.strip()) # "Python is FUN"
print(text.replace("FUN", "awesome")) # " Python is awesome "Booleans (bool)
python
is_active = True
is_finished = False
# Comparison operators return booleans
print(10 > 5) # True
print(3 == 4) # FalseNone Type
None represents the absence of a value:
python
result = None
print(result) # NoneChecking Types
python
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("Hello")) # <class 'str'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>Dynamic Typing
A variable's type can change:
python
x = 10 # x is int
x = "hello" # x is now str
x = 3.14 # x is now floatWarning
Dynamic typing is flexible but can cause bugs. Use meaningful variable names and be consistent with types.
Success
Remember: int for whole numbers, float for decimals, str for text, bool for true/false, and None for nothing.
Practice Questions
- What will
type(3.14)return? - Convert
"100"to an integer. - What's wrong with
2nd_place = "Bob"? - Write an f-string that says "Alice is 30 years old".
- What's the difference between
10and10.0? - What does
"hello".upper()return? - True or False: Python requires you to declare variable types.
- What does
Nonerepresent in Python? - Convert the float
99.9to an integer — what's the result? - Write a Python variable name using snake_case for a user's email address.
Progress30%