Chapter 2: Variables and Data Typesยถ
๐ Open Notebookยถ
๐บ Video Tutorialยถ
Learn Python VARIABLES in 10 minutes! โ (10:20)
๏ฟฝ๐ What Youโll Learnยถ
Variables are fundamental in programming - they let you store and manipulate data in your programs!
๐ฏ Learning Objectivesยถ
Understand what variables are and how to create them
Learn about Pythonโs basic data types (str, int, float, bool)
Practice using f-strings to format output
Combine variables with conditional statements
๐ Concept Explanationยถ
What are Variables?ยถ
Variables are named containers that store data values. Think of them as labeled boxes where you can put different types of information.
name = "Alice" # name is the variable, "Alice" is the value
age = 25 # age stores the number 25
Pythonโs Basic Data Typesยถ
1. String (str) - Text Dataยถ
name: str = "John"
message: str = "Hello, World!"
Strings hold text
Must be enclosed in quotes (single or double)
2. Integer (int) - Whole Numbersยถ
age: int = 20
quantity: int = 100
Integers are whole numbers (no decimals)
Can be positive or negative
3. Float (float) - Decimal Numbersยถ
price: float = 19.99
pi: float = 3.14159
Floats are numbers with decimal points
Used for precise calculations
4. Boolean (bool) - True or Falseยถ
is_student: bool = True
is_available: bool = False
Booleans can only be
TrueorFalseUsed for logic and conditions
Type Annotationsยถ
The : type syntax is called a type hint:
name: str = "Alice" # Explicitly states this is a string
Type hints are optional but make code more readable!
F-Strings (Formatted Strings)ยถ
F-strings let you embed variables directly in 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
๐ก Examplesยถ
Creating Variablesยถ
# String variable
favorite_food: str = "Pizza"
# Integer variable
student_count: int = 30
# Float variable
temperature: float = 72.5
# Boolean variable
is_raining: bool = False
Using Variablesยถ
first_name = "John"
last_name = "Doe"
full_name = f"{first_name} {last_name}"
print(full_name) # Output: John Doe
โ๏ธ Practice Exercisesยถ
Create variables for your name, age, height (in meters), and whether you like programming
Print all these variables using f-strings
Create a variable called
temperatureand use it in an if-else statement to check if itโs hot (> 30) or coldCreate variables for a product (name, price, quantity) and calculate the total cost
๐ Common Mistakesยถ
Forgetting quotes around strings:
name = Aliceโ should bename = "Alice"โUsing spaces in variable names:
first name = "John"โ should befirst_name = "John"โStarting variable names with numbers:
1st_nameโ should befirst_nameโ
๐ Variable Naming Rulesยถ
Can contain letters, numbers, and underscores
Must start with a letter or underscore
Case-sensitive (
ageandAgeare different)Use snake_case for multi-word variables:
first_name,student_count
๐ Try It Yourselfยถ
Modify main.py to create variables about yourself and print them in creative ways!
๐ Key Takeaways from Videoยถ
Variables store data values that can be reused
Use if-elif-else for conditional logic
Follow along with the video for hands-on practice
๐ก These points cover the main concepts from the video tutorial to help reinforce your learning.
๐ Next Chapterยถ
Continue to Chapter 3: Type Casting to learn how to convert between data types!