Chapter 2: Variables and Data Typesยถ

๐Ÿš€ Open Notebookยถ

Open In Colab Open In Kaggle

๐Ÿ“บ Video Tutorialยถ

Watch on YouTube

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 True or False

  • Used 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ยถ

  1. Create variables for your name, age, height (in meters), and whether you like programming

  2. Print all these variables using f-strings

  3. Create a variable called temperature and use it in an if-else statement to check if itโ€™s hot (> 30) or cold

  4. Create variables for a product (name, price, quantity) and calculate the total cost

๐Ÿ” Common Mistakesยถ

  • Forgetting quotes around strings: name = Alice โŒ should be name = "Alice" โœ…

  • Using spaces in variable names: first name = "John" โŒ should be first_name = "John" โœ…

  • Starting variable names with numbers: 1st_name โŒ should be first_name โœ…

๐Ÿ“ Variable Naming Rulesยถ

  • Can contain letters, numbers, and underscores

  • Must start with a letter or underscore

  • Case-sensitive (age and Age are 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ยถ

  1. Variables store data values that can be reused

  2. Use if-elif-else for conditional logic

  3. 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!