PythonBit Logo

2. Python Variables

Learn how to store and work with different types of data in Python


Level Up with Python Variables and Data Types! 🎮

Hey coding champions! Remember how you made your computer say hello? Now we're going to learn how to make it remember things! 🧠

🚀 Today's Agenda 🚀

1. What are Variables? 📦

Think of variables like your own digital backpack - they're containers where you can store different things! Just like how you might have:

  • A pocket for your phone 📱
  • A space for your books 📚
  • A special spot for your snacks 🍪

In Python, variables are like these pockets where we can store different types of information!

Exercise 1 • python101-2-variables-types
No output yet...

2. The Cool Rules for Naming Your Variables 📝

Just like how you can't name a pet "123" (imagine calling that at the park! 😅), Python has some rules for naming variables:

  1. Start with a letter or underscore (_)
  2. Can use letters, numbers, and underscores
  3. NO SPACES allowed! (use_underscores_instead)
  4. Names are case-sensitive (myName and myname are different!)
good_name = "Perfect!"
123_bad_name = "Nope!"
my space = "Won't work!"

3. Different Types of Data: Python's Collection 🎨

Imagine your digital backpack has special pockets that only hold certain types of things - just like how you wouldn't put a juice box in your pencil case! Let's discover Python's special containers:

3.1. Integers (int): The Whole Numbers Gang 🔢

These are your everyday counting numbers! No decimals allowed in this club.

  • Ages (like 10, 11, 12)
  • Number of pets (1, 2, 3)
  • High score in your favorite game (1000!)

When we want to combine numbers with text using +, we need to use a special magic spell called str(). It's like giving our numbers a special costume so they can join the text party! 🎉

Fix this code by adding str(12):

Exercise 2 • python101-2-variables-types
No output yet...

Fix this code by adding the following to your code: str() where needed.

Exercise 3 • python101-2-variables-types
No output yet...

3.2. Floating Points (float): The Decimal Squad 🎯

These numbers love to play with decimal points! They're perfect for:

  • Your height (5.5 feet)
  • Temperature (72.3 degrees)
  • Pizza slices (you can have 2.5 slices!)
  • Money in your piggy bank ($10.25)

Just like integers, floats need the str() spell to play nice with text strings!

Exercise 4 • python101-2-variables-types
No output yet...

3.3. Strings (str): The Text Team 📝

Strings are like text messages - they can hold letters, words, sentences, and even emojis!

  • Names ("Alex", "Sam")
  • Secret messages ("Meet me at lunch!")
  • Favorite colors ("blue")
  • Even numbers in quotes ("42" - but you can't do math with these!)

Pro Tip: Strings need to wear quotation marks (" ") like a special uniform!

No need to add str() here! We are connecting strings with strings.

Exercise 5 • python101-2-variables-types
No output yet...

3.4. Booleans (bool): The Yes/No Crew ✅❌

Booleans are like light switches - they can only be True or False. Super useful for:

  • Checking if a game is over (game_over = True)
  • Seeing if you completed your homework (homework_done = False)
  • Keeping track if you're logged in (is_logged_in = True)
  • Testing if it's your birthday (is_birthday = True)

Fun fact: Booleans are named after George Boole, a mathematician who loved working with true/false logic!

Exercise 6 • python101-2-variables-types
No output yet...

4. Fun With Variables! 🎪

Watch what happens when we mix and match:

Exercise 7 • python101-2-variables-types
No output yet...

5. Pro Tips for Working with Different Types 🌟

  1. 🔄 Type Conversion Spells:

    • str() turns things into text
    • int() turns things into whole numbers
    • float() turns things into decimal numbers
    • bool() turns things into True/False
  2. 🎨 Mixing Types:

    • Text + Text = Works! ✅ ("Hello " + "there")
    • Text + Number = Needs str()! ❌ ("Age: " + str(12))
    • Number + Number = Math! ✅ (5 + 3 = 8)
  3. 🎯 When to Use Each:

    • Counting things? Use integers!
    • Measuring things? Use floats!
    • Writing messages? Use strings!
    • Yes/No questions? Use booleans!

6. 🎯 Mission: Code Your Profile!

Let's create a coder profile! Try to fill in this code for Amy:

  1. Name: "Amy"
  2. age = 24
  3. Favorite hobby: "Gaming"
  4. Likes coding: True
Exercise 8 • python101-2-variables-types
No output yet...

7. 🌟 Bonus Challenge: Variable Valor!

Can you guess what these will do? Try them out!

Exercise 9 • python101-2-variables-types
No output yet...

8. 🎯 Final Mission: Fix the Broken Code!

Detective work time! This code has some problems. Fix it and print:

"Sam is 13 years old and loves the number 3.5!"
Exercise 10 • python101-2-variables-types
No output yet...

Remember: Every great coder started exactly where you are now! Keep experimenting, and don't be afraid to make mistakes - they're just learning opportunities in disguise! 🌈

Need a hint? Just ask! Together, we'll crack the code! 💪

Great job finishing the tutorial!

Ready to test your knowledge?

Take a quick quiz to reinforce what you've learned and make sure you've mastered the key concepts.