0% found this document useful (0 votes)
7 views16 pages

G12 Python Chapter

This document outlines a series of lessons on coding with Python, focusing on fundamental concepts such as loops, conditionals, functions, and lists. It includes exercises for practical application, such as modifying programs, drawing shapes with the turtle library, and creating graphical user interfaces using Tkinter. The lessons aim to build foundational skills for advanced Python programming.

Uploaded by

انا زينة
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

G12 Python Chapter

This document outlines a series of lessons on coding with Python, focusing on fundamental concepts such as loops, conditionals, functions, and lists. It includes exercises for practical application, such as modifying programs, drawing shapes with the turtle library, and creating graphical user interfaces using Tkinter. The lessons aim to build foundational skills for advanced Python programming.

Uploaded by

انا زينة
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Coding with Python

3
Objectives of the lesson : Reviewing the skills learned in Grade 11
Chapter 1: Coding With Python | Lesson 1

Reminder by examples

In this first lesson, you will explore all the skills learned in Grade 11 as you need them to move forward with advanced
skills in Python.

Exercise 1

Examine the Python program on the right.

1 The “for” command is used to:

3 How mane times are the commands on lines 2


and 3 repeated?

2 How mane variables are used ? List them. 4 Why is “float” used on line 3?

Try the program on your computer. Modify it in order to accept 2 grades for each student and then to calculate and
display their average.

Once the program is running correctly, modify it again in order to create a global variable named “total” in order to
calculate and display the average of all the students.

Exercise 2

Examine the Python programs below and describe their utilities.

1 The first program will display

2 The second program will display

3 The third program will display

4 The fourth program will display

4
Exercise 3

After completing the excercise below, you will be able to solve any problem using embedded conditionnal statement
“if”.

1 Describe the utility of this program.

2 Name the variables used.

3 How many times will the program run the instructions between lines 3 and 13?

4 What happens if the user types the letter “R”?

5 Modify the program above as shown. Test it and write down the improvement made.

5
Objectives of the lesson : 1- Combining loops with the “if” statement
2- Drawing shapes with the turtle 3- Using functions
Chapter 1: Coding With Python | Lesson 2

Combining loops with conditional statements


1- Write the Python program to do the following:

1- The program will be repeated until the user types the word “exit”.
2- The user is asked to type a number and then to display if it is EVEN or ODD.
3- At the end of the program, the results below should be displayed:

A- How many times are ODD numbers typed Their total is


B- How many times are EVEN numbers typed Their total is

On line 1, the sign % (modulo) will give the reminder of


1
the division.

In our example, the content of the variable “number” will


be divided by 2 and the reminder will be 1 or 0.

Drawing shapes with the turtle


2- Write the Python program to do the following:

1- Use the library “turtle” to draw geometrical shapes as follows:


2- The user is asked to choose between 3 shapes: Square, Rectable or Equilateral Triangle.
3- The 3 letters to be accepted by the program are: S - R - T.
4- According to the user’s choice, the turtle will draw the appropriate shape.
5- Use the “for” loop to draw the geometrical shapes.

1 On line 1, the “turtle” library is called. 1

The commands to control the movement


2
of the turtle are: forwad - backward -
right - left - penup() - pendown().

3- Examine the program that draws a square.

Complete the program to make


2
the turtle draw 3 squares as
shown.

6
Using functions to create a guessing game

After finishing the exercise below, you will be able to use the random library to define and to call a procedure in a
program.

1 The command “import random” on line 1 allows you to:

2 The commands on lines 3 and 4 allows you to:

3 The command on line 7 allows you to:

4 Between lines 10 and 18, the program will:

Controlling the number of inputs


Modify the program above to limit the number of trials to 5 times only.
After 5 trials, the program will stop asking to input a number.
7
Objectives of the lesson : 1- Defining a list 2- Creating a list 3- Filling a list
4- Displaying the content of a list.
Chapter 1: Coding With Python | Lesson 3

Exploring the use of lists in Python


A list is a data structure in Python that allows you to store a collection of items in a specific order. A list is compared
to a variable, It can hold a lot of information at the same time. You can modify, add or remove elements after creating
a list. Lists can contain elements of different data types, such as integers, strings, or floats.

Creating a list
You can create a list in Python by enclosing a comma-
separated sequence of elements within square
brackets [ ]. Examine the example.

Displaying an item from a list


The index of the first item in a list is 0. Examine
1 the “print” command to display the first item. 1

2 Examine how to display all the items of a list.

3 print(list1[3]) display the item 2

4 print(list1[5]) display the item

5 To control the display of a list, use a “for” loop.

Examine the result of the “for”


6
loop to display the content of
a list.

