Skip to main content

Practice Makes Perfect

Reading about Python is easy, but actually writing it builds your muscle memory! Try typing these exercises into your hello.py file and running them.

Hands-On Practice

Exercise 1: The Swapping Trick

Imagine you have two glasses. Glass A has Apple Juice, and Glass B has Mango Juice. How do you swap them? In Python, it is incredibly easy!

a = 10
b = 99

# The magic Python swap trick!
a, b = b, a

print("A is now:")
print(a) # Output: 99

print("B is now:")
print(b) # Output: 10

Exercise 2: Building a Profile

Let's use all three data types we learned (Strings, Numbers, and Booleans) to create a user profile!

# A String (Text)
player_name = "Rahul"

# An Integer (Whole Number)
level = 5

# A Boolean (True/False)
is_vip = True

print("Player Name:")
print(player_name)

Common Beginner Questions (Q&A)

Q1: Can I change a variable's type later?

Answer: Yes! In Python, a variable is just a box. You can put a number (10) in it today, and put a word ("Hello") in it tomorrow. Python doesn't mind at all!

my_box = 100
my_box = "Now I am text!" # This is perfectly fine in Python

Q2: What happens if I write True with a lowercase t?

Answer: Python will crash! Python is very strict about capital letters. True and False must always have capital first letters, otherwise Python thinks you are trying to use a variable name that doesn't exist.

Q3: Why does dividing two integers give me a float?

Answer: If you divide 10 / 2, the answer in Python is 5.0 (a float), not 5. Python does this automatically to make sure it doesn't lose any decimal points if the division isn't perfectly even!

Module Summary

  • Variables: Storage boxes for your data (e.g., score = 10).
  • Strings: Raw text wrapped in quotation marks ("Hello").
  • Numbers: Integers are whole numbers (5), Floats are decimals (5.5).
  • Booleans: Simple Yes/No switches (True or False).

🎉 You have completed Module 2! 🎉