Learn to code with fun examples and interactive exercises!
Lesson 1 of 10
Welcome to the amazing world of Python programming! ๐
Python is like a magical language that helps us talk to computers. Just like how you use words to talk to your friends, we use Python code to tell computers what to do!
Let's start with the most famous program in the world - saying "Hello, World!"
print("Hello, World!")
The print() function is like a megaphone ๐ข - it tells the computer to show something on the screen!
Type your name in the box below and click "Say Hello!"
Variables are like magic boxes ๐ฆ where you can store things! You can put numbers, words, or anything you want inside them.
It's super easy to create a variable in Python:
name = "Alice"
age = 10
favorite_color = "blue"
print("My name is", name)
print("I am", age, "years old")
print("My favorite color is", favorite_color)
You can change what's inside your magic box anytime!
score = 0
print("Starting score:", score)
score = 100
print("New score:", score)
Fill in the boxes to create your own character:
Python is like a super calculator! ๐งฎ You can do amazing math with it!
# Addition
result = 5 + 3
print("5 + 3 =", result)
# Subtraction
result = 10 - 4
print("10 - 4 =", result)
# Multiplication
result = 6 * 7
print("6 * 7 =", result)
# Division
result = 20 / 4
print("20 / 4 =", result)
# Find the bigger number
num1 = 15
num2 = 23
bigger = max(num1, num2)
print("The bigger number is:", bigger)
# Round a decimal number
price = 12.99
rounded_price = round(price)
print("Rounded price:", rounded_price)
Enter two numbers and see the magic happen!
In Python, text is called a "string" - like a string of letters! ๐งต
message = "Hello there!"
name = "Python"
# Combine strings together
greeting = "Hi " + name + "!"
print(greeting)
# Make text UPPERCASE or lowercase
loud_message = message.upper()
quiet_message = message.lower()
print("Loud:", loud_message)
print("Quiet:", quiet_message)
sentence = "Python is awesome"
# Count letters
length = len(sentence)
print("This sentence has", length, "characters")
# Replace words
new_sentence = sentence.replace("awesome", "amazing")
print(new_sentence)
# Split into words
words = sentence.split()
print("Words:", words)
Type a sentence and watch it transform!
Let's make our programs interactive! We can ask the user questions and get answers! ๐ฃ๏ธ
# Ask for the user's name
name = input("What's your name? ")
print("Nice to meet you,", name)
# Ask for their age
age = input("How old are you? ")
print("Wow, you're", age, "years old!")
# Ask for their favorite animal
animal = input("What's your favorite animal? ")
print("Cool!", animal, "is a great choice!")
# Get a number from the user
age_text = input("Enter your age: ")
age_number = int(age_text) # Convert to integer
# Now we can do math with it!
next_year = age_number + 1
print("Next year you'll be", next_year, "years old!")
Let's create a fun profile!
Programs need to make decisions! We use "if" statements to help our program think! ๐ง
age = 12
if age >= 13:
print("You're a teenager!")
else:
print("You're still a kid!")
# Multiple conditions
weather = "sunny"
if weather == "sunny":
print("Let's go to the park! โ๏ธ")
elif weather == "rainy":
print("Let's stay inside and read! ๐ง๏ธ")
else:
print("Let's see what the day brings!")
# Different ways to compare
score = 85
if score == 100:
print("Perfect score!")
elif score >= 90:
print("Excellent work!")
elif score >= 80:
print("Good job!")
else:
print("Keep practicing!")
Enter your test score to see your grade!
Sometimes we want to do the same thing many times. Loops help us do that! ๐ก
# Print numbers 1 to 5
for i in range(1, 6):
print("Number:", i)
# Print each letter in a word
word = "PYTHON"
for letter in word:
print("Letter:", letter)
# Countdown!
for count in range(5, 0, -1):
print(count)
print("Blast off! ๐")
# Keep asking until we get the right answer
secret_number = 7
guess = 0
while guess != secret_number:
guess = int(input("Guess the number (1-10): "))
if guess == secret_number:
print("You got it! ๐")
else:
print("Try again!")
Choose how many stars you want in your pattern!
Lists are like shopping lists - you can store multiple items in order! ๐
# Create a list of favorite foods
foods = ["pizza", "ice cream", "cookies", "apples"]
# Print the whole list
print("My favorite foods:", foods)
# Get one item from the list
first_food = foods[0] # Lists start counting from 0!
print("My #1 favorite food:", first_food)
# Add new items
foods.append("chocolate")
print("Updated list:", foods)
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
# Sort the list
numbers.sort()
print("Sorted numbers:", numbers)
# Find the biggest and smallest
biggest = max(numbers)
smallest = min(numbers)
print("Biggest:", biggest, "Smallest:", smallest)
# Count how many items
count = len(numbers)
print("We have", count, "numbers")
Add your favorite things to the list!
Functions are like mini-programs that do specific jobs! They help us organize our code! ๐ง
# Define a function
def say_hello():
print("Hello there! ๐")
print("Welcome to Python!")
# Use the function
say_hello()
# Function with parameters
def greet_person(name):
print(f"Hello, {name}! Nice to meet you!")
# Use the function with different names
greet_person("Alice")
greet_person("Bob")
# Function that returns a value
def add_numbers(a, b):
result = a + b
return result
# Use the function
answer = add_numbers(5, 3)
print("5 + 3 =", answer)
# Function to calculate area of a rectangle
def calculate_area(length, width):
area = length * width
return area
# Function to check if a number is even
def is_even(number):
if number % 2 == 0:
return True
else:
return False
# Use the functions
room_area = calculate_area(10, 12)
print("Room area:", room_area, "square feet")
number = 8
if is_even(number):
print(f"{number} is even!")
else:
print(f"{number} is odd!")
Let's create a function that calculates your age next year!
Let's put everything together and create an awesome number guessing game! ๐ฎ
import random
def number_guessing_game():
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 0
max_attempts = 7
print("๐ฎ Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
print(f"You have {max_attempts} attempts to guess it!")
while attempts < max_attempts:
# Get user's guess
guess = int(input("Enter your guess: "))
attempts += 1
# Check the guess
if guess == secret_number:
print(f"๐ Congratulations! You guessed it in {attempts} attempts!")
return
elif guess < secret_number:
print("๐ Too low! Try a higher number.")
else:
print("๐ Too high! Try a lower number.")
# Show remaining attempts
remaining = max_attempts - attempts
if remaining > 0:
print(f"You have {remaining} attempts left.")
# Game over
print(f"๐ฅ Game Over! The number was {secret_number}")
print("Better luck next time!")
# Start the game
number_guessing_game()
Try to guess the secret number between 1 and 100!