A Fun Guide to Coding with Python
Chapter 1: Welcome to Python!
What is Python?
Python is a simple, readable programming language used for games, apps, and even websites like YouTube! It’s perfect for beginners because it’s easy to understand.
How to Start?
Download Python from python.org (ask an adult if needed).
Use an editor like IDLE (comes with Python) or an online tool like Replit.
Let’s write your first code!
Example 1: Say Hello
python
CollapseWrapCopy
print("Hello, world!")
Type this in your Python editor and run it. It will display "Hello, world!" on the screen.
Try changing the text to your name, like print("Hi, [Your Name]!").
Chapter 2: Playing with Numbers
Python can do math for you! Let’s try some simple calculations.
Example 2: Basic Calculator
python
CollapseWrapCopy
print(5 + 3) # Addition
print(10 - 4) # Subtraction
print(2 * 6) # Multiplication
print(15 / 3) # Division
Run this code. What numbers do you see?
Try your own numbers, like print(7 * 8).
Bonus Challenge:
Can you make Python calculate how many candies you’d have if you got 10 candies and ate 3?
(Hint: Use print(10 - 3))
Chapter 3: Variables – Your Storage Boxes
Think of variables as boxes where you store things like numbers or words.
Example 3: Storing Your Age
python
CollapseWrapCopy
age = 14
print("I am", age, "years old!")
age is the variable. You can change 14 to your real age.
Run it. What does it say?
Example 4: A Fun Game Score
python
CollapseWrapCopy
score = 100
score = score + 50 # Add 50 points
print("Your score is", score)
This adds 50 to your score. Try adding more points!
Chapter 4: Asking Questions with Input
Let Python talk to you! Use input() to let users type something.
Example 5: Guess My Name
python
CollapseWrapCopy
name = input("What’s your name? ")
print("Nice to meet you,", name, "!")
Run this. Type your name when it asks. What happens?
Change the message to something funny, like "Wow,", name, "is a cool name!".
Chapter 5: Making Choices with If
Python can make decisions using if. Let’s try it!
Example 6: Are You Old Enough?
python
CollapseWrapCopy
age = int(input("How old are you? "))
if age >= 13:
print("Welcome to the club!")
else:
print("Sorry, you’re too young!")
int() turns your input into a number.
Run it and type your age. What message do you get?
Try typing 12 or 15 to see different results.
Chapter 6: Loops – Doing Things Again and Again
Loops repeat code. Let’s use a for loop to count.
Example 7: Counting to 5
python
CollapseWrapCopy
for i in range(5):
print("Count:", i)
This counts from 0 to 4. Why not 5? range(5) stops before 5.
Change it to range(10) and see what happens!
Example 8: Sing a Song
python
CollapseWrapCopy
for i in range(3):
print("Happy birthday to you!")
print("Happy birthday, dear friend!")
print("Happy birthday to you!")
This repeats the line 3 times. Can you make it repeat 5 times?
Chapter 7: Your First Mini-Project – Number Guessing Game
Let’s combine everything into a fun game!
Example 9: Guess the Number
python
CollapseWrapCopy
secret_number = 7
guess = int(input("Guess a number between 1 and 10: "))
if guess == secret_number:
print("You win! Great guess!")
else:
print("Oops! The number was", secret_number)
Run it and guess a number. Did you win?
Change secret_number to another number between 1 and 10. Play again!
Chapter 8: What’s Next?
You’ve learned:
Printing messages
Using numbers and variables
Asking for input
Making choices with if
Repeating with loops
Try This:
Make a quiz that asks 3 questions and gives a score.
Create a story where the user picks what happens next.
Happy coding
Learning Goals:
Understand how to define and use functions in Python.
Learn how to gather input from the user and process it.
Understand the structure and syntax of conditional statements (if-else) in Python.
Learn how to properly structure code for readability and functionality.
1. Functions in Python
A function is a block of code that performs a specific task. In Python, you can define a function using the def keyword, followed by the function name, parentheses, and a colon. For example:
def greet(name):
return f"Hello, {name}!" # Returns a greeting message
Function definition: def greet(name): defines a function called greet that accepts one parameter name.
Return statement: The function returns a greeting string using an f-string to include the name provided.
2. Working with User Input
You can ask for user input using the input() function. However, the input() function always returns a string, so you may need to convert it to other data types (e.g., integer or float) if necessary:
name = input("Enter name: ") # Accepts the user's name as input
number1 = int(input("Enter the first number: ")) # Accepts an integer input
Data conversion: The int() function is used to convert the input into an integer because input() returns a string by default.
3. Example Code: Greeting and Addition
Below is an example that combines user input, functions, and conditionals. It includes a function to greet the user by name and another function to add two numbers provided by the user.
# Function to greet the user
def greet(name):
return f"Hello, {name}!" # Returning the greeting as a string
# Function to add two numbers
def kirakira(number1, number2):
number = number1 + number2
return number # Returning the sum
# Asking for the user's name
name = input("Enter name: ")
# Calling the greet function and printing the returned value
message = greet(name)
print(message)
# Checking if the name is "Nor"
if name == "Nor":
print(f"If x is Nor, you are, {name}")
else:
print("You are not Nor")
# Asking the user to input two numbers
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
# Calling the kirakira function and printing the result
result = kirakira(number1, number2)
print(result)
4. Key Learning Points:
Function Definitions:
Ensure each function is properly defined with its own indentation and no overlap with others.
Conditional Statements:
The if statement must be followed by a colon (:), and the code within the if and else blocks must be indented.
You can use an else statement to provide an alternative action when the if condition is false.
Input Conversion:
Always convert user input to the appropriate data type (e.g., int, float) if you need to perform mathematical operations on it.