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!
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:
- Start with a letter or underscore (_)
- Can use letters, numbers, and underscores
- NO SPACES allowed! (use_underscores_instead)
- 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)
:
Fix this code by adding the following to your code: str()
where needed.
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!
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.
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!
4. Fun With Variables! 🎪
Watch what happens when we mix and match:
5. Pro Tips for Working with Different Types 🌟
-
🔄 Type Conversion Spells:
str()
turns things into textint()
turns things into whole numbersfloat()
turns things into decimal numbersbool()
turns things into True/False
-
🎨 Mixing Types:
- Text + Text = Works! ✅ ("Hello " + "there")
- Text + Number = Needs str()! ❌ ("Age: " + str(12))
- Number + Number = Math! ✅ (5 + 3 = 8)
-
🎯 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:
- Name: "Amy"
- age = 24
- Favorite hobby: "Gaming"
- Likes coding: True
7. 🌟 Bonus Challenge: Variable Valor!
Can you guess what these will do? Try them out!
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!"
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! 💪