Challenge
Write the Python program to:
1- Fill a list of 20 items.
2- To display it in a reserve order.
3- To display only EVEN items (items 1,3,5...)
8
Filling a list from users’ inputs
Most of the programs in different languages include a routine to store, from the users’ input, data in a list.

1
1 At line 1, a list named “x” is declared empty.
2

At lines 2 and 3, a “for”loop is created to fill


2
in the different items of the list using the 3
command “append”.

Examine the output of the program. 5


3
values are stored in the list using the
command at line 3.

To display the content of the list, add to the


4 program above the command “print(x)”.

Displaying the content of a list in reverse

1
1
Examine the program on the right. On lines 5
and 6, a loop is added to display the items of
the list in reverse.

2 Try the program and analyse why an error 2

message is displayed?

3 Correct the program to work properly.

Challenges

Write the Python program to: 7 4 3 10


1
1- Fill two lists “x” and “y” with 4 items each. x[0] x[1] x[2] x[3]
2- To display the content of “x” in a normal order (from item 0 till item 10).
3- To display the content of “y” in a reverse order (from item 10 till item 0). 2 4 7 10
4- Create a new list called “z” to be filled with a calculation as shown. y[0] y[1] y[2] y[3]

9 8 10 20
4

Create the Python program that fills in, from the keyboard, a list whose
2
length will be determined by the user.

9
Objective of the lesson : Searching for maximum and minimum values in a list
Chapter 1: Coding With Python | Lesson 4

Controlling the list in Python


In the previous lesson, you learned how to create a list in order to display its content in addition to filling in a list from
the input keyboard.
In this lesson you will discover additional features for “list” manipulations.

Flashback
Examine the program on the right then complete the
missing parts on lines 2 and 3 in order to make the
user define the size of the list.

Complete the code on line 2 to make


1 the user store the length of the list
from the keyboard.

2 Complete the command of line 2. 3 Complete the command of line 3.

Complete the program above in order to fill in the content of the list “x” in a new list “y”.

Add to the program above a third loop in order to fill in the content of the list “x” in a third list “z” while reversing
the content.

1 Examine the content of the list “x”. 1 7 4 78 20 2 20 78 4 7

x[0] x[1] x[2] x[3] z[0] z[1] z[2] z[3]


2 And that of the list “z”: x[0] =z[3], etc.

Challenge
Write the Python program to:
1- Fill a list of 20 items.
2- Add a condition to the program in order to
replace all the values greater than 10 with 0.
10
Finding the minimum and the maximum
In the below program, you will explore how to write a program to display the minimum and maximum values of a list.

At lines 1 to 4, a function is defined with 1


1
parameter. 1

Describe what happens in the function “find_


2
max_min”:
3

At line 6, On line 7,
3 4

Finding another way

In the above program, you used a function and the 2 commands “min” and “max” to find out the minimum and maximum
values in a list. In this exercise, you will learn another method to search for those values.

At lines 1 and 2,
1 1

At line 3
2

At lines 6 to 8,
3

Challenge
Create the Python program that fills in, from the keyboard, a list whose
2
length will be determined by the user. then to replace all the values less
than 10 with 0 and the values greater than 10 with 1.
11
Objectives of the lesson : 1- Searching a value in a list 2- Counting the
number of times a value is repeated in a list 3- Sorting and reversing a list.
Chapter 1: Coding With Python | Lesson 5

Manipulating multiple lists


In the below program, you will learn how to manage the grades of students in a classroom.

How many lists are created in the


1 p r o g ra m ?

2 List them:

3 The for loop will turn times. 4 The command append is used to:

Now that the 3 lists are filled in with data, it is time to modify the program to calculate the average of each student
and the average of the entire classroom.
Propose a strategy of work to do the above task and try it on your computer.

Challenges
Write a Python program that fills in a new list with 10 items then create a new list containing the squares of
1 even numbers from an existing list.

2 Write a Python function that takes a list of numbers as input and returns the sum of all the elements in the list.

Write a Python program that utilizes the built-in functions len(), max(), min(), and sum() to perform operations
3 on lists.

Write a Python program that fills in two list of 5 items then swaps their content. The content of the first list is
4 move to the second list and vice versa.

Explore
Try the program shown on the right and describe
its utility.

12
Searching a value in a list
We consider that a list is filled in and that we want to know if a value of your choice is found among the list’s elements.
To solve the exercise, you should first declare a variable “found” and assign to it the value “False”.
Once the list is filled in, a loop will be created in order to compare the content of every element of the list with the
value to be found. In case the value is found, we assign the value “True” to the variable “found”.

1 Describe the utility of the Python program.

At line 7, the variable “value” is used to


2

