Unlike some other languages, Python doesn’t require you to declare a variable’s type. Python figures it out automatically!
Variable Naming Rules
Rule
Valid Examples
Invalid Examples
Must start with letter or underscore
name, _count
1name, @value
Can contain letters, numbers, underscores
user_1, total_sum
user-1, total.sum
Case-sensitive
Name ≠ name
-
Cannot be Python keywords
my_class
class, if, for
Naming Conventions
Python programmers follow these conventions:
naming.py
1
2
3
4
5
6
7
8
9
10
# Good variable names (snake_case)first_name ="John"total_price =99.99is_active =Trueuser_count =42# Avoid these styles in PythonfirstName ="John"# This is camelCase (used in other languages)TOTALITEMS =10# ALL CAPS is for constantsx ="John"# Too short, not descriptive
Best Practice
Use descriptive names that explain what the variable holds. user_age is much better than x or ua.
Python Data Types
Python has several built-in data types. Let’s explore the most common ones:
flowchart TB
A[Python Data Types] --> B[Numeric]
A --> C[Text]
A --> D[Boolean]
A --> E[None]
B --> F[int - integers]
B --> G[float - decimals]
C --> H[str - strings]
D --> I[True/False]
Integers (int)
Whole numbers without decimal points:
integers.py
1
2
3
4
5
6
age =25year =2024negative_number =-10large_number =1_000_000# Underscores for readabilityprint(type(age)) # Output: <class 'int'>
Floats can have precision issues. 0.1 + 0.2 equals 0.30000000000000004, not 0.3. For financial calculations, use the decimal module.
Strings (str)
Text data enclosed in quotes:
strings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Single or double quotes work the samename ='Alice'greeting ="Hello, World!"# Triple quotes for multi-line stringsmessage ="""This is a
multi-line
string."""# String with quotes insidequote ="She said 'Hello'"another ='He replied "Hi"'print(type(name)) # Output: <class 'str'>
Booleans (bool)
True or False values:
booleans.py
1
2
3
4
5
6
7
8
is_active =Trueis_logged_in =False# Booleans from comparisonsis_adult = age >=18# True if age is 18 or morehas_permission =Trueand is_active
print(type(is_active)) # Output: <class 'bool'>
None Type
Represents the absence of a value:
none.py
1
2
3
result =None# Variable exists but has no value yetprint(type(result)) # Output: <class 'NoneType'>
# String to Integerage_str ="25"age_int =int(age_str)
print(age_int +5) # Output: 30# Integer to Stringnumber =42number_str =str(number)
print("The answer is "+ number_str)
# String to Floatprice_str ="19.99"price_float =float(price_str)
print(price_float *2) # Output: 39.98# Float to Integer (truncates decimal)temperature =23.7temp_int =int(temperature)
print(temp_int) # Output: 23 (not rounded!)
Conversion Errors
Not all conversions work! int("hello") will cause an error because “hello” isn’t a valid number.
Conversion Reference
Function
Converts To
Example
int()
Integer
int("42") → 42
float()
Float
float("3.14") → 3.14
str()
String
str(100) → "100"
bool()
Boolean
bool(1) → True
Working with Strings
Strings are one of the most used data types. Here are some common operations:
String Concatenation
string_concat.py
1
2
3
4
5
6
7
8
9
10
first_name ="John"last_name ="Doe"# Using + operatorfull_name = first_name +" "+ last_name
print(full_name) # Output: John Doe# Using f-strings (recommended!)greeting =f"Hello, {first_name}{last_name}!"print(greeting) # Output: Hello, John Doe!
F-Strings (Formatted String Literals)
fstrings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
name ="Alice"age =25height =5.6# Embed variables directly in stringsmessage =f"My name is {name}, I'm {age} years old."print(message)
# You can even do calculationsprint(f"In 10 years, I'll be {age +10}")
# Format numbersprice =19.999print(f"Price: ${price:.2f}") # Output: Price: $19.99
Use F-Strings!
F-strings (introduced in Python 3.6) are the modern way to format strings. They’re readable and efficient.
Multiple Assignment
Python lets you assign multiple variables at once:
multiple_assignment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Assign same value to multiple variablesx = y = z =0print(x, y, z) # Output: 0 0 0# Assign different values in one linename, age, city ="Alice", 25, "New York"print(name) # Output: Aliceprint(age) # Output: 25print(city) # Output: New York# Swap variables (Python magic!)a =1b =2a, b = b, a
print(a, b) # Output: 2 1
Summary
Today you learned:
Variables store data with meaningful names
Python has several data types: int, float, str, bool, None
Use type() to check data types
Convert between types with int(), float(), str(), bool()
F-strings are the best way to format strings
Practice Exercise
Try creating these variables:
Your name as a string
Your age as an integer
Your height in meters as a float
Whether you’re a student as a boolean
Use an f-string to print all information
Next Steps
Tomorrow, we’ll learn about operators and expressions - how to perform calculations and make comparisons in Python.
Complete the quiz below to test your knowledge!
Quiz
Complete this quiz with a minimum score of 80% to mark Day 2 as complete.
Loading quiz...
Discussion
Have questions or want to discuss this lesson? Join the conversation below!
Settings
Appearance
Cloud Sync
Sync StatusChecking...
Link or Sign In to sync progress across devices
Data
This will delete all your progress for this course
Discussion
Have questions or want to discuss this lesson? Join the conversation below!