This lesson introduces the basics of programming using Python. Students will learn about variables, data types, basic input/output, conditional statements, loops, and functions. By the end of the lesson, students will create a simple program that combines these concepts.
- Understand and use variables and data types.
- Write basic input and output commands.
- Use conditional statements to control program flow.
- Create and use loops to handle repetition.
- Define and call functions to organize code.
- Explain what programming is and its importance.
- Discuss why Python is a great beginner-friendly language.
- Show a simple Python program:
print("Hello, world!")Activity: Ask students to run this code in their Python environment.
- Concept: Explain variables as containers for storing data.
- Example:
name = "Alice"
age = 25
height = 5.6
is_student = True- Discuss basic data types:
str(string)int(integer)float(floating-point number)bool(boolean)
Activity: Have students declare their own variables and print them.
print(6+5) # add
print(6-5) # subtract
print(6*5) # multiply
print(6/5) # divide
print(6%5) # modulo- Concept: Getting input from users and displaying output.
- How to Accept Input: Explain the
input()function.- Syntax:
variable = input("Prompt text") - Input is always stored as a string; use type conversion (
int(),float()) when needed.
- Syntax:
- Example:
name = input("What is your name? ")
print("Hello, " + name + "!")
age = int(input("How old are you? "))
print("You are", age, "years old.")Activity: Create a program that asks for the user’s age and calculates the year they were born.
- Concept: Making decisions using
if,elif, andelse. - Example:
age = int(input("Enter your age: "))
if age < 18:
print("You are a minor.")
elif age == 18:
print("You just became an adult!")
else:
print("You are an adult.")Activity: Write a program to determine if a number entered by the user is odd or even.
In order to complete this assignment, you will have to use the operator modulo (%)
number = int(input("Enter a number: "))
print("The remainder of the number/2 is ", number % 2)
if number % 2 == 0:
print("The number is even")- Concept: Repeating actions using
forandwhileloops. - Examples:
For Loop:
for i in range(5):
print("This is iteration", i)While Loop:
count = 0
while count < 5:
print("Count is", count)
count += 1Activity: Write a program that prints the first 10 numbers of the Fibonacci sequence.
- Concept: Organizing code into reusable blocks.
- Example:
def helloFunction():
return "Hello World!"
print(helloFunction)- Concept: Using functions with arguments.
- Example:
def greet(name):
return "Hello, " + name + "!"
print(greet("Alice"))Activity: Create a function to calculate the area of a rectangle, given its length and width.
Objective: Combine all the learned concepts into a single program.
Project: Create a simple quiz program.
- Ask the user 3-5 questions.
- Track their score.
- Provide feedback at the end based on their score.
Example Starter Code:
def ask_question(question, correct_answer):
answer = input(question + " ")
return answer.lower() == correct_answer.lower()
score = 0
if ask_question("What is the capital of France?", "Paris"):
score += 1
if ask_question("What is 2 + 2?", "4"):
score += 1
print("Your final score is:", score)- Practice writing Python programs that use the concepts learned in class.
- Suggested problems:
- Write a program to check if a number is prime.
- Create a program that calculates the factorial of a number.
- Build a basic calculator that can add, subtract, multiply, and divide.