Introduction to Programming with Python for Middle and High School Students (English version)
1. Introduction to Python
What is Python?
Python is a high-level, interpreted, and versatile programming language known for its clear syntax and ease of learning.
First Steps with Python
Python uses “scripts” to execute commands, and a Python script typically ends in .py
Code Example: Hello World
# This is a comment - it is not executed by Python
# Prints a message on the screen
print("Hello World!")
2. Python Basics
Variables in Python
- A variable is like a box where you can store information.
- Example:
number = 5
stores the value 5 in the variablenumber
Using input()
to Enter Data
- The
input()
function allows the user to enter data. - Example:
name = input("What is your name? ")
Code Example : Personalized Greeting
# Asks for the user's name
name = input("What is your name? ")
# Displays a greeting
print("Hello, " + name + "!")
3. Conditional Structures
Imagine you have a robot that follows instructions you give it. Sometimes, you want the robot to do something if a condition is true, and something else if the condition is false. This is where if
and else
come in Python!
Using if-else
in Python :
The if-else
structure allows for decision-making based on conditions. Example: Execute a block of code if a condition is true.
Code Example: Even or Odd
# Asks for a number from the user
number = int(input("Enter a number: "))
# Checks if the number is even or odd
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
4. Loops in Python
- For Loops A
for
loop repeats an action a certain number of times.
Code Example : Display Numbers from 1 to 5
# Uses a for loop to display numbers from 1 to 5
for i in range(1, 6):
print(i)
Code Example: Multiplication Table
# Asks for a number from the user
number = int(input("Enter a number: "))
# Displays the multiplication table for this number
for i in range(1, 11):
print(number, "x", i, "=", number * i)
5. Python and Mathematics Activities
Calculate the Area of a Circle
# Program to calculate the area of a circle
radius = float(input("Enter the circle radius: "))
area = 3.14 * (radius ** 2)
print("The area of the circle is:", area)
Find the Maximum of Two Numbers
# Program to find the maximum of two numbers
number1 = float(input("Enter the first number: "))
number2 = float(input("Enter the second number: "))
maximum = max(number1, number2)
print("The maximum of the two numbers is:", maximum)
Check if a Number is Even
# Program to check if a number is even
number = int(input("Enter a number: "))
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Addition of Two Numbers
# Program to add two numbers
number1 = float(input("Enter the first number: "))
number2 = float(input("Enter the second number: "))
result = number1 + number2
print("The sum of the two numbers is:", result)
Calculate the Average of Three Numbers
# Program to calculate the average of three numbers
number1 = float(input("Enter the first number: "))
number2 = float(input("Enter the second number: "))
number3 = float(input("Enter the third number: "))
average = (number1 + number2 + number3) / 3
print("The average of the three numbers is:", average)
Check if a Number is Positive, Negative, or Zero
# Program to check if a number is positive, negative, or zero
number = float(input("Enter a number: "))
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
6. Introduction to Turtle
What is Turtle?
- Turtle is a built-in graphical library in Python, used to create simple drawings and animations.
Configuring Turtle
- Turtle is included in the standard Python installation. To use it, simply import it into your script.
Code Example : Your First Drawing
import turtle
# Creates a new turtle object
t = turtle.Turtle()
# Moves the turtle to draw
t.forward(100) # Moves forward 100 pixels
t.left(90) # Turns 90 degrees
t.forward(100)
# Ends the drawing
turtle.done()
7. Drawing with Turtle
Basic Commands
forward(x)
: moves forward x pixels.backward(x)
: moves backward x pixels.right(angle)
: turns right by angle degrees.left(angle)
: turns left by angle degrees.
Code Example : Drawing a Square
import turtle
t = turtle.Turtle()
# Repeats 4 times to draw a square
for _ in range(4):
t.forward(100)
t.left(90)
turtle.done()
More Advanced Projects with Turtle : Using Loops
for
loops are useful for repeating actions.
Code Example : Drawing a Circle
import turtle
t = turtle.Turtle()
# Uses a loop to draw a circle
for _ in range(360):
t.forward(1)
t.left(1)
turtle.done()
Drawing Polygons with a Function
import turtle # Imports the Turtle module for graphical drawing
# Definition of the polygon function
def polygon(color, sides, size):
turtle.fillcolor(color) # Sets the fill color of the polygon
turtle.begin_fill() # Starts the color filling process
# Loop to draw each side of the polygon
for _ in range(sides):
turtle.forward(size) # Moves the turtle the length 'size'
turtle.left(360 / sides) # Turns the turtle to form the polygon's angles
turtle.end_fill() # Ends the color filling once the polygon is complete
# Initialization of the Turtle environment
window = turtle.Screen() # Creates a new Turtle window for drawing
turtle.speed(3) # Sets the turtle's drawing speed
# Example of using the polygon function
polygon("blue", 5, 100) # Uses the function to draw a blue pentagon
window.mainloop() # Keeps the window open to display the drawing
# In this program, the polygon function is defined to create a polygon
# according to given specifications. You can call this function with different
# values to create various polygons. For example, polygon("red", 4, 80)
# would draw a red square.
8. Movement Commands
Movement commands allow moving the turtle and creating various shapes.
forward(distance)
orfd(distance)
: Moves the turtle forward by ‘distance’ units.backward(distance)
orbk(distance)
: Moves the turtle backward by ‘distance’ units.right(angle)
orrt(angle)
: Turns the turtle right by ‘angle’ degrees.left(angle)
orlt(angle)
: Turns the turtle left by ‘angle’ degrees.goto(x, y)
: Moves the turtle to the specified position.setx(x)
: Moves the turtle to the x-coordinate.sety(y)
: Moves the turtle to the y-coordinate.circle(radius)
: Draws a circle with the specified radius.dot(size=None, color=None)
: Draws a dot.
9. Control Commands
These commands include the turtle’s movement speed and pen control.
speed(speed)
: Sets the movement speed of the turtle (1–10).penup()
orpu()
: Lifts the pen, no drawing when the turtle moves.pendown()
orpd()
: Puts down the pen, draws when the turtle moves.
10. Pen and Color Commands
These commands change the pen color, and start and end the filling of shapes.
color(color)
: Changes the pen color.begin_fill()
: Begins to fill the drawn shape.end_fill()
: Ends the filling of the shape.fillcolor(color)
: Changes the fill color.
11. Window Commands
These commands control the Turtle window interface, such as the title, background color, and clear or reset drawings.
title(title_string)
: Sets a title for the Turtle window.bgcolor(color)
: Changes the window's background color.clear()
: Clears the turtle's drawing but keeps the window open.reset()
: Resets the turtle to its original position and state.bye()
: Closes the Turtle window.
12. Common Colors
- You can specify colors using names (like “red”, “green”, “blue”), or using hexadecimal codes (like “#FF0000” for red).
- Basic colors: “red”, “green”, “blue”, “yellow”, “black”, “white”, “pink”, “orange”, “purple”, “grey”
Example Code Using Turtle
import turtle
turtle.speed(1)
turtle.color("blue")
turtle.forward(100)
turtle.right(90)
turtle.color("green")
turtle.forward(100)
turtle.done()
EduPython : An IDE Specially Designed for Education
EduPython, an Integrated Development Environment (IDE), has been specially designed to facilitate the learning of Python programming for middle and high school students. This pedagogical tool makes learning programming accessible and engaging, thanks to an intuitive user interface and a range of features suited for beginners.
Key Features of EduPython :
- Simplified Interface : EduPython offers a streamlined and easy-to-navigate interface, allowing students to focus on the essentials: coding and understanding programming concepts.
- Integrated Pedagogical Resources : The IDE includes resources and code examples to help students get started and understand the fundamentals of Python.
- Compatibility with School Curricula : EduPython is designed to align with school curriculums, making its integration into classrooms smooth and effective.
EduPython is thus an ideal choice for educators looking to introduce Python programming in their courses in an interactive and structured manner.
Download Links for the Latest Version EP3.1 :
- Installable Version: Download the latest version: 3.1
- Compressed Version: zip version
TI Calculators: Versatile Tools for Learning Python
The recent versions of the TI-83 / TI-84 Edition Python calculators from Texas Instruments represent a remarkable advance in STEM (Science, Technology, Engineering, and Mathematics) education. These calculators, beyond their classic functionalities, are equipped to run Python programs, thus offering a portable and accessible platform for learning programming.
Advantages of TI Edition Python Calculators for Python :
- Portability : Students can practice Python programming anywhere, without needing a computer.
- Practical Learning : The ability to code directly on the calculator allows for a tactile and interactive learning experience.
- Integration with Mathematics : Students can use Python to explore complex mathematical concepts, making learning more dynamic and applied.
The recent versions of the TI-83 or TI-84 Edition Python calculators from Texas Instruments are thus valuable tools for students looking to combine the learning of mathematics and programming.
Discover Playful Education with Programmable Educational Robots
Learning programming is not limited to using computers. Programmable educational robots bring a tangible and interactive dimension to programming education. These little robotic companions are designed to teach coding concepts in a fun and practical way.
Educational Robots and Python
Several educational robots offer full or partial compatibility with the Python programming language. This means that students can write Python scripts to control the behavior of these robots. Here are some examples of popular educational robots that can be programmed in Python:
- LEGO Mindstorms : These LEGO robot kits allow students to design and program robots using Python, providing an immersive STEM learning experience.
- Sphero : Sphero robots, like the Sphero Bolt, can be programmed in Python through the Sphero Edu programming interface. Students can create routes, games, and artistic projects with these robots.
- Ozobot : These small educational robots are perfect for introducing younger children to programming using Python. Students can create visual codes to control Ozobot’s movements and colors.
- micro:bit: Although not a robot per se, the micro:bit is a fantastic educational tool for teaching programming in Python. It can be used to create interactive projects and connected objects.
Discover the Raspberry Pi Pico
The Raspberry Pi Pico is an affordable microcontroller board that works seamlessly with Python. It is ideal for electronics and automation projects. Students can program the Pico in Python to control sensors, actuators, and other electronic components.
Elektor The Raspberry Pi Pico is also compatible with MicroPython, a lightweight version of Python designed for microcontrollers. This makes it accessible to students and educators who want to explore electronics and automation while using a familiar programming language.
Embark on a Creative and Educational Journey with Python and Turtle
You’ve just acquired the foundations of programming in Python and Turtle, and it’s just the beginning of a fascinating adventure into the world of coding. The realm of Python unfolds before you, brimming with creative and technical possibilities. Harness these tools to bring your unique ideas to life through custom scripts and original drawings.
Learning programming is not just a skill; it’s a gateway to limitless creativity and innovation. Take inspiration from young prodigies like Idder Moutia, who started coding at the age of 11 and continues to explore the vast opportunities offered by technology. His journey is a source of inspiration, proving that age is not a barrier to learning and mastering programming.
Also, discover forward-thinking institutions like School 1337, a talent incubator where passion and competence converge. These stimulating environments are fertile grounds to cultivate your curiosity and develop your programming skills.
As educators and students, you are in control of this educational journey. Encourage curiosity, foster innovation, and embrace challenges. Programming is a universal language that opens countless doors in the fields of technology, science, and beyond.
Take the reins of your learning, create, explore, and innovate. The world of coding is vast and filled with possibilities — take the leap and discover where your ideas can lead you.
Conclusion
The use of educational IDEs such as EduPython, recent versions of TI-83 or TI-84 Edition Python graphing calculators, programmable educational robots, and the Raspberry Pi Pico in Python education opens new pedagogical perspectives and broadens the horizons of programming learning. They offer students the opportunity to explore areas such as robotics, electronics, and automation while developing their Python skills.
These tools make programming learning more accessible, interactive, and connected to real-world applications in STEM fields. They are major assets for educators who want to impart programming skills while enhancing understanding of mathematics and science. They open the door to infinite learning possibilities. Encourage your students to immerse themselves in this world of interactive and creative learning. Who knows what future innovations they might inspire?