Write the “for” loop in order to search if the


3
data stored in the variable “value” exists in
the list.

Counting an item in a list


Propose a strategy in order to know how many times the value to be found is repeated in the list?

Sorting and reversing a list


Use the sort() method to sort the list in
1
ascending order.
Use the sort(reverse=True) method to sort the
2
list in descending order.
Use the reverse() method to reverse the order
3
of elements in the list.

13
Objectives of the lesson : 1- Learning about Tkinter 2- Learning how to
create a simple graphical user interface using Tkinter.
Chapter 1: Coding With Python | Lesson 6

Introducing tkinter

tkinker is a library for Python to create Graphical User Interface (GUI) for interactive and user-friendly applications
by providing a set of widgets (buttons, text boxes, etc.) that can be added to an application. These widgets can be
customized and arranged to create a professional-looking interface.

What is a Graphical User Interface (GUI)?


GUI is in every application we use on our computers,
mobile phones and websites.
1
Examine the GUI interface for a restaurant
1 software.

Give examples of GUI for applications you know.

Start using tkinter


To use tkinter, you need to call the tkinter library at the beginning of the program.

Start the program with the line


1 code: 1
from tkinter import *.

root = Tk() is used to create the 2


2 main window of your application.

The main window can be


3 customized by changing its
properties (title and size) and
adding widgets (buttons, labels
and text boxes).

Once you run the program, a


4 window will be displayed on the
screen.
4

14
A title for your window

To make the content of the window more significant, a title should be set to it.

1 Add the code on line 3.

1
Run the program and examine the
2 result.
3
The title “this is my first tkinter
3 project” is displayed on the title
bar of the windows.

A label in the windows


It is time to display titles and messages inside the window.

Examine the code on lines 4-6.


1 This will create a new Label object
inside the main window. 1
The size of the window is set to
300*200 pixels.
The Label object is stored in the
variable x.
3
In the Label object:

1- a root is set to:

2- a text is set to:

Project to do
pack() places the label widget onto Write the Python code to create a
2
the window. window with the properties below:

[Link]() will launch the 1 Title : A new project in Python.


3 main event loop to activate the
window. 2 Background Colour : Green.

3 Label : Ready for the Challenge.

15
Objectives of the lesson : 1- Creating a button with an event handler
2- Creating an image using the PIL library 3- Modifying the colours of an image
Chapter 1: Coding With Python | Lesson 7

A button for an action

The Button widget is used to create interactive objects that respond to the user input. When the user clicks on a
button, it triggers a command or a function that performs an action, such as saving a file, opening a dialogue box or
switching to a different screen.

Creating a button

In order to create a button, use the object


Button.
1
Examine the code at line 8. The
1 command parameter is used to
name the function to be called
when the button is clicked.

handle_click is the name of the


2 function set on line 5. When the
button is clicked, the function will
be executed.

Displaying a message with the button click


In this exercise, you will learn how to
display a message when a button is clicked.

At lines 9 and 10, a button is


1 defined and once it is clicked, the
function “show_message” will be
displayed.
1

Displaying an image with the button click


In this exercise, you will learn how to
display an image when a button is clicked.

The PIL (Python Image Library) will


1 be explored in the next exercise.

The image “red_image.png” should


2 be in the same folder as the Python
document.

16
Creating an image using the PIL library

The PIL (Python Image Library) is used to create and edit pictures.

1 At line 1, the PIL librairy is imported

1
The command [Link] will
2 create a new image with the
following properties:
2
At lines 8 and 9, the image is save
3 on the hard disk then displayed on
the screen.

Modifying the colours of an image

Using the library PIL, you can control every single pixel in an image.

Examine the picture below, it was


1 created using the Python program
shown on the right.

The picture created has the


2 following dimensions: 1000*450px.

At line 3, a for loop is defined to scan


3 the height of the pictures.

At line 4, a for loop is defined to scan


4 the

from line 5 till line 14, a condition is


5 set to

At lines 16 and 17, the image is


6

17
Objective of the lesson : Creating patterns using the PIL library
Chapter 1: Coding With Python | Lesson 8

Controlling each pixel

In the following project, you will discover how to control each pixel in the image knowing their coordinates x and y.
Since the picture has two dimensions x and y, it is a must to use two embedded loops in order to control every single
pixel.

Examine the inner loop defined at


1 line 4. 2

Examine the outer loop defined at 1


2 line 3.
3
At line 5, a condition is set to
3

4
Examine how a diagonal line is
4 drawn across the red square.

Drawing vertical lines

Modify the above program to create the pattern


1 on the right. 1

2
Drawing horizontallines

Modify the above program to create the pattern


2 on the right.

18

You might also like