DEV Community

Cover image for Python: Day Two – Variables, Data Types, and Putting Stuff in Boxes
Bonface Thuo
Bonface Thuo

Posted on

Python: Day Two – Variables, Data Types, and Putting Stuff in Boxes

Welcome back to Day 2, coders! πŸš€ Yesterday we mastered the art of making the console say "Hello, World!" and survived the great indentation panic. πŸ˜… Today, we are leveling up! We need to teach our Python scripts how to actually remember things. 🧠

Grab your beverage of choice β˜•, and let's talk about variables, data types, and data structures! πŸŽ‰

πŸ“¦ Variables: The Magic Storage Boxes

Think of your computer's memory as a giant, empty warehouse. 🏭 A variable is basically a cardboard box you bring into that warehouse. You scribble a name on the outside of the box with a sharpie πŸ–οΈ, and you chuck some data inside! πŸ“₯

In Python, creating a variable is ridiculously easy. You don't have to announce what kind of box it is beforehand (unlike some other languages 🀭). You just name it and put something in it:

# Naming our boxes and putting stuff inside 🎁
player_name = "Mario"
score = 100
health = 99.5
is_game_over = False

print(player_name) # Prints: Mario
Enter fullscreen mode Exit fullscreen mode

Rule of thumb: πŸ“ Give your variables clear names! user_age = 25 is fantastic. 🌟 x = 25 will make you hate yourself in exactly two weeks when you completely forget what x stands for. 😭

🧬 Data Types: What's inside the box?

Now, what exactly can we put in these boxes? Understanding the type of your data is half the battle when you're coding. Here are the four basic building blocks you will use 99% of the time: 🧱

1. Strings (str) πŸ”€
Strings are just text. Wrap them up in single or double quotes. 🧢 If it has quotes, Python treats it like words, even if it looks like math! πŸš«βž•

greeting = "Hello there!"
fake_number = "42" # Python thinks this is a word, not math! 🀯
Enter fullscreen mode Exit fullscreen mode

2. Integers (int) πŸ”’
Whole numbers. Positive or negative, but absolutely no decimals allowed! πŸ›‘ Great for counting your extra lives. πŸ‘Ύ

lives_left = 3
temperature = -5
Enter fullscreen mode Exit fullscreen mode

3. Floats (float) πŸ’§
Numbers with a decimal point. Whenever you need exact precision (like currency πŸ’Έ, weights βš–οΈ, or scientific measurements πŸ§ͺ), you use a float!

price = 19.99
pi_value = 3.14159
Enter fullscreen mode Exit fullscreen mode

4. Booleans (bool) 🚦
True or False. Yes or No. πŸŸ’πŸ”΄ That’s it! These are the ultimate decision-makers in your code. (Note: The T and F must be capitalized in Python, or it throws a tantrum! 😀)

has_coffee = True
is_tired = False
Enter fullscreen mode Exit fullscreen mode

πŸš€ Today's Challenge πŸ†
Try creating a mini-profile for your favorite fictional character using just variables! πŸ¦Έβ€β™‚οΈ Create a string for their name, an integer for their age, a float for their power level (like 9000.5), and a boolean for whether they are currently saving the world (True or False). Print them all out and see what happens!

That's a wrap for Day 2! 🎬 We can now store text and numbers like pros. Tomorrow, we are going to tackle Lists and learn how to pack our virtual backpacks with all these awesome variables. πŸŽ’ Are you feeling like a true programmer yet? 😎 Let me know in the comments how your character profile challenge goes! πŸπŸ’»πŸ‘‡

Top comments (0)