Hands-On Python BEGINNER - Musa Arda
Hands-On Python BEGINNER - Musa Arda
PYTHON
with 162 Exercises, 3 Projects, 3 Assignments &
Final Exam
BEGINNER
Musa Arda
OceanofPDF.com
Hands-On Python with 162 Exercises, 3 Projects, 3
Assignments & Final Exam: Beginner
by Musa Arda
OceanofPDF.com
Contents
Preface
1. Introduction
2. Getting Started
3. The First Program
4. Variables
5. Functions I
6. Project 1 – Functions
7. Assignment 1 – Functions
8. Conditional Statements
9. Functions II
10. Loops
11. Strings
12. Project 2 - Words
13. Assignment 2 – Words
14. List
15. Dictionary
16. Tuple
17. Set
18. Comprehension
19. Project 3 - Snake Game
20. Assignment 3 - Snake Game
21. Final Exam
22. Conclusion
OceanofPDF.com
Preface
About This Book
This book is an in-depth and activity-based introduction to
Python programming. It follows a unique approach by combining
the theory of the language with the hands-on coding exercises. It
will start from the absolute zero point, from ‘Hello World!’, and
will cover all of the fundamental concepts. You will learn and
practice all the basics of Python programming in this book.
Here is what you will find in this book:
Theory: In each topic, we will cover all the Theoretical Details
with example coding.
True/False (42 questions): We will have True/False questions
at the end of the sub-topics.
Coding Exercises (120 questions): At the end of each chapter,
we will have Coding Exercise, Quizzes, of 10 questions.
Projects (3): We will have 3 Projects in this book. You will
learn how to apply Python concepts on real world problems.
Assignments (3): After each project, you will have an
Assignment. These assignments will let you build the project from
scratch on your own.
Final Exam: At the end of this book you will have the Final
Exam. It is a multiple choice exam with 20 questions and a limited
duration. The exam will let you to test your Python level.
How to Contact Us
Please feel free to get in contact with the author. To comment
or ask technical questions about this book, you can send email to
[email protected].
OceanofPDF.com
1. Introduction
Who Is This Book For?
The goal of this book is to help students to learn Python
programming language in a hands-on and project-based manner.
With its unique style of combining theory and practice, this
book is for:
OceanofPDF.com
2. Getting Started
A Brief History of Python
Python is a widely used and general-purpose, high-level
programming language. Its design philosophy emphasizes code
readability and its syntax allows programmers to express concepts
in fewer lines of code. Python is dynamically-typed and garbage-
collected. It supports multiple programming paradigms, including
procedural, object-oriented and functional programming.
It was initially designed by Guido van Rossum in the late
1980s and developed by Python Software Foundation. Python was
named after the BBC TV show Monty Python's Flying Circus.
In 1991, Van Rossum published the code labeled version 0.9.0.
Then in 1994, Python reached version 1.0.
Python 2.0 was released in 2000, which started the Python 2.x
family that has been used widely for almost a decade. The final
release, Python 2.7.18, occurred on 2020 and marked the end-of-
life of Python 2. As of January 1st, 2020 no new bug reports, fixes,
or changes will be made to Python 2, and Python 2 is no longer
supported.
Python 3.0 (also called "Python 3000" or "Py3K") was released
on December 3, 2008 and it was a major, backwards-incompatible
release. Since Python 3.0 broke backward compatibility, much
Python 2 code does not run unmodified on Python 3. Although
some old projects still use Python 2, the preferred approach is using
Python 3. As of this writing, the latest version is 3.9, but every code
we write in this book should run on Python 3.6 or any later version.
Installing Anaconda
To begin Python programming we have to install Python and a
development environment (IDE - Integrated Development
Environment) on our machine. We can install Python directly from
www.python.org but to make things easier for you, we will not do
this. Instead, we will install Anaconda which is one the most
popular platforms for Python distribution. Anaconda comes with
built-in Python in it, so you do not need a standalone Python
installation. Anaconda also includes JupyterLab which will be our
IDE for this book.
To install Anaconda, navigate to www.anaconda.com. This is
the official web site of Anaconda, and here under Products, select
the Individual Edition. Individual Edition is free and is more than
enough for every development we do in this book.
The next screen is a very important step. There are two options
here. The first one is about the PATH environment variable. It asks
you to add Anaconda to your PATH variable. You should select it,
because we want to run Anaconda prompt directly from command
line interface. (See Figure 2-2 above). To be able to run “conda
…” prompts we need to add Anaconda to the PATH.
The second option is about registering Anaconda as the default
Python interpreter. You may have different installations of Python
on your machine. You can either check this option or not, it’s up to
you. And if you check, it will not affect other Python installations. I
prefer to check it and continue.
Figure 2-8: Advanced installation options
And now the installation will start. It may take several minutes
depending on your machine configuration. After it finishes
installing, just click on “Next and Finish” to finalize installation.
Figure 2-9: Installation progress
On the left side of JupyterLab, you see the File Browser (1) to
organize your files and folders and Running Terminals and Kernels
(2) to manage the kernel sessions, which we will see later. And on
the right hand side you see the code editor, which we will be using
to write Python code.
You can create, rename, copy or delete files and folders in the
file browser. It works like the regular file operations on your
operating system. To follow the topics more easily, I recommend
you to copy the entire book content folders and files from GitHub
repository on your machine. When you copy the files to your local
machine, please make sure you can access them via JupyterLab file
browser. And here is the complete folder structure for this book.
Figure 2-14: Folder structure
If you see the same output without getting any errors, then
congratulations, you managed to setup Anaconda and JupyterLab
successfully. Your development environment is up and running
now.
Before moving to the next section, I want to show another way
to check whether Anaconda is installed on your machine or not.
This time we will check it from the Anaconda Prompt. From the
start menu open Anaconda Prompt, and type in conda --version .
It should display the Anaconda version installed on your machine.
And in a new line this time type python --version. It should
display the Python version which is installed with Anaconda.
Menu Bar
The menu bar at the top of JupyterLab has top-level menus that
expose actions available in JupyterLab with their keyboard
shortcuts. The default menus are:
Left Sidebar
The left sidebar contains a number of commonly-used tabs,
such as a file browser, the list of running kernels and terminals, the
command palette, the notebook cell tools inspector, the tab list (list
of tabs in the main work area) and the extension manager.
The left sidebar can be collapsed or expanded by selecting
“Show Left Sidebar” in the View menu or by clicking on the active
sidebar tab.
Main Work Area
The main work area in JupyterLab enables you to arrange
documents (notebooks, text files, etc.) and other activities
(terminals, code consoles, etc.) into panels of tabs that can be
resized or subdivided. When you double click on a file in the file
browser, it opens up in the main work area. And here you can edit
the file content. Notebook files in the main work area consist of
two parts, the toolbar and the code editor.
Let’s start working in JupyterLab. First we will create a folder.
To do this, navigate to 2_Getting_Started folder inside JupyterLab.
And inside this folder create a new folder named
3_Jupyter_Notebook_Basics. Finally create a notebook file inside
this folder and name it as Jupyter_Basics.ipynb.
You can find the list of all operations related to kernels under
the Kernel menu in the top menu bar.
The main work area is the place where you write, edit and run
Python code. Two main parts in the work area are the Toolbar and
the Code Editor. Toolbar gives a quick way of performing the most-
used operations within the notebook, by clicking on an icon.
Here are the buttons in the toolbar and their functionalities:
Save: Save the notebook contents and create checkpoint
Insert: Insert a cell below the active cell
Cut: Cut the selected cells
Copy: Copy the selected cells
To run the cell or a block cells, you first select what you want
to run and then click on the Run button in the toolbar. When you
run a cell, you will see the cell output and a number in the square
brackets next to cell. That number is the running order of that
particular cell in the current active kernel.
Now that we know what the virtual environments are, let’s see
them in Anaconda Navigator.
Now you can see your new virtual environment in the venv list.
It has also been activated after creation. We will talk about
activating a venv in this section. You will also see some packages
been already installed in your venv. These are mandatory packages
for Python to run. And the package named pip is the most
important one. In standalone Python installation, we use pip to
install other Python packages. Since we are using Anaconda, we
will be using conda to install packages, instead of pip.
Figure 2-32: The new venv and important packages in it
Figure 2-34: List of Virtual Environments on our machine with the base
as the active one.
OceanofPDF.com
3. The First Program
Starter Notebook File
At the beginning of each chapter we will start with a Starter
Notebook File. This file includes only the chapter title. And you are
expected to write your code in this file while learning the concepts.
You can either download it from Github Repository or you can
create it manually. If you choose to download it you can skip this
section go to the next one, Hello World. If you want to create it
from scratch, here is how you can do this.
As you may remember from the previous chapter, in Python
everything starts with virtual environments (venv). That’s why we
have to activate the related venv (python_intro). To do this, let’s
first see the venv list on our machine. Open Anaconda Prompt, type
conda env list and hit enter. If you see python_intro in the list it
means you have successfully created that venv. If not, then you
have to repeat Python Virtual Environments section in Chapter 2.
Let’s move on and activate the python_intro virtual
environment. Type conda activate python_intro into the
Anaconda Prompt and hit enter. Now this venv should be active.
You should see its name in the parentheses at the beginning of the
command line.
Now, as the final step let’s launch JupyterLab. In the Anaconda
Prompt, type jupyter lab and hit enter. The JupyterLab should
open up in a new tab in your default web browser.
Inside JupyterLab, navigate to folder 3_The_First_Program,
create a new notebook file and rename it as First_Program.ipynb.
This will be the file we will be using in this chapter. Select the first
cell and change its type as Markdown. We will write the chapter
title in this cell. Type the text # First Program in it. The #
symbol means <h1> tag in HTML. Which means it will be large
enough to server as the title cell. If you run the cell, you will see
the formatted chapter title.
Here is the starter notebook file for this chapter:
Hello World!
Finally we are ready to write our first program. A program is a
procedure that tells the computer how to succeed a given task.
Probably the one below will be the easiest Python program that you
can write. It will just print Hello World! on the screen. And here
it is:
[
1 1 print("Hello World!")
]:
[
1 Hello World!
]:
[
2 1 print("This is the second line of code")
]:
[
2 This is second the line of code
]:
Arithmetic Operations
Arithmetic Operations in Python are as follows:
Addition: +
Subtraction: -
Multiplication: *
Division: /
Exponential: **
Modulo Operator: %
Floor Division: //
Let’s pass 4.6 now, a floating point number. As you may guess
Python will give you float as its type. Don’t worry we will see all
of these types and more in detail.
[2
1 type(4.6)
1]:
[2
float
1]:
You have completed your first program now. You have some
basic understanding about types and values and how to print those
using Python functions. Now it’s time to test what you have learned
so far. First you will have True/False questions and then a quiz of
10 questions.
OceanofPDF.com
True / False - First Program
For each questions below, decide whether it’s true or false.
Q1:
print(Hello World) statement is going to print "Hello
World" on the screen.
True
False
Q2:
The output of type(12) statement, is different from the
output of type("12") statement.
True
False
OceanofPDF.com
SOLUTIONS - True / False - First Program
Here are the solutions for True / False questions.
S1:
print(Hello World) statement is going to print "Hello
World" on the screen.
True
False
It’s false because the statement inside the print() function
should be in quotes like print("Hello World").
S2:
The output of type(12) statement, is different from the
output of type("12") statement.
True
False
It’s true because type(12) will print int but type("12") will
print str.
OceanofPDF.com
QUIZ - The First Program
Now it’s time to solve the quiz for this chapter. You can
download the quiz file, QUIZ_First_Program.ipynb, from the
Github Repository of this book. It is in the folder named
3_The_First_Program.
At the end of each chapter you will have a quiz. There will be
10 questions in each quiz. Below each question, you will find the
expected answer for it. And you will try to find that answer with
Python. Quizzes are distributed as Jupyter Notebook files. After
you download the quiz file, you should put it in a folder where you
can access it inside the JupyterLab. You will also find the quiz
questions in this section. But remember, you cannot learn a
programming language just by reading it. You should code and try
it yourself.
In the next section you will find the quiz solutions. But before
reading the solutions, you are expected to solve the questions on
your own. Every question in the quiz aims to teach you a specific
topic, so make sure you try to solve each one of them. After you try
and even after you solve them, I strongly recommend to check the
solutions provided here. It is very important because the solutions
in this book aim to teach you different ways to approach Python
problems. Here are the questions for this chapter:
QUIZ - First Program:
Q1:
How many seconds are there in 24 minutes and 36 seconds?
Answer: 1476
Q2:
The Batmobile, having average speed of 80 km/hour will
travel from Gotham to Arkham. We know the distance
between Gotham and Arkham, which is 450 km's. How many
minutes will it take for Batmobile to travel from Gotham to
Arkham?
Answer: 337.5
Q3:
What is 14th power of 2?
Answer: 16384
Q4:
Can you print "Python, I love you :)" on the screen?
Answer: Python, I love you :)
Q5:
Can you find the type of number 15?
Answer: <class 'int'>
Q6:
Can you print the type of text "15"?
Answer: <class 'str'>
Q7:
What is the remainder when we divide 49 by 5?
Answer: 4
Q8:
What is the floor division (integer division) result of
dividing 49 by 5?
Answer: 9
Q9:
What is the average of sum of squares of integers from 1 to
5?
(average of this sum -> 12 + 22 + 32 + 42 + 52)
Answer: 11.0
Q10:
Solve this equation for x with Python:
x 2 = 4 3 + 17
What is the value of x?
Answer: 9.0
OceanofPDF.com
SOLUTIONS - The First Program
Here are the solutions for the quiz for Chapter 3 - The First
Program.
SOLUTIONS - First Program:
S1:
Since there are 60 seconds in one minute, there will be 24 *
60 seconds in 24 minutes. Then we add 36 seconds. And the
total is 24 * 60 + 36 seconds.
[
1 1 24 * 60 + 36
]:
[
1 1476
]:
S2:
The number of hours that the Batmobile will get to Arkham is
distance / average speed, which is 450 / 80 hours. And if we
convert this result into minutes it will be (450 / 80) * 60.
[
2 1 (450 / 80) * 60
]:
[
2 337.5
]:
S3:
The result of 2 to the power of 14 is 2 1 4 which is denoted as
2**14 in Python syntax.
[ 1 2**14
3
]:
[
3 16384
]:
S4:
In Python, to print text we use print() built-in function.
Inside the parentheses of the function, we pass the text we
want to print. This is called the parameter and here our
parameter is the text of "Python, I love you :)". So the syntax
is print("Python, I love you :)").
[
4 1 print("Python, I love you :)")
]:
[
4 Python, I love you :)
]:
S5:
To get the type of any object in Python we use, type() built-
in function. The parameter will be the object which want to
get the type. Here it is an integer value of 15. So the syntax
is type(15).
[
5 1 type(15)
]:
[
5 int
]:
S6:
In this question we will print the type of the text “15”. Be
careful, it is not an integer now. It is a text, because it is
surrounded with quotation marks. Actually it is called a
String (str), and you will learn Strings in very detail later
on. Here I just wanted to show you the difference between,
15 and “15” for Python. The code to get its type is
print(type("15")).
[
6 1 print(type("15"))
]:
[
6 <class 'str'>
]:
S7:
To get the remainder we use Modulo Operator (%). It is used
just like ordinary math operators. So the result of 49 % 5 is
4.
[
7 1 49 % 5
]:
[
7 4
]:
S8:
The floor division (integer division) is a special kind of
division operation. It gives you the floor of the division
result (dividend). The floor of a real number is the largest
integer that is less than or equal to that number. In this
question we need the result of 49 // 5. If it was a normal
division then the result would be 9.8. But the operation here
is the floor division, so we will take the largest integer that
is less than or equal to 9.8, which is 9.
[
8 1 49 // 5
]:
[ 9
8
]:
S9:
In this question, we need the average of this summation:
1 2 +2 2 +3 2 +4 2 +5 2 . Since there are 5 terms here
we will divide this sum into 5. So the
solution is (1**2 + 2**2 + 3**2 + 4**2 + 5**2) / 5 which
is 11.0.
[
9 1 (1**2 + 2**2 + 3**2 + 4**2 + 5**2) / 5
]:
[
9 11.0
]:
S10:
Throughout this book, we will have some mathematical
operations. But the aim is to get you hands-on with problem
solving in Python, not to learn mathematics. In this question
we have a math equation to solve for x. To find x, we need to
get the square root of the right hand side, 4**3 + 17. And the
square root is nothing but the power of 1/2. So the solution is
(4**3 + 17)**(1/2).
[1
1 (4**3 + 17)**(1/2)
0]:
[1
9.0
0]:
OceanofPDF.com
4. Variables
What is a Variable?
Inside JupyterLab, navigate to folder 4_Variables. Then create
a new notebook file and rename it as Variables.ipynb. This will be
the starter file that we will be using in this chapter. You can either
download it from Github Repository or you can create it manually.
If you want to create it from scratch, revisit the section Starter
Notebook File in Chapter 3.
Variables are one the main building blocks of programming. A
Variable is a name referring to a value. It is the identifier of a
specific part of the computer memory which holds that value in it.
In other words, it is the name of the address for that value in the
memory.
Let’s define another variable. This time let’s make its value as a
floating point number. And we will print its value right after we
define it.
[
5 1 pi = 3.1415
]:
2 print(pi)
[
5 3.1415
]:
Reassigning:
In Python, you can reassign a variable. Reassigning means that
you can connect a different value with a previously assigned
variable. In other words, you can change the value which lives in
that memory address. The address which your variable is linked to.
Remember our first variable was myNumber and we assigned
to value 12 to it. Now let’s reassign it. We want its new value to be
“Python Hands-On”.
[
9 1 myNumber = "Python Hands-On"
]:
2 myNumber
[
9 Python Hands-On'
]:
OceanofPDF.com
True / False - What is a Variable?
For each questions below, decide whether it’s true or false.
Q1:
Variable is a name referring to a value.
True
False
Q2:
Value is the name of the address of a variable.
True
False
OceanofPDF.com
SOLUTIONS - True / False - What is a Variable?
Here are the solutions for True / False questions.
S1:
Variable is a name referring to a value.
True
False
It’s true because “Variable is a name referring to a value”.
S2:
Value is the name of the address of a variable.
True
False
It’s false because the opposite is true: Variable, is the name
of the address of a value.
OceanofPDF.com
Variable Names
In Python we have some conventions while declaring the
variables. In general variable names:
1number
3_rd_position
average speed
step size
You will get the same error no matter where you use @ char in
the variable name. Let’s see it in the middle of the name.
[1
1 ten@th = 10
4]:
2 ten@th
[1
File "<ipython-input-14-7f06a1bf1aba>", line 1
4]:
ten@th = 10
^
SyntaxError: cannot assign to operator
Important Note:
Python is CASE SENSITIVE. Case sensitive means that a is
different from A . The variable average_speed is different from
the variable Average_Speed . Let’s prove this with an example.
We will define a variable with small letter a and assign value
10 to it. Then we will define another variable with capital letter A
and assign value 45 to it. And finally we will print the values of
both variables. And you will see they are different.
[1
1 a = 10
7]:
2 A = 45
[1
1 print(a)
8]:
2 print(A)
[1
10
8]:
45
Unicode Characters:
Although most Python environments support Unicode
Characters in variable names, I strongly recommend not use them
while declaring your variables. You should only use English
Alphabet letters. The safest way is only using ASCII (American
Standard Code for Information Interchange) characters, which are
allowed.
Let’s say you want to define a variable with name omega (Ω),
which is a letter in Greek alphabet. Instead of using its Greek
symbol, write the full name, like omega .
[2
1 Ω_omega = 80
0]:
2 print(Ω_omega)
[2
80
0]:
You may not get an error if you use the symbol, but to be on
the safe side, avoid using non-ASCII characters.
[2
1 omega = 80
1]:
2 print(omega)
[2
80
1]:
OceanofPDF.com
True / False - Variable Names
For each questions below, decide whether it’s true or false.
Q1:
We can declare a variable named True as True
= correct.
True
False
Q2:
We can create a variable named 10_mults as 10_mults
= [10, 20, 30, 40, 50].
True
False
OceanofPDF.com
SOLUTIONS - True / False - Variable Names
Here are the solutions for True / False questions.
S1:
We can declare a variable named True as True
= correct.
True
False
It’s false because: True is a Python Keyword, so you cannot
use it as a variable name.
S2:
We can create a variable named 10_mults as 10_mults
= [10, 20, 30, 40, 50].
True
False
It’s false because: Variable Names cannot start with a
number.
OceanofPDF.com
Python Data Types
As you learned in the previous chapter, every value has a type.
Variables can store data of different types, and different types can
do different things. You will learn more about the types in the next
chapters.
Python has the following built-in data types:
Text Type : str
Numeric Types : int, float, complex
Sequence Types : list, tuple, range
Mapping Type : dict
Set Types : set, frozenset
Boolean Type : bool
Binary Types : bytes, bytearray, memoryview
Text Type:
Strings are text types in Python. In other words they are
sequences of character data. The string type in Python is called
str . String literals may be delimited using either single (‘) or
double quotes (“). All the characters between the opening delimiter
and matching closing delimiter are part of the string:
Let’s see a string with single quotes:
[2
1 # str (string) with single quotes
2]:
2 text = 'This is a single quoted string'
3 type(text)
[2
str
2]:
And now let’s define another string but with double quotes this
time.
[2
1 # str (string) with double quotes
3]:
2 text = "This is a double quoted string"
3 type(text)
[2
str
3]:
Numeric Types:
There are three numeric types in Python: int, float, complex
Int: int, or integer, is a whole number, positive or negative,
without decimals of unlimited length. Examples: 1, 400, -500780,
999999999999 etc.
[2
1 # int (integer)
4]:
2 integer_number = 16
3 type(integer_number)
[2
int
4]:
Sequence Types:
In Python, sequence types are: list, tuple, range
List: Lists are used to store multiple items in a single variable.
Lists are created using square brackets, [] .
[2
1 # list
7]:
2 my_list = [1, 2, 3, 4, 5]
3 print(my_list)
[2
[1, 2, 3, 4, 5]
7]:
Let’s see how we loop over a range. You haven’t learned the
loops yet but don’t worry, it is very easy to understand.
[3
1 for i in up_to_five:
1]:
2 print(i)
[3
0
1]:
1
2
3
4
And that’s it. You told Python to loop over up_to_five range
and it did. It printed the integers from 0 to 5 (not included).
Mapping Type:
Mapping Type is dictionary (dict) in Python.
Dictionary: dict, or dictionary, is used to store data values in
key:value pairs. A dictionary is a collection which is does not
allow duplicate keys. Dictionaries are written with curly brackets,
{} , and have keys and values.
[3
1 # dictionary
2]:
2 user = {
3 "name": "Musa",
4 "lastname": "Arda",
5 "language": "Python"
6 }
7
8 print(user)
[3 {'name': 'Musa', 'lastname': 'Arda', 'language':
2]: 'Python'}
Set Types:
In Python set types are: set and frozenset. We will only cover
set in this book.
Set: Sets are used to store multiple items in a single variable. A
set is a collection of unique items. In other words, set items do not
allow duplicate values.
[3
1 # set
4]:
2 set_of_numbers = {1, 2, 2, 3, 1, 3, 4, 4, 1}
3 print(set_of_numbers)
[3
{1, 2, 3, 4}
4]:
Boolean Type:
Boolean type is simply bool in Python.
In programming, most of the time, you need to evaluate an
expression. Evaluation is determining if the expression is either
True or False. This is where Booleans came into the picture.
Boolean values are two constant objects True and False . They
are used to represent truth values in Python.
Let’s see how we define bool variables in Python.
[3
1 # booleans
6]:
2 correct = True
3 wrong = False
OceanofPDF.com
True / False - Python Data Types
For each questions below, decide whether it’s true or false.
Q1:
Numeric Types are int, float and complex.
True
False
Q2:
We use type() function to see the type of a variable.
True
False
OceanofPDF.com
SOLUTIONS - True / False - Python Data Types
Here are the solutions for True / False questions.
S1:
Numeric Types are int, float and complex.
True
False
OceanofPDF.com
Type Conversion
You can easily convert one type to another in Python. All you
have to do is to call special functions like int() , float() , str()
etc. These are built-in functions for type conversion. There are
many of them, but for now we will start with the first three of them.
int(): int() function converts strings and floating point numbers
into integers.
Let’s define a string first then convert it to an integer.
[3
1 # a string variable
9]:
2 str_num = "24"
3 type(str_num)
[3
str
9]:
As you see in cell 41, we do all the operations in one cell. First
we create a floating point variable my_decimal . Then we convert
it to integer. The int() function creates a new variable for us and
the type of this new variable is integer. We named this new variable
as my_int . And finally we print the type of my_int which is int .
Here you should note that, the type conversion in Python
doesn’t affect the original variable. In other words, the variable you
pass into the parentheses of int() function does not change. int()
function gives you a new variable. And this new variable is
independent of the original one.
float(): float() function converts strings and integers to floating
point numbers.
[4
1 my_float = float(4)
2]:
2 my_float
[4
4.0
2]:
OceanofPDF.com
True / False - Type Conversion
For each questions below, decide whether it’s true or false.
Q1:
The result of the code line below is float.
type("3.84")
True
False
Q2:
If you run the code lines below it will print int as the
output.
num = int(3.14159)
print(num)
True
False
OceanofPDF.com
SOLUTIONS - True / False - Type Conversion
Here are the solutions for True / False questions.
S1:
The result of the code line below is float.
type("3.84")
True
False
OceanofPDF.com
Python Comments
When writing code in Python, it’s important to make sure that
your code can be easily understood by other developers. One of the
main ways to increase readability of your code is by using
comments.
Comments are lines of code that are ignored by the Python
Interpreter. Python Interpreter is the application to run your
Python code. It is included in the Anaconda installation. Interpreter
runs your Python code line by line, from the beginning to the end.
But it does not run comment lines in the code. It just ignores them.
You can write comments in two ways in Python: One Line
Comment or Multi-Line Comments.
Multi-Line Comments:
Multi-Line Comments are created with a pair of triple quotes,
either single quote or double quotes.
[4
1 """
9]:
2
3 Multi-Line Comment
4
5 In Python, Multi-Line comments,
6 are created via a pair of triple quotes: """ """
7
8 As in this case.
9
10 """
OceanofPDF.com
True / False – Python Comments
For each questions below, decide whether it’s true or false.
Q1:
If you run the code below it will print 10 as the output.
# define a variable as x
# x = 10
# print its value
print(x)
True
False
Q2:
In Python;
for one line comment we use a pair of tree double
quotes: """ """,
for multi-line comments we use: # symbol.
True
False
OceanofPDF.com
SOLUTIONS - True / False - Python Comments
Here are the solutions for True / False questions.
S1:
If you run the code below it will print 10 as the output.
# define a variable as x
# x = 10
# print its value
print(x)
True
False
It’s false. You will get an error if you run this code. The
error will be: “NameError: name 'x' is not defined”. It means
you have not defined a variable named x yet. As you see in
the code the definition of x is a comment line, # x = 10.
Which means Python Interpreter will ignore that line. So
there is no variable named x in your code. And the print(x)
statement will raise an error.
S2:
In Python;
for one line comment we use a pair of tree double
quotes: """ """,
for multi-line comments we use: # symbol.
True
False
OceanofPDF.com
Numeric Operations
We have already seen Arithmetic Operations in Chapter 3. But
we didn’t know much about variables at that time. Now that we
know how to define variables and what the numeric type variables
are, we will revisit the Arithmetic Operations again. But this time
we will call it as Numeric Operations.
Numeric Operations means the operations we do on numeric
data types in Python, which are int, float and complex . We will
be using int and float types in this section. Let’s see numeric
operations one by one.
To begin with, we will create two numeric variables: a and b .
[5
1 # define two variables
3]:
2
3 a = 30
4 b = 7
Now let’s see all the arithmetic operations with these variables:
addition, subtraction, multiplication, division, floor division
(integer division), modulo, and power (exponentiation).
[5
1 print(a + b) # addition
4]:
[5
37
4]:
[5
1 print(a - b) # subtraction
5]:
[5
23
5]:
[5
1 print(a * b) # multiplication
6]:
[5
210
6]:
[5
1 print(a / b) # division
7]:
[5
4.285714285714286
7]:
[5
1 print(a // b) # floor division (integer division)
8]:
[5
4
8]:
[5
1 print(a % b) # modulo
9]:
[5
2
9]:
[6
1 print(a**b) # power
0]:
[6
21870000000
0]:
Inside the print() function in cell 61, the first operation is the
division, 14 / 7 which results in 2.0 . The second one is the
multiplication, 4 * 5 which is 20 . And then addition and
subtraction. The final result is 13.0 . Since we have one floating
point number in the calculation, the whole result becomes a float.
Shorthand Operations:
In Python, for most of the arithmetic operations we have the
shorthand form. Let’s see them in action.
[6
1 a = 9
2]:
2
3 # increment a by 1
4 a = a + 1
5 a
[6
10
2]:
We add 4 to the old value of a , which was 10. And now the
final value of a is 14. This is ordinary way for the addition
operation. Now let’s see the shorthand increment ( += ).
[6
1 # Shorthand Increment -> +=
4]:
2
3 a += 1 # a = a + 1
4 a
[6
15
4]:
OceanofPDF.com
SOLUTIONS - True / False - Numeric Operations
Here are the solutions for True / False questions.
S1:
The result of Shorthand Addition Operator +=, will
print 20 on the screen.
x = 20
x += 20
print(x)
True
False
It’s false. The last value of x is 40.
S2:
The last value of p will be 12.0.
p = 8
p = p * 3
p /= 2
True
False
It’s true. First p is 8. Then it will become 24 when
multiplied by 3. And finally it will become 12.0 via /=
operation.
OceanofPDF.com
String Operations
In Python, Strings are displayed via double quotes (" ") or
single quotes (' '). There is one important point that you should
note for using the quotes. Opening and Closing Quotes should
match. Which means, if you start with a double quote, then you
should end with a double quote. Same goes for single quotes also.
[6
1 string_double = "This is a double quotes string."
8]:
2 print(string_double)
[6
This is a double quotes string.
8]:
What if, you need to use both types of quotes in the same
string? Let’s see it with an example:
[7
1 # If you need to use both in the same string
0]:
2
3 single_in_double = "Python's the best language."
4 print(single_in_double)
[7
Python's the best language.
0]:
In cell 70, we use single quotes inside the double quotes. Now
let’s see how we can use double quotes inside the single ones.
[7 double_in_single = 'Jenny said "Run, Forest, run",
1
1]: and he did.'
2 double_in_single
[7
‘Jenny said "Run, Forest, run", and he did.'
1]:
Now let’s see if we confuse the single quotes with double ones
in the same string. We will start with double quotes but end up with
single ones.
[7
1 false_usage = "This is a 'double quote'
2]:
[7
File "<ipython-input-72-8168303684c0>", line 1
2]:
false_usage = "This is a 'double quote'
^
SyntaxError: EOL while scanning string literal
subtraction (-)
division (/)
Now let’s try to add them and see the result. Before reading the
next lines and running it on your computer, try to make a guess.
What do you expect if you add two strings together?
[7
1 # Addition in Strings (Concatenation)
6]:
2
3 first + second
[7
‘SunnyDays'
6]:
OceanofPDF.com
True / False – String Operations
For each questions below, decide whether it’s true or false.
Q1:
In Python, we can use double quotes (" ") and single
quotes (' ') to create Strings.
True
False
Q2:
The Interpreter will run the code below without an
error.
print("Python's Syntax allow create strings with both '
and " quotes.")
True
False
OceanofPDF.com
SOLUTIONS - True / False - String Operations
Here are the solutions for True / False questions.
S1:
In Python, we can use double quotes (" ") and single
quotes (' ') to create Strings.
True
False
It’s true. In Python, we can use double quotes (" ") and
single quotes (' ') to create Strings.
S2:
The Interpreter will run the code below without an
error.
print("Python's Syntax allow create strings with both '
and " quotes.")
True
False
It’s false. The double (" “) quotes in the middle will finalize
the string. So the last part which is, " quotes.", will raise
SyntaxError.
OceanofPDF.com
Keywords
Python has a set of keywords that are reserved words and
cannot be used as variable names, function names, or any other
identifiers. All the Python keywords are in the keyword package.
To be able to use a package in Python, we use import
statement. Import itself is a keyword and it’s used to import
packages in your project. We will be using import statement quite a
lot during this book. So, don’t worry, you will get used to it.
[8
1 # first import the keyword package
1]:
2
3 import keyword
And that’s it. You import the keyword package and it’s ready to
use. Let’s see the complete list of Python keywords. The keyword
list is in a variable called kwlist inside keyword package. To get
it, we use the syntax of <package_name>.<variable_name> ,
like keyword.kwlist . We will get it and assign it to a local
variable.
[8
1 # Get keyword list
2]:
2 # And assign this list to a variable
3
4 python_keywords = keyword.kwlist
5
6 # print keywords
7 print(python_keywords)
['False', 'None', 'True', 'and', 'as', 'assert', 'async',
'await', 'break', 'class', 'continue', 'def', 'del', 'elif',
[8
'else', 'except', 'finally', 'for', 'from', 'global', 'if',
2]:
'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
And in the output of cell 82, you see the complete list of
keywords in Python. All you have to do is to call keyword.kwlist
variable, assign it to a variable and then print it.
You are not allowed to use any of these keywords as variable
names. It’s forbidden and you will get SyntaxError if you try.
Let’s assume we want to create a variable with name “import”.
[8
1 import = 45
3]:
[8
3]: File "<ipython-input-83-82fef50b7975>", line 1
import = 45
^
SyntaxError: invalid syntax
None :
Let’s check another one. This time check for the word
“None”.
[8
1 keyword.iskeyword('None')
6]:
[8
True
6]:
As you may guessed, the word “None” is also a Python
keyword. None is the special keyword used to define a null value,
or no value at all. Simply it means ‘nothing’. None is not the same
as 0, False , or an empty string. It is a data type of its own
( NoneType ) and only None can be None .
Let’s try another one. We will check the word “return” this
time.
[8
1 keyword.iskeyword('return')
7]:
[8
True
7]:
OceanofPDF.com
True / False - Keywords
For each questions below, decide whether it’s true or false.
Q1:
We import the keyword package to see Python
Keywords as below:
import keyword
True
False
Q2:
To see whether word for is a keyword for Python or not
we can use:
for.iskeyword()
True
False
OceanofPDF.com
SOLUTIONS - True / False - Keywords
Here are the solutions for True / False questions.
S1:
We import the keyword package to see Python
Keywords as below:
import keyword
True
False
It’s true. import keyword is the right syntax to import the
keyword package.
S2:
To see whether word for is a keyword for Python or not
we can use:
for.iskeyword()
True
False
It’s false. To check if for is a Python keyword the right
syntax is: keyword.iskeyword('for').
OceanofPDF.com
QUIZ - Variables
Now it’s time to solve the quiz for this chapter. You can
download the quiz file, QUIZ_Variables.ipynb, from the Github
Repository of this book. It is in the folder named 4_Variables. Here
are the questions for this chapter:
QUIZ - Variables:
Q1:
Fill in the blanks to print "Hello World!":
.....("Hello World!")
Q2:
Fix the typo (coding error) below:
print("Alice's Adventures')
Q3:
Convert the line below into a Comment Line. (One line
comment)
I am a Comment Line :)
Q4:
Convert the lines below into comment lines. (multi-line
comment):
This comment line
Is multiple lines comment
Not just one...
Q5:
Print the text below exactly as it is shown:
Who in the world am I?
Ah, that's the great puzzle!
'Alice in Wonderland'
Q6:
Create a variable named car_model and assign the value
'Volvo' to it.
Q7:
Create a variable named x and set its value to 50.
Q8:
Create two variables x and y. And assign them as 20 and 50
respectively.
Create a variable z, and assign x + y to it.
Then print z.
Complete the missing codes below according to the
declarations.
1 x = 20
2 y = 80
3
4 ... = x + y
5 print(...)
Q9:
Remove forbidden characters from the variable name, and
print its value.
2my-first_variable*β = "Beta"
Answer: Beta
Q10:
Fill in the blanks to assign three variables at the same time.
And then print their values.
Hint: In Python you can do multiple assignments at once.
Python assigns variables from right to left.
x ... y ... z = "Python"
Answer:
Python
Python
Python
OceanofPDF.com
SOLUTIONS - Variables
Here are the solutions for the quiz for Chapter 4 - Variables.
SOLUTIONS - Variables:
S1:
It asks to fill the blanks. We have to use print() function to
be able to print something on the screen. And the solution is:
[
1 1 # S 1:
]:
2
3 print("Hello World!")
[
1 Hello World!
]:
S2:
The typo here is about the quotes in strings. Since the text
starts with double quotes, it should end also with them. But
in the question the quote is a single quote.
[
2 1 # S 2:
]:
2
3 print("Alice's Adventures")
[
2 Alice's Adventures
]:
S3:
Single line comments are created with # sign. So to convert
any line or code segment into the Python comments we
simply put # sign in front of them.
[
3 1 # S 3:
]:
2
3 # I am a Comment Line :)
S4:
To convert a text into multi-line comments, we surround it
with a pair of triple quotes, which can be either double or
single quotes.
[
4 1 # S 4:
]:
2
3 """
4 This comment line
5 Is multiple lines comment
6 Not just one...
7 """
S5:
We will solve this questions in two different ways.
The first way is to use three separate print() statements.
The second way is more elegant. Into a single print()
function, we will pass the text as it is, inside a pair of triple
quotes, which will make it a formatted text. A pair of triple
quotes (either single or double) is also used for printing
formatted text.
[
5 1 # S 5:
]:
2
3 # 1st Way
4 print("Who in the world am I?")
5 print(" Ah, that's the great puzzle!")
6 print(" 'Alice in Wonderland'")
7
8 # 2nd Way
9 print("""
10 Who in the world am I?
11 Ah, that's the great puzzle!
12 'Alice in Wonderland'
13 """)
[ Who in the world am I?
5 Ah, that's the great puzzle!
]: 'Alice in Wonderland'
S6:
[
6 1 # S 6:
]:
2
3 car_model = 'Volvo'
4 car_model
[
6 ‘Volvo'
]:
S7:
[
7 1 # S 7:
]:
2
3 x = 50
4 x
[
7 50
]:
S8:
[
8 1 # S 8:
]:
2
3 x = 20
4 y = 80
5
6 z = x + y
7 print(z)
[
8 100
]:
S9:
Here, there are multiple mistakes in the variable name. First
of all it cannot start with a number, so we will delete the
number 2 at the beginning. The new name is my-
first_variable*β. Now the next problem is the dash sign, or
minus, (-). You are not allow to use it in the variable name
because it has a special meaning as a math operation. You can
use underscore (_) character instead of minus sign. So let’s
replace “-“ with “_”. The new name is my_first_variable*β.
And there are two special characters which are forbidden, *
and β. We will remove them. And finally our variable name is
my_first_variable.
[
9 1 # S 9:
]:
2
3 my_first_variable = "Beta"
4 print(my_first_variable)
[
9 Beta
]:
S10:
In Python you can assign multiple variables with the same
value at once, by using = consecutively. They will all get
created and assigned simultaneously. Before doing a multiple
assignment, let us first create every variable, x, y and z one
by one. You will see that it is exactly the same thing to create
them all at once. Here is the solution:
[1
1 # S 10:
0]:
2
3 # create them one by one
4 # x = 'Python'
5 # y = 'Python'
6 # z = 'Python'
7
# create them all at once with multiple
8
assignment
9 x = y = z = 'Python'
10
11 # now print their values
12 print(x)
13 print(y)
14 print(z)
Python
[1
Python
0]:
Python
OceanofPDF.com
5. Functions I
What is a Function?
In Chapter 4, we learned one of the main building blocks of
programming, Variables. In this chapter we will learn another
building block which is Functions. Functions are crucial to every
computer program. They are very useful and have many
advantages. One of the most prominent advantage of functions is,
they prevent code repetitions. In this book, we have two chapters
dedicated to functions.
In computer programming, a Function is a special code
fragment written to perform a specific task. A function only runs
when it is called. It may have arguments (parameters). And it may
return data as a result.
Calling a Function:
To call a function you just write the function name followed by
parentheses: function_name() . Here the function takes no
arguments, because the parentheses are empty. This is how we call
a function without any arguments.
If we want to call a function with arguments we have to pass
them inside the parentheses: function_name(argument_1,
argument_2, ...)
So far, we have called some of the Python built-in functions.
Like print(), type(), int(), float(), str() , etc. Let’s call them
again and this time our focus is how we call them and how we pass
arguments to them.
We will start with the type() function:
[
1 1 type(42)
]:
[
1 1 int
]:
OceanofPDF.com
SOLUTIONS - True / False - What is a Function?
Here are the solutions for True / False questions.
S1:
Functions can take arguments but they cannot return a
value.
True
False
It’s false. Functions can take parameters and can return a
value.
S2:
You will get -18.0 if you run float("-18").
True
False
It’s true because the float("-18") expression will return
-18.0.
OceanofPDF.com
Math Functions
We use the math module to do mathematical operations in
Python. A module is any Python file that includes executable
Python code. Most of the math related functions can be found in
the math module. In this section we will import the math module
and use some of the functions in it.
Let’s import math module first:
[1
1 # import math module
3]:
2 import math
Let’s print the math module to see basically what it looks like:
[1
1 # see the module info
4]:
2 print(math)
[1
<module 'math' (built-in)>
4]:
The module info in cell 14 is just the basic data. Now let’s see
the details. To get the detailed info about any module or function
we use help() function. Let’s call it for math module.
[1
1 # see detailed docs
5]:
2
3 # help() -> gives you the detailed docs
4 help(math)
[1
Help on built-in module math:
5]:
NAME
math
DESCRIPTION
This module provides access to the mathematical
functions
defined by the C standard.
FUNCTIONS
acos(x, /)
Return the arc cosine (measured in radians) of
x.
acosh(x, /)
Return the inverse hyperbolic cosine of x.
asin(x, /)
Return the arc sine (measured in radians) of x.
…
….
As you see in cell 17, line 10 we use the pi number in the math
module, to calculate the circumference of a circle. We define a
variable named r to keep the radius. Then we calculate the
circumference by the formula of 2 * math.pi * r and we assign
the result to a variable named perimeter .
Let’s see another example. This time we will calculate the sine
of 30 degrees. We will be using the math.sin() function. If you
check its documentation via help(math.sin) , you will see that its
argument should be radians, not degrees. Here is the
documentation: sin(x, /) Return the sine of x (measured in
radians). So to be able to use math.sin() function we need the
radians of 30 degrees. First we will calculate the radians of 30
degrees, then we will pass that value into math.sin() function.
See the steps below:
[1
1 # Example:
8]:
2
3 # Calculate the sine of 30 degrees -> sin(30)
4
5 degree = 30
6
7 # calculate radian
8 radian = math.radians(degree)
9
10 # calculate the sine
11 sine = math.sin(radian)
12
13 print(sine)
[1
0.49999999999999994
8]:
Composition of Functions:
In cell 18 above, we define a variable named degree to keep
the value of 30 degrees (line 5). Then we calculate the radian of the
variable degree and assign to a variable named radian (line 8).
The purpose of radian variable is to keep the radians returned
from math.radians(degree) . Then finally in line 11 we get the
sine value via the function call math.sin(radian) . And we assign
the result to a variable named sine .
The two variables, degree and radian , in cell 18, are only
there to keep some values to be used in the next step. We call these
kind of variables as temporary variables. Most of the time they
just serve to keep some temporary values.
Instead of using temporary variables, we can chain the
function calls . Chaining the functions is called composition. We
can write more compact code via composition of functions. You
will see plenty of examples on function chaining throughout this
book.
Chaining is very simple, you just use the function call
whenever you need a temporary variable. In the cell 19 below, we
will chain the function calls we did in cell 18. And the result will be
the same.
[1
1 # chain the functions
9]:
2 # first call math.radians(degree)
3 # then pass it into math.sin()
4
5 degree = 30
6 sine = math.sin(math.radians(degree))
7 print(sine)
[1
0.49999999999999994
9]:
OceanofPDF.com
SOLUTIONS - True / False - Math Functions
Here are the solutions for True / False questions.
S1:
We can use help() function to get information about a
function or a module.
True
False
It’s true. help() function will give info about the object you
asked for.
S2:
To get sine value of variable named radian and assign
this value to a variable named sine_rad we can do:
sine_rad.math.sin(radian)
True
False
It’s false. It should be sine_rad = math.sin(radian).
Assignment is done via "=".
OceanofPDF.com
Defining Functions
How do we define a function in Python? It’s very easy, we use
the def keyword. Let’s define our first function:
[2
1 # Without Parameters
0]:
2
3 def my_first_function():
4 # print line
5 print("My first function")
Indentation:
Indentation in Python is crucial. Actually it’s a part of the
syntax. Python uses indentation to indicate a block of code (scope).
You can either use tabs or 4 spaces to create an indent.
Let’s assume we want to print a student’s data, which is the
name, age and the language.
[2
1 # Student Data
2]:
2 print("Name: John Doe")
3 print("Age: 24")
4 print("Language: Python")
[2
Name: John Doe
2]:
Age: 24
Language: Python
Now that we have three functions for student name, age and
language, whenever we need to print the student data we can call
these functions.
[3
1 # Print Student with functions
0]:
2
3 student_name()
4 student_age()
5 student_language()
[3
Name: John Doe
0]:
Age: 24
Language: Python
As you see we get all the student data printed. It’s not bad, but
still not very efficient. What if we need to print the student data one
more time? We have to call all three functions again, which makes
three lines of code each time. Can’t we find a better way?
[3
1 # print student data again
1]:
2 # we have to call all 3 functions again
3
4 student_name()
5 student_age()
6 student_language()
[3
Name: John Doe
1]:
Age: 24
Language: Python
As you see it gives the same result with less code. This is the
power of functions and you should use them to write better code. If
you need to print the student data one more time, just call the
wrapper function again:
[3
1 # print student data again
4]:
2 student_data()
[3
Name: John Doe
4]:
Age: 24
Language: Python
So far we print the students first and last name together, like
“John Doe”. Now let’s assume we want to print the students first
and last names separately. Something like “Name: John”,
“Lastname: Doe”. To do this we will define two separate functions,
one for the first name and the other for the last name:
[3 # create two separate functions for firstname and
1
5]: lastname
2
3 def student_firstname():
4 print("Name: John")
5
6 def student_lastname():
7 print("Lastname: Doe")
You learned how to define functions, how to call them and how
to nest them in other functions. Remember that we have nested
student_name function in student_data . Before moving on to
the next section I want to show you the execution flow of code. In
which order, the Python Interpreter runs your code when you call
functions.
Execution Flow:
OceanofPDF.com
True / False - Defining Functions
For each questions below, decide whether it’s true or false.
Q1:
We can use the expression;
print(student_data)
to call student_data function and print its result.
True
False
Q2:
def A():
print("A")
def B():
print("B")
def C():
A()
def D():
A()
B()
C()
We have functions as defined here.
If we call function D as D(), what will be printed on
the screen is:
A
B
A
True
False
OceanofPDF.com
SOLUTIONS - True / False - Defining Functions
Here are the solutions for True / False questions.
S1:
We can use the expression;
print(student_data)
to call student_data function and print its result.
True
False
It’s false. To call a function, you have to add parentheses just
after function name as: student_data().
S2:
def A():
print("A")
def B():
print("B")
def C():
A()
def D():
A()
B()
C()
We have functions as defined here.
If we call function D as D(), what will be printed on
the screen is:
A
B
A
True
False
It’s true. D will call A first, then it will call B, then C. And
C will call A again.
OceanofPDF.com
Parameters & Arguments
So far, we defined functions with no parameters. Now we will
see how to define functions that take one or more parameters. By
the way, it’s time to talk a little bit about Parameters vs.
Arguments.
In computer programming, the terms parameter and argument
can be used for the same thing which is the information that you
pass into a function. So they refer to the same thing with a slight
difference.
A Parameter is the variable listed inside the parentheses in the
function definition. An Argument is the value that are sent to the
function when it is called.
Now it’s time to define a function that takes a parameter.
[3
1 # define a function which takes a parameter
9]:
2
3 def print_square(number):
4
5 # get the square
6 sqr = number**2
7
8 # print sqr
9 print(sqr)
OceanofPDF.com
True / False - Parameters & Arguments
For each questions below, decide whether it’s true or false.
Q1:
You can define a function having two parameters as
follows:
def two_param_function(a b):
print(a)
print(b)
True
False
Q2:
In Python, a function must return the parameters it
received.
True
False
OceanofPDF.com
SOLUTIONS - True / False - Parameters & Arguments
Here are the solutions for True / False questions.
S1:
You can define a function having two parameters as
follows:
def two_param_function(a b):
print(a)
print(b)
True
False
It’s false. There must be a comma between the parameters.
S2:
In Python, a function must return the parameters it
received.
True
False
It’s false. A function can return parameters back but this is
not a must.
OceanofPDF.com
Scope
Scope is the region where the variables exist. In other words, a
variable will only be visible to and accessible by the code in its
scope. Scope is determined by indentation in Python.
When we create a function, it has its own scope. The function
scope starts right after the colon (:) and includes any line indented
in the function. Let’s see it with an example:
[5
1 # -------- not function scope --------
0]:
2 # -------- not function scope --------
3
4 # function scope example
5 def scope_fn():
6 # +++ function scope starts here +++
7
8 scope_var = 100
9 print(scope_var)
10
11 s2 = scope_var * 2
12 print(s2)
13
14 # +++ function scope ends here +++
15
16 # -------- not function scope --------
17 # -------- not function scope --------
In cell 50, you can see the function scope. Lines from 6 to 14
are in the function scope. The lines from 1 to 4 and 15 to 17 are
outside the function scope. Now let’s call this function:
[5
1 # call the function
1]:
2 scope_fn()
[5
100
1]:
200
In cell 50, there are two variables inside the function scope,
scope_var and s2 . They exist only inside this function scope.
Let’s see if we can reach them outside the function.
[5
1 # reach the variable inside the function
2]:
2 # try to print scope_var
3 print(scope_var)
[5
NameError: name 'scope_var' is not defined
2]:
Let’s define two variables in the global scope and then access
them from within a function.
[5
1 # global scope
3]:
2 short = 40
3 long = 60
In the output of cell 58, we can see that the variable short has
a new value of 50. Now comes the question. What happened to the
global variable short ? Let’s see its value, by printing it.
[5
1 # print global short variable again
9]:
2 print(short)
[5
40
9]:
As you see in the output of cell 59, the value of the global
variable short is still 40 . It hasn’t changed. Interesting right? We
changed its value inside the change_globals function. Or “we
think” we changed. Actually we didn’t change the global variable.
When we reassign the variable short in line 4 inside the
change_globals function, Python creates a new variable with the
same name. This new variable is just a local duplicate. It only
exists inside the change_globals function. The new value you
assign to it, will not affect the global variable short .
How do we change the global variables? The answer is simple.
We have to use the global keyword with the variable name. Let’s
see:
[6 # try to change the global variables -> global
1
0]: keyword
2
3 def change_globals():
4 global short
5 short = 5000
6 print(short)
As you see in the output of cell 62, our global variable, short ,
is changed now. That’s how we can modify global variables in the
inner scopes.
OceanofPDF.com
True / False - Scope
For each questions below, decide whether it’s true or false.
Q1:
In Python, a function can only use the variables which
are defined in its own scope.
True
False
Q2:
In Python, scope is determined via the indentation.
True
False
OceanofPDF.com
SOLUTIONS - True / False - Scope
Here are the solutions for True / False questions.
S1:
In Python, a function can only use the variables which
are defined in its own scope.
True
False
It’s false. A function can use the variables in its own scope,
moreover can also use the variables of its parent scopes. It
can use global scope variables for instance.
S2:
In Python, scope is determined via the indentation.
True
False
It’s true. Python uses indentation for scoping.
OceanofPDF.com
Return
A function takes arguments (if any), performs some operations,
and returns a value, if it’s needed. The value that a function returns
to the caller is known as the functions return value. This is
achieved via a special keyword named return . The return
keyword, exits the function and tells Python to run the rest of the
main program. In more simple words; we use the return keyword
to exit a function and return a value.
[6
1 # define a fn to return a value
3]:
2
3 def cube(n):
4
5 # calculate the cube
6 n_cube = n**3
7
8 # return this n_cube
9 return n_cube
As you see in cell 66, we chain the cube and the print
functions. And we get the cube of 3, which is 27, in the output.
Functions which do not return any value are generally called as
Void Functions.
OceanofPDF.com
True / False - Return
For each questions below, decide whether it’s true or false.
Q1:
The function below will return the square of num
parameter back:
def get_square(num):
square= num**2
print(square)
True
False
Q2:
def cube(n):
n_cube = n**3
return n_cube
print(cube(3))
cube function above is a function which returns a
value.
Since we did not assign that returning value to any
variable the expression:
print(cube(3))
will cause NameError.
True
False
OceanofPDF.com
SOLUTIONS - True / False - Return
Here are the solutions for True / False questions.
S1:
The function below will return the square of num
parameter back:
def get_square(num):
square= num**2
print(square)
True
False
It’s false. For a function to return a value back, it must have
"return ......" statement at the end.
S2:
def cube(n):
n_cube = n**3
return n_cube
print(cube(3))
cube function above is a function which returns a
value.
Since we did not assign that returning value to any
variable the expression:
print(cube(3))
will cause NameError.
True
False
It’s false. We do not have to assign the return value of a
function to a variable. print() function will print the
returning value from the cube function.
OceanofPDF.com
Docstring
Function docstrings are the string literals that appear right after
the definition of a function. It tells about the purpose of the
function, input and output parameters, return value (if any) and any
data about the function. Let’s see how we define a function that has
a docstring.
[6
1 # define a function with docstring
7]:
2
3 import math
4
5 def get_power(num, p):
6 """
7 Calculates the power of number.
8 Parameters: int num, p
9 Returns: the give power of the number
10 """
11
12 # calculate the power
13 power = math.pow(num, p)
14
15 # return the power
16 return power
As you see in the output of cell 70, we can even get the source
code of a function with double question marks ( ?? ).
Built-in Methods and Attributes:
Everything in Python is an object, and almost every object has
built-in methods and attributes. They are called dunder methods
(or attributes). We can get them via shortcuts. The word “dunder”
simply means “double underscores: __”. Functions are objects
too, so they have built-in attributes. Let’s print some of the dunder
attributes for our get_power function.
[7
1 # Docstring -> .__doc__
1]:
2 get_power.__doc__
'\n Calculates the power of
[7
number.\n Parameters: int num,
1]:
p\n Returns: the give power of the number\n '
When you run the cell with the input() function, JupyterLab
will display a text box for the user input. And the kernel will stay
busy until the user presses Enter. After pressing the Enter key,
Python will execute the next line after the input() function. As
you see in cell 75 below:
[7
1 # ask for the user name
5]:
2
3 input("Please enter your name:")
[7
Please enter your name: Musa Arda
5]:
'Musa Arda'
Since the input() function returns the user input, you can
assign it to a variable, and use it whenever you need. See the code
in cell 76, line 3. We assign the user input to a variable named
user_name.
[7
1 # ask for the user name and assign to variable
6]:
2
3 user_name = input("Please enter your name:")
4 print("The user name is: " + user_name)
[7
Please enter your name: Musa Arda
6]:
The user name is: Musa Arda
OceanofPDF.com
True / False - Docstring
For each questions below, decide whether it’s true or false.
Q1:
In Python, the docstring of a function is declared above
the function name:
"""
This function calculates the power.
Parameters: int num, int p
Returns: the power p of num
"""
def get_power(num, p):
power = math.pow(num, p)
return power
True
False
Q2:
To see the docstring of a function, we can use the
dunder attribute "_doc_" as follows:
<function_name>._doc_
True
False
OceanofPDF.com
SOLUTIONS - True / False - Docstring
Here are the solutions for True / False questions.
S1:
In Python, the docstring of a function is declared above
the function name:
"""
This function calculates the power.
Parameters: int num, int p
Returns: the power p of num
"""
def get_power(num, p):
power = math.pow(num, p)
return power
True
False
It’s false. Docstring must be inside the function scope, not
above of it.
S2:
To see the docstring of a function, we can use the
dunder attribute "_doc_" as follows:
<function_name>._doc_
True
False
It’s false. Dunder's are used with two underscores (__), not
one.
OceanofPDF.com
QUIZ - Functions I
Now it’s time to solve the quiz for this chapter. You can
download the quiz file, QUIZ_Functions_I.ipynb, from the
Github Repository of this book. It is in the folder named
5_Functions_I. Here are the questions for this chapter:
QUIZ - Functions I:
Q1:
Define a function named first_function that prints "Hello
World!".
[
1 1 # Q 1:
]:
2
3 # --- your solution here --- #
4
5 # call the function you defined
6 first_function()
[
1 Hello World!
]:
Q2:
Define a function named say_hello that takes two string
parameters.
We will assume this parameters be two separate names.
(name_1 and name_2).
Function will concatenate these names with "and".
Then it will print as:
"Hello name_1 and name_2."
Expected Output: Hello Clark Kent and Superman.
[
2 1 # Q 2:
]:
2
3 # --- your solution here --- #
4
5 # call the function
6 say_hello("Clark Kent" ,"Superman")
7 say_hello("Bruce Wayne" ,"Batman")
8 say_hello("Peter Parker" ,"Spiderman")
[
2 Hello Clark Kent and Superman
]:
Hello Bruce Wayne and Batman
Hello Peter Parker and Spiderman
Q3:
Define a function named say_my_name that asks user to
enter his/her name.
This function will print as: "Hi [user_name]."
Hint: to get input from user -> input()
[
3 1 # Q 3:
]:
2
3 # --- your solution here --- #
4
5 # call the function you defined
6 say_my_name()
[
3 Please enter your name: musa arda
]:
Hi musa arda
Q4:
Define a function named get_max that takes 3 parameters as
integer.
Function will return the maximum of these three numbers.
Add a docstring for this function.
Hint: Python has max() built-in function.
You can use help(max) to see how to use the max() function.
Or you can find the documentation here.
[
4 1 # Q 4:
]:
2
3 # --- your solution here --- #
4
5 # call function -> get the returned value and print
6 max_number = get_max(4, 82, 19)
7 print(max_number)
8
9 # docstring
10 help(get_max)
11 print(get_max.__doc__)
[
4 82
]:
Help on function get_max in module __main__:
get_max(num_1, num_2, num_3)
This function gets 3 numbers and selects the
maximum.
Parameters: int num_1, num_2, num_3
Returns: Value from Python's max function
[
5 1 # Q 5:
]:
2
3 # --- your solution here --- #
4
5 # function call
6 txt = " PyThon MaCHine LeARning "
7 final_txt = strip_and_lower(txt)
8 print(final_txt)
[
5 python machine learning
]:
Q6:
Define a function named add which takes two integer
parameters.
The function will return the sum of these parameters.
[
6 1 # Q 6:
]:
2
3 # --- your solution here --- #
4
5 # function call
6 n_1 = 45
7 n_2 = 20
8 addition = add(n_1, n_2)
9 print(addition)
[
6 65
]:
Q7:
Define a function named minimum_difference which is
taking 3 numeric parameters.
Function will calculate the difference between each pairs.
And return the minimum of these differences.
Hints:
[
7 1 # Q 7:
]:
2
3 # --- your solution here --- #
4
5 # function call
6 a = 8
7 b = 5
8 c = 10
9 min_diff = minimum_difference(a, b, c)
10 print(min_diff)
[
7 2
]:
Q8:
Define a function named squre_root which is taking an
integer parameter.
Function will return the square root of this parameter.
Hint: Square Root -> math.sqrt()
[
8 1 # Q 8:
]:
2
3 import math
4
5 # --- your solution here --- #
6
7 # function call
8 num = 81
9 sqr = squre_root(num)
10 print(sqr)
[
8 9.0
]:
Q9:
Define a function named get_logarithm which takes a
numeric parameter.
Function will return the logarithm of this number.
Hint: logarithm -> math.log()
[
9 1 # Q 9:
]:
2
3 import math
4
5 # --- your solution here --- #
6
7 # function call
8 n = 12
9 log = get_logarithm(n)
10 print(log)
11
12 # chained function call
13 print(get_logarithm(n))
[
9 2.4849066497880004
]:
2.4849066497880004
Q10:
Define a function named hypotenuse which takes two
numeric parameters.
Let's assume these numbers are two sides of a right triangle.
The function will return the hypotenuse of this triangle.
Hint:
hipo 2 = a 2 + b 2
[1
1 # Q 10:
0]:
2
3 import math
4
5 # --- your solution here --- #
6
7 # function call
8 # 3-4-5 triangle
9 print(hypotenuse(3, 4))
10
11 # 7-24-25 triangle
12 print(hypotenuse(7, 24))
13
14 # 8-15-17 triangle
15 print(hypotenuse(8, 15))
[1
5.0
0]:
25.0
17.0
OceanofPDF.com
SOLUTIONS - Functions I
Here are the solutions for the quiz for Chapter 5 - Functions I.
SOLUTIONS - Functions I:
S1:
[
1 1 # S 1:
]:
2
3 def first_function():
4 print("Hello World!")
5
6 # call the function you defined
7 first_function()
[
1 Hello World!
]:
S2:
[
2 1 # S 2:
]:
2
3 def say_hello(name_1, name_2):
concat = "Hello " + name_1 + " and " +
4
name_2
5 print(concat)
6
7 # call the function you defined
8 say_hello("Clark Kent" ,"Superman")
9 say_hello("Bruce Wayne" ,"Batman")
10 say_hello("Peter Parker" ,"Spiderman")
[
2 Hello Clark Kent and Superman
]:
Hello Bruce Wayne and Batman
Hello Peter Parker and Spiderman
S3:
[
3 1 # S 3:
]:
2
3 def say_my_name():
4
5 # input() -> waits for enter
6 name = input("Please enter your name: ")
7
8 # continues after pressing enter
9 print("Hi {0}".format(name))
10
11 # call the function you defined
12 say_my_name()
[
3 Please enter your name: musa arda
]:
Hi musa arda
S4:
[
4 1 # S 4:
]:
2
3 def get_max(num_1, num_2, num_3):
4 """
This function gets 3 numbers and selects the
5
maximum.
6 Parameters: int num_1, num_2, num_3
7 Returns: value from Python's max() function
8 """
9 # get the maximum -> max()
10 maximum = max(num_1, num_2, num_3)
11 # return the maximum
12 return maximum
13
14 # call function -> get the returned value and print
15 max_number = get_max(4, 82, 19)
16 print(max_number)
17
18 # docstring
19 help(get_max)
20 print(get_max.__doc__)
[
4 82
]:
Help on function get_max in module __main__:
get_max(num_1, num_2, num_3)
This function gets 3 numbers and selects the
maximum.
Parameters: int num_1, num_2, num_3
Returns: Value from Python's max function
Now let’s turn it to the left by 90 degrees. If you run the code
in cell 4 below, you will see the turtle changes its direction
upwards:
[
4 1 # turn the turtle to the left by 90 degrees
]:
2 t.lt(90)
Now if you run the cells from 5 to 10, the turtle will draw a
square, by moving forward ( fd ) and turning left ( lt ) 4 times each.
It will end up at the same point and in the same direction where it
started:
[
5 1 # move turtle forward by 100 pixels
]:
2 t.fd(100)
[
6 1 # turn the turtle to the left by 90 degrees
]:
2 t.lt(90)
[
7 1 # move turtle forward by 100 pixels
]:
2 t.fd(100)
[
8 1 # turn the turtle to the left by 90 degrees
]:
2 t.lt(90)
[
9 1 # move turtle forward by 100 pixels
]:
2 t.fd(100)
[1
1 # turn the turtle to the left by 90 degrees
0]:
2 t.lt(90)
Figure 6-4: A full square with turtle
If we want to clear the screen and move the turtle to its initial
position, all we have to do is to call the clear() function.
[1
1 # clear screen and reset the turtle to initial position
1]:
2 t.clear()
Now that we know how basically the turtle moves, let’s draw
some interesting shapes with it.
OceanofPDF.com
Square
To create a square with the turtle, we have to follow the fd-lt
pattern 4 times. Let’s write all the steps one by one first:
[1
1 # We want to create a square
3]:
2
3 # Move 100 pixel forward
4 # Turn left 90 deg
5
6 # Move 100 pixel forward
7 # Turn left 90 deg
8
9 # Move 100 pixel forward
10 # Turn left 90 deg
11
12 # Move 100 pixel forward
13 # Turn left 90 deg
14
15 t.fd(100)
16 t.lt(90)
17
18 t.fd(100)
19 t.lt(90)
20
21 t.fd(100)
22 t.lt(90)
23
24 t.fd(100)
25 t.lt(90)
With the code in cell 13, we can draw a perfect square. But
there is an issue here. We repeated exactly the same lines of code 4
times. Repetition is something you should always avoid in
programming. Actually there is a loop here. We haven’t covered
the Loops yet, but here we can see a little bit of them. Chapter 10 is
dedicated to Loops, but we can write a simple for loop here. The
for loop will repeat how many times we want to, 4 in our case.
And it will repeat the fd-lt pattern.
Before the for loop, let’s reset the screen. Then we can run our
loop.
[1
1 turtle.resetscreen()
4]:
[1
1 # with loop
5]:
2 # it's going to repeat fd-lt patter 4 times
3
4 for i in range(4):
5 t.fd(100)
6 t.lt(90)
Now let’s use this for loop inside a function. We will create a
function named square . The parameters will be:
a turtle object, t
int d (distance), in pixels
bool is_screen_reset , to check if the caller wants a
screen reset
[1
1 # square function
6]:
2
3 def square(t, d, is_screen_reset):
4 """
5 It draws square with turtle.
Parameters: turtle t, int d, bool
6
is_screen_reset
7 Returns: None
8 """
9
10 # reset screen
11 if is_screen_reset == True:
12 reset()
13
14 # draw a square
15 for i in range(4):
16 t.fd(d)
17 t.lt(90)
Now that we defined it, let’s call the square() function to first
reset the screen and draw a square of size 200 pixels:
[1
1 square(t, 200, True)
8]:
Now let’s say we don’t want to reset the screen. We want the
old square to stay, but we will draw a new one on to it. The size
will be 100 pixels for the new one:
[2
1 square(t, 100, False)
0]:
If we run the code in cell 20, we will see two squares on the
turtle screen.
Figure 6-5: Two squares with sizes 300 and 100 pixels
[2
1 # draw 10 squares of size: 20 - 40 - 60 80 - ... - 200
1]:
2
3 # first reset the screen just once
4 reset()
5
6 # loop to draw the circles
7 for i in range(1, 11):
8 square(t, i * 20, False)
OceanofPDF.com
Polygon
We will draw a polygon now. The difference between a square
and a polygon is that the polygon may have varying number of
sides. It doesn’t have to be 4 as in the square. And we will take the
number of sides as a parameter for our polygon() function. The
parameters will be:
a turtle object, t
int d (distance), in pixels
int n , number of sides
bool is_screen_reset , to check if the caller wants a
screen reset
[2
1 # polygon function
2]:
2
3 def polygon(t, d, n, is_screen_reset):
4 """
5 Draws a polygon.
Parameters: turtle t, int d (pixels), int n
6
(number of sides),
7 bool is_screen_reset
8 Returns: None
9 """
10
11 # resetting
12 if is_screen_reset:
13 reset()
14
15 # angle
16 angle = 360 / n
17
18 # polygon
19 for i in range(n):
20 t.fd(d)
21 t.lt(angle)
Named Arguments:
When you call a function you may want to pass the arguments
with their names. This is a good practice which improves code
readability. Arguments which you pass with their names are called
named arguments (parameters). We can pass the named
arguments like: function_name(parameter_name =
parameter_value) . Let’s call the polygon() function with
named arguments this time:
[2
1 # call the polygon fn with named arguments
4]:
2 polygon(t=t, d=120, n=8, is_screen_reset=False)
As you see in cell 24, we call the polygon() function, with the
named arguments. Now we know which value goes to which
parameter. And here is the resulting screen:
Figure 6-8: Calling the polygon() function with named arguments
without resetting the screen
OceanofPDF.com
Circle
Now let’s draw a circle with the turtle. It might be a little
tricky. Let’s first define the circle() function then talk about the
steps. One important point to note is, the circle() function will not
do any drawing in it. It will call the polygon() function and pass
the necessary arguments. All the drawing will take place in the
polygon() function. A circle is nothing but a polygon with
infinitely many sides.
The parameters of the circle() will be:
a turtle object, t
int r , the radius of the circle
bool is_screen_reset , to check if the caller wants a
screen reset
[2
1 import math
6]:
2
3 def circle(t, r, is_screen_reset):
4 """
5 Draws a circle.
Parameters: turtle t, int r, bool
6
is_screen_reset
7 Returns: None (void)
8 """
9
10 # perimeter -> total distance to draw
11 perimeter = 2 * math.pi * r
12
13 # angles to turn at each step
14 angles = 10
15
16 # steps -> 360 / angles
17 # angle of an arc
18 steps = int(360 / angles)
19
20 # distance -> distance to draw in each arc
21 distance = perimeter / steps
22
23 # call polygon
polygon(t, d=distance, n=steps,
24
is_screen_reset=is_screen_reset)
Now let’s define the star() function. The parameters will be:
a turtle object, t
int d (distance), in pixels
bool is_screen_reset , to check if the caller wants a
screen reset
string color , the color name
[3
1 def star(t, d, is_screen_reset, color):
0]:
2 """
3 Draws a star with 5 sizes.
Parameters: turtle t, int d, bool
4
is_screen_reset, str color
5 """
6
7 # resetting
8 if is_screen_reset:
9 reset()
10
11 # angle
12 angle = 180 / 5
13 angle = 180 - angle
14
15 # line color -> pen color
16 t.color(color)
17
18 # start the filling
19 t.fillcolor(color)
20 t.begin_fill()
21
22 for i in range(5):
23 t.fd(d)
24 t.lt(angle)
25
26 # end the filling
27 t.end_fill()
Figure 6-11: The start with 300 distance and green color
Let’s draw another star, but do not reset the screen this time.
We will have two stars when we run cell 32.
[3
1 star(t, 200, is_screen_reset=False, color='orange')
2]:
Our star() function looks good but we can improve it. For
example it doesn’t have to be 5 sided. Let’s add another parameter,
the number of sides, n , and draw stars with different shapes. The
angle will be dependent on the number of sizes, n .
[3 # redefine the star function with number of sides
1
4]: as a new parameter
2
3 def star(t, d, n, is_screen_reset, color):
4 """
5 Draws a star with n sizes.
Parameters: turtle t, int d, int n, bool
6
is_screen_reset, str color
7 """
8
9 # resetting
10 if is_screen_reset:
11 reset()
12
13 # angle
14 angle = 180 / n
15 angle = 180 - angle
16
17 # line color -> pen color
18 t.color(color)
19
20 # start the filling
21 t.fillcolor(color)
22 t.begin_fill()
23
24 for i in range(n):
25 t.fd(d)
26 t.lt(angle)
27
28 # end the filling
29 t.end_fill()
And here is the result. A beautiful star with 15 sides, red outline
and yellow fill color:
Figure 6-15: A star with 15 sides, different pen color and fill color
And that’s the end of the first project in this book. We defined
functions, passed arguments, modify them and reuse them in this
project. Now you should have a good understanding of basics of
functions in Python. We will have the second part of functions in
Chapter 9.
OceanofPDF.com
7. Assignment 1 – Functions
We finished our first project, Project 1 - Functions. Now it’s
your turn to recreate the same project. This chapter is the
assignment on the Project 1 Functions. In this assignment, you will
use Python's built-in Turtle module. You will define functions, and
call them via parameters to draw shapes with turtle. So you will be
able to practice functions by yourself.
To complete the assignment, follow the instructions where you
see a #TODO statement. You can also download the assignment as
a Jupyter file in the Github repository of the book. You can find the
solutions in the previous chapter.
The Assignment
Complete the code with respect to #TODO statements.
[
1 1 # import the module -> turtle
]:
2
3 #TODO - import turtle module
[
2 1 #TODO - create a turtle object and name it as t
]:
[
3 1 # move turtle forward by 100 pixels
]:
2 t.fd(100)
[
4 1 # turn the turtle to the left by 90 degrees
]:
2 t.lt(90)
[
5 1 #TODO - move turtle t 100 pixels forward
]:
[
6 1 #TODO - turn turtle to the left by 90 degrees
]:
[
7 1 #TODO - move turtle t 100 pixels forward
]:
[
8 1 #TODO - turn turtle to the left by 90 degrees
]:
[
9 1 #TODO - move turtle t 100 pixels forward
]:
[1
1 #TODO - turn turtle to the left by 90 degrees
0]:
Move functions:
[1
1 # clear screen and reset the turtle to initial position
1]:
2 t.clear()
[1
1 # reset the screen
2]:
2 turtle.resetscreen()
[1
1 # We want to create a square
3]:
2
3 # Move 100 pixel forward
4 # Turn left 90 deg
5
6 # Move 100 pixel forward
7 # Turn left 90 deg
8
9 # Move 100 pixel forward
10 # Turn left 90 deg
11
12 # Move 100 pixel forward
13 # Turn left 90 deg
14
15 t.fd(100)
16 t.lt(90)
17
18 t.fd(100)
19 t.lt(90)
20
21 t.fd(100)
22 t.lt(90)
23
24 t.fd(100)
25 t.lt(90)
[1
1 #TODO - reset turtle screen
4]:
[1
1 # with loop
5]:
2 # it's going to repeat fd-lt patter 4 times
3
4 for i in range(...#TODO...):
5 #TODO - move forward and turn left
Square:
Create a square with a function -> square
Parameters: turtle object, d in pixels and is_screen_reset
[1
1 # square function
6]:
2
3 def square(t, d, is_screen_reset):
4 """
5 It draws square with turtle.
Parameters: turtle t, int d, bool
6
is_screen_reset
7 Returns: None
8 """
9
10 # reset screen
11 if is_screen_reset == True:
12 reset()
13
14 # draw a square
15 #TODO - create a loop to draw a circle
[1
1 # reset function
7]:
2
3 def reset():
4 #TODO - call turtle modules resetscreen function
[1
1 square(t, 200, True)
8]:
[1 #TODO - call square function with: t, 300 distance
1
9]: and resetting screen
[2 #TODO - call square function with: t, 100 distance
1
0]: and NOT resetting screen
[2
1 # draw 10 squares of size: 20 - 40 - 60 80 - ... - 200
1]:
2
3 # first reset the screen just once
4 reset()
5
6 # loop to draw the circles
7 for i in range(1, 11):
#TODO - call square function with NOT resetting
8
screen and appropriate parameters
Polygon:
Draw a polygon.
[2
1 # polygon function
2]:
2
3 def polygon(t, d, n, is_screen_reset):
#TODO - write the function documentation
4
(docstring)
5
6 # resetting
#TODO - check if screen will be reset and do
7
it if needed
8
9 # angle
10 angle = 360 / n
11
12 # polygon
13 for i in range(...#TODO...):
#TODO - move and turn the turtle to draw
14
polygon
[2
1 # draw a 6 sided hexagon
3]:
2 polygon(t, 100, 6, True)
Named Arguments:
When you call a function and passing parameters:
function_name(parameter_name = parameter_value)
[2
1 # call the polygon fn with named arguments
4]:
2 polygon(t=t, d=120, n=8, is_screen_reset=False)
[2
1 # call the square fn with named arguments
5]:
2
#TODO - call square function with named
3
parameters
# -> t for turtle, 250 for d and True for
4
is_screen_reset
Circle:
Draw a Circle ->
t
r
is_screen_reset
[2
1 import math
6]:
2
3 def circle(t, r, is_screen_reset):
4 """
5 Draws a circle.
Parameters: turtle t, int r, bool
6
is_screen_reset
7 Returns: None (void)
8 """
9
10 # perimeter -> total distance to draw
# TODO - Calculate the perimeter of circle ->
11
2 * math.pi * r
12
13 # angles to turn at each step
14 angles = 10
15
16 # steps -> 360 / angles
17 # angle of an arc
# TODO - Calculate number of steps for arc
18
of 10 degrees, then convert into int
19
20 # distance -> distance to draw in each arc
# TODO - Calculate the distance which is
21
perimeter / steps
22
23 # call polygon
#TODO - Call polygon function with
24
appropriate parameters
[2 # call the circle function with radius of 200 and
1
7]: resetting the screen
2 circle(t, 200, True)
[2
1 # reset the screen first
8]:
2 # TODO - call reset() function
3
4 # call the circle in a loop
5 for i in # TODO - Range from 1 to 4:
6
# TODO - Call circle() function with i * 50
7
radius and do not reset screen
Star:
Draw a star.
[2
1 # angle of a 5 sized star
9]:
2
3 angle = 180 / 5
4 angle = 180 - angle
5 print(angle)
[2
144.0
9]:
[3
1 def star(t, d, is_screen_reset, color):
0]:
2 """
3 Draws a star with 5 sizes.
Parameters: turtle t, int d, bool
4
is_screen_reset, str color
5 """
6
7 # resetting
# TODO - call reset screen based on
8
is_screen_reset
9
10 # angle
11 # TODO - Calculate the angle for 5 sizes
12
13 # line color -> pen color
14 t.color(color)
15
16 # start the filling
17 t.fillcolor(color)
18 t.begin_fill()
19
20 # 5 sized star
21 for i in range(...#TODO...):
#TODO - move and turn the turtle to draw
22
a star
23
24 # end the filling
25 t.end_fill()
[3
1 star(t, 300, is_screen_reset=True, color='green')
1]:
[3
1 star(t, 200, is_screen_reset=False, color='orange')
2]:
[3
1 star(t, 100, is_screen_reset=False, color='purple')
3]:
Variable sized star.
[3 # redefine the star function with number of sides
1
4]: as a new parameter
2
3 def star(t, d, n, is_screen_reset, color):
4 """
5 Draws a star with n sizes.
Parameters: turtle t, int d, int n, bool
6
is_screen_reset, str color
7 """
8
9 # resetting
# TODO - call reset screen based on
10
is_screen_reset
11
12 # angle
13 # TODO - Calculate the angle for n sizes
14
15 # line color -> pen color
16 # TODO - set the line color
17
18 # start the filling
19 # TODO - set the fill color
20 # TODO - begin fill
21
22 # n sized star
23 for i in range(...#TODO...):
#TODO - move and turn the turtle to draw
24
a star
25
26 # end the filling
27 # TODO - end fill
[3 star(t, 300, 11, is_screen_reset=True,
1
5]: color='yellow')
Different fill and pen colors.
[3 # redefine the star function with pen_color and
1
6]: fill_color
2
def star(t, d, n, is_screen_reset, pen_color,
3
fill_color):
4 """
5 Draws a star with n sizes.
Parameters: turtle t, int d, int n, bool
6
is_screen_reset, str pen_color, fill_color
7 """
8
9 # resetting
# TODO - call reset screen based on
10
is_screen_reset
11
12 # angle
13 # TODO - Calculate the angle for n sizes
14
15 # line color -> pen color
16 # TODO - set the line color
17
18 # start the filling
19 # TODO - set the fill color
20 # TODO - begin fill
21
22 # n sized star
23 for i in range(...#TODO...):
#TODO - move and turn the turtle to draw
24
a star
25
26 # end the filling
27 # TODO - end fill
[3 star(t, 300, 15, is_screen_reset=True,
1
7]: pen_color='red', fill_color='yellow')
[3
1 # last statement -> before close
8]:
2 # restart the kernel after you close the turtle
3
4 turtle.mainloop()
OceanofPDF.com
8. Conditional Statements
What is a Conditional Statement?
Python runs the code from top to down. We call it as the
Execution Flow. There will be places in the code that you may
want to control this flow. For example, let's say you want to print
the number if and only if it is positive. This decision making is
called conditional execution and in Python it is done via if…
elif…else statements. In this chapter we will learn the conditional
statements.
A conditional statement is an expression which evaluates
either True or False . Let’s see some examples:
[
1 1 6 == 6
]:
[
1 True
]:
[
5 1 type(True)
]:
[
5 bool
]:
Boolean Expressions:
A boolean expression evaluates to one of two states True or
False . Let’s see what they are in Python. In the list below, every
boolean expression checks for a specific condition:
x == y : x is equal to y
x != y : x is not equal to y
x < y : x is less than y
x > y : x is greater than y
x <= y : x is less than or equal to y
x >= y : x is greater than or equal to y
Logical Operators:
In Python, we have 3 logical operators: and , or , and not .
and:
[
7 1 True and True
]:
[ True
7
]:
[
8 1 True and False
]:
[
8 False
]:
[
9 1 False and True
]:
[
9 False
]:
[1
1 False and False
0]:
[1
False
0]:
or:
[1
1 True or True
1]:
[1
True
1]:
[1
1 True or False
2]:
[1
True
2]:
[1
1 False or True
3]:
[1
True
3]:
[1
1 False or False
4]:
[1
False
4]:
[1
1 # or
8]:
2 print("---- or ----")
3 "{0} or {1}: {2}".format(a, b, a or b)
[1
---- or ----
8]:
'True or False: True'
In cell 18, this time we put a or b as the third argument in the
format() function call. And it evaluates as True , because the
variable a is True and b is False. True or False is True .
OceanofPDF.com
True / False - What is a Conditional Statement?
For each questions below, decide whether it’s true or false.
Q1:
In Python = and == are the same. They both check for
equality.
True
False
Q2:
(True and False) or (False or True) expression will
evaluate as True.
True
False
OceanofPDF.com
SOLUTIONS - True / False - What is a Conditional
Statement?
Here are the solutions for True / False questions.
S1:
In Python = and == are the same. They both check for
equality.
True
False
It’s false. "=" is assignment, whereas "==" is equality check.
They are different operators.
S2:
(True and False) or (False or True) expression will
evaluate as True.
True
False
It’s true. First parenthesis is False, and the second one is
True. Since there is "or" between parentheses, the final
result is True.
OceanofPDF.com
if
The if statement in Python, is used for decision-making
operations. It contains a block of code which only runs when the
condition given in the if statement is True . Let’s see how it works
in the code:
[1
1 # Print x, if it is greater than zero (positive)
9]:
2
3 x = 7
4
5 if x > 0:
6 print("{} is greater than 0.".format(x))
[1
7 is greater than 0.
9]:
[2
1 x = -10
0]:
2
3 if x > 0:
4 print("{} is greater than 0.".format(x))
5
6 print("end of if statement")
[2
end of if statement
0]:
[2
1 x = 6
3]:
2
3 print("---------")
4
5 if x > 0 and x <= 10:
print("{} is greater than 0 and less than or equal
6
to 10.".format(x))
7
8 print("---------")
[2
---------
3]:
6 is greater than 0 and less than or equal to 10.
---------
[2
1 x = 49
5]:
2
3 print("---------")
4
5 if x >= 0 and x < 20:
print("{} is greater than or equal 0 and less than
6
20.".format(x))
7
8 print("---------")
[2
---------
5]:
---------
In cell 25, the result of the expression x >= 0 and x < 20 will
return False when x = 49 . The code line 6 is not executed.
[2
1 x = -6
6]:
2
3 print("---------")
4
5 if x >= 0 and x < 20:
print("{} is greater than or equal 0 and less than
6
20.".format(x))
7
8 print("---------")
[2
---------
6]:
---------
In cell 26, the result of the expression x >= 0 and x < 20 will
return False when x = -6 . The code line 6 is not executed.
[2
1 # or
7]:
2
3 # if x is greater than 100 or less than 0, print it
4
5 x = 80
6
7 print("---------")
8
9 if x > 100 or x < 0:
print("{} is greater than 100 or less than
10
0.".format(x))
11
12 print("---------")
[2
---------
7]:
---------
In cell 27, the result of the expression x > 100 or x < 0 will
return False when x = 80 . The code line 10 is not executed.
[2
1 x = 280
8]:
2
3 print("---------")
4
5 if x > 100 or x < 0:
print("{} is greater than 100 or less than
6
0.".format(x))
7
8 print("---------")
[2
---------
8]:
280 is greater than 100 or less than 0.
---------
In cell 28, the result of the expression x > 100 or x < 0 will
return True when x = 280 . So the code line 6 is executed and it
will print the text “280 is greater than 100 or less than 0.” .
[2
1 x = -300
9]:
2
3 print("---------")
4
5 if x > 100 or x < 0:
print("{} is greater than 100 or less than
6
0.".format(x))
7
8 print("---------")
[2
---------
9]:
-300 is greater than 100 or less than 0.
---------
In cell 29, the result of the expression x > 100 or x < 0 will
return True when x = -300 . So the code line 6 is executed and it
will print the text “-300 is greater than 100 or less than 0.” .
OceanofPDF.com
True / False - if
For each questions below, decide whether it’s true or false.
Q1:
The code below will print as:
60 is greater than or equal to 0 or less than 20
x = 60
if x >= 0 or x < 20:
print("{} is greater than or equal to 0 or less than
20".format(x))
True
False
Q2:
The code below will print as:
49 is greater than or equal to 0 and less than 20
x = 49
if x >= 0 and x < 20:
print("{} is greater than or equal to 0 and less than
20".format(x))
True
False
OceanofPDF.com
SOLUTIONS - True / False - if
Here are the solutions for True / False questions.
S1:
The code below will print as:
60 is greater than or equal to 0 or less than 20
x = 60
if x >= 0 or x < 20:
print("{} is greater than or equal to 0 or less than
20".format(x))
True
False
It’s true, because x >= 0 or x < 20 evaluates True.
S2:
The code below will print as:
49 is greater than or equal to 0 and less than 20
x = 49
if x >= 0 and x < 20:
print("{} is greater than or equal to 0 and less than
20".format(x))
True
False
It’s false. x < 20 will evaluates False when x = 49. Since
"and" wants both sides to be True, the code in if statement
will not run.
OceanofPDF.com
else
Most of the time we need more than one condition to check.
For multiple conditions we use else statement. The else statement
is used when you have to judge one condition on the basis of the
other. If one condition turns out to be False , then there should be
another condition that should justify the statement.
[3
1 # if x is integer, print "INTEGER"
2]:
2 # else print "NOT INTEGER"
3
4 # for integer check -> type()
5
6 x = 9
7
8 if type(x) == int:
9 print("INTEGER")
10 else:
11 print("NOT INTEGER")
[3
INTEGER
2]:
In cell 32, we check if the type of variable x is integer ( int ) or
not. Since its value is 9, which is an integer, the if check returns
True and the code line 9 is executed. And it prints INTEGER .
[3
1 # lets define a string (str) variable
3]:
2 # and check its type
3 x = "xyz"
4
5 if type(x) == int:
6 print("INTEGER")
7 else:
8 print("NOT INTEGER")
[3
NOT INTEGER
3]:
OceanofPDF.com
True / False - else
For each questions below, decide whether it’s true or false.
Q1:
The code below will print ODD:
x = 17
if x % 2 == 0:
print("ODD")
else:
print("EVEN")
True
False
Q2:
Value of x will be 6 after executing the code below:
x = 5
if False:
x -= 1
else:
x += 1
True
False
OceanofPDF.com
SOLUTIONS - True / False - else
Here are the solutions for True / False questions.
S1:
The code below will print ODD:
x = 17
if x % 2 == 0:
print("ODD")
else:
print("EVEN")
True
False
It’s false. It writes "ODD" when the number is even, and
"EVEN" when it is odd :)
S2:
Value of x will be 6 after executing the code below:
x = 5
if False:
x -= 1
else:
x += 1
True
False
It’s true. if False statement will never run, so the else block
will be executed and x will be incremented by 1.
OceanofPDF.com
elif
So far, we only checked for two conditions:
if
else
[3
1 a = 1
8]:
2
3 # write elif without if
4 elif a == 1:
5 print("a is 1")
6 else:
7 print("not 1")
[3
File "<ipython-input-38-5d7d570f4f1f>", line 4
8]:
elif a == 1:
^
SyntaxError: invalid syntax
Exercise:
Let’s say we want to find which day of the week, based on the
user input. We will define a function named day_of_week . It will
ask for a number between 1 and 7. And it will decide the day of the
week by using this number.
[3
1 def day_of_week():
9]:
2
3 # ask for number of the day
number_of_day = int(input("Please enter a
4
number (1-7): "))
5
6 # define a variable for name of the day
7 name_of_day = ""
8
9 # conditions
10 if number_of_day == 1:
11 name_of_day = 'MONDAY'
12 elif number_of_day == 2:
13 name_of_day = 'TUESDAY'
14 elif number_of_day == 3:
15 name_of_day = 'WEDNESDAY'
16 elif number_of_day == 4:
17 name_of_day = 'THURSDAY'
18 elif number_of_day == 5:
19 name_of_day = 'FRIDAY'
20 elif number_of_day == 6:
21 name_of_day = 'SATURDAY'
22 else:
23 name_of_day = 'SUNDAY'
24
25 # return name of day
26 return name_of_day
The function definition is in cell 39. We ask for the user input
with the input() function in line 4. And we convert the input to an
integer via the int() function. We define a variable named
name_of_day to keep the name. You can see the if-elif
statements for all the possibilities. If none of the conditions are
True , then the else statement in line 22 will execute.
Now let’s call the function:
[4
1 day_of_week()
0]:
[4
Please enter a number (1-7): 4
0]:
'THURSDAY'
[4
1 day_of_week()
1]:
[4
Please enter a number (1-7): 7
1]:
'SUNDAY'
[4
1 day_of_week()
2]:
[4
Please enter a number (1-7): 0
2]:
'SUNDAY'
[4
1 # a letter instead of a number
3]:
2 day_of_week()
[4
Please enter a number (1-7): A
3]:
ValueError: invalid literal for int() with base 10:
'A'
As you see in cell 43, we get ValueError when the user input
is not a number. The error occurs in line 4, where we try to convert
the input into an integer with int() function.
[4
1 # a number greater than 7
4]:
2 day_of_week()
[4
Please enter a number (1-7): 99
4]:
'SUNDAY'
[4
1 # a number less than 1
5]:
2 day_of_week()
[4
Please enter a number (1-7): -6
5]:
'SUNDAY'
As you see in cell 48, it passes the first check but fails at the
second one, because the input is not in the expected range. So our
second test is successful. Now let’s do a test for the third case:
[4
1 # test for a number less than 1
9]:
2 day_of_week()
[4
Please enter a number (1-7): -4
9]:
Please enter an integer.
And we see that we can handle the last case too. For now, our
function seems working correctly. Let’s also call with a valid day
number.
A note on isdigit() function: The isdigit() function doesn’t
take negative numbers into account. It assumes negative numbers
as stings, not numbers. So if you run it with a negative number in
quotes like: “-4”.isdigit(), you will get False .
[5
1 # call with a valid day number
0]:
2 day_of_week()
[5
Please enter a number (1-7): 6
0]:
------ CONTROL #1 is SUCCESSFUL ------
------ CONTROL #2 is SUCCESSFUL ------
'SATURDAY'
In cell 50, we call the function with number 6. It passes both
checks and returns SATURDAY . Which means our function works
as expected and it’s bug-free.
OceanofPDF.com
True / False - elif
For each questions below, decide whether it’s true or false.
Q1:
When the code below runs and the user enters 0 (zero),
it will print "ZERO" on the screen.
def is_negative():
user_input = input("Please enter a number: ")
if not user_input.isdigit():
return
else:
n = int(user_input)
if n <= 0:
print("NEGATIVE")
elif n == 0:
print("ZERO")
else:
print("POZITIVE")
True
False
Q2:
The code below will print TRUE:
name = "Python"
elif name == "Pyrhon":
print("FALSE")
elif name == "Python":
print("TRUE")
else:
print("NOT SURE")
True
False
OceanofPDF.com
SOLUTIONS - True / False - elif
Here are the solutions for True / False questions.
S1:
When the code below runs and the user enters 0 (zero),
it will print "ZERO" on the screen.
def is_negative():
user_input = input("Please enter a number: ")
if not user_input.isdigit():
return
else:
n = int(user_input)
if n <= 0:
print("NEGATIVE")
elif n == 0:
print("ZERO")
else:
print("POZITIVE")
True
False
It’s false. The first condition is "if n <= 0". It will evaluate
True if the user enters 0. So it will print "NEGATIVE".
S2:
The code below will print TRUE:
name = "Python"
elif name == "Pyrhon":
print("FALSE")
elif name == "Python":
print("TRUE")
else:
print("NOT SURE")
True
False
It’s false. You cannot use elif without if. So it will throw a
SyntaxError.
OceanofPDF.com
Nested Conditionals
In Python we can nest one conditional within another. Nesting
is simply putting one if block inside of another if block. Nested
conditionals enables us to make complex decisions based on
different scenarios.
Before dive into the examples on nested conditionals, there is a
logical operator which we skipped so far. It’s the not operator, and
now it’s time to see how it works.
not :
not operator turns a boolean ( True or False ) into its
opposite. Let’s see with examples.
[5
1 not True
1]:
[5
False
1]:
[5
1 not False
2]:
[5
True
2]:
As you see in cell 51, not True is equal to False . And in cell
52, not False is equal to True .
[5
1 x = 5
3]:
2 x == 5
[5
True
3]:
In cell 56 we set the if logic in the opposite way, with the not
operator. We first checked if the value of x is not 5 as: if not x
== 5 . Since this check fails, the code in the else block is executed.
In both cells, 55 and 56, we get the same result but in the opposite
ways.
Now we are ready for the examples of nested conditionals. In
the first example we will have two numbers and we will check if
they are equal or not. If they are not equal then we want to see
which one is bigger.
[5
1 # Example:
7]:
2
3 x = 300
4 y = 40
5
6 if x == y:
7 print("x is equal to y")
8 else:
9 # x is not equal to y
10 if x > y:
11 print("x is greater than y")
12 elif x == y:
13 print("x is equal to y")
14 else:
15 print("x is less than y")
[5
x is greater than y
7]:
In the next example, we will ask for a number from the user.
Then we will make two decisions. If the number is less than 10 and
if it’s an odd or even number. We will define a function,
odd_or_even , for this task. Let’s start:
[5
1 # Example:
9]:
2 # ask a number from user
3
4 # if the number is less than 10:
5 # if odd -> ODD LESS THAN 10
6 # if even -> EVEN LESS THAN 10
7
8 # if it is greater than 10
9 # if odd -> ODD
10 # if even -> EVEN
11
12 def odd_or_even():
13 # ask for input
14 user_input = input("Please enter a number: ")
15
16 # input() -> str -> cast to int()
17 num = int(user_input)
18
19 # conditions
20 if num < 10:
21 # odd or even
22 if num % 2 == 1:
23 print("ODD LESS THAN 10")
24 else:
25 print("EVEN LESS THAN 10")
26 else:
27 # odd or even
28 if num % 2 == 1:
29 print("ODD")
30 else:
31 print("EVEN")
OceanofPDF.com
True / False - Nested Conditionals
For each questions below, decide whether it’s true or false.
Q1:
The code below will print K2 :
y = 5
if not y != 5:
print("K1")
else:
print("K2")
True
False
Q2:
The code below will print A != B :
a = 3
b = 5
if a != b:
print("A != B")
else:
if a > b:
print("A > B")
else:
print("A < B")
True
False
OceanofPDF.com
SOLUTIONS - True / False - Nested Conditionals
Here are the solutions for True / False questions.
S1:
The code below will print K2 :
y = 5
if not y != 5:
print("K1")
else:
print("K2")
True
False
It’s false. Since "not y != 5" evaluates to True, it will print
K1.
S2:
The code below will print A != B :
a = 3
b = 5
if a != b:
print("A != B")
else:
if a > b:
print("A > B")
else:
print("A < B")
True
False
It’s true. "a != b" will evaluate to True so, the first if
statement will execute.
OceanofPDF.com
Recursion
From the previous chapters, we know that a function can call
other functions. It can also call itself, which we call Recursion. For
example, consider a function that prints numbers from a given
number up to 0 in reverse order. This function can achieve this task,
by calling itself recursively.
As you see in cells 65 and 66, it starts printing the first number,
10, which is the parameter value you call the function with for the
first time. Then it decreases one by one and prints them all. Until it
hits the number 0.
Let’s do another example. This time we will define a function
with name write_text . It will have two parameters, a text and a
number n . And it will print the text n times recursively. At each
call to itself, it will decrease the number n by 1.
[6
1 # Example:
7]:
# create a function which will print the text
2
parameter given to it.
# The function will write this text number n
3
times.
4 # n is second parameter
5
6 def write_text(text, n):
7 # exit condition
8 if n <= 0:
9 return
10 else:
11 print(text)
12 write_text(text, n-1)
The recursion is in line 12, where it calls itself with the same
text parameter and n-1 . Let’s call it:
[6
1 write_text('Hands-On Python is great :)', 5)
8]:
[6
Hands-On Python is great :)
8]:
Hands-On Python is great :)
Hands-On Python is great :)
Hands-On Python is great :)
Hands-On Python is great :)
Now if you call it, it will print the numbers from 80 to 100
successfully.
[7
1 up_to_hundred(80)
3]:
[7
80
3]:
81
82
…
…
99
100
OceanofPDF.com
True / False - Recursion
For each questions below, decide whether it’s true or false.
Q1:
When a function calls other functions (not itself), this
is called Recursion.
True
False
Q2:
If you do not set a proper exit condition the function
will call itself up to infinity.
True
False
OceanofPDF.com
SOLUTIONS - True / False - Recursion
Here are the solutions for True / False questions.
S1:
When a function calls other functions (not itself), this
is called Recursion.
True
False
It’s false. Recursion is term used when the function calls
itself.
S2:
If you do not set a proper exit condition the function
will call itself up to infinity.
True
False
It’s true. In Recursion, function will call itself up to infinity
if there is no proper exit condition.
OceanofPDF.com
QUIZ - Conditional Statements
Now it’s time to solve the quiz for this chapter. You can
download the quiz file, QUIZ_Conditional_Statements.ipynb,
from the Github Repository of this book. It is in the folder named
8_Conditional_Statements. Here are the questions for this chapter:
QUIZ - Conditional Statements:
Q1:
Ask the user to enter a number.
Find out if this number is Odd or Even.
[
1 1 # Q 1:
]:
2
3 # --- your solution here --- #
[
1 Please enter a number: 8
]:
8 is EVEN
Q2:
Ask for a letter from the user.
Find out if it is a vowel or consonant.
[
2 1 # Q 2:
]:
2
3 # --- your solution here --- #
[
2 Please enter a letter: u
]:
VOWEL: u
Q3:
Define a function with name which_shape.
The function will ask a number from the user.
It will decide the name of the shape according to that
number, and return that name.
Sides & Shapes:
3 - Triangle
4 - Rectangle
5 - Pentagon
6 - Hexagon
7 and more - Polygon
[
3 1 # Q 3:
]:
2
3 # --- your solution here --- #
4
5 # call the function you defined
6 shape = which_shape()
7 print(shape)
[
3 Please enter the number of sides: 4
]:
Rectangle
Q4:
Define a function to print the number of days in the given
month.
Function name will be number_of_days_in_month.
It will ask month name from the user.
Number of Days in Gregorian calendar:
January - 31
February - 28 or 29
March - 31
April - 30
May - 31
June - 30
July - 31
August - 31
September - 30
October - 31
November - 30
December – 31
Expected Output:
Please enter the month name: October
'There are 31 days in October'
[
4 1 # Q 4:
]:
2
3 # --- your solution here --- #
4
5 # call the function you defined
6 number_of_days_in_month()
[
4 Please enter the month name: February
]:
'There are 28 or 29 days in February'
Q5:
Define a function to ask the length of sides of a triangle.
Function name is name_the_triangle.
The function will decide the type of triangle, according to the
length of the sides.
And will return its name.
Types of Triangles:
Equilateral: three equal sides
Isosceles: two equal sides
Scalene: no equal sides
[
5 1 # Q 5:
]:
2
3 # --- your solution here --- #
4
5 # call the function you defined
6 print(name_the_triangle())
[
5 Length for the first side: 10
]:
Length for the second side: 45
Length for the third side: 20
Scalene
Q6:
Define a function that decides what to wear, according to the
month and number of the day.
Function name will be what_to_wear and it will ask for the
name of the month and day number.
According to month and day number, it will check for the
season and with season, it will return what to wear.
There are 4 months which are in between the seasons; March,
June, September and December. For these months, the first 15
days will be in the previous season. For example for March,
it will be Winter until 15th. And it will be Spring after the
15th.
Seasons and Garments:
Spring: Shirt
Summer: T-shirt
Autumn: Sweater
Winter: Coat
Expected Output:
Enter month name: March
Enter day number: 24
In March, day 24 we wear Shirt
[
6 1 # Q 6:
]:
2
3 # --- your solution here --- #
4
5 # call the function you defined
6 what_to_wear()
[
6 Enter month name: September
]:
Enter day number: 8
In September, day 8 we wear T-shirt
Q7:
Define a function named which_day.
It will ask for any of the capital letters: F, M, S, T, W.
These are the first letters of the day names in a week.
The function will decide which day by the first letter.
It may ask additional questions if needed?
Days: Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday, Sunday
Additional questions by letter:
S:
T:
"at the beginning of the week" or
"at the middle of the week"
Expected Output:
Please enter any of the capital letters: F, M, S, T, W : T
Is it at the beginning of the week? Yes - No? No
Your day: THURSDAY
[
7 1 # Q 7:
]:
2
3 # --- your solution here --- #
4
5 # call the function you defined
6 which_day()
[
7 Enter one of: F, M, S, T, W : T
]:
Is it at the beginning of the week? Yes - No : No
Your day: THURSDAY
Q8:
Define a function named odds_evens.
It will ask for a positive number from the user.
Check if the input is an integer or not.
Also check if the number is a positive number.
Decide if the number is ODD or EVEN and print:
Expected Output:
Please enter a number: 18
EVEN of TWENTY
[
8 1 # Q 8:
]:
2
3 # --- your solution here --- #
4
5 # call the function you defined
6 odds_evens()
[
8 Please enter a number: 18
]:
EVEN OF TVENTY
Q9:
Sum of Odds and Evens:
If the sum of two numbers is Odd then only one of these is
Odd.
It the sum of two numbers is Even then both are Odd or both
are Even.
Define a function named sum_of_odds_and_evens.
It will ask for two integers from the user.
Then it will decide if the sum is Odd or Even.
Important Note: Do not calculate the actual summation as x
+ y.
Expected Output:
First Number: 8
Second Number: 3
Sum is ODD
[
9 1 # Q 9:
]:
2
3 # --- your solution here --- #
4
5 # call the function you defined
6 sum_of_odds_and_evens()
[
9 Number One: 8
]:
Number Two: 3
Sum is ODD
Q10:
Define a function named count_down.
It will ask for two integers from the user.
First it will decide which one of them is small and which one
is large.
Then it will print the numbers starting from the large one up
to the small one (backwards).
Hints:
[1
1 # Q 10:
0]:
2
3 # --- your solution here --- #
4
5 # call the function you defined
6 count_down()
[1
Number One: 26
0]:
Number Two: 12
small 12
large 26
26
25
24
…
…
13
12
OceanofPDF.com
SOLUTIONS - Conditional Statements
Here are the solutions for the quiz for Chapter 8 - Conditional
Statements.
SOLUTIONS - Conditional Statements:
S1:
[
1 1 # S 1:
]:
2
3 # ask for number
4 num = int(input("Please enter a number: "))
5
6 # decide
7 if num % 2 == 0:
8 print(num, "is EVEN")
9 else:
10 print(num, "is ODD")
[
1 Please enter a number: 8
]:
8 is EVEN
S2:
[
2 1 # S 2:
]:
2
3 # ask for a letter
4 letter = input("Please enter a letter: ")
5
if letter == 'a' or letter == 'e' or letter == 'i' or letter
6
== 'o' or letter == 'u':
7 print("VOWEL:", letter)
8 else:
9 print("CONSONANT:", letter)
[
2 Please enter a letter: u
]:
VOWEL: u
S3:
[
3 1 # S 3:
]:
2
3 def which_shape():
4 """
Asks for a number and returns the name of the
5
shape.
6 Parameters: None
7 Returns: str shape_name
8 """
9
10 # get the number
number_of_sides = int(input("Please enter the
11
number of sides: "))
12
13 # define a variable to keep the shape name
14 shape_name = ""
15
16 if number_of_sides == 3:
17 shape_name = 'Triangle'
18 elif number_of_sides == 4:
19 shape_name = 'Rectangle'
20 elif number_of_sides == 5:
21 shape_name = 'Pentagon'
22 elif number_of_sides == 6:
23 shape_name = 'Hexagon'
24 else:
25 shape_name = 'Polygon'
26
27 # return the shape name
28 return shape_name
29
30 # call the function you defined
31 shape = which_shape()
32 print(shape)
[
3 Please enter the number of sides: 4
]:
Rectangle
S4:
[
4 1 # S 4:
]:
2
3 def number_of_days_in_month():
4 # variable for number of days
5 days = 31
6
7 # ask for the month
month = input("Please enter the month name:
8
")
9
# Now check months with number of days 28-
10
29, 30 and 31
11 if month == 'April' or month == 'June' or \
month == 'September' or month ==
12
'November':
13 days = 30
14 elif month == 'February':
15 days = '28 or 29'
16
return 'There are ' + str(days) + ' days in ' +
17
str(month)
18
19 # call the function you defined
20 number_of_days_in_month()
[
4 Please enter the month name: February
]:
'There are 28 or 29 days in February'
S5:
[
5 1 # S 5:
]:
2
3 def name_the_triangle():
4 # variable to keep name
5 name = ""
6
7 # ask for lengths
side_1 = float(input('Length for the first side:
8
'))
side_2 = float(input('Length for the second
9
side: '))
side_3 = float(input('Length for the third side:
10
'))
11
12 # conditions
13 if side_1 == side_2 and side_1 == side_3:
14 name = 'Equilateral'
elif side_1 == side_2 or side_1 == side_3 or
15
side_2 == side_3:
16 name = 'Isosceles'
17 else:
18 name = 'Scalene'
19
20 return name
21
22 # call the function you defined
23 print(name_the_triangle())
[
5 Length for the first side: 10
]:
Length for the second side: 45
Length for the third side: 20
Scalene
S6:
[
6 1 # S 6:
]:
2
3 def what_to_wear():
4 # ask for month and day number
5 month = input("Enter month name: ")
6 day = int(input("Enter day number: "))
7
8 # keep the garment
9 garment = ""
10
11 # decide what to wear
12 if month == 'January' or month == 'February':
13 garment = 'Coat'
14 elif month == 'March':
15 # March 15
16 if day <= 15:
17 garment = 'Coat'
18 else:
19 garment = 'Shirt'
20 elif month == 'April' or month == 'May':
21 garment = 'Shirt'
22 elif month == 'June':
23 # June 15
24 if day <= 15:
25 garment = 'Shirt'
26 else:
27 garment = 'T-shirt'
28 elif month == 'July' or month == 'August':
29 garment = 'T-shirt'
30 elif month == 'September':
31 # September 15
32 if day <= 15:
33 garment = 'T-shirt'
34 else:
35 garment = 'Sweater'
elif month == 'October' or month ==
36
'November':
37 garment = 'Sweater'
38 elif month == 'December':
39 # December 15
40 if day <= 15:
41 garment = 'Sweater'
42 else:
43 garment = 'Coat'
44
print('In {0}, day {1} we wear
45
{2}'.format(month, day, garment))
46
47 # call the function you defined
48 what_to_wear()
[
6 Enter month name: September
]:
Enter day number: 8
In September, day 8 we wear T-shirt
S7:
[7]
1 # S 7:
:
2
3 def which_day():
4 # ask for a letter
letter = input("Enter one of: F, M, S, T, W :
5
")
6
7 # check if the letter is in list
if not (letter == 'F' or letter == 'M' or letter
8
== 'S' or letter == 'T' or letter == 'W'):
9 print("Wrong letter.")
10 return
11
12 # if the letter is correct
13 else:
14 # letter controls
15 # F
16 if letter == 'F':
17 day = 'FRIDAY'
18 # M
19 elif letter == 'M':
20 day = 'MONDAY'
21 # S
22 elif letter == 'S':
23 # ask additional question
first_day_of_weekend = input('Is it the
24
first day of the weekend? Yes - No: ')
25
26 if first_day_of_weekend == 'Yes':
27 day = 'SATURDAY'
28 else:
29 day = 'SUNDAY'
30 # T
31 elif letter == 'T':
32 # ask additional question
beginning_of_week = input('Is it at the
33
beginning of the week? Yes - No : ')
34
35 if beginning_of_week == 'Yes':
36 day = 'TUESDAY'
37 else:
38 day = 'THURSDAY'
39 # W
40 elif letter == 'W':
41 day = 'WEDNESDAY'
42
43 print('Your day:', day)
44
45 # call the function you defined
46 which_day()
[7]
Enter one of: F, M, S, T, W : T
:
Is it at the beginning of the week? Yes - No :
No
Your day: THURSDAY
S8:
[
8 1 # S 8:
]:
2
3 def odds_evens():
4 n = input("Please enter a number: ")
5
6 # if int
7 if not n.isdigit():
8 print('Not a number')
9 return
10
11 # cast to int
12 n = int(n)
13
14 # Positivity
15 if n <= 0:
16 print('Not Positive.')
17 return
18 # It is positive
19 else:
20 # odd?
21 if n % 2 == 1:
22 print('ODD')
23 else:
24 # even
25 if 1 <= n <= 10:
26 print('EVEN OF TEN')
27 elif 11 <= n <= 20:
28 print('EVEN OF TVENTY')
29 elif n > 20:
30 print("EVEN")
31
32 # call the function you defined
33 odds_evens()
[ Please enter a number: 18
8
]:
EVEN OF TVENTY
S9:
[
9 1 # S 9:
]:
2
3 def sum_of_odds_and_evens():
4 # get numbers
5 n1 = int(input("Number One: "))
6 n2 = int(input("Number Two: "))
7
8 # if only one is Odd -> summation ODD
9 # (n1 is odd and n2 is even)
10 # (n1 is even and n2 is odd)
if (n1 % 2 == 1 and n2 % 2 == 0) or (n1 % 2
11
== 0 and n2 % 2 == 1):
12 print("Sum is ODD")
13 else:
14 print("Sum is EVEN")
15
16 # call the function you defined
17 sum_of_odds_and_evens()
[
9 Number One: 8
]:
Number Two: 3
Sum is ODD
S10:
[1
1 # S 10:
0]:
2
3 def count_down():
4 # get numbers
5 num1 = int(input("Number One: "))
6 num2 = int(input("Number Two: "))
7
8 # variables for small and large
9 small = 0
10 large = 0
11
12 if num1 <= num2:
13 small = num1
14 large = num2
15 else:
16 small = num2
17 large = num1
18
19 print("small", small)
20 print("large", large)
21
22 # call recursion
23 print_down(small, large)
24
25
26 # recursion function
27 def print_down(end, value):
28
29 # exit condition
30 if value == end:
31 print(value)
32 return
33 else:
34 print(value)
35 print_down(end, value - 1)
36
37 # call the function you defined
38 count_down()
[1
Number One: 26
0]:
Number Two: 12
small 12
large 26
26
25
24
…
…
13
12
OceanofPDF.com
9. Functions II
This chapter is the second part of functions. We will learn more
about functions and most of the main concepts related to them.
Let’s start.
Return Value
Most of the time functions return a value after execution. The
caller of that function receives this returned value and uses it. There
are functions which do not return any value, which we call as void
functions.
[
1 1 import math
]:
2
3 def area_of_circle(radius):
4 # calculate the area and assign to a variable
5 a = math.pi * radius**2
6
7 # return the area
8 return a
The function receives the radius of the circle as the parameter.
It calculates the area of the circle, assigns it to a variable and
returns that variable in line 8. Let’s call this function and use the
returning value.
[
2 1 area = area_of_circle(4)
]:
2 print(area)
[
2 50.26548245743669
]:
[
3 1 # function without temp variable
]:
2
3 def area_of_circle_without_temp(radius):
4 # calculate the area and return it
5 return math.pi * radius**2
In cell 3, we define a new function with name
area_of_circle_without_temp . Instead of using a temp variable,
we calculate the area and return it in the same line. We will get the
same result if we call it with the same argument:
[
4 1 area = area_of_circle_without_temp(4)
]:
2 print(area)
[
4 50.26548245743669
]:
Incremental Development
While developing computer programs, we cannot consider and
code everything at once. That's why we have to concentrate on
Incremental Development. Incremental Development is essential
for debugging. If you cannot debug every step of your code, and
make sure that it works correctly, you cannot be sure about its
quality.
Example:
Let's say we are trying to calculate the distance (Euclidean
distance) between two points. In Math it is very easy to calculate
via Pythagoras' Theorem:
In cell 7, we call the function with four arguments: x1, y1, x2,
y2 with values 1, 6, 4, 10 respectively. And difference between
the x values is 4 - 1 = 3 and the y values is 10 - 6 = 4 . And
these are the values that get printed when we call the function.
Which means our function calculates the distances correctly. Let’s
move on to the next step.
[
# Step 2: Calculate the sum of squares and print
8 1
it
]:
2
3 def distance(x1, y1, x2, y2):
4
5 # first diff of x's
6 dx = x2 - x1
7
8 # diff of y's
9 dy = y2 - y1
10
11 # <------- DEBUG -------> #
12 # print("dx:", dx)
13 # print("dy:", dy)
14
15 # calculate sum of squares
16 sum_of_squares = dx**2 + dy**2
17
18 # <------- DEBUG -------> #
19 print("sum_of_squares:", sum_of_squares)
And let’s call the function again to see if the sum of squares is
calculated correctly:
[
9 1 distance(1, 6, 4, 10)
]:
[
9 sum_of_squares: 25
]:
Compositions
In its core, Functional Programming is about dividing tasks
into pieces and define separate functions for these tasks. Each
function is responsible from its own task.
Example:
Let's say we have two points. And let's assume the line segment
combining these two points as the radius of a circle. Let's try to
calculate the area of the circle having this radius. Instead of doing
all the steps in one giant function we will create separate functions
for each task and call them when needed.
[1 def area_of_circle_combining_two_points(x1,
1
4]: y1, x2, y2):
2 """
Calculates the area of circle with radius from
3
point one to point two.
Parameters: int x1, y1 first point, int x2, y2
4
second point
5 Returns: The area of circle
6 """
7
8 # calculate the radius from points
9 # distance between point 1 and point 2
10 r = distance(x1, y1, x2, y2)
11
12 # calculate the area
13 area = area_of_circle(r)
14
15 # return the result
16 return area
Now that we have our bool functions, let’s use them. They will
tell us if a given number is either odd or even.
[1
1 is_even(11)
7]:
[1
False
7]:
[1
1 is_odd(11)
8]:
[1
True
8]:
[1
1 is_even(40)
9]:
[1
True
9]:
Now let’s assign this function to a variable. And then call the
new variable:
[2
1 hello = say_hello
7]:
[2
1 hello("Hi yourself Developer")
8]:
[2
Hi yourself Developer
8]:
In the output of cell 30, we see that the args are (5, 7) and its
type is a tuple . To be sure, let’s call it with another set of
arguments and see the result one more time:
[3
1 summation(5, 7, 1, 4, 3)
1]:
[3
args: (5, 7, 1, 4, 3)
1]:
<class 'tuple'>
As you see in cell 31, the args are (5, 7, 1, 4, 3) and its type
is a tuple . Perfect. We are now sure that, we can handle any
number of parameters with *args . And also we can access any
single parameter in it.
[3
1 # summation
2]:
2
3 def summation(*args):
4 # get the sum of args with built-in sum() fn
5 summation_result = sum(args)
6
7 print(summation_result)
Since we are interested in the sum of all parameters in the
args tuple , we can use the built-in Python function sum() to get
the summation. And that’s what we do in cell 32, line 6. Let’s call it
and see if it works:
[3
1 summation(5, 7)
3]:
[3
12
3]:
[3
1 summation(5, 7, 1, 4, 3)
4]:
[3
20
4]:
[4
1 text = 'Hi there you Python'
0]:
2 text.split()
[4
['Hi', 'there', 'you', 'Python']
0]:
As you see in the output of cell 58, the source code for
triple_multiplier function is: return lambda a: a * n. Which
proves that it is the lambda function returned from multiply_by
function. The same is true for double_multiplier :
[5
1 double_multiplier??
9]:
[5
Signature: double_multiplier(a)
9]:
Docstring: <no docstring>
Source: return lambda a: a * n
File: 9_functions_ii\ref\<ipython-input-51-6d3f>
Type: function
Let’s do a final example on this multiply_by function. This
time let’s pass the value of 5:
[6
1 penta_multiplier = multiply_by(5)
0]:
Nested Functions
In Python we can define functions inside the body of other
functions. These functions which we define in the others are called
nested functions.
As an example we will use nested functions, to check whether
a given number is a common multiple of 5 and 8.
[6
1 # 2 nested functions inside the main function
3]:
2
3 def is_it_common_multiple(n):
4 """
Checks whether the given number is a
5
common multiple of 5 and 8.
6 Parameter: int n
Returns: True if its multiplier of 5 and 8,
7
False otherwise
8 """
9
10 # nested function 1 -> 5
11 def multiple_of_five(n):
12 if n % 5 == 0:
13 return True
14 else:
15 return False
16
17 # nested function 2 -> 8
18 def multiple_of_eight(n):
19 if n % 8 == 0:
20 return True
21 else:
22 return False
23
24 # Check both conditions
if multiple_of_five(n) and
25
multiple_of_eight(n):
26 return True
27 else:
28 return False
In cell 63, we define two nested functions, multiple_of_five
and multiple_of_eight . They are both bool functions and in the
if block (line 25) we call them. We pass the number n as the
argument to them. And they return either True or False . If both
return True , then this means the number n is a common multiple
of 5 and 8.
Let’s call our is_it_common_multiple function and see the
results:
[6
1 is_it_common_multiple(24)
4]:
[6
False
4]:
[6
1 is_it_common_multiple(40)
5]:
[6
True
5]:
In cell 67, we get the first letter in the text variable via the
code: text[0]. Here, 0 is the index of the first item. And the letter
at the index 0 is ‘T’ .
Since we know how to access the first element in the text
variable, let’s try to modify it:
[6
1 text[0] = 'P'
8]:
[6 TypeError: 'str' object does not support item
8]: assignment
In cell 68, we get the TypeError when we try to modify the
text variable. And the error description says that we cannot mutate
a string ( str ) object. Now comes a question: How will we change
this string, if we cannot mutate it? The answer is, we reassign it.
Instead of mutating a string we should reassign it. Reassignment
doesn't mean Mutation.
Now, to fix the text variable let’s reassign the correct value:
[6
1 # reassign the text variable
9]:
2 text = 'Python'
Let’s print its value to see if the text variable has changed:
[7
1 print(text)
0]:
[7
Python
0]:
[7
1 # get the item at index 2
2]:
2 my_list[2]
[7
'C'
2]:
Rule of Thumb:
Immutable Types Pass by Value: If you pass an Immutable
Object ( int , str , etc.) as a parameter to a function; only its
duplicate will pass to the function. Not itself, just a copy of it. This
is called pass by value.
Mutable Types Pass by Reference: If you pass a Mutable
Object ( list, dict, set ) as a parameter to a function; its reference
will pass to the function. The object itself will be accessible inside
the function. This is called pass by reference. Reference means
their memory addresses.
[7
1 # Example:
5]:
2
3 # IMMUTABLE
4 # str
5
6 language = 'Python'
print("------ Before passing to function ----- :
7
", language)
8
9 # function is changing the parameter
10 def change_language(name):
11 name = 'Java'
12
13 # call function and pass language
14 change_language(language)
15
print("------ After passing to function ----- : ",
16
language)
[7
------ Before passing to function ----- : Python
5]:
------ After passing to function ----- : Python
OceanofPDF.com
QUIZ - Functions II
Now it’s time to solve the quiz for this chapter. You can
download the quiz file, QUIZ_Functions_II.ipynb, from the
Github Repository of this book. It is in the folder named
9_Functions_II. Here are the questions for this chapter:
QUIZ – Functions II:
Q1:
Many times, we need input from the user.
Define a generic function to ask for user input.
Function name will be get_input.
Function will take the input text as parameter and use it
while asking for input from the user.
[
1 1 # Q 1:
]:
2
3 # --- your solution here --- #
4
5
6 # call the function you defined
7 # Ask for an integer
8 number = get_input("Please enter a number: ")
9 print(number)
10
11 # Ask for the month name
12 month = get_input("Please enter month name: ")
13 month
[
1 Please enter a number: 78
]:
78
Please enter month name: June
'June'
Q2:
We already know that, get_input function in Question 1
returns str.
Now we will define a new function named is_integer.
It will get a string parameter and will check if its value is
integer or not.
It will return True if the parameter is integer.
[
2 1 # Q 2:
]:
2
3 # --- your solution here --- #
4
5
6 # call the function you defined
7
8 # let's test is_integer function
9 # first call with a string
10 input_text = "Please enter a number: "
11 user_input = get_input(input_text)
12 is_input_integer = is_integer(user_input)
13 print(is_input_integer)
14
15 # call with an integer
16 input_text = "Please enter a number: "
17 user_input = get_input(input_text)
18 is_input_integer = is_integer(user_input)
19 print(is_input_integer)
[
2 Please enter a number: A
]:
False
Please enter a number: -8
True
Q3:
Get an integer from the user, using functions in Q1 and Q2.
Which will guarantee that it is an integer.
If the input is not integer, ask recursively.
Function name will be get_integer.
Hints:
Use Recursion
[
3 1 # Q 3:
]:
2
3 # --- your solution here --- #
4
5
6 # call the function you defined
7 # do not enter an integer for first several times
8 num = get_integer("please enter an integer: ")
9 print(num)
[
3 please enter an integer: asdf
]:
please enter an integer: qwerty
please enter an integer: 78abc
please enter an integer: 89
89
Q4:
Define a function named day_of_week.
The function will ask the user to enter an integer between 1
and 7.
And it will decide the day name according to that number.
Hints:
use functions in Q3
check for number being in range 1-7
[
4 1 # Q 4:
]:
2
3 # --- your solution here --- #
4
5
6 # call the function you defined
7 day_of_week()
[
4 Please enter a day number 1-7: asdfg
]:
Please enter a day number 1-7: rt
Please enter a day number 1-7: 5
'Friday'
Q5:
Run the functions below and examine the Stack Trace.
Stack Trace means the execution order of functions.
Make sure you understand the execution order.
[
5 1 # Q 5:
]:
2
3 def n(c):
4 power = m(c, c)
5 print(c, power)
6 return power
7
8 def m(x, y):
9 x = x + 2
10 return x**y
11
12 def p(x, y, z):
13 summation = x + y + z
14 sqr = n(summation)**2
15 return sqr
16
17 a = 1
18 b = a + 1
19 print(p(a, b+1, a+b))
[
5 7 4782969
]:
22876792454961
Stack Trace:
--- your solution here ---
Q6:
Define a function named arithmetic_mean.
The function will get parameters that are not known.
(unknown parameters)
Namely, the number of parameters is not known in advance.
The function will print the arithmetic mean of these
parameters.
Hints:
*args
Arithmetic Mean = Summation / Number of Elements
sum()
len()
[
6 1 # Q 6:
]:
2
3 # --- your solution here --- #
4
5
6 # call the function you defined
7 mean = arithmetic_mean(2, 5, 11)
8 print(mean)
[
6 6.0
]:
Q7:
Define a function named area_of_circle.
It will take the radius as parameter and calculates the area of
the circle.
The function body should be one line of code.
Hints:
Area of Circle = π * r 2
[
7 1 # Q 7:
]:
2
3 # --- your solution here --- #
4
5
6 # call the function you defined
7 area_of_circle(10)
[
7 314.1592653589793
]:
Q8:
Define a function named perimeter_of_circle.
It will take the radius as parameter and calculates the
perimeter of the circle.
The function body should be one line of code.
Hints:
Perimeter of Circle = 2 * π * r
[
8 1 # Q 8:
]:
2
3 # --- your solution here --- #
4
5
6 # call the function you defined
7 perimeter_of_circle(10)
[
8 62.83185307179586
]:
Q9:
Define a function named area_of_rectangle.
The parameters will be length (l) and width (w).
The function body should be one line of code.
Hints:
Area of Rectangle = l * w
[
9 1 # Q 9:
]:
2
3 # --- your solution here --- #
4
5
6 # call the function you defined
7 area_of_rectangle(5, 12)
[
9 60
]:
Q10:
Define a function to calculate the surface area of a right
cylinder.
The name will be area_of_cylinder and it will take two
parameters.
The first parameter r will be the radius of top and base
circles.
The second parameter h will be the height of cylinder.
Hints:
[1
1 # Q 10:
0]:
2
3 # --- your solution here --- #
4
5
6 # call the function you defined
7 area_of_cylinder(10, 2)
[1
753.9822368615504
0]:
OceanofPDF.com
SOLUTIONS - Functions II
Here are the solutions for the quiz for Chapter 9 - Functions II.
SOLUTIONS - Functions II:
S1:
[
1 1 # S 1:
]:
2
3 def get_input(text):
4 """
5 Generic function to get input.
6 Parameter: str text
7 Returns: the user input, str
8 """
9
10 # ask for input
11 user_input = input(text)
12
13 return user_input
14
15
16 # call the function you defined
17 # Ask for an integer
18 number = get_input("Please enter a number: ")
19 print(number)
20
21 # Ask for the month name
22 month = get_input("Please enter month name: ")
23 print(month)
[ Please enter a number: 78
1
]:
78
Please enter month name: June
'June'
S2:
[
2 1 # S 2:
]:
2
3 def is_integer(text):
4 """
5 Checks if the text is integer.
6 Parameter: str text
7 Returns: True if text is integer
8 """
9
10 # possibilities
11
# 1 - there may be spaces at the beginning and
12
end of string -> str.strip()
13 text = text.strip()
14
# 2- There may be +,- signs at the beginning,
15
+9, -8 -> str.strip(<character>)
16 text = text.strip('+-')
17
18 # Now we can check if integer -> isdigit()
19 if text.isdigit():
20 return True
21 else:
22 return False
23
24
25 # call the function you defined
26
27 # let's test is_integer function
28 # first call with a string
29 input_text = "Please enter a number: "
30 user_input = get_input(input_text)
31 is_input_integer = is_integer(user_input)
32 print(is_input_integer)
33
34 # call with an integer
35 input_text = "Please enter a number: "
36 user_input = get_input(input_text)
37 is_input_integer = is_integer(user_input)
38 print(is_input_integer)
[
2 Please enter a number: A
]:
False
Please enter a number: -8
True
S3:
[
3 1 # S 3:
]:
2
3 def get_integer(input_text):
4 """
5 Gets integer from user (eventually).
6 Parameters: str input_text
7 Returns: int user_input
8 """
9
10 # ask for input
11 user_input = get_input(input_text)
12
13 # check if int -> exit condition
14 if is_integer(user_input):
15 return int(user_input)
16 else:
17 # ask again
18 return get_integer(input_text)
19
20
21 # call the function you defined
22 # do not enter an integer for first several times
23 num = get_integer("please enter an integer: ")
24 print(num)
[
3 please enter an integer: asdf
]:
please enter an integer: qwerty
please enter an integer: 78abc
please enter an integer: 89
89
S4:
[
4 1 # S 4:
]:
2
3 def day_of_week():
4
5 # get an integer first
day_num = get_integer("Please enter a day
6
number 1-7: ")
7
8 # check if 1-7
9 if not 1 <= day_num <= 7:
10 return "Invalid day number. Not in 1-7."
11
12 # If we passed -> 1-7
13
14 if day_num == 1:
15 return 'Monday'
16 elif day_num == 2:
17 return 'Tuesday'
18 elif day_num == 3:
19 return 'Wednesday'
20 elif day_num == 4:
21 return 'Thursday'
22 elif day_num == 5:
23 return 'Friday'
24 elif day_num == 6:
25 return 'Saturday'
26 else:
27 return 'Sunday'
28
29
30 # call the function you defined
31 day_of_week()
[
4 Please enter a day number 1-7: asdfg
]:
Please enter a day number 1-7: rt
Please enter a day number 1-7: 5
'Friday'
S5:
[
5 1 # S 5:
]:
2
3 def n(c):
4 power = m(c, c)
5 print(c, power)
6 return power
7
8 def m(x, y):
9 x = x + 2
10 return x**y
11
12 def p(x, y, z):
13 summation = x + y + z
14 sqr = n(summation)**2
15 return sqr
16
17 a = 1
18 b = a + 1
19 print(p(a, b+1, a+b))
[
5 7 4782969
]:
22876792454961
S6:
[
6 1 # S 6:
]:
2
3 def arithmetic_mean(*args):
4
5 # calculate the summation
6 summation = sum(args)
7
8 # number of elements
number_of_elements = len(args) # len ->
9
length
10
11 return summation / number_of_elements
12
13
14 # call the function you defined
15 mean = arithmetic_mean(2, 5, 11)
16 print(mean)
[
6 6.0
]:
S7:
[
7 1 # S 7:
]:
2
3 import math
4
5 def area_of_circle(r):
6 """Returns the area of circle"""
7 return math.pi * r**2
8
9
10 # call the function you defined
11 area_of_circle(10)
[
7 314.1592653589793
]:
S8:
[
8 1 # S 8:
]:
2
3 import math
4
5 def perimeter_of_circle(r):
6 """Returns the perimeter of circle"""
7 return 2 * math.pi * r
8
9
10 # call the function you defined
11 perimeter_of_circle(10)
[
8 62.83185307179586
]:
S9:
[
9 1 # S 9:
]:
2
3 def area_of_rectangle(l, w):
4 return l * w
5
6
7 # call the function you defined
8 area_of_rectangle(5, 12)
[
9 60
]:
S10:
[1
1 # S 10:
0]:
2
3 def area_of_cylinder(r, h):
4 """
5 Calculates the total surface area of cylinder.
6 Parameters:
7 * r: int radius
8 * h: int height
9 Returns: int total surface area
10 """
11
12 # first calculate the area of circles (2)
13 circle = area_of_circle(r)
14
15 # lateral surface (rectangle)
16 # Perimeter of circle and height
17 # lateral area = perimeter * height
18
19 # perimeter
20 perimeter = perimeter_of_circle(r)
21
22 # rectangle
23 rectangle = area_of_rectangle(perimeter, h)
24
25 # total area
26 total_area = 2 * circle + rectangle
27
28 return total_area
29
30
31 # call the function you defined
32 area_of_cylinder(10, 2)
[1
753.9822368615504
0]:
OceanofPDF.com
10. Loops
This chapter is about loops. A loop is an iteration over a
sequence. In Python, loops are built via while and for keywords.
Let’s learn the loops and some important concepts related to them.
while Loop
The while loop is one of the two types of loops we have in
Python. The while loop executes a set of statements as long as its
condition is true. Let’s start the examples of the while loop.
Condition Variable:
If you do not set the condition variable properly, then the loop
will never stop, which we call as an Infinite Loop. You should
always check if your condition variable is updated properly in the
while loop.
In cell 4, we get the number from the user. Then we set the
condition variable, i , as 2. And the while loop checks if i <
number . If it’s True , then we check if the number is divisible by
i as: number % i == 0 . And if this is True , then we print the
number i . In line 13, we update the condition variable.
for Loop
The second type of loop we have in Python, is the for loop.
It’s simpler than the while loop and more commonly used. You do
not need to set and update a condition variable externally. The for
loop manages this automatically for you. Let’s see it with
examples.
[1
1 # range(start, end, step)
4]:
2 # step -> - (minus) -> backwards
3 for i in range(20, 0, -1):
4 if i % 2 == 1:
5 print(i)
[1
19
4]:
17
15
13
11
9
7
5
3
1
len
In Python, for sequence types ( str , list , tuple , etc.), to get
the length of the object we use the built-in len() function. It returns
the number of items in the sequence.
[1
1 len("Python")
7]:
[1
6
7]:
In cell 19, we ask for the user input. We already know that, the
input() function always return the user input as a string. That’s
why the len() function in line 5, returns the number of characters
in it.
Now let’s see the documentation of the len() function:
[2
1 len?
0]:
[2
Signature: len(obj, /)
0]:
Docstring: Return the number of items in a
container.
Type: builtin_function_or_method
enumerate
In Python, when looping over sequences we might need the
index of current iteration. In these cases we use the built-in
enumerate() function to get the index.
Example: Remember in cell 16, we defined a for loop to
concatenate the “-” char into the letters of a string. But there was a
bug in it. Now, let’s refactor that code and remove the unnecessary
"-" from the end of text.
[2
1 # ask for an input
1]:
2 user_input = input("Please enter a text: ")
3
4 # variable for new input
5 new_input = ""
6
7 # loop with enumerate
8 for index, letter in enumerate(user_input):
9 # concatenate the letter
10 new_input += letter
11
12 # if this is the last index
13 if index < len(user_input) - 1:
14 new_input += "-"
15
16 print(new_input)
[2
Please enter a text: Python
1]:
P-y-t-h-o-n
Let’s talk a little bit on the final index, since it’s important
when we loop over sequences. Let’s say we want to get the final
index for the text of “pandas”:
[2
1 len('pandas') - 1
3]:
[2
5
3]:
As you see in cell 23, the length of the text “pandas” is 6 but
the final index is 5. That’s because indices start from 0 in Python.
Let’s loop over this text and print the indexes:
[2
1 # 0, 1, 2, 3, 4, 5
4]:
2
3 for index, letter in enumerate('pandas'):
4 print(index)
[2
0
4]:
1
2
3
4
5
Index Example:
In this example we will start from 10 and print up to 100
(included) with step size of 5. As: 10, 15, 20, ..., 100. In this list of
numbers we want to find the number at index of 3. Index 3 means,
the number in 4th order. Because the index starts from 0.
First let’s see the number in the list:
[2
1 # first let's see the numbers
5]:
2 for number in range(10, 101, 5):
3 print(number)
[2
10
5]:
15
20
25
30
…
…
95
100
Nested Loops
Most of the time we need to nest the loops in programming.
Actually, it’s very simple in Python. You need to keep the right
level of indentation at your loops, that’s all. Here is the syntax for a
nested loop:
# outer loop
for .....:
# nested loop
for ......:
# nested loop
for .......:
# nested loop
for ......:
In cell 28, the outer loop is for the rows. We loop over 3 rows.
The row number is called i at each iteration. And in each row, we
have a nested loop for the columns. At the beginning of each row
we set an empty string to keep the stars as: stars = "" . Then in
line 12, we start the columns loop. The column number is j . For
each column iteration we concatenate a star and a space ( “* ” ) to
the stars variable as: stars += "* " . And after we finish iterating
over all the columns, we print the stars variable in line 17.
Now let’s do the same thing with a pair of nested for loops.
You will see it’s simpler with for loops.
[2
1 # for
9]:
2
3 # iterate over rows
4 # iterate over columns
5
6 # rows
7 for i in range(3):
8 stars = ""
9
10 # columns
11 for j in range(3):
12 stars += "* "
13
14 print(stars)
[2
* * *
9]:
* * *
* * *
In cell 29, the outer loop is for the rows and the inner one is for
the columns. We use the range() function in both. The idea is the
same. At each row, we set an empty string to keep the stars as:
stars = "" . Then inside the columns loop we concatenate "* " to
the stars variable (line 12). And finally we print the stars
variable in line 14.
In cell 30, we loop over the letters in the string with a for
loop. And inside the loop, we check if the current character is a
space or not as: if character == ' ' . If it’s a space then we exit
the loop with the break keyword in line 10.
In cell 32, we have condition check inside the for loop. In line
3 we check it the current number, i , is an odd number. If it’s odd
then we skip the current iteration with the continue keyword in
line 4. And the loop goes on with the next number. If it’s not an odd
number we print that number in line 7.
for-else
Sometimes you need to know if your loop has completed
successfully. Successful loop means a loop with no break
statement executed in it. For these case we use for-else structures.
Here is the syntax of a for-else block:
for .......:
..........
..........
break
..........
..........
else:
...... no break executed ......
...... the break executed in the loop ......
In cell 35, the condition check in the for loop never becomes
True. Since there is no number 17 in the range(2,10) . And this
means that we never encounter the break statement in the line 6.
In other words, the loop completes successfully. The else block
gets executed, and it prints the text of “Loop executed
successfully” in line 8.
OceanofPDF.com
QUIZ - Loops
Now it’s time to solve the quiz for this chapter. You can
download the quiz file, QUIZ_Loops.ipynb, from the Github
Repository of this book. It is in the folder named 10_Loops. Here
are the questions for this chapter:
QUIZ - Loops:
Q1:
Define a function that draw a square of stars on the screen.
The function name will be sqaure_of_stars_with_while.
The parameter will be number of stars in one side of square.
Hints:
[
9 1 # Q 9:
]:
2
3 # ----- your solution here -----
4
5
6 # call the function you defined
7 trees_and_fives()
[
9 1
]:
2
Trees
4
Fives
Trees
7
8
Trees
Fives
11
Trees
13
14
Trees&Fives
16
17
Trees
…
…
38
Trees
Fives
41
Trees
43
44
Trees&Fives
46
47
Trees
49
Fives
Q10:
Define a function named concat_sentence.
The function will take a string as parameter (sentence).
It will first split this sentence into its words.
Then it will split each word into its characters.
And it will add "-" between each character.
Moreover it will add "__" (double underscore) between the
words.
And it will return the final sentence.
Hints:
OceanofPDF.com
SOLUTIONS - Loops
Here are the solutions for the quiz for Chapter 10 - Loops.
SOLUTIONS - Loops:
S1:
[
1 1 # S 1:
]:
2
3 def sqaure_of_stars_with_while(n):
4 """
5 Function to draw a square of stars.
6 Parameter: int n, number of stars
7 Returns: None
8 """
9
10 # rows
11 i = 0
12 while i < n:
13
14 stars = ""
15
16 # columns
17 j = 0
18 while j < n:
19 stars += "* "
20 j += 1
21
22 # print stars for row
23 print(stars)
24
25 i += 1
26
27
28 # call the function you defined
29 n = int(input("Enter the number of stars: "))
30 sqaure_of_stars_with_while(n)
[
1 Enter the number of stars: 5
]:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
S2:
[
2 1 # S 2:
]:
2
3 def sqaure_of_stars_with_for(n):
4 """
5 Function to draw a square of stars.
6 Parameter: int n, number of stars
7 Returns: None
8 """
9
10 # rows
11 for i in range(n):
12
13 stars = ""
14
15 # columns
16 for j in range(n):
17 stars += "* "
18
19 # print row
20 print(stars)
21
22
23 # call the function you defined
24 n = int(input("Enter the number of stars: "))
25 sqaure_of_stars_with_for(n)
[
2 Enter the number of stars: 5
]:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
S3:
[
3 1 # S 3:
]:
2
3 def lower_triangle_with_while(n):
4
5 # rows
6 i = 0
7 while i < n:
8
9 stars = ""
10
11 # columns
12 j = 0
13 while j <= i:
14 stars += "* "
15 j += 1
16
17 print(stars)
18
19 i += 1
20
21
22 # call the function you defined
23 lower_triangle_with_while(5)
[
3 *
]:
* *
* * *
* * * *
* * * * *
S4:
[
4 1 # S 4:
]:
2
3 def lower_triangle_with_for(n):
4
5 # rows
6 for i in range(n):
7
8 stars = ""
9
10 # columns
11 for j in range(i+1):
12 stars += "* "
13
14 print(stars)
15
16
17 # call the function you defined
18 lower_triangle_with_for(5)
[
4 *
]:
* *
* * *
* * * *
* * * * *
S5:
[
5 1 # S 5:
]:
2
3 def upper_triangle_with_for(n):
4
5 # rows
6 for i in range(n, 0, -1):
7
8 stars = ""
9
10 # columns
11 for j in range(i):
12 stars += "* "
13
14 print(stars)
15
16
17 # call the function you defined
18 upper_triangle_with_for(5)
[
5 * * * * *
]:
* * * *
* * *
* *
*
S6:
[
6 1 # S 6:
]:
2
3 def isosceles_triangle_with_for(n):
4
5 # lower triangle
6 lower_triangle_with_for(n - 1)
7
8 # upper triangle
9 upper_triangle_with_for(n)
10
11
12 # call the function you defined
13 isosceles_triangle_with_for(5)
[
6 *
]:
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
S7:
[
7 1 # S 7:
]:
2
3 def is_prime(n):
4 """
5 Checks if the number is prime or not.
6 Parameter: int n
7 Returns: True if n is prime, False otherwise
8 """
9
10 prime = True
11
12 for i in range(2, n):
13 # if n is divisible by i
14 if n % i == 0:
15 prime = False
16
17 # return the prime variable
18 return prime
19
20
21 # call the function you defined
22 is_prime(7)
[
7 True
]:
S8:
[
8 1 # S 8:
]:
2
3 def prime_factors(n):
4
5 for i in range(2, n):
6
7 # if n is divisible by i -> i is a factor
8 if n % i == 0:
9
10 # check if i is prime
11 if is_prime(i):
12 print(i)
13
14
15 # call the function you defined
16 prime_factors(24)
[
8 2
]:
3
S9:
[
9 1 # S 9:
]:
2
3 def trees_and_fives():
4
5 tree = 'Trees'
6 five = 'Fives'
7
8 for i in range(1, 51):
9
10 # both 3 and 5
11 if i % 3 == 0 and i % 5 == 0:
12 print(tree+'&'+five)
13 elif i % 5 == 0:
14 print(five)
15 elif i % 3 == 0:
16 print(tree)
17 else:
18 print(i)
19
20
21 # call the function you defined
22 trees_and_fives()
[
9 1
]:
2
Trees
4
Fives
Trees
7
8
Trees
Fives
11
Trees
13
14
Trees&Fives
16
17
Trees
…
…
38
Trees
Fives
41
Trees
43
44
Trees&Fives
46
47
Trees
49
Fives
S10:
[1
1 # S 10:
0]:
2
3 def concat_sentence(sentence):
4
5 # take the words list
words = sentence.split() # split from space
6
chars
7
8 # variable for new sentence
9 concatted_sentence = ""
10
11 # loop #1 -> words
12 for word_index, word in enumerate(words):
13 # print(word)
14
15 concatted_word = ""
16
17 # loop #2 -> chars
18 for char_index, char in enumerate(word):
19 concatted_word += char
20
21 # add the '-' -> check last
22 if char_index < len(word) - 1:
23 concatted_word += '-'
24
25 # print(concatted_word)
26
27 concatted_sentence += concatted_word
28
29 # check for last index
30 if word_index < len(words) -1:
31 concatted_sentence += '__'
32
33 return concatted_sentence
34
35
36 # call the function you defined
37 sentence = "This is a long story Rango"
38 final_sentence = concat_sentence(sentence)
39 print(final_sentence)
[1
T-h-i-s__i-s__a__l-o-n-g__s-t-o-r-y__R-a-n-g-o
0]:
OceanofPDF.com
11. Strings
What is a String?
Textual data in Python is handled with str objects, or strings .
Strings are immutable sequences of characters (chars). String
literals are written in a variety of ways:
[
3 1 # we want the letter in fruit of order 3
]:
2 # order 3 = index 2
3 fruit[2]
[
3 'a'
]:
Since we are looking for the item at 3rd order (index 2), we just
write as: fruit[2] . At it returns the value of ‘a’ . Here is another
example in cell 4:
[
4 1 # the letter in 6th order
]:
2 # order 6 = index 5
3 fruit[5]
[
4 'e'
]:
Let’s say, we want to get the last element in the fruit variable.
To do this, we first need the length of the string. Let’s see it in the
next section.
String Length
In Python, to get the length of sequence types we use the
standard len() function. Let’s find the length of the fruit variable
we defined in cell 1.
[
5 1 # length of fruit variable
]:
2 len(fruit)
[
5 6
]:
Let’s find the last item in the fruit variable in just one line:
[
7 1 # in one line
]:
2 # the last element
3 fruit[len(fruit) - 1]
[
7 'e'
]:
To get the last element we put the last index in square brackets
as: fruit[len(fruit) - 1].
Let’s see what happens, if you write a wrong index in the
square brackets. An index which does not exist in the string:
[
8 1 # wrong last index => fruit[6]
]:
2 fruit[len(fruit)]
[
8 IndexError: string index out of range
]:
String Slicing
A part of a sequence is called a slice. For slicing we use
indexing. The syntax of slicing on a sequence is: sequence[start
: end : step]. Here the start is included but the end is excluded.
The default values for the start, end and step are as follows:
[1
1 s = 'Python Hands-On'
2]:
How to read:
s[0,6] : Start slicing from index 0 up to index 6 (step is 1)
Let’s do another example to get the ‘Hands’ word out of the
text. The letter ‘H’ is at the index of 7 and the letter ‘s’ is at index
11. But the ending index should be 12, if we want to include 11. So
here is the slice:
[1
1 # We want the word Hands
5]:
2 s[7:12]
[1
'Hands'
5]:
How to Read:
s[7:12] : Start slicing from index 7 up to index 12 (step is 1)
The step size, also known as stride, determines the amount
you increase or decrease the index. In other words, step size
indicates how big the stride is. Let’s see some examples:
[1
1 # define a new variable
6]:
2 numbers = '123456789'
3 numbers
[1
'123456789'
6]:
Let’s say we want to get only the odd numbers out of the
numbers string. Here the start should be the first index, which 0.
And the end is the last index, which is 1 minus the length of the
string. Both start and end indices are at their default values. So we
can skip both of them. The only thing we need to type is the step
size:
[1
1 # get the odd numbers
7]:
2 numbers[::2]
[1
'13579'
7]:
Now, let’s get only the even numbers. This time we will start
from index 1. The end index will be the default value and the step
size is 2 again:
[1
1 # get even numbers
8]:
2 numbers[1::2]
[1
'2468'
8]:
Slicing can also be used for copying objects. If you slice the
whole object, then it will create a copy of it. Let’s do it:
[1
1 # we want to slice all
9]:
2 # copy the whole sequence
3 numbers[::]
[1
'123456789'
9]:
In cell 19, we slice the string with leaving start, end and step
size as blank. Which means all three will have the default values
and we will get the whole string. We get a copy of the string
actually. You can assign this copy to a new variable as we do in cell
20:
[2
1 # the complete slice means the copy
0]:
2 copy_of_numbers = numbers[::]
3 copy_of_numbers
[2
'123456789'
0]:
Negative Index
We have two index types for slicing: Positive Index and
Negative Index.
Positive Index:
Negative Index:
Now let’s find the last element with the negative index:
[2
1 # get the last element
2]:
2 # negative index
3 numbers[-1]
[2
'9'
2]:
s P y t h o n
index -----> 0 1 2 3 4 5
negative index <----- -6 -5 -4 -3 -2 -1
Let’s get the same letters with both index types side by side:
[2
1 # last letter
4]:
2 s[5], s[-1]
[2
('n', 'n')
4]:
[2
1 # first letter
5]:
2 s[0], s[-6]
[2
('P', 'P')
5]:
[2
1 # 4th index (normal index)
6]:
2 # -2 index (negative index)
3 s[4], s[-2]
[2
('o', 'o')
6]:
Practical Way:
The sum of absolute values of the normal index and the
negative index is equal to the length of the sequence. So if you
have any of them, either positive or negative, it is very easy to find
the other one.
In our case the length of the variable s is 6. And as you see in
cell 26, the positive index is 4 and the negative index is -2. The
sum of absolute values of them is 6, which is the length.
[2
1 # sum of absolute values is the length
7]:
2 # these indices points the same letter
3 s[-2], s[4]
[2
('o', 'o')
7]:
Reverse Slicing
Reverse Slicing is slicing the sequence from end to start (from
right to left). We can use negative step size for Reverse Slicing.
Reverse Slicing:
[2
1 # print numbers (normal)
8]:
2 numbers[::]
[2
'123456789'
8]:
Now let’s use the reverse slicing to print the numbers variable
in reverse order:
[2
1 # print numbers from end to start -> reverse order
9]:
2 numbers[::-1]
[2
'987654321'
9]:
Step Size:
[3
1 # print numbers from start to end incrementing by 2
0]:
2 # step is 2
3 numbers[::2]
[3
'13579'
0]:
[3
1 # print numbers from end to start decrementing by 2
1]:
2 # step is -2
3 numbers[::-2]
[3
'97531'
1]:
Let’s get the slice of ‘456’ with the normal index and default
step size (1):
[3
1 # normal index --------->
2]:
2 # 456 -> index from 3 up to 6
3 numbers[3:6]
[3
'456'
2]:
Now let’s get the same slice, ‘456’ with the negative index
and positive step size.
[3
1 # negative index <---------
3]:
2 # 456 -> index from -6 up to -3
3 numbers[-6:-3:1]
[3
'456'
3]:
Now that we have the part before letter ‘w’ and the part after
it, in cells 37 and 38, we can concatenate them with the capital
letter ‘W’. And we will reassign this concatenation to the
say_hello variable.
[3
1 # finally concatenate the parts and W
9]:
2 say_hello = slice_1 + "W" + slice_2
3 say_hello
[3
'Hello World'
9]:
String Methods
Strings have built-in methods in Python. These methods are
useful and can make your code much cleaner if you learn how to
use them. Here you can find the complete list of string methods in
official Python documentation.[7]
[4
1 word = 'computer'
0]:
2 word
[4
'computer'
0]:
[4
1 word_upper_case = word.upper()
1]:
2 word_upper_case
[4
'COMPUTER'
1]:
[4
1 A = 'Python'
8]:
2 B = 'Machine'
3 C = 'Learning'
4
5 arg = '{0} - {1} {2}'.format(A, B, C)
6 arg
[4
'Python - Machine Learning'
8]:
[5
1 word.find('in')
1]:
[5
1
1]:
In cell 51, the find() method looks for the ‘in’ substring in the
variable word . The first index where this substring starts is 1, so it
returns 1.
[5
1 word.find('t')
2]:
[5
-1
2]:
In cell 52, the find() method looks for letter ‘t’ in the variable
word . The value of the variable is ‘window’ . The letter ‘t’
doesn’t exist in this value, so it returns -1.
We can specify the start and end indices for the find()
method. The start index is where it starts searching. And the end
index means, up to which index it will search. The complete syntax
for the find() method is: find(text_to_search, start, end) .
[5
1 # specify starting index
3]:
2 word.find('w', 1)
[5
5
3]:
In cell 54, the find() method starts from index 2 and ends at
index 5. It looks for the letter ‘a’ only in this range. And it returns
index 4. Which is where the first occurrence of letter ‘a’ exists.
[6 new_language = language.replace('2008',
1
6]: '2021').replace('Java', 'Python')
2 new_language
[6
'In 2021 Python is the most popular language.'
6]:
in Keyword
To check if a string contains a char or another string we use the
‘in’ keyword. The ‘in’ keyword returns True if the string
contains a char or another string, False otherwise.
[6
1 # define two variables
7]:
2 program = 'Python Django App'
3 app = 'App'
[6
1 # check if app is in program
8]:
2 app in program
[6
True
8]:
[6
1 'Dj' in program
9]:
[6
True
9]:
[7
1 'Dj' in app
0]:
[7
False
0]:
[7
1 '2' in 'abc1234'
1]:
[7
True
1]:
Example: Ask for an input from the user. Check if the input
includes the letter 'a' nor not. If it's True print as: 'This input
has a in it' .
[7
1 def is_a_in_it():
2]:
2 # ask for input
3 text = input('Please enter a text: ')
4
5 # check if text has letter a in it
6 if 'a' in text:
7 print('This input has a in: {0}'.format(text))
8 else:
print('This input has NO a in:
9
{0}'.format(text))
Since the text “Deep Learning” has the letter ‘a’ in it, our
function returns True .
Now comes the best part. Actually we can write the
is_a_in_it() function in only one line. Let’s do it:
[7
1 # one line function
6]:
2 def is_a_in_it():
3 # ask the input and return if it has a in it
4 return 'a' in input('Please enter a text: ')
In cell 76, we ask for the user input and check if it has the letter
‘a’ in it, in just one line. This is what we call the functional
programming approach. No temporary variables and code is self-
explanatory.
[7
1 is_a_in_it()
7]:
[7
Please enter a text: Machine Learning
7]:
True
String Comparison
Sometimes we need to check two strings to see if they are
equal or which one is greater (alphabetically comes later). We call
this as String Comparison and it’s important for you to understand
how Python compares the strings.
[7
1 # define a variable
8]:
2 fruit = 'apple'
3
4 # check its value
5 if fruit == 'apple':
6 print('yes it is apple')
7 else:
8 print('no it is not apple')
[7
yes it is apple
8]:
[8
1 # is letter 'o' is less than 'a'
0]:
2 'o' < 'a'
[8
False
0]:
The ASCII code of the capital letters are less than their
lowercase forms. For example the ASCII code for capital letter ‘A’
is 65.
[8
1 ord('A')
3]:
[8
3]: 65
[8
1 # see if 'A' is less than 'a'
4]:
2 ord('A') < ord('a')
[8
4]: True
[8
1 'A' < 'a'
5]:
[8
5]: True
As you see in cells 84 and 85, In Python 'A' is less than 'a'.
OceanofPDF.com
QUIZ - Strings
Now it’s time to solve the quiz for this chapter. You can
download the quiz file, QUIZ_Strings.ipynb, from the Github
Repository of this book. It is in the folder named 11_Strings. Here
are the questions for this chapter:
QUIZ – Strings:
Q1:
Define a function named only_first_letters.
It will take a string as the parameter.
And it will convert all the first letters of each word into
upper case.
All the remaining letter will be lower case.
[
1 1 # Q 1:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 proper_text = only_first_letters('pyThon iS grEAt')
8 print(proper_text)
[
1 Python Is Great
]:
Q2:
Define a function taking a text and a letter as parameters.
The function name will be count_the_letter.
Function will return the number (count) of occurrences of the
letter in the text.
Hints:
[
2 1 # Q 2:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 text = input('Please enter a text: ')
8 letter = input('Now a letter: ')
9 count_the_letter(text, letter)
[
Please enter a text: Python Hands-On is a perfect
2
book :)
]:
Now a letter: e
2
Q3:
Define a function named count_letters.
It will ask for sentence from the user.
And it will count the occurrences of each letter and print it.
Hints:
Expected Output:
Please enter a sentence: pandas
letter p: 1 time(s)
letter a: 2 time(s)
letter n: 1 time(s)
letter d: 1 time(s)
letter s: 1 time(s)
[
3 1 # Q 3:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 count_letters()
[
3 Please enter a sentence: pandas
]:
letter p: 1 time(s)
letter a: 2 time(s)
letter n: 1 time(s)
letter d: 1 time(s)
letter s: 1 time(s)
Q4:
Your text is: 'Monday Tuesday Wednesday Thursday
Friday'.
Do slicing operations on this text and answer the following
questions:
[
4 1 # Q 4:
]:
2
text = 'Monday Tuesday Wednesday Thursday
3
Friday'
4
5
6 # find the first 3 chars
7 # ---- your solution here ----
8
9 # find the chars between index 7 and 10 (inc.)
10 # ---- your solution here ----
11
12 # find the chars between index 2 and 12 (exc.)
13 # ---- your solution here ----
14
15 # find the chars at indices 3, 4 and 5
16 # ---- your solution here ----
[
4 the first 3 chars: Mon
]:
the chars between index 7 and 10 (inc.): Tues
the chars between index 2 and 12 (exc.): nday
Tuesd
the chars at indices 3, 4 and 5: day
Q5:
Your text is: 'Monday Tuesday Wednesday'
Do slicing operations on this text and answer the following
questions:
[
5 1 # Q 5:
]:
2
3 text = 'Monday Tuesday Wednesday'
4
5
6 # find the chars from index 1 to 7 with step size 2
7 # ---- your solution here ----
8
# find the chars from index 3 to 20 with step size
9
3
10 # ---- your solution here ----
11
12 # find the chars with even indices (0, 2, 4, 6 ...)
13 # ---- your solution here ----
14
15 # find the chars with odd indices (1, 3, 5, 7 ...)
16 # ---- your solution here ----
[
5 the chars from index 1 to 7 with step size 2: ody
]:
the chars from index 3 to 20 with step size 3: d
eaWn
the chars with even indices (0, 2, 4, 6 ...): Mna
usa ensa
the chars with odd indices (1, 3, 5, 7 ...):
odyTedyWdedy
Q6:
Your text: 'Monday Tuesday Wednesday'
Do slicing operations on this text and answer the following
questions:
[
7 1 # Q 7:
]:
2
3 text = 'Monday Tuesday Wednesday'
4
5
6 # find the chars between 2nd and 6th from last
7 # ---- your solution here ----
8
# find the chars between 2nd (inc.) and 6th from
9
last
10 # ---- your solution here ----
11
# find the negative index which correspond to the
12
char with index 10
13 # abs(index) + abs(negative_index) = length
14 # ---- your solution here ----
15
16 # find all characters except the last one
17 # ---- your solution here ----
18
# find all characters except the fist and the last
19
one
20 # ---- your solution here ----
[
7 the chars between 2nd and 6th from last: nesd
]:
the chars between 2nd (inc.) and 6th from last:
nesda
negative_index: -14
all characters except the last one: Monday
Tuesday Wednesda
all characters except the fist and the last one:
onday Tuesday Wednesda
Q8:
Define a function named split_email.
It will ask for an email from the user.
And it will split the email into user name and domain name
using index.
Hints:
Expected output:
Please enter an email: [email protected]
User Name: abc
Domain: acme.corp
[
8 1 # Q 8:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 split_email()
[
8 Please enter an email: [email protected]
]:
User Name: abc
Domain: acme.com
Q9:
Define a function named reverse.
It will ask for a sentence from the user and return the reverse
of this sentence.
Expected Output:
Please enter a sentence: yesterday came suddenly
'ylneddus emac yadretsey
[
9 1 # Q 9:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 reverse()
[
9 Please enter a sentence: yesterday came suddenly
]:
'ylneddus emac yadretsey'
Q10:
Define a function named replace_with_next_letter.
It will ask for an input from the user.
And will replace every character with the next letter in the
English alphabet.
Hints:
alphabet_lower = 'abcdefghijklmnoprstuvyz'
alphabet_upper = 'ABCDEFGHIJKLMNOPRSTUVYZ'
use index to get the related letter
Expected Output:
Please enter a text: Python
Rzuipo
Please enter a text: abcZ
bcdA
[1
1 # Q 10:
0]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 new_text = replace_with_next_letter()
8 print(new_text)
9
10 new_text = replace_with_next_letter()
11 print(new_text)
[1
Please enter a sentence: Python
0]:
Rzuipo
Please enter a sentence: abcZ
bcdA
OceanofPDF.com
SOLUTIONS - Strings
Here are the solutions for the quiz for Chapter 11 - Strings.
SOLUTIONS - Strings:
S1:
[
1 1 # S 1:
]:
2
3 # Long Way
4 # def only_first_letters(text):
5
6 # # make all letters -> lower
7 # lower_text = text.lower()
8
9 # # then, only make first letters as capital
10 # capital_text = lower_text.title()
11
12 # return capital_text
13
14 # Short Way
15 def only_first_letters(text):
16 # chain
17 return text.lower().title()
18
19
20 # call the function you defined
proper_text = only_first_letters('pyThon iS
21
grEAt')
22 print(proper_text)
[ Python Is Great
1
]:
S2:
[
2 1 # S 2:
]:
2
3 def count_the_letter(text, letter):
4 return text.count(letter)
5
6
7 # call the function you defined
8 text = input('Please enter a text: ')
9 letter = input('Now a letter: ')
10 count_the_letter(text, letter)
[
Please enter a text: Python Hands-On is a perfect
2
book :)
]:
Now a letter: e
2
S3:
[
3 1 # S 3:
]:
2
3 def count_letters():
4
5 sentence = input('Please enter a sentence: ')
6
7 letters_printed = ""
8
9 for letter in sentence:
10
11 # if it's a space -> continue
12 if letter == ' ':
13 continue
14
15 # this letter is not a space
16 count = count_the_letter(sentence, letter)
17
18 # print
19 # check if already printed
20 if not letter in letters_printed:
print("letter {0}: {1}
21
time(s)".format(letter, count))
22 letters_printed += letter
23
24
25 # call the function you defined
26 count_letters()
[
3 Please enter a sentence: pandas
]:
letter p: 1 time(s)
letter a: 2 time(s)
letter n: 1 time(s)
letter d: 1 time(s)
letter s: 1 time(s)
S4:
[
4 1 # S 4:
]:
2
text = 'Monday Tuesday Wednesday Thursday
3
Friday'
4
5
6 # find the first 3 chars
7 # text[0:3] or text[:3]
8 print('the first 3 chars: ', text[:3])
9
10 # find the chars between index 7 and 10 (inc.)
11 # text[7:11]
print('the chars between index 7 and 10 (inc.): ',
12
text[7:11])
13
14 # find the chars between index 2 and 12 (exc.)
15 # text[2:12]
print('the chars between index 2 and 12 (exc.): ',
16
text[2:12])
17
18 # find the chars at indices 3, 4 and 5
19 # text[3:6]
20 print('the chars at indices 3, 4 and 5: ', text[3:6])
[
4 the first 3 chars: Mon
]:
the chars between index 7 and 10 (inc.): Tues
the chars between index 2 and 12 (exc.): nday
Tuesd
the chars at indices 3, 4 and 5: day
S5:
[
5 1 # S 5:
]:
2
3 text = 'Monday Tuesday Wednesday'
4
5
6 # find the chars from index 1 to 7 with step size 2
7 # text[1:7:2]
print('the chars from index 1 to 7 with step size
8
2: ', text[1:7:2])
9
# find the chars from index 3 to 20 with step size
10
3
11 # text[3:20:3]
print('the chars from index 3 to 20 with step size
12
3: ', text[3:20:3])
13
14 # find the chars with even indices (0, 2, 4, 6 ...)
15 # text[::2]
print('the chars with even indices (0, 2, 4, 6 ...):
16
', text[::2])
17
18 # find the chars with odd indices (1, 3, 5, 7 ...)
19 # text[1::2]
print('the chars with odd indices (1, 3, 5, 7 ...): ',
20
text[1::2])
[
5 the chars from index 1 to 7 with step size 2: ody
]:
the chars from index 3 to 20 with step size 3: d
eaWn
the chars with even indices (0, 2, 4, 6 ...): Mna
usa ensa
the chars with odd indices (1, 3, 5, 7 ...):
odyTedyWdedy
S6:
[
6 1 # S 6:
]:
2
3 text = 'Monday Tuesday Wednesday'
4
5
6 # find the last character
7 # text[-1]
8 print('the last character: ', text[-1])
9
10 # find the 3rd character from last
11 # text[-3]
12 print('the 3rd character from last: ', text[-3])
13
14 # find last 3 characters
15 # -3, -2, -1
16 # text[-3:]
17 print('last 3 characters: ', text[-3:])
18
19 # print the text in reverse order
20 # text[::-1]
21 print('the text in reverse order: ', text[::-1])
[
6 the last character: y
]:
the 3rd character from last: d
last 3 characters: day
the text in reverse order: yadsendeW yadseuT
yadnoM
S7:
[
7 1 # S 7:
]:
2
3 text = 'Monday Tuesday Wednesday'
4
5
6 # find the chars between 2nd and 6th from last
7 # "-6, -5, -4, -3," -2, -1
8 # text[-6:-2]
print('the chars between 2nd and 6th from last: ',
9
text[-6:-2])
10
# find the chars between 2nd (inc.) and 6th from
11
last
12 # "-6, -5, -4, -3, -2," -1
13 # text[-6:-1]
print('the chars between 2nd (inc.) and 6th from
14
last: ', text[-6:-1])
15
# find the negative index which correspond to the
16
char with index 10
17 # abs(index) + abs(negative_index) = length
18 length = len(text)
19 negative_index = length - 10
20 negative_index = -negative_index
21 print('negative_index: ', negative_index)
22
23 # find all characters except the last one
24 # text[:-1]
print('all characters except the last one: ',
25
text[:-1])
26
# find all characters except the fist and the last
27
one
28 # text[1:-1]
print('all characters except the fist and the last
29
one: ', text[1:-1])
[
7 the chars between 2nd and 6th from last: nesd
]:
the chars between 2nd (inc.) and 6th from last:
nesda
negative_index: -14
all characters except the last one: Monday
Tuesday Wednesda
all characters except the fist and the last one:
onday Tuesday Wednesda
S8:
[
8 1 # S 8:
]:
2
3 def split_email():
4 email = input("Please enter an email: ")
5
6 # check @ and find index
7 index = email.find('@')
8
9 # find() -> if finds index,
10 # not find -1
11 if index == -1:
12 print('Invalid email format.')
13 else:
14 print("User Name:", email[:index])
15 print("Domain:", email[index+1:])
16
17
18 # call the function you defined
19 split_email()
[
8 Please enter an email: [email protected]
]:
User Name: abc
Domain: acme.com
S9:
[
9 1 # S 9:
]:
2
3 def reverse():
4 sentence = input('Please enter a sentence: ')
5 # return the sentence in reverse order
6 return sentence[::-1]
7
8
9 # call the function you defined
10 reverse()
[
9 Please enter a sentence: yesterday came suddenly
]:
'ylneddus emac yadretsey'
S10:
[1
1 # S 10:
0]:
2
3 def replace_with_next_letter():
4
5 sentence = input('Please enter a sentence: ')
6
7 alphabet_lower = 'abcdefghijklmnoprstuvyz'
alphabet_upper =
8
'ABCDEFGHIJKLMNOPRSTUVYZ'
9
10 new_sentence = ""
11
12 def is_lower(letter):
13 return letter in alphabet_lower
14
15 def is_upper(letter):
16 return letter in alphabet_upper
17
18 def next_letter(alphabet, letter):
19 index = alphabet.find(letter)
20 if index == len(alphabet) - 1:
21 index = -1
22 return alphabet[index + 1]
23
24 for letter in sentence:
25
26 # lower or upper
27 if is_lower(letter):
new_letter = next_letter(alphabet_lower,
28
letter)
29 elif is_upper(letter):
new_letter = next_letter(alphabet_upper,
30
letter)
31 else:
32 new_letter = letter
33
34 # concat the new_letter into new_sentence
35 new_sentence += new_letter
36
37 return new_sentence
38
39
40 # call the function you defined
41 new_text = replace_with_next_letter()
42 print(new_text)
43
44 new_text = replace_with_next_letter()
45 print(new_text)
[1
Please enter a sentence: Python
0]:
Rzuipo
Please enter a sentence: abcZ
bcdA
OceanofPDF.com
12. Project 2 - Words
This is our second project in this book. It will cover the topics
what we have seen so far. Mainly it’s going to be on functions,
strings and string operations.
In this project we will search for words based on predefined
criteria. For instance, which words does not include any vowels or
the letters of which words are in alphabetical order.
The words are from Lorem Ipsum. Lorem Ipsum is simply
dummy text of the printing and typesetting industry.
You can find the source code of this project and the words.txt
file in the Github repository of the book. It is in the folder named
12_Project_2_Words.
Preparation
We will be reading a text file with Python. To be able to read a
file we use the built-in open() function. The syntax is as follows:
open(file, mode) .
Here the file is a physical file that is on the computer. We will
open the words.txt file in this project. You must have words.txt file
in the same folder where you run the code for the notebook file
(.ipynb file) of this project. They need to be in the same folder for
this project code to work properly.
The second parameter of the open() function is the mode . It
is the reading mode. It tells in which mode you intend to open the
file.
The mode options are as follows:
r: read (default)
a: append
w: write
x: create
When we run cell 1, the open() function will open the file
named ‘words.txt’ and it will return a file object. This file
object contains the actual file contents.
Now that we have the file object with all the content in it, let’s
read it. To start with, we will read it line by line. A single line of
content at each reading operation. This is done via the readline()
function.
[
2 1 # let's read the first line
]:
2 file.readline()
[
2 'Lorem\n'
]:
In cell 2, we get the first line of content in the file object, which
is 'Lorem\n' . Here is ‘\n’ character is the new line character.
Now let’s read the second line in the file:
[
3 1 # let's read the second line
]:
2 file.readline()
[
3 'ipsum\n'
]:
As you see in cell 3, we get the second line in the file. And
again it has the new line character after it as: 'ipsum\n' .
Every time we run the file.readline() function, Python reads
the current line and move to the next line waiting for the next
readline() function call. Like a cursor reading line by line.
Now let’s see the most common special characters which we
encounter when reading file content.
Special Characters:
space
\n : new line character
\r : carriage return (enter)
Now let’s run the readline() function for the third time and
assign the returning value to a variable:
[
4 1 # run readline for 3rd time
]:
2 # assign the result to a variable
3 line = file.readline()
4 line
[
4 'dolor\n'
]:
Instead of reading the file line by line, let’s print the first 6
lines with a loop. And since we need the index value, we will use
the enumerate() function.
[
5 1 # print the first 6 lines
]:
2 file = open('words.txt')
3
4 # we need index -> enumerate
5 for index, line in enumerate(file):
6 if index <= 5:
7 print(line)
[
5 Lorem
]:
ipsum
dolor
sit
amet
consectetur
Exercises
Exercise 1:
We will print the words which have number of letters more
than 10.
[
7 1 # read the file
]:
2 file = open('words.txt')
3
4 for line in file:
5 # split() -> \n
6 word_list = line.split()
7
8 # word is the first element in list
9 word = word_list[0]
10
11 # check number of letters
12 if len(word) > 10:
13 print(word)
[
7 consectetur
]:
pellentesque
sollicitudin
scelerisque
ullamcorper
condimentum
Suspendisse
In cell 7, in the for loop we get each line in the file one by
one: for line in file . Then we split the line and assign it to a
variable as: word_list = line.split() . Since the split() function
returns a list, we need the first element of this list. That’s the
element at index 0 and we get it as: word = word_list[0]. Then
in line 12, we check if the length of the word is greater than 10.
We print the word if it’s True .
Exercise 2:
Define a new function. This function will search for the words
which have no vowels in it. The vowels are a, e, i, o and u .
[
8 1 def check_for_vowels():
]:
2 # read the file
3 file = open('words.txt')
4
5 for line in file:
6 # split() -> \n
7 word_list = line.split()
8
9 # word is the first element in list
10 word = word_list[0]
11
12 # check if word includes any vowel
13 if not 'a' in word and \
14 not 'e' in word and \
15 not 'i' in word and \
16 not 'o' in word and \
17 not 'u' in word:
18 print(word)
Exercise 3:
Get the words which includes the letters 'ae' together and in
this order.
[1
1 def check_for_ae():
2]:
2 # read the file
3 file = open('words.txt')
4
5 for line in file:
6 # split() -> \n
7 word_list = line.split()
8
9 # word is the first element in list
10 word = word_list[0]
11
12 # convert into lower case
13 word = word.lower()
14
15 # check if included vowel
16 if 'ae' in word:
17 print(word)
In cell 12, line 16, inside the check_for_ae function, we
check if ‘ae’ is in the word as follows: if 'ae' in word . If it’s
true then we print the word . Rest is the same as
check_for_vowels() function. Let’s call it and see the words.
[1
1 check_for_ae()
3]:
[1
vitae
3]:
praesent
aenean
maecenas
Exercise 4:
We will define a bool function called
is_any_forbidden_letter . It will take letters which are forbidden
and a text as its parameters. It will check if this text includes any of
these forbidden letters. And return either True or False .
[1
1 def is_any_forbidden_letter(text, forbidden_letters):
4]:
2 # loop over every letter in the text
3 for letter in text:
4 if letter in forbidden_letters:
5 return True
6 else:
7 return False
Now let’s call the function with 2 different sets of sentence and
forbidden letters:
[1
1 sentence = 'This is a forbidden sentence.'
5]:
2 forbidden_letters = 'ae'
3 # call the function with these arguments
4 is_any_forbidden_letter(sentence, forbidden_letters)
[1
True
5]:
[1
1 sentence = 'This is the second forbidden sentence.'
6]:
2 forbidden_letters = 'xy'
3
4 is_any_forbidden_letter(sentence, forbidden_letters)
[1
False
6]:
Exercise 5:
Define a function named only_uses_these_letters . It will
take some letters and a text. It will check if this text uses only these
letters. If the text uses only these letters then it will return True . If
the text uses any other letter which is not one these letters, it will
return False .
[1
1 def only_uses_these_letters(text, letters):
7]:
2 # loop over the chars in text
3 for char in text:
4 # if the char in letters or not
# check if char is an alphabetical letter ->
5
isalpha()
6 if char.isalpha() and not char in letters:
7 return False
8 else:
9 return True
Exercise 6:
Now let’s use the function in Exercise 4, to get the Lorem
words only using 'ens' letters. No other letters.
[2
1 def only_uses_these_letters_lorem(letters):
0]:
2 # read the file
3 file = open('words.txt')
4
5 for line in file:
6 # split() -> \n
7 word_list = line.split()
8
9 # word is the first element in list
10 word = word_list[0]
11
12 # convert into lower case
13 word = word.lower()
14
15 if only_uses_these_letters(word, letters):
16 print(word)
Here we get the Lorem words which only use ‘ens’ letters.
Exercise 7:
Define a function named uses_all . It will take a text and some
letters. If the text uses all of these letter at least once, it will return
True . And it will return False if there is any letter that the text
does not use.
[2
1 def uses_all(text, letters):
2]:
2 # define a variable to keep the result
3 it_uses_all = True
4
5 for letter in letters:
# if letter is not text -> text is not using
6
this letter
7 if not letter in text:
8 it_uses_all = False
9
10 # loop finishes
11 return it_uses_all
Exercise 8:
Use the function in Exercises 6 and for Lorem letters find the
words using all of the letters in 'aei' .
[2
1 def uses_all_lorem(letters):
5]:
2 # read the file
3 file = open('words.txt')
4
5 for line in file:
6 # split() -> \n
7 word_list = line.split()
8
9 # word is the first element in list
10 word = word_list[0]
11
12 # convert into lower case
13 word = word.lower()
14
15 if uses_all(word, letters):
16 print(word)
Exercise 9:
Use the functions defined in Exercise 4 and Exercise 6 to find
the Lorem words which use only “fir” letters and use them all.
The function name will be only_uses_all .
[2
1 def only_uses_all(letters):
7]:
2 # read the file
3 file = open('words.txt')
4
5 for line in file:
6 # split() -> \n
7 word_list = line.split()
8
9 # word is the first element in list
10 word = word_list[0]
11
12 # convert into lower case
13 word = word.lower()
14
15 # call the functions in E4 and E6
if only_uses_these_letters(word, letters)
16
and uses_all(word, letters):
17 print(word)
Exercise 10:
If the letters in a word is in alphabetical order we call it as an
ordered_word .
Define a function named is_ordered_word . It will take a
string parameter as text . And it will return True if it is an ordered
word.
Some examples of ordered words: abc, agor, def.
[2
1 def is_ordered_word(word):
9]:
2 # previous letter is the first letter in word
3 prev = word[0]
4
5 for letter in word:
6 # check if prev is smaller
7 if letter < prev:
8 return False
9
# reassign the previous as the current
10
letter
11 prev = letter
12
13 # return True -> because loop has completed
14 return True
Exercise 11:
Use function in Exercise 9 to find out which Lorem words are
ordered.
[3
1 def is_ordered_word_lorem():
3]:
2 # read the file
3 file = open('words.txt')
4
5 for line in file:
6 # split() -> \n
7 word_list = line.split()
8
9 # word is the first element in list
10 word = word_list[0]
11
12 # convert into lower case
13 word = word.lower()
14
15 # check if this word is an ordered word
16 if is_ordered_word(word):
17 print(word)
OceanofPDF.com
13. Assignment 2 – Words
We finished our second project, Project 2 - Words. Now it’s
your turn to recreate the same project. This chapter is the
assignment on the Project 2 - Words.
In this assignment you will search for words based on
predefined criteria. For instance, which words does not include any
vowels or the letters of which words are in alphabetical order.
The words are from Lorem Ipsum. Lorem Ipsum is simply
dummy text of the printing and typesetting industry.
To complete the assignment, follow the instructions where you
see a #TODO statement. You can also download the assignment
Jupyter file in the Github repository of the book. You can find the
solutions in the previous chapter.
The Assignment
Complete the code with respect to #TODO statements.
Preparation:
To read the words:
open() function
open(file, mode)
mode:
r: read (default)
a: append
w: write
x: create
[
1 1 # open()
]:
2 file = open('words.txt')
[
2 1 # let's read the first line
]:
2 file.readline()
[
2 'Lorem\n'
]:
[
3 1 # let's read the second line
]:
2 file.readline()
[
3 'ipsum\n'
]:
Special Chars:
[
4 1 # run readline for 3rd time
]:
2 # assign the result to a variable
3 line = file.readline()
4 line
[
4 'dolor\n'
]:
[
5 1 # print the first 6 lines
]:
2 file = open('words.txt')
3
4 # we need index -> enumerate
5 for index, line in enumerate(file):
6 if index <= 5:
7 print(line)
[
5 Lorem
]:
ipsum
dolor
sit
amet
consectetur
[
6 1 # remove new lines
]:
2 file = open('words.txt')
3
4 for index, line in enumerate(file):
5 if index <= 5:
# split() -> splits the text from space chars
6
(space, \n, \r)
7 word_list = line.split()
8 print(word_list)
[
6 ['Lorem']
]:
['ipsum']
['dolor']
['sit']
['amet']
['consectetur']
Exercise 1:
We will print the words which have number of letters more
than 10.
[
7 1 # read the file
]:
2 # TODO - Read the words into a file
3
4 for line in file:
5
6 # split() -> \n
7 word_list = # TODO - split the line
8
9 # word is the first element in list
10 word = # TODO - get the first element of list
11
12 # check number of letters
# TODO - check if the length of the letter is
13
greater than 10
[
7 consectetur
]:
pellentesque
sollicitudin
scelerisque
ullamcorper
condimentum
Suspendisse
Exercise 2:
Define a new function. This function will search for the words
which have no vowels in it. The vowels are a, e, i, o and u .
[
8 1 def check_for_vowels():
]:
2
3 # read the file
4 # TODO - Read the words into a file
5
6 for line in file:
7
8 # split() -> \n
9 # TODO - split the line
10
11 # word is the first element in list
12 # TODO - get the first element of list
13
14 # check if included vowel
15 # TODO - check if the word not in vowels
[
9 1 check_for_vowels()
]:
[
9 In
]:
trylyk
Antryl
Exercise 3:
Get the words which includes the letters 'ae' together and in this
order.
[1
1 def check_for_ae():
2]:
2
3 # read the file
4 # TODO - Read the words into a file
5
6 for line in file:
7
8 # split() -> \n
9 # TODO - split the line
10
11 # word is the first element in list
12 # TODO - get the first element of list
13
14 # convert into lower case
15 # TODO - convert the word into lower case
16
17 # check if included vowel
18 # TODO - check if 'ae' is in the word
[1
1 check_for_ae()
3]:
[1
vitae
3]:
praesent
aenean
maecenas
Exercise 4:
We will define a bool function called
is_any_forbidden_letter . It will take letters which are forbidden
and a text as its parameters. It will check if this text includes any of
these forbidden letters. And return either True or False .
[1
1 def is_any_forbidden_letter(text, forbidden_letters):
4]:
2
3 for letter in text:
4 # TODO - check if letter in forbidden letters
5 # TODO - return true
6 else:
7 # TODO - return false
[1
1 sentence = 'This is a forbidden sentence.'
5]:
2 forbidden_letters = 'ae'
3 # call the function with these arguments
4 is_any_forbidden_letter(sentence, forbidden_letters)
[1
True
5]:
[1
1 sentence = 'This is the second forbidden sentence.'
6]:
2 forbidden_letters = 'xy'
3
4 is_any_forbidden_letter(sentence, forbidden_letters)
[1
False
6]:
Exercise 5:
Define a function named only_uses_these_letters . It will
take some letters and a text. It will check if this text uses only these
letters. If the text uses only these letters then it will return True . If
the text uses any other letter which is not one these letters, it will
return False .
[1
1 def only_uses_these_letters(text, letters):
7]:
2
3 for char in text:
4 # if the char in letters or not
# check if char is an alphabetical letter ->
5
isalpha()
# TODO - check if char is alphabetical letter
6
and it is not in letters
7 # TODO - return false
8 else:
9 # TODO - return true
[1
1 sentence = 'check'
8]:
2 letters = 'chek'
3
4 only_uses_these_letters(sentence, letters)
[1
True
8]:
[1
1 sentence = 'check this'
9]:
2 letters = 'chek'
3
4 only_uses_these_letters(sentence, letters)
[1
False
9]:
Exercise 6:
Now let’s use the function in Exercise 4, to get the Lorem
words only using 'ens' letters. No other letters.
[2
1 def only_uses_these_letters_lorem(letters):
0]:
2
3 # read the file
4 # TODO - Read the words into a file
5
6 for line in file:
7
8 # split() -> \n
9 # TODO - split the line
10
11 # word is the first element in list
12 # TODO - get the first element of list
13
14 # convert into lower case
15 # TODO - convert the word into lower case
16
# TODO - check if the word only uses these
17
letters
[2
1 letters = 'ens'
1]:
2 only_uses_these_letters_lorem(letters)
[2
sesse
1]:
nesnes
senesses
Exercise 7:
Define a function named uses_all . It will take a text and some
letters. If the text uses all of these letter at least once, it will return
True . And it will return False if there is any letter that the text
does not use.
[2
1 def uses_all(text, letters):
2]:
2
3 it_uses_all = True
4
5 for letter in ...... :
# if letter is not text -> text is not using
6
this letter
7 # TODO - check if letter is not in text
8 # TODO - assign false to it_uses_all
9
10 # loop finishes
11 # TODO - return it_uses_all
[2
1 letters = 'abd'
3]:
2 text = 'This is a bold sentence.'
3
4 uses_all(text, letters)
[2
True
3]:
[2
1 letters = 'abdf'
4]:
2 text = 'This is a bold sentence.'
3
4 uses_all(text, letters)
[2
False
4]:
Exercise 8:
Use the function in Exercises 6 and for Lorem letters find the
words using all of the letters in 'aei' .
[2
1 def uses_all_lorem(letters):
5]:
2
3 # read the file
4 # TODO - Read the words into a file
5
6 for line in file:
7
8 # split() -> \n
9 # TODO - split the line
10
11 # word is the first element in list
12 # TODO - get the first element of list
13
14 # convert into lower case
15 # TODO - convert the word into lower case
16
# TODO - check if the word uses all the
17
letters
[2
1 letters = 'aei'
6]:
2 uses_all_lorem(letters)
[2
feugiat
6]:
penatibus
parturient
venenatis
vitae
aliquet
sapien
viverra
vehicula
habitasse
Exercise 9:
Use the functions defined in Exercise 4 and Exercise 6 to find
the Lorem words which use only “fir” letters and use them all.
The function name will be only_uses_all .
[2
1 def only_uses_all(letters):
7]:
2
3 # read the file
4 # TODO - Read the words into a file
5
6 for line in file:
7
8 # split() -> \n
9 # TODO - split the line
10
11 # word is the first element in list
12 # TODO - get the first element of list
13
14 # convert into lower case
15 # TODO - convert the word into lower case
16
# TODO - check if the word uses only these
17
letters and uses them all
[2
1 letters = 'fir'
8]:
2
3 only_uses_all(letters)
[2
frifirri
8]:
Exercise 10:
If the letters in a word is in alphabetical order we call it as an
ordered_word .
Define a function named is_ordered_word . It will take a
string parameter as text. And it will return True if it is an ordered
word.
Some examples of ordered words: abc, agor, def.
[2
1 def is_ordered_word(word):
9]:
2
3 # previous letter is the first letter in word
prev = # TODO - assign the first letter in
4
word to prev
5
6 for letter in word:
7
8 # check if prev is smaller
# TODO - check if the letter is smaller
9
than prev
10 # TODO - return false
11
# reassign the previous as the current
12
letter
prev = # TODO - assign the current letter
13
to prev
14
15 # return True -> because loop has completed
16 return True
[3
1 word = 'agor'
0]:
2
3 is_ordered_word(word)
[3
True
0]:
[3
1 word = 'python'
1]:
2
3 is_ordered_word(word)
[3
False
1]:
[3
1 word = 'hnty'
2]:
2
3 is_ordered_word(word)
[3
True
2]:
Exercise 11:
Use function in Exercise 9 to find out which Lorem words are
ordered.
[3
1 def is_ordered_word_lorem():
3]:
2
3 # read the file
4 # TODO - Read the words into a file
5
6 for line in file:
7
8 # split() -> \n
9 # TODO - split the line
10
11 # word is the first element in list
12 # TODO - get the first element of list
13
14 # convert into lower case
15 # TODO - convert the word into lower case
16
17 # check if this word is an ordered word
# TODO - check if the word is an ordered
18
word
[3
1 is_ordered_word_lorem()
4]:
[3
in
4]:
at
in
eu
et
dis
ac
ex
a
est
[3
1 word = 'Nam'
5]:
2
3 is_ordered_word(word)
[3
True
5]:
[3
1 # if 'N' is less than 'a'
6]:
2 'N' < 'a'
[3
True
6]:
OceanofPDF.com
14. List
What is a List?
A list is a sequence used to store multiple items in a single
variable. List is one of four built-in data types in Python used to
store collections of data. The other three are Dictionary, Tuple
and Set . We will see each of them in the succeeding chapters. In
most of other programming languages there are Arrays, while in
Python we have Lists .
Lists are created using a pair of square brackets [] . And the
items in a list are separated by commas. Since Lists are of sequence
types, you can access their items with indices.
Let’s define a list with 4 items in it:
[
1 1 my_list = [10, 20, 30, 40]
]:
2 my_list
[
1 [10, 20, 30, 40]
]:
The list in cell 2 is called letters and has 5 strings in it. This is
a list of only strings. Now let’s print its type to see if it is really a
list:
[
3 1 type(letters)
]:
[
3 list
]:
The list in cell 5 have items which are also lists. For example
the item [1, 2, 3] is a list. Now let’s get some items of the
nested_list :
[
6 1 # item at index 0
]:
2 nested_list[0]
[
6 'city'
]:
[
7 1 # item at index 3
]:
2 nested_list[3]
[
7 True
]:
[
8 1 # item at index 4
]:
2 nested_list[4]
[ [1, 2, 3]
8
]:
[
9 1 # item at index 6
]:
2 nested_list[6]
[
9 ['a', 'b']
]:
In cell 10, we first get the item at index 4 in the list as:
nested_list[4] . This gives us the nested list of [1, 2, 3] . Now we
can get its items with index. And to get the item at index 2 we use:
nested_list[4][2] . Which returns 3 .
Let’s do another example to get the items of the nested list.
This time let’s first see the nested list.
[1
1 # item at index 6 is a nested list
1]:
2 nested_list[6]
[1
['a', 'b']
1]:
[1
1 # get the item 0 at of the nested list
2]:
2 nested_list[6][0]
[1
'a'
2]:
As you see in cell 15, we mutate the cars list. We change the
item at index 0 as ‘Audi’ .
Let’s define another list and change one of its items:
[1
1 nums = [1, 2, 3, 4, 5]
6]:
2 nums[4]
[1
5
6]:
[1
1 nums[4] = '55555'
7]:
2 nums
[1
[1, 2, 3, 4, '55555']
7]:
If you try to access an item with an index which does not exist
in the list, you will get IndexError . Let’s see an example:
[1
1 # Access an index which does not exist
8]:
2 # IndexError: list index out of range
3 nums[6]
[1
IndexError: list index out of range
8]:
For our nums list, the maximum index is 4 . And as you see in
cell 18, we got an IndexError when we try to get item at index
6 , which does not exist.
As a final example, let’s get items in the cars list with
negative indices. First let’s remember the positive and negative
indices on the cars list:
cars ‘Audi' ‘Ford' ‘Mazda'
index -----> 0 1 2
negative index <-
-3 -2 -1
----
[1
1 cars[-1]
9]:
[1
'Mazda'
9]:
[2
1 cars[-2]
0]:
[2
'Ford'
0]:
[2
1 cars[-3]
1]:
[2
'Audi'
1]:
Let’s say we need to print the index for each car. Remember
that we use enumerate() function to get the index in the for
loop.
[2
1 # we need the index -> enumerate()
3]:
2 for index, car in enumerate(cars):
3 print("{0} - {1}".format(index, car))
[2
0 - Audi
3]:
1 - Ford
2 - Mazda
Here comes the question: What happens to the nums list when
we change its items in the for loop? To see the answer let’s print
the nums list one more time:
[2
1 nums
6]:
[2
[1, 2, 3, 4, 5]
6]:
As you see in cell 26, it hasn’t changed. The reason is, we only
changed the copies of the items in the loop. Not the actual items in
the list. So the list stayed untouched. To be able to mutate the list in
the loop we have call its items with their index numbers.
[2
1 # we want to change the original list in loop
7]:
2 # we need index to update the element -> list
3 for index, num in enumerate(nums):
4 # calculate the new num
5 new_num = num * 10
6
7 # update the list via index
8 nums[index] = new_num
In cell 27, line 8, we update the item in the list with the index
as: nums[index] = new_num . Now the list is mutated. Let’s see
its final value by printing it again:
[2
1 nums
8]:
[2
[10, 20, 30, 40, 50]
8]:
As you see in cell 28, the items in the list are changed. And the
list has been mutated.
List Operations
In Python, we are allowed to do addition ( + ) and
multiplication ( * ) operations on Lists. They don’t work like
normal mathematical operations. Lists have their own logic for
them. Let’s see with examples:
+ Operator: The addition (+) operator concatenates the lists
one after another. The same as we did with Strings.
[3
1 # define two lists
1]:
2 a = [10, 20, 30]
3 b = [4, 8, 12]
4
5 # now add the lists
6 c = a + b
7 print(c)
[3
[10, 20, 30, 4, 8, 12]
1]:
In cell 31, we add two lists and the result is a new list with the
items of both being concatenated.
[3
1 # define three lists
2]:
2 a = [10, 20, 30]
3 b = [4, 8, 12]
4 c = ['A', 'B', 'C']
5
6 # add them
7 d = a + b + c
8 d
[3
[10, 20, 30, 4, 8, 12, 'A', 'B', 'C']
2]:
As you see in cell 34, we multiply the cars list by 2 and the
result is 2 times of the items in the list, repeated respectively.
[3
1 [5] * 4
5]:
[3
[5, 5, 5, 5]
5]:
[3
1 [11, 22] * 3
6]:
[3
[11, 22, 11, 22, 11, 22]
6]:
List Slicing
We covered slicing in very detail in Chapter 11 - Strings. The
good news is, List slicing works in the same way as Strings. Let’s
see it with examples:
[3
1 my_list = ['a', 'b', 'c', 'd', 'e', 'f']
7]:
2 my_list
[3
['a', 'b', 'c', 'd', 'e', 'f']
7]:
[3
1 # we want elements from index 1 to 4 (exc)
8]:
2 my_list[1:4]
[3
['b', 'c', 'd']
8]:
In cell 39, we get the first 4 items. These are the items at
indices of 0, 1, 2, and 3. And the slice is my_list[0:4] . The
starting index is 0 and we already know that 0 is the default value
for start. So we can omit it as follows:
[4
1 # omit 0 for start
0]:
2 my_list[:4]
[4
['a', 'b', 'c', 'd']
0]:
We get the same result in cells 39 and 40.
Let’s say we want to get all the items in the list. Remember we
omit all 3 parameters; start, end, step. We leave them as default
values as: [::] . And this slice gives us a copy of the complete list.
Here it is:
[4
1 # get all items of list
1]:
2 my_list[::]
[4
['a', 'b', 'c', 'd', 'e', 'f']
1]:
Now let’s say, we need the items whose index numbers are
even numbers. The first index is 0 and it’s an even number. If we
keep a step size of 2, up to the end, we will get all the items with
even indices:
[4
1 # get the items with even indices
3]:
2 # 0, 2, 4, ...
3 my_list[::2]
[4
['a', 'c', 'e']
3]:
Now let’s say we need the items that have odd index numbers.
This time we have to start from 1, and again the step size is 2. We
will go up to the end, so we can leave the end parameter as default:
[4
1 # get the elements with odd index
4]:
2 # 1, 3, 5 ...
3 my_list[1::2]
[4
['b', 'd', 'f']
4]:
In cell 47, we copy the my_list with complete slicing. And the
name of the new list is my_list_copy . Now let’s modify the new
list by changing its items at indices 0 and 1:
[4
1 # mutate the new list -> if it affects the original list
8]:
2 my_list_copy[0] = 'A'
3 my_list_copy[1] = 'B'
[5
1 letters = ['a', 'b', 'c', 'd', 'e']
3]:
2 letters
[5
['a', 'b', 'c', 'd', 'e']
3]:
[5
1 letters.append('f')
4]:
2 letters
[5
['a', 'b', 'c', 'd', 'e', 'f']
4]:
In cell 59, we extend the letters list with list t . And the final
value of the letters list the concatenation of elements from both
lists respectively.
sort(): Sorts the list in-place, which means the list gets
mutated when we call the sort() method on it. So be careful when
you use it.
[6
1 # define a list with unsorted elements
1]:
2 t = [4, 2, 3, 1, 5, 6, 9, 8, 7]
3 # sorts the list in-place
4 t.sort()
5 t
[6
[1, 2, 3, 4, 5, 6, 7, 8, 9]
1]:
In cell 62, we sort the list a and as you see it has been mutated.
reverse : We can declare the sort direction via the reverse
parameter. It has two options: True or False .
[6
1 # reverse = False
3]:
2 a = ['c', 'b', 'a', 'd']
3 a.sort(reverse=False)
4 a
[6
['a', 'b', 'c', 'd']
3]:
As you see in cell 67, the value of the list a has not changed.
Let’s do another example. This time let’s use the reverse
parameter.
[6
1 # sort in reverse order
8]:
2 d = sorted(a, reverse=True)
3 d
[6
['d', 'c', 'b', 'a']
8]:
1- pop()
If we know the index to delete, we use the pop() method. We
just pass the index of the item we want to delete as the argument.
The pop() method removes the element from the list and returns
that element.
[6
1 a_list = ['x', 'y', 'z', 't']
9]:
2 a_list
[6
['x', 'y', 'z', 't']
9]:
[7
1 # delete element at index 2
0]:
2 deleted_element = a_list.pop(2)
3 deleted_element
[7
'z'
0]:
2- del
If we do not need the deleted element than we can use the del
keyword. The del statement removes the element from the list and
returns None . Its syntax is: del my_list[index] .
[7
1 my_list = ['k', 'l', 'm', 'n', 'o']
4]:
2 my_list
[7
['k', 'l', 'm', 'n', 'o']
4]:
[7
1 # delete element at index 3
5]:
2 del my_list[3]
In cell 75, we delete the element at index 3. Now let’s see the
final value of my_list . You will see, the letter ‘n’ is not in the list
anymore, because it was the item at index 3.
[7
1 # my_list after deletion
6]:
2 my_list
[7
['k', 'l', 'm', 'o']
6]:
[7
1 # redefine the list
7]:
2 my_list = ['k', 'l', 'm', 'n', 'o', 'p', 'r']
3 my_list
[7
['k', 'l', 'm', 'n', 'o', 'p', 'r']
7]:
[7
1 # delete indices from 1-5
8]:
2 del my_list[1:5]
[7
1 # my_list after deletion
9]:
2 my_list
[7
['k', 'p', 'r']
9]:
3- remove()
If we know the element itself, which we want to delete we can
use the remove() method. Its syntax is: r emove(element) . Here
the parameter is not an index it’s the element itself. The
remove(element) method call will remove the element from the
list.
[8
1 # redefine my_list
0]:
2 my_list = ['k', 'l', 'm', 'n', 'o', 'p', 'r']
3 my_list
[8
['k', 'l', 'm', 'n', 'o', 'p', 'r']
0]:
[8
1 # remove element 'm' from my_list
1]:
2 my_list.remove('m')
[8
1 # my_list after remove
2]:
2 my_list
[8
['k', 'l', 'n', 'o', 'p', 'r']
2]:
In cell 85, we call the remove() method with the element ‘x’ .
Since my_list has no element as ‘x’ we got ValueError:
list.remove(x): x not in list.
[8
1 type(day_letters)
8]:
[8
list
8]:
In cell 92, we split the mailbox string at the ‘-’ chars as:
mailbox.split('-') . And it returns the list of words.
Now let’s do the conversion in the opposite direction. We will
convert a list into a string this time. One of the simplest way to do
this is using the built-in join() method. As its name implies, it
joins the elements of the list and creates a string. It’s just the
opposite of the split() method.
[9
1 words = ['monday', 'is', 'the', 'first', 'day']
3]:
2 words
[9
['monday', 'is', 'the', 'first', 'day']
3]:
[9
1 # join with '-' char between items
4]:
2 sentence = '-'.join(words)
3 sentence
[9
'monday-is-the-first-day'
4]:
In cell 94, we join the elements of the words list into a string.
And we name this string as sentence . The syntax is as: '-
'.join(words) . Here, ‘-’ is the character with which we want to
join. Python will put this character in-between each item pairs in
the list. And the argument to the join() method is the list.
Let’s join the list elements again. But this time we will place
the whitespace character in-between the items:
[9
1 # join with whitespace char between items
5]:
2 sentence = ' '.join(words)
3 sentence
[9
'monday is the first day'
5]:
In cell 102, we define two string variables both have the same
value as ‘apple’ . Now let’s see their ids in the memory.
[10
1 # the id of variable a
3]:
2 id(a)
[10
2092223467696
3]:
[10
1 # the id of variable b
4]:
2 id(b)
[10
2092223467696
4]:
You might get surprised that you see they have the same id
numbers. How is that possible? Didn’t we define two separate
variables as a and b ? Yes we did actually.
Let’s ask Python whether they have the same values or not.
Remember, we use the equality check ( == ) for value comparison.
[10
1 # equality check
5]:
2 a == b
[10
True
5]:
[10
1 # identity check
6]:
2 id(a) == id(b)
[10
True
6]:
Interesting, right? Python tells us that they are the same object.
In other words, a and b refers to the same address in the memory,
instead being 2 different objects. To understand the reason we first
need to know Primitive and Non-Primitive Types in Python.
In Python Types are:
Primitive Types : int, float, string, bool
Non-Primitive Types : list, dict, tuple, set
These two variables, a and b , have the same values and they
are strings ( str ) which is a Primitive Type. Since their values are
the same, and they are Primitives, Python does not create two
separate variables, instead, it combines both to the same address, to
save memory. That’s why you see the same id numbers for both of
them.
Now let’s reassign another value to the variable b , and see
what happens:
[10
1 # reassign another value to variable b
7]:
2 b = 'orange'
3 b
[10
'orange'
7]:
As you see in cells 108 and 109, the id of variable a is still the
same number which is 2092223467696 . But the id of b has
changed. Now it’s 2092223339760 . Which means that, from now
on, a and b are not the same object. Their memory addresses are
not the same anymore. Since we change the value of b , Python
creates a new address (id) for it.
Let’s do the equality and identity checks one more time for a
and b :
[11
1 # equality check
0]:
2 a == b
[11
False
0]:
[11
1 # identity check
1]:
2 id(a) == id(b)
[11
False
1]:
We can see that their values are equal in cell 113. Now let’s
print their id numbers:
[11
1 # id of x
4]:
2 id(x)
[11
2092222595456
4]:
[11
1 # id of y
5]:
2 id(y)
[11
2092222212480
5]:
is Statement
We know that id(x) == id(y) is the object comparison. It
checks whether x and y are identical or not. In Python we have a
more elegant way for identity check: is statement. It’s syntax is: x
is y . And it returns True if x and y are the same object
(identical), False otherwise.
[11
1 # define Two lists
7]:
2 a = [1, 2, 3, 4, 5]
3 b = [1, 2, 3, 4, 5]
[11
1 # value comparison
8]:
2 a == b
[11
True
8]:
In cell 118, we check the values of two lists, and since their
values are the same a == b returns True.
[11
1 # object comparison
9]:
2 id(a) == id(b)
[11
False
9]:
In cell 119, we check if the two lists, a and b , are the same
object via id(a) == id(b) expression. And it returns False , since
Lists are Non-Primitive. Python creates distinct objects for Non-
Primitive types even if they have the same values.
[12
1 # is statement
0]:
2 # if they are identical
3 a is b
[12
False
0]:
In cell 120, we do the object comparison with the is statement.
We ask Python if ‘a is b’ and it returns False , because they are
two distinct objects.
Let’s do another example with the int type now. Since int is a
Primitive type, we know that the two variables will be identical if
they instantiate with the same value.
[12
1 # define 2 integers
1]:
2 # int is Primitive
3 a = 2
4 b = 2
[12
1 # value comparison
2]:
2 a == b
[12
True
2]:
[12
1 # object comparison
3]:
2 a is b
[12
True
3]:
In cells 122 and 123, two int variables, a and b , have the
same values and they are identical. In other words they are the
same variable (object) because ‘a is b’ returns True .
Now let’s reassign one of them with a different value. We know
that, as soon as their values differ, Python separates them into two
distinct variables. Let’s see:
[12
1 # reassign b
4]:
2 b = 888
[12
1 # object comparison
5]:
2 a is b
[12
False
5]:
Aliasing
Aliasing means different names referring to the same object.
Let’s assume we have two variables x and y with the same id
numbers. Since their ids are the same, they are identical. In other
words they refer to the same object in the memory. We call x and
y as aliases.
As you see in the output of cell 140, a and b have exactly the
same values. But we only mutate b , we didn’t touch the variable
a . How is this possible? The answer is that, they are still the same
object. Let’s prove it by using the is statement:
[14
1 # object comparison
1]:
2 a is b
[14
True
1]:
As you see in cell 141, they are identical. Which means they
are still the same object. In other words, they are just the aliases of
the same object. So we conclude that; for Non-Primitive Types,
mutation does not break aliasing. They remain referring to the
same memory address even if we mutate one of them.
OceanofPDF.com
QUIZ - List
Now it’s time to solve the quiz for this chapter. You can
download the quiz file, QUIZ_List.ipynb, from the Github
Repository of this book. It is in the folder named 14_List. Here are
the questions for this chapter:
QUIZ - List:
Q1:
Define a function named sum_of_list.
The parameter will be a list.
The function will add all the elements in the list and return
the summation result.
Expected Output:
nums = [1, 2, 3, 4, 5, 'a']
sum_of_list(nums) -> 15
[
1 1 # Q 1:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 nums = [1, 2, 3, 4, 5, 'a']
8 summation = sum_of_list(nums)
9 print(summation)
[
1 15
]:
Q2:
Define a function named two_levels_sum.
The parameter will be a list of two levels (nested list).
The function will add all the elements including the nested
lists and return the summation result.
Hints:
isdigit()
type()
Expected Output:
a_list = [[5, 8], [1, 4, 7], [10], 3, 'a']
summation = two_levels_sum(a_list) -> 38
[
2 1 # Q 2:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 a_list = [[5, 8], [1, 4, 7], [10], 3, 'a']
8 summation = two_levels_sum(a_list)
9 print(summation)
[
2 38
]:
Q3:
The function you defined in Q2 can only add lists of two
levels.
Now we will convert this into a generic function that can take
any levels of nested lists.
Function name will be nested_sum.
It will add all the elements of all lists including nested lists
and return the overall summation result.
Hints:
Recursion
isdigit()
type()
Expected Output:
a_list = [[-5, [9, 1, [6, 2]], 8], [1, 4, 7, [8, 2]], [10, [-1, 2]],
3]
summation = nested_sum(a_list) -> 57
[
3 1 # Q 3:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
a_list = [[-5, [9, 1, [6, 2]], 8], [1, 4, 7, [8, 2]], [10,
7
[-1, 2]], 3]
8 summation = nested_sum(a_list)
9 print(summation)
[
3 57
]:
Q4:
Define a function named squares.
It will calculate the square of each item in the list and
append it to a new list.
It will return the new list of squares.
Expected Output:
numbers = [1,2,3,4,5]
squares(numbers) -> [1, 4, 9, 16, 25]
[
4 1 # Q 4:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 numbers = [1,2,3,4,5]
8 result = squares(numbers)
9 print(result)
[
4 [1, 4, 9, 16, 25]
]:
Q5:
Define a function named sum_of_squares.
It will calculate the squares of items first and will add them
to create items of a new list.
Each item in the new list will be the sum of items from the
old list starting from index 0 up to the item's index.
For example in the new list the item in index 4 is going to be
the sum of squares of items at indices 0-1-2-3-4 of parameter
list.
And it will return the new list.
Hints:
Function Call:
a_list = [1,2,3,4,5]
result = sum_of_squares(a_list)
print(result)
Expected Output:
[1, 5, 15, 37, 83]
[
5 1 # Q 5:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 a_list = [1,2,3,4,5]
8 result = sum_of_squares(a_list)
9 print(result)
[
5 [1, 5, 15, 37, 83]
]:
Q6:
Define a function that return the difference between items at
odd indices (1,3,5...) and items at even indices (0,2,4...).
The function name will be odd_even_difference.
Hints:
Function Call:
a_list = [1,2,3,4,5,6]
diff = odd_even_difference(a_list)
print(diff)
Expected Output:
3
[
6 1 # Q 6:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 a_list = [1,2,3,4,5,6]
8 diff = odd_even_difference(a_list)
9 print(diff)
[
6 3
]:
Q7:
Define a function that crop the list parameter.
The function will be crop_the_list.
Cropping means remove the first and the last elements from
the list.
The function will mutate the original list parameter namely it
will modify the list in-place.
It will not return any value.
Test the function and prove it mutates the list which you pass
as argument.
Function Call:
nums = [1,2,3,4,5,6,7]
print('the list before passing as parameter:', nums)
crop_the_list(nums)
print('the list before passing as parameter:', nums)
Expected Output:
the list before passing as parameter: [1, 2, 3, 4, 5, 6, 7]
the list before passing as parameter: [2, 3, 4, 5, 6]
[
7 1 # Q 7:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 nums = [1,2,3,4,5,6,7]
print('the list before passing as parameter:',
8
nums)
9 crop_the_list(nums)
print('the list before passing as parameter:',
10
nums)
[
the list before passing as parameter: [1, 2, 3, 4, 5,
7
6, 7]
]:
the list before passing as parameter: [2, 3, 4, 5, 6]
Q8:
Define a function named sort_the_list.
It will take a list and sort type (is_reverse) as parameters.
is_reverse will be boolean and it will decide if the sort will
be ascending or descending. It's default value will be False.
sorted()
no loops
do not mutate the original list
Function Call:
a = [12, 4, 2, 1, 6, 3, 45]
print('a:', a)
sorted_a = sort_the_list(a, False)
print('sorted - ascending', sorted_a)
sorted_a = sort_the_list(a, True)
print('sorted - descending', sorted_a)
print('a after sort_the_list function call:', a)
Expected Output:
a: [12, 4, 2, 1, 6, 3, 45]
sorted - ascending [1, 2, 3, 4, 6, 12, 45]
sorted - descending [45, 12, 6, 4, 3, 2, 1]
a after sort_the_list function call: [12, 4, 2, 1, 6, 3, 45]
[
8 1 # Q 8:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 a = [12, 4, 2, 1, 6, 3, 45]
8 print('a:', a)
9
10 sorted_a = sort_the_list(a, False)
11 print('sorted - ascending', sorted_a)
12
13 sorted_a = sort_the_list(a, True)
14 print('sorted - descending', sorted_a)
15
16 print('a after sort_the_list function call:', a)
[
8 a: [12, 4, 2, 1, 6, 3, 45]
]:
sorted - ascending [1, 2, 3, 4, 6, 12, 45]
sorted - descending [45, 12, 6, 4, 3, 2, 1]
a after sort_the_list function call: [12, 4, 2, 1, 6,
3, 45]
Q9:
The Consecutive Numbers in Math are actually Lists in
Python.
Gauss' Method for Summing Consecutive Numbers is a
method that gives you the summation of numbers.
And that's how Gauss' Method works:
start -> 1
end -> 100
step_size -> 2
Hints:
Function Call:
conseq_sum = gauss(2, 100, 3)
conseq_sum
Expected Output:
1650.0
[
9 1 # Q 9:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 conseq_sum = gauss(2, 100, 3)
8 print(conseq_sum)
[
9 1650.0
]:
Q10:
Define a function to check if a given list is sorted in
descending order or not.
The function name will be is_it_sorted_descending.
It will return True if the list items are in descending order,
False otherwise.
Function Calls:
a_list = [2, 3, 4, 7]
print(is_it_sorted_descending(a_list))
a_list = [7, 7, 4, 3, 2, 1, 1]
print(is_it_sorted_descending(a_list))
a_list = ['a', 'b', 'c', 'd']
print(is_it_sorted_descending(a_list))
a_list = ['d', 'c', 'b', 'a']
print(is_it_sorted_descending(a_list))
Expected Output:
False
True
False
True
[1
1 # Q 10:
0]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 a_list = [2, 3, 4, 7]
8 print(is_it_sorted_descending(a_list))
9
10 a_list = [7, 7, 4, 3, 2, 1, 1]
11 print(is_it_sorted_descending(a_list))
12
13 a_list = ['a', 'b', 'c', 'd']
14 print(is_it_sorted_descending(a_list))
15
16 a_list = ['d', 'c', 'b', 'a']
17 print(is_it_sorted_descending(a_list))
[1
False
0]:
True
False
True
OceanofPDF.com
SOLUTIONS - List
Here are the solutions for the quiz for Chapter 14 - List.
SOLUTIONS - List:
S1:
[
1 1 # S 1:
]:
2
3 def sum_of_list(a_list):
4 # variable to keep sum
5 summation = 0
6
7 for i in a_list:
8 # control for being integer
9 if str(i).isdigit():
10 summation += i
11
12 return summation
13
14
15 # call the function you defined
16 nums = [1, 2, 3, 4, 5, 'a']
17 summation = sum_of_list(nums)
18 print(summation)
[
1 15
]:
S2:
[ 1 # S 2:
2
]:
2
3 def two_levels_sum(p_list):
4 summation = 0
5
6 for item in p_list:
7 # type check -> list
8 if type(item) == list:
9 # item is a list -> loop over its elements
10 for inner_item in item:
11 if str(inner_item).isdigit():
12 summation += inner_item
13 # type check -> int
14 elif str(item).isdigit():
15 summation += item
16
17 return summation
18
19
20 # call the function you defined
21 a_list = [[5, 8], [1, 4, 7], [10], 3, 'a']
22 summation = two_levels_sum(a_list)
23 print(summation)
[
2 38
]:
S3:
[
3 1 # S 3:
]:
2
3 def nested_sum(p_list):
4 summation = 0
5
6 for item in p_list:
7 if type(item) == int:
8 summation += item
9 elif type(item) == list:
# if the item is list -> we will loop over
10
its elements
11 # recursion
12 summation += nested_sum(item)
13
14 return summation
15
16
17 # call the function you defined
a_list = [[-5, [9, 1, [6, 2]], 8], [1, 4, 7, [8, 2]],
18
[10, [-1, 2]], 3]
19 summation = nested_sum(a_list)
20 print(summation)
[
3 57
]:
S4:
[
4 1 # S 4:
]:
2
3 def squares(p_list):
4 # new_list = []
5 new_list = list()
6
7 for i, element in enumerate(p_list):
8 new_list.append(element**2)
9
10 return new_list
11
12
13 # call the function you defined
14 numbers = [1,2,3,4,5]
15 result = squares(numbers)
16 print(result)
[
4 [1, 4, 9, 16, 25]
]:
S5:
[
5 1 # S 5:
]:
2
3 def sum_of_squares(p_list):
4 new_list = []
5
6 for element in p_list:
7 # get total of new list
8 new_list_total = sum_of_list(new_list)
9
# get square of the current element and add
10
it new_total
11 new_total = new_list_total + element**2
12
13 # append new total to new_list
14 new_list.append(new_total)
15
16 return new_list
17
18
19 # call the function you defined
20 a_list = [1,2,3,4,5]
21 result = sum_of_squares(a_list)
22 print(result)
[
5 [1, 5, 15, 37, 83]
]:
S6:
[
6 1 # S 6:
]:
2
3 def odd_even_difference(p_list):
4 # odd elements
5 odds = p_list[1::2]
6
7 # odd summation
8 odds_sum = sum_of_list(odds)
9
10 # even elements
11 evens = p_list[::2]
12
13 # event summation
14 evens_sum = sum_of_list(evens)
15
16 # get difference
17 diff = odds_sum - evens_sum
18
19 return diff
20
21
22 # call the function you defined
23 a_list = [1,2,3,4,5,6]
24 diff = odd_even_difference(a_list)
25 print(diff)
[
6 3
]:
S7:
[
7 1 # S 7:
]:
2
3 def crop_the_list(p_list):
4 # remove the first element
5 p_list.pop(0)
6
7 # remove the final element
8 p_list.pop()
9
10
11 # call the function you defined
12 nums = [1,2,3,4,5,6,7]
print('the list before passing as parameter:',
13
nums)
14 crop_the_list(nums)
print('the list before passing as parameter:',
15
nums)
[
the list before passing as parameter: [1, 2, 3, 4,
7
5, 6, 7]
]:
the list before passing as parameter: [2, 3, 4, 5,
6]
S8:
[
8 1 # S 8:
]:
2
3 def sort_the_list(p_list, is_reverse = False):
4 sorted_list = []
5
6 # if it is reverse
7 if is_reverse:
sorted_list = sorted(p_list,
8
reverse=is_reverse)
9 else:
10 sorted_list = sorted(p_list)
11
12 return sorted_list
13
14
15 # call the function you defined
16 a = [12, 4, 2, 1, 6, 3, 45]
17 print('a:', a)
18
19 sorted_a = sort_the_list(a, False)
20 print('sorted - ascending', sorted_a)
21
22 sorted_a = sort_the_list(a, True)
23 print('sorted - descending', sorted_a)
24
25 print('a after sort_the_list function call:', a)
[
8 a: [12, 4, 2, 1, 6, 3, 45]
]:
sorted - ascending [1, 2, 3, 4, 6, 12, 45]
sorted - descending [45, 12, 6, 4, 3, 2, 1]
a after sort_the_list function call: [12, 4, 2, 1, 6,
3, 45]
S9:
[
9 1 # S 9:
]:
2
3 def gauss(start=1, end=100, step=2):
4
5 # create a list with range()
6 a_list = list(range(start, end, step))
7
8 # reverse list
9 r_list = a_list[::-1]
10
11 # summation
12 summation = 0
13
14 for i in range(len(a_list)):
15 pair_sum = a_list[i] + r_list[i]
16 summation += pair_sum
17
18 result = summation / 2
19
20 return result
21
22
23 # call the function you defined
24 conseq_sum = gauss(2, 100, 3)
25 print(conseq_sum)
[
9 1650.0
]:
S10:
[1
1 # S 10:
0]:
2
3 def is_it_sorted_descending(p_list):
4
5 # create a new list
6 sorted_list = p_list[::]
7
# sort the new list in descending order ->
8
sort()
9 sorted_list.sort(reverse=True)
10
11 if p_list == sorted_list:
12 return True
13 else:
14 return False
15
16
17 # call the function you defined
18 a_list = [2, 3, 4, 7]
19 print(is_it_sorted_descending(a_list))
20
21 a_list = [7, 7, 4, 3, 2, 1, 1]
22 print(is_it_sorted_descending(a_list))
23
24 a_list = ['a', 'b', 'c', 'd']
25 print(is_it_sorted_descending(a_list))
26
27 a_list = ['d', 'c', 'b', 'a']
28 print(is_it_sorted_descending(a_list))
[1
False
0]:
True
False
True
OceanofPDF.com
15. Dictionary
What is a Dictionary?
Dictionary (dict) is a collection of data values in key:value
pairs. Dictionaries store multiple values like Lists. But they are
different. In a List, indices are integer (int) and they are defined
implicitly by Python. We don't define indices for Lists. But for a
Dictionary, we define keys manually, and they don’t have to be
integers. They may be any suitable type.
Let's say we want to store employee data; name, age,
department.
[
1 1 employee = {
]:
2 'name': 'Clark Kent',
3 "age": 37,
4 "department": 'News Service'
5 }
Key Value
name Clark Kent
age 37
department News Service
Creating a Dictionary
Dictionaries are created with curly brackets: {} . A pair of
empty brackets creates an empty dictionary.
[
3 1 # empty dict
]:
2 cars_empty = {}
dict() :
Another way to create a dictionary is using the dict()
constructor. Let’s define an empty dictionary by using its
constructor and print its type:
[
7 1 # empty dict with constructor
]:
2 cars_empty_2 = dict()
[
8 1 type(cars_empty_2)
]:
[
8 dict
]:
In cell 11, on the cars dictionary we call the item with the key
‘Audi’ as: cars['Audi'] . And it returns ‘Germany’ which is the
value corresponding that key. Let’s call one more time with the key
as ‘Fiat’ :
[1
1 cars['Fiat']
2]:
[1
'Italy'
2]:
What happens if you pass a key that does not exist in the
dictionary? Let’s see:
[1
1 # no element with key -> Ford
3]:
2 cars['Ford']
[1
KeyError: 'Ford'
3]:
Let’ add another item. But this time, we will deliberately make
a mistake in the value:
[1
1 numbers[6] = 'sixty'
7]:
2 numbers
[1 {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6:
7]: 'sixty'}
Now let’s add a new element to the car dict. We will first
define the new item as a dictionary with a single element. Then we
will update the car dict with this new variable.
[2
1 # define the new element as a dict
2]:
2 element_to_add = {'color': 'red'}
3
4 # add this element to the dict
5 car.update(element_to_add)
6
7 print(car)
[2 {'brand': 'Ford', 'model': 'Mustang', 'year': 1964,
2]: 'color': 'red'}
[2
1 # inside parentheses
3]:
2 car.update({ 'age': 50 })
In cell 23, we add the new item in form of key:value pair
directly. And let’s print the car dict again:
[24
1car
]:
[24 {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red', 'age':
]: 50}
The update() function can take more than one element as the
argument. We get the chance to add multiple elements at once in
that way. Let’s see it:
[2
1 # add more than one key:value pair
5]:
2 elements_to_add = {
3 'price': 100000,
4 'motor': 1.6,
5 'age': 72
6 }
7
8 car.update(elements_to_add)
Now let’s print the car to see the new items in it:
[2
1 car
6]:
[2
{'brand': 'Ford',
6]:
'model': 'Mustang',
'year': 1964,
'color': 'red',
'age': 72,
'price': 100000,
'motor': 1.6}
pop() :
The pop() removes the element from the dictionary and
returns the deleted element. When calling the pop() function on
Dictionaries, we pass the key of the item as the argument.
[2
1 # define an empty dict
8]:
2 number_names = {}
3
4 # add items with keys
5 number_names['one'] = 1
6 number_names['two'] = 2
7 number_names['three'] = 3
8 number_names['four'] = 4
9 number_names['five'] = 5
10 number_names['six'] = 6
11 number_names['seven'] = 7
12 number_names['eight'] = 8
13
14 # print the dict
15 number_names
[2
{'one': 1,
8]:
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8}
Let’s delete one more element. The one with the key of
‘seven’ for example:
[3
1 # remove the item with key 'seven'
1]:
2 number_names.pop('seven')
3
4 # print number_names again
5 number_names
[3 {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six':
1]: 6}
del :
The second way of deleting elements from Dictionaries is using
the del statement. It removes the item but does not return
anything.
[3
1 # redefine an empty dict
2]:
2 number_names = {}
3
4 # add items with keys
5 number_names['one'] = 1
6 number_names['two'] = 2
7 number_names['three'] = 3
8 number_names['four'] = 4
9 number_names['five'] = 5
10 number_names['six'] = 6
11 number_names['seven'] = 7
12 number_names['eight'] = 8
13
14 # print the dict
15 number_names
[3
{'one': 1,
2]:
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8}
Let’s delete the item with key ‘one’ from the dictionary:
[3
1 # delete item 'one' with del
3]:
2 del number_names['one']
3
4 # print the dict
5 number_names
[3 {'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6,
3]: 'seven': 7, 'eight': 8}
What happens if you pass a key which does not exist in the
dictionary? Python will throw a KeyError if it cannot find that
item in the dictionary.
[3
1 # pass a key which does not exist
5]:
2 del number_names['ten']
[3
KeyError: 'ten'
5]:
In cells 37 and 38, we get the dict elements with keys ‘seven’
and ‘two’ respectively. As you see, Python returns their values.
dict.items() :
Most of the time we need to get all the items in a dictionary.
Python has a special method for this which is items() . It’s syntax
is: dict.items() . It returns a view object named dict_items . The
dict_items is a list of key-value pairs of the dictionary. These key-
value pairs are in form of Tuples , which we will cover in the next
chapter. The view object will reflect any changes done to the
dictionary.
Let’s get all the items in our number_names dict:
[3
1 # get all items in the dict
9]:
2 number_names.items()
[3 dict_items([('one', 1), ('two', 2), ('three', 3), ('four',
9]: 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8)])
dict.keys() :
If we only need the keys in a dictionary, we use keys() method
for this. The syntax is dict.keys() and it returns a view object,
dict_keys , that displays a list of all the keys in the dictionary in
order of insertion. The view object will reflect any changes done to
the dictionary.
[4
1 # get all keys
0]:
2 number_names.keys()
[4 dict_keys(['one', 'two', 'three', 'four', 'five', 'six',
0]: 'seven', 'eight'])
dict.values() :
If we need only the values in a dictionary we use values()
method. It’s syntax is dict.values() and it returns a view object,
dict_values . The view object contains the values of the dictionary,
in the form of a list. And it will reflect any changes done to the
dictionary.
[4
1 # get the values only
1]:
2 number_names.values()
[4
dict_values([1, 2, 3, 4, 5, 6, 7, 8])
1]:
Let’s say, we want to print only the keys. We can loop over
keys() to achieve this:
[4
1 # only the keys
4]:
2 for k in number_names.keys():
3 print(k)
[4
one
4]:
two
three
four
five
six
seven
eight
Before ending this section, let’s talk a bit more on reading the
elements of a dictionary. We will redefine the car dict to use in our
next examples.
[4
1 car = {
6]:
2 'brand': 'Ford',
3 'model': 'Mustang',
4 'year': 1964,
5 'color': 'red',
6 'age': 72,
7 'price': 100000,
8 'motor': 1.6
9 }
Let’s try get the elements with keys ‘brand’ and ‘BRAND’ :
[4
1 car['brand']
7]:
[4
'Ford'
7]:
[4
1 # KeyError: 'BRAND'
8]:
2 car['BRAND']
[4
KeyError: 'BRAND'
8]:
get() :
In cell 48, we get an error when we try to access an element
which does not exist. We mainly call errors as Exceptions in
programming. And if you do not properly handle your errors
(exceptions) your program might crash. As it did in cell 48.
Now let’s say, we want to get an element from the dictionary
but we don’t want to get any errors if it does not exist. Hopefully,
we have the get() method for this. It’s syntax is
dict.get(element) and it returns the element if it exists. If not, it
does not throw any exceptions, simply returns None .
[4
1 # get the item 'brand'
9]:
2 car.get('brand')
[4
'Ford'
9]:
len() :
The built-in len() function gives you the length of the
dictionary. Length means the total number of elements which are
key-value pairs. Let’s see the length of our car dict:
[5
1 # length of the car dict
1]:
2 len(car)
[5
7
1]:
in :
[5
1 # is there a key as 'height'
4]:
2 'height' in car.keys()
[5
False
4]:
And we here is the loop over the values in the cars dict:
[6
1 # loop over the values
2]:
2 for country in cars.values():
3 print(country)
[6
Germany
2]:
Japan
Italy
US
Example:
Define a function named count_letters . It will take a text as
the parameter. It will count each letter in the text and print them
with their counts like: {'a': 3, 's': 3, 'e': 1 } . It should only
count alphabetical letters, not any other characters.
[6
1 def count_letters(text):
3]:
2 # a dict for numbers and counts
3 letters = dict()
4
5 # loop over the letters in the text
6 for char in text:
7 # check if letter is alphabetical
8 if char.isalpha():
9 letters[char] = letters.get(char, 0) + 1
10
11 return letters
Reverse Lookup
In dictionaries we mainly look for a key and try to find the
value corresponding to that key. That is the main idea of dictionary
search in Python. And it’s very fast because it uses a hash table
structure. Hash tables are special data structures in which the index
values of the data element is generated from a hash function. That
makes accessing the data much faster because the index value
behaves as a key for the data value.
Now, let's say we want to do the opposite. Let's assume, we
have a value this time and we want to find the key corresponding
this value.
Here are the possible consequences for this reverse lookup:
In cell 69, we loop over the keys in the dictionary with a for
loop. If we find the key which corresponds to the value parameter,
we return it in line 7. If we cannot find any keys then the for loops
succeeds without any return statement, which means the else
block gets executed. Inside the else block have a special syntax to
raise an exception in Python. raise is the keyword and it’s syntax
is: raise SomeError . We raise a KeyError as: raise
KeyError('We could not find with this value:', value) . We can
pass any text we want into the KeyError() constructor. The error
description is up to us.
Now that our function is capable of handling cases when no
keys exist, let’s call it one more time.
[7
1 value = 333
0]:
2 reverse_lookup(dictionary, value)
[7 KeyError: ('We could not find with this value:',
0]: 333)
[7
1 def lists_of_letters(text):
2]:
2
3 # get the dictionary of occurrences
4 # by calling the count_letters function
dictionary_with_letter_key =
5
count_letters(text)
6
7 dictionary_with_count_key = {}
8
for letter, count in
9
dictionary_with_letter_key.items():
# if this count is not in keys -> add it and
10
assign a list of key
11 if count not in dictionary_with_count_key:
dictionary_with_count_key[count] =
12
[letter]
13 else:
14 # this value is in letters keys
dictionary_with_count_key[count].append
15
(letter)
16
17 return dictionary_with_count_key
Here are the steps for how we define the lists_of_letters
function in cell 72:
As you see in cell 74, we tried to set the list a , as the key for
the dictionary s , but Python raised an exception. TypeError:
unhashable type: 'list' . Which states that a list is an unhashable
type, and you cannot use an unhashable type as dictionary keys.
Rule of Thumb: Dictionary Keys must be hashable types.
A hash is a function which takes any value and returns an
integer for that value. A Dictionary uses the hash values for the
keys to store and access the items. In other words, it uses the hash
value like an index for the keys.
Remember we talked about Mutable and Immutable Types.
Now we are ready to see the connection between all these concepts.
Mutable vs. Immutable:
Hash values for Mutable types may change (because
they can be mutated)
Hash values for Immutable types cannot change (they
cannot be mutated)
OceanofPDF.com
QUIZ - Dictionary
Now it’s time to solve the quiz for this chapter. You can
download the quiz file, QUIZ_Dictionary.ipynb, from the Github
Repository of this book. It is in the folder named 15_Dictionary.
Here are the questions for this chapter:
QUIZ - Dictionary:
Q1:
Define a function named words_dict.
It will read en_words.txt file and will create a dictionary
from these words.
It will only take the words that have more than or equal to 19
letters.
The Key of the dictionary will be the word itself and the
Value will be number of letters in the word.
The function will return this dictionary.
Hints:
open()
Expected Output:
{
'anticonservationist': 19,
'comprehensivenesses': 19,
'counterdemonstration': 20,
'counterdemonstrations': 21,
...
}
[
1 1 # Q 1:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 words_dictionary = words_dict()
8 words_dictionary
[
1 {'anticonservationist': 19,
]:
'comprehensivenesses': 19,
'counterdemonstration': 20,
'counterdemonstrations': 21,
'counterdemonstrator': 19,
'counterdemonstrators': 20,
'counterinflationary': 19,
'counterpropagations': 19,
'counterretaliations': 19,
'disinterestednesses': 19,
'electrocardiographs': 19,
'extraconstitutional': 19,
'hyperaggressiveness': 19,
'hyperaggressivenesses': 21,
'hypersensitivenesses': 20,
'inappropriatenesses': 19,
'inconsideratenesses': 19,
'interdenominational': 19,
'irreconcilabilities': 19,
'microminiaturization': 20,
'microminiaturizations': 21,
'miscellaneousnesses': 19,
'multidenominational': 19,
'representativenesses': 20}
Q2:
Define a function named words_length_dict.
It will read en_words.txt file and will create a dictionary
from these words.
It will only take the words that have more than or equal to 19
letters.
The Key will be the length (number of characters in word)
and the Value will be a List of words which have that length.
The function will return this dictionary.
Hints:
open()
Expected Output:
{19: ['anticonservationist', 'comprehensivenesses',
'counterdemonstrator', ...],
20: ['counterdemonstration', 'counterdemonstrators',
'hypersensitivenesses', ...],
21: ['counterdemonstrations', 'hyperaggressivenesses',
'microminiaturizations']}
[
2 1 # Q 2:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 length_words = words_length_dict()
8 length_words
[
2 {19: ['anticonservationist',
]:
'comprehensivenesses',
'counterdemonstrator',
'counterinflationary',
'counterpropagations',
'counterretaliations',
'disinterestednesses',
'electrocardiographs',
'extraconstitutional',
'hyperaggressiveness',
'inappropriatenesses',
'inconsideratenesses',
'interdenominational',
'irreconcilabilities',
'miscellaneousnesses',
'multidenominational'],
20: ['counterdemonstration',
'counterdemonstrators',
'hypersensitivenesses',
'microminiaturization',
'representativenesses'],
21: ['counterdemonstrations',
'hyperaggressivenesses',
'microminiaturizations']}
Q3:
Define 4 functions named car_1, car_2, car_3, car_4.
These functions will create dictionaries as below (name of
the dictionary will be car):
{'brand': 'Ford',
'model': 'Mustang',
'year': 1964,
'color': 'Red',
'price': 30000,
'km': 89000,
'motor': 1.6}
Functions will create the dictionary by different ways and
return the dictionary.
Hints:
{ }
dict()
update()
Expected Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color':
'Red', 'price': 30000, 'km': 89000, 'motor': 1.6}
[3
1 # Q 3:
]:
2
3 # Function 1
4 # ---- your solution here ----
5
6 # call the function you defined
7 print(car_1())
8
9
10 # Function 2
11 # ---- your solution here ----
12
13 # call the function you defined
14 print(car_2())
15
16
17 # Function 3
18 # ---- your solution here ----
19
20 # call the function you defined
21 print(car_3())
22
23
24 # Function 4
25 # ---- your solution here ----
26
27 # call the function you defined
28 print(car_4())
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964,
[3
'color': 'Red', 'price': 30000, 'km': 89000, 'motor':
]:
1.6}
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964,
'color': 'Red', 'price': 30000, 'km': 89000, 'motor':
1.6}
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964,
'color': 'Red', 'price': 30000, 'km': 89000, 'motor':
1.6}
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964,
'color': 'Red', 'price': 30000, 'km': 89000, 'motor':
1.6}
Q4:
Define a function named create_a_new_car.
It will call one of the functions defined in Q3 and will get
the car dictionary.
Then it will copy the items of this car dictionary into another
dictionary via a loop.
It will first copy all the elements in car dictionary, then
create new keys via appending "_2" at the end of existing
key, and create a new element.
Values will be the same.
It will return the new dictionary.
Hint:
copy()
update()
items()
Expected Output:
{
'brand': 'Ford',
'model': 'Mustang',
'year': 1964,
'color': 'Red',
'price': 30000,
'km': 89000,
'motor': 1.6
'brand_2': 'Ford',
'model_2': 'Mustang',
'year_2': 1964,
'color_2': 'Red',
'price_2': 30000,
'km_2': 89000,
'motor_2': 1.6
}
[
4 1 # Q 4:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 create_a_new_car()
[
4 {'brand': 'Ford',
]:
'model': 'Mustang',
'year': 1964,
'color': 'Red',
'price': 30000,
'km': 89000,
'motor': 1.6,
'brand_2': 'Ford',
'model_2': 'Mustang',
'year_2': 1964,
'color_2': 'Red',
'price_2': 30000,
'km_2': 89000,
'motor_2': 1.6}
Q5:
Define a function named concat_dicts.
It will concatenate the dictionaries below and return the
resulting dict.
The function will take these dictionaries as parameters.
Hints:
Dictionaries to concat:
d1={4:120, 7:60}
d2={'A': 300, 'B':400}
d3={True: 'Correct', False: 'Incorrect'}
Expected Output:
{4: 120, 7: 60, 'A': 300, 'B': 400, True: 'Correct', False:
'Incorrect'}
[
5 1 # Q 5:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 d1={4:120, 7:60}
8 d2={'A': 'AAA', 'B':'BBB'}
9 d3={True: 'Correct', False: 'Incorrect'}
10
11 d = concat_dicts(d1, d2, d3)
12 print(d)
[
{4: 120, 7: 60, 'A': 'AAA', 'B': 'BBB', True:
5
'Correct', False: 'Incorrect'}
]:
Q6:
Define a function named sum_of_same_keys.
It will take two dictionaries (d1, d2) as parameters.
And it will sum the values of items having the same key in
both dicts.
It will not take distinct keys. So it will return a new
dictionary with only common keys.
Hints:
Parameters:
d1 = {'a': 10, 'b': 30, 'c':50}
d2 = {'a': 40, 'b': 60, 'd':90}
Expected Output:
{'a': 50, 'b': 90}
[
6 1 # Q 6:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 d1 = {'a': 10, 'b': 30, 'c':50, 'd': 100}
8 d2 = {'a': 40, 'b': 60, 'd':90, 'e': 50}
9 sum_of_same_keys(d1, d2)
[
6 {'a': 50, 'b': 90, 'd': 190}
]:
Q7:
Let's improve the function in Q6.
It will work the same for the same keys.
But this time we will also include the different keys.
Function name will be
sum_of_same_keys_value_of_distinct_ones.
Hints:
Parameters:
d1 = {'a': 10, 'b': 30, 'c':50}
d2 = {'a': 40, 'b': 60, 'd':90}
Expected Output:
{'a': 50, 'b': 90, 'c': 50, 'd': 90}
[
7 1 # Q 7:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 d1 = {'a': 10, 'b': 30, 'c':50}
8 d2 = {'a': 40, 'b': 60, 'd':90}
9 sum_of_same_keys_value_of_distinct_ones(d1, d2)
[
7 {'a': 50, 'b': 90, 'c': 50, 'd': 90}
]:
Q8:
Define a function named delete_odds.
It will take a dictionary as parameter.
It will delete the items with odd indices from the dictionary
and return a new dictionary with remaining items.
Hints:
Parameter Dictionary:
dictionary = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F'}
Expected Output:
{'a': 'A', 'c': 'C', 'e': 'E'}
[
8 1 # Q 8:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
dictionary = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E',
7
'f': 'F'}
8 evens = delete_odds(dictionary)
9 print(evens)
[
8 {'a': 'A', 'c': 'C', 'e': 'E'}
]:
Q9:
Define a function named convert_lists_into_dict.
It will take two lists as parameters.
The function will use the first list elements as Keys and
second list elements as Values and it will create a dictionary.
Then it will return this dictionary.
Hints:
enumerate()
Parameters:
l_1 = ['name', 'lastname', 'age', 'gender']
l_2 = ['John', 'Doe', 100, 'Male']
Expected Output:
{'name': 'John', 'lastname': 'Doe', 'age': 100, 'gender': 'Male'}
[
9 1 # Q 9:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 l_1 = ['name', 'lastname', 'age', 'gender']
8 l_2 = ['John', 'Doe', 100, 'Male']
9 employee = convert_lists_into_dict(l_1, l_2)
10 print(employee)
[
{'name': 'John', 'lastname': 'Doe', 'age': 100,
9
'gender': 'Male'}
]:
Q10:
Let's consider a function with keys being both numbers and
letters.
Example: {'a': 'A', 'b': 'B', 2: 200, 'd': 'D', 5: 300, 'f': 'F', 1:
50}
Define a function named alphabetical.
It will delete the elements with keys being number.
And it will return the final dictionary which has only
alphabetical keys.
Hints:
Parameter Dictionary:
dictionary = {'a': 'A', 'b': 'B', 2: 200, 'd': 'D', 5: 300, 'f': 'F',
1: 50}
Expected Output:
dictionary before calling alphabetical: {'a': 'A', 'b': 'B', 2:
200, 'd': 'D', 5: 300, 'f': 'F', 1: 50}
dictionary after calling alphabetical: {'a': 'A', 'b': 'B', 'd': 'D',
'f': 'F'}
[1
1 # Q 10:
0]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
dictionary = {'a': 'A', 'b': 'B', 2: 200, 'd': 'D', 5:
7
300, 'f': 'F', 1: 50}
print("dictionary before calling alphabetical:",
8
dictionary)
9 alphabetical(dictionary)
print("dictionary after calling alphabetical:",
10
dictionary)
[1 dictionary before calling alphabetical: {'a': 'A',
0]: 'b': 'B', 2: 200, 'd': 'D', 5: 300, 'f': 'F', 1: 50}
dictionary after calling alphabetical: {'a': 'A', 'b':
'B', 'd': 'D', 'f': 'F'}
OceanofPDF.com
SOLUTIONS - Dictionary
Here are the solutions for the quiz for Chapter 15 - Dictionary.
SOLUTIONS - Dictionary:
S1:
[
1 1 # S 1:
]:
2
3 # Let's define a CONSTANT
4 # and we will use it as a global variable
5 MIN_CHAR_LENGTH = 19
6
7
8 def words_dict():
9
10 # an empty dict
11 dictionary = dict()
12
13 # read the words
14 file = open('en_words.txt')
15
16 # iterate over file -> line by line
17 for i, line in enumerate(file):
18
19 # line -> \n, ' '
20 line_list = line.split()
21
22 word = line_list[0]
23
24 # if i < 20:
25 # print(word)
26
27 # check for lenth
28 if len(word) >= MIN_CHAR_LENGTH:
29 # print(word)
30
31 # check if it has not been added already
32 if not word in dictionary:
33 # add the word into dict
34 dictionary[word] = len(word)
35
36 return dictionary
37
38
39 # call the function you defined
40 words_dictionary = words_dict()
41 words_dictionary
[
1 {'anticonservationist': 19,
]:
'comprehensivenesses': 19,
'counterdemonstration': 20,
'counterdemonstrations': 21,
'counterdemonstrator': 19,
'counterdemonstrators': 20,
'counterinflationary': 19,
'counterpropagations': 19,
'counterretaliations': 19,
'disinterestednesses': 19,
'electrocardiographs': 19,
'extraconstitutional': 19,
'hyperaggressiveness': 19,
'hyperaggressivenesses': 21,
'hypersensitivenesses': 20,
'inappropriatenesses': 19,
'inconsideratenesses': 19,
'interdenominational': 19,
'irreconcilabilities': 19,
'microminiaturization': 20,
'microminiaturizations': 21,
'miscellaneousnesses': 19,
'multidenominational': 19,
'representativenesses': 20}
S2:
[
2 1 # S 2:
]:
2
3 # global constant
4 MIN_CHAR_LENGTH = 19
5
6 def words_length_dict():
7
8 # an empty dict
9 dictionary = dict()
10
11 # read the words
12 file = open('en_words.txt')
13
14 # iterate over file -> line by line
15 for i, line in enumerate(file):
16
17 # line -> \n, ' '
18 line_list = line.split()
19
20 word = line_list[0]
21
22 # get the length
23 length = len(word)
24
25 # check for length
26 if len(word) >= MIN_CHAR_LENGTH:
27
28 # first time -> [word]
29 if not length in dictionary:
30 dictionary[length] = [word]
31 else:
32 dictionary[length].append(word)
33
34 return dictionary
35
36
37 # call the function you defined
38 length_words = words_length_dict()
39 length_words
[
2 {19: ['anticonservationist',
]:
'comprehensivenesses',
'counterdemonstrator',
'counterinflationary',
'counterpropagations',
'counterretaliations',
'disinterestednesses',
'electrocardiographs',
'extraconstitutional',
'hyperaggressiveness',
'inappropriatenesses',
'inconsideratenesses',
'interdenominational',
'irreconcilabilities',
'miscellaneousnesses',
'multidenominational'],
20: ['counterdemonstration',
'counterdemonstrators',
'hypersensitivenesses',
'microminiaturization',
'representativenesses'],
21: ['counterdemonstrations',
'hyperaggressivenesses',
'microminiaturizations']}
S3:
[3
1 # S 3:
]:
2
3 # Function 1
4 def car_1():
5 car = {}
6 car['brand'] = 'Ford'
7 car['model'] = 'Mustang'
8 car['year'] = 1964
9 car['color'] = 'Red'
10 car['price'] = 30000
11 car['km'] = 89000
12 car['motor'] = 1.6
13 return car
14
15 # call the function you defined
16 print(car_1())
17
18
19 # Function 2
20 def car_2():
21 car = dict()
22 car['brand'] = 'Ford'
23 car['model'] = 'Mustang'
24 car['year'] = 1964
25 car['color'] = 'Red'
26 car['price'] = 30000
27 car['km'] = 89000
28 car['motor'] = 1.6
29 return car
30
31 # call the function you defined
32 print(car_2())
33
34
35 # Function 3
36 def car_3():
37 car = dict()
38 car.update(
39 {
40 'brand': 'Ford',
41 'model': 'Mustang',
42 'year': 1964,
43 'color': 'Red',
44 'price': 30000,
45 'km': 89000,
46 'motor': 1.6
47 }
48 )
49 return car
50
51 # call the function you defined
52 print(car_3())
53
54
55 # Function 4
56 def car_4():
57 car = dict(
58 {
59 'brand': 'Ford',
60 'model': 'Mustang',
61 'year': 1964,
62 'color': 'Red',
63 'price': 30000,
64 'km': 89000,
65 'motor': 1.6
66 }
67 )
68 return car
69
70 # call the function you defined
71 print(car_4())
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964,
[3
'color': 'Red', 'price': 30000, 'km': 89000, 'motor':
]:
1.6}
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964,
'color': 'Red', 'price': 30000, 'km': 89000, 'motor':
1.6}
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964,
'color': 'Red', 'price': 30000, 'km': 89000, 'motor':
1.6}
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964,
'color': 'Red', 'price': 30000, 'km': 89000, 'motor':
1.6}
S4:
[
4 1 # S 4:
]:
2
3 def create_a_new_car():
4
5 # first get the car dictionary
6 car = car_4()
7
8 # add this car dict into a new dict
9 new_car = car.copy()
10
11 # loop over items
12 for item in car.items():
13
14 # print(item)
15
16 key = item[0]
17 value = item[1]
18
19 key += '_2'
20
21 new_car[key] = value
22
23 return new_car
24
25
26 # call the function you defined
27 create_a_new_car()
[
4 {'brand': 'Ford',
]:
'model': 'Mustang',
'year': 1964,
'color': 'Red',
'price': 30000,
'km': 89000,
'motor': 1.6,
'brand_2': 'Ford',
'model_2': 'Mustang',
'year_2': 1964,
'color_2': 'Red',
'price_2': 30000,
'km_2': 89000,
'motor_2': 1.6}
S5:
[
5 1 # S 5:
]:
2
3 def concat_dicts(d1, d2, d3):
4
5 dictionary = {}
6
7 # Multiple Dictionaries -> (d1, d2, ...)
8
9 for e in (d1, d2, d3):
10 dictionary.update(e)
11
12 return dictionary
13
14
15 # call the function you defined
16 d1={4:120, 7:60}
17 d2={'A': 'AAA', 'B':'BBB'}
18 d3={True: 'Correct', False: 'Incorrect'}
19
20 d = concat_dicts(d1, d2, d3)
21 print(d)
[
{4: 120, 7: 60, 'A': 'AAA', 'B': 'BBB', True:
5
'Correct', False: 'Incorrect'}
]:
S6:
[
6 1 # S 6:
]:
2
3 def sum_of_same_keys(d1, d2):
4
5 # check 1 -> dict types
if not isinstance(d1, dict) or not isinstance(d2,
6
dict):
raise Exception('Both parameters must be
7
dictionary type!')
8
9 # check 2 -> length
10 if len(d1) != len(d2):
raise Exception('Dictionaries must be the
11
same length!')
12
13 # passed
14 dictionary = {}
15
16 for key in d1:
17 if key in d2:
18 dictionary[key] = d1[key] + d2[key]
19
20 return dictionary
21
22
23 # call the function you defined
24 d1 = {'a': 10, 'b': 30, 'c':50, 'd': 100}
25 d2 = {'a': 40, 'b': 60, 'd':90, 'e': 50}
26 sum_of_same_keys(d1, d2)
[
6 {'a': 50, 'b': 90, 'd': 190}
]:
S7:
[
7 1 # S 7:
]:
2
def
3 sum_of_same_keys_value_of_distinct_ones(d1,
d2):
4
5 # check 1 -> dict types
if not isinstance(d1, dict) or not isinstance(d2,
6
dict):
raise Exception('Both parameters must be
7
dictionary type!')
8
9 # check 2 -> length
10 if len(d1) != len(d2):
raise Exception('Dictionaries must be the
11
same length!')
12
13 # passed
14 dictionary = {}
15
16 # loop over d1
17 for key in d1:
18 if key in d2:
19 dictionary[key] = d1[key] + d2[key]
20 else:
21 dictionary[key] = d1[key]
22
23 # loop over d2
24 for key in d2:
25 if not key in d1:
26 dictionary[key] = d2[key]
27
28 return dictionary
29
30
31 # call the function you defined
32 d1 = {'a': 10, 'b': 30, 'c':50}
33 d2 = {'a': 40, 'b': 60, 'd':90}
sum_of_same_keys_value_of_distinct_ones(d1,
34
d2)
[
7 {'a': 50, 'b': 90, 'c': 50, 'd': 90}
]:
S8:
[
8 1 # S 8:
]:
2
3 def delete_odds(dictionary):
4
5 d = {}
6
for index, item in
7
enumerate(dictionary.items()):
8 key = item[0]
9 value = item[1]
10
11 if index % 2 == 0:
12 d[key] = value
13
14 return d
15
16
17 # call the function you defined
dictionary = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e':
18
'E', 'f': 'F'}
19 evens = delete_odds(dictionary)
20 print(evens)
[
8 {'a': 'A', 'c': 'C', 'e': 'E'}
]:
S9:
[
9 1 # S 9:
]:
2
3 def convert_lists_into_dict(list1, list2):
4
5 dictionary = dict()
6
7 for index, key in enumerate(list1):
8 dictionary[key] = list2[index]
9
10 return dictionary
11
12
13 # call the function you defined
14 l_1 = ['name', 'lastname', 'age', 'gender']
15 l_2 = ['John', 'Doe', 100, 'Male']
16 employee = convert_lists_into_dict(l_1, l_2)
17 print(employee)
[
{'name': 'John', 'lastname': 'Doe', 'age': 100,
9
'gender': 'Male'}
]:
S10:
[1
1 # S 10:
0]:
2
3 def alphabetical(dictionary):
4
5 # list to keep keys to delete
6 keys_to_delete = []
7
8 # get the keys to delete
9 for key in dictionary.keys():
10 if not str(key).isalpha():
11 keys_to_delete.append(key)
12
# loop over keys to delete -> delete that key
13
from the dict
14 for key in keys_to_delete:
15 if key in dictionary.keys():
16 dictionary.pop(key)
17
18
19 # call the function you defined
dictionary = {'a': 'A', 'b': 'B', 2: 200, 'd': 'D', 5:
20
300, 'f': 'F', 1: 50}
print("dictionary before calling alphabetical:",
21
dictionary)
22 alphabetical(dictionary)
print("dictionary after calling alphabetical:",
23
dictionary)
[1 dictionary before calling alphabetical: {'a': 'A',
0]: 'b': 'B', 2: 200, 'd': 'D', 5: 300, 'f': 'F', 1: 50}
dictionary after calling alphabetical: {'a': 'A', 'b':
'B', 'd': 'D', 'f': 'F'}
OceanofPDF.com
16. Tuple
Tuple Creation
Tuple is a Sequence Type like String and List. The elements of
a Tuple are separated by commas. Tuples can include any type of
elements. And they are indexed (implicitly) by integers like Lists.
The main difference between Tuples and List is; Lists are Mutable
Types while Tuples are Immutable.
Let’s see how we create a Tuple:
[
1 1 t = 'x', 'y', 'z', 'q', 'p'
]:
2 print(t)
[
1 ('x', 'y', 'z', 'q', 'p')
]:
In cell 6, we try with parentheses around the value ‘x’ . But it’s
still a string. So, how do we create a tuple with just one element?
Answer: You add a comma after the element.
[
7 1 one_tuple = ('x',)
]:
2 print(one_tuple)
3 type(one_tuple)
[
7 ('x',)
]:
tuple
And in cell 8, we define the one element tuple as: 'y', . Since
the parentheses are not mandatory we omit them. But we have to
use the extra comma.
tuple() :
Another way to create a tuple is the tuple() constructor. Let’s
create some tuples with the constructor.
[
9 1 # empty tuple
]:
2 t = tuple()
3 t
[
9 ()
]:
In cell 11, we create a tuple with just one element. We just pass
the element into the tuple() constructor as the argument:
tuple('x') .
Let’s pass a string into the tuple() constructor and see what
happens:
[1
1 lang = tuple('Python')
2]:
2 lang
[1
('P', 'y', 't', 'h', 'o', 'n')
2]:
[1
1 # define a list
5]:
2 my_list = ['A', 'B', 'C', 'D']
3
4 # pass my_list into tuple()
5 list_tuple = tuple(my_list)
6
7 # print the list_tuple
8 list_tuple
[1
('A', 'B', 'C', 'D')
5]:
Slicing:
If there is indexing in a type then there is also slicing. In
Tuples, slicing works exactly the same as in Lists.
[1
1 # define a text
6]:
2 text = 'Moon is the orbit of Earth.'
3
4 # convert the text into a tuple
5 tup = tuple(text)
6
7 # print the tuple
8 print(tup)
[1 ('M', 'o', 'o', 'n', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 'o', 'r',
6]: 'b', 'i', 't', ' ', 'o', 'f', ' ', 'E', 'a', 'r', 't', 'h', '.')
In cell 16, we create a tuple out of a string. And you see the
tuple in the output. Now, let’s do some slicing on this tuple:
[1
1 # slicing
7]:
2 # slice the word -> Moon
3 tup[0:4]
[1
('M', 'o', 'o', 'n')
7]:
Tuple Comparison
In cell 25, we compare two strings. Since the first letters are
‘a’ in both strings, Python will move on to the second elements. In
the string on the left the second element is ‘b’ while it is ‘t’ in the
string on the right. So string on the left is less than the one on the
right. That’s why the result of comparison is True .
Let’s do the same comparison with tuples this time:
[2
1 # Tuple Comparison
6]:
2 ('a', 'b', 'x', 'c') < ('a', 't', 'e', 'd')
[2
True
6]:
Now they are tuples, not strings. But the comparison is exactly
the same. The first elements are equal, but the second element on
the left is less than the second one on the right. So the first tuple is
less than the second one.
[2
1 # String Comparison
7]:
2 'abc' < 'aac'
[2
False
7]:
[2
1 # Tuple Comparison
8]:
2 tuple('abc') < tuple('aac')
[2
False
8]:
In cell 27, we compare strings and we can see that 'abc' is less
not than 'aac' . So the output is False . That’s exactly the same for
tuples in cell 28. The expression of tuple('abc') < tuple('aac')
returns False .
[2
1 # Tuple Comparison
9]:
2 (1, 2, 3) < (5, 4, 3)
[2 True
9]:
In cell 29, since the first element of the left hand side is 1 and
the first one on the right is 5, Python decides the tuple on the left
is less than the one on the right. It doesn’t look other items.
[3
1 # Tuple Comparison
0]:
2 (5, 40, 200) < (6, 1, 3)
[3
True
0]:
In cell 30, the first element on the left is 5 . The first one on the
right is 6 . So the tuple on the left is less than the one on the right.
[3
1 # String Comparison
1]:
2 '1243' < '1234'
[3
False
1]:
[3
1 # Tuple Comparison
2]:
2 tuple('1243') < tuple('1234')
[3
False
2]:
Important:
In Tuple Assignment, number of variables must be the same on
both sides of the assignment.
[3
1 # number of items are different in two sides
7]:
2 num_1, num_2 = 500, 800, 600
[3 ValueError: too many values to unpack (expected
7]: 2)
Example: Split the user name and domain name from an email
via Tuple Assignment.
[3
1 # split the string at the car '@'
8]:
2 '[email protected]'.split('@')
[3
['johndoe', 'example.com']
8]:
In cell 38, we split the email of '[email protected]' via
the split() method. And we already know that split() returns a
list of the parts. We want to assign these two parts to variables.
[3
1 # split the string and do tuple assignment
9]:
user_name, domain =
2
'[email protected]'.split('@')
[4
1 print(user_name)
0]:
2 print(domain)
[4
johndoe
0]:
example.com
Example:
The built-in divmod() function takes two parameters:
dividend
divisor
quotient
remainder
[4
1 # divide 23 by 4
4]:
2 result = divmod(23, 4)
3 result
[4
(5, 3)
4]:
In cell 45, we get the items of result tuple via indices as:
result[0] and result[1] . And we assign these items to two
variables as quotient and remainder .
We can unpack the result of divmod(23, 4) in just one line
with tuple assignment. Let’s do it:
[4
1 # tuple assignment
6]:
2 # unpacking
3 quotient, remainder = divmod(23, 4)
4
5 print('quotient:', quotient)
6 print('remainder:', remainder)
[4
quotient: 5
6]:
remainder: 3
Example:
Define a function with unknown parameters ( *args ). The
function name will be sum_and_multiply and it will return the
summation and multiplication result of these arguments.
Before writing the actual code for this function, let’s do a quick
debugging. We will print the *args parameter and see what is in.
[4
1 def sum_and_multiply(*args):
7]:
2 print(args)
3 print(type(args))
[4
1 sum_and_multiply(2, 5, 4, 3)
8]:
[4
(2, 5, 4, 3)
8]:
<class 'tuple'>
Example:
Define a function taking a list as the parameter. The function
name will be simple_stats . And it will return three simple
statistics as:
min
max
mean (average)
zip() Function
List
Tuple
String
Range
As you see in the output of cell 55, each element in the zip
object is a Tuple:
Let’s try to access the elements of the zip object via indices and
see what happens:
[5
1 # try to get the item at index 0
6]:
2 zip_obj[0]
[5
TypeError: 'zip' object is not subscriptable
6]:
In cell 57, we zip two lists together with the zip(d1, d2)
expression in line 6. This returns a zip object which is an iterator
and we name it as new_zip . Then in line 9, we convert this
iterator to a list with the list() constructor as: zip_list =
list(new_zip) . The zip_list is a list of tuples. The item pairs
inside these tuples are from d1 and d2 respectively.
Now we can access any of the elements in the zip_list ,
because it is a list now.
[5
1 zip_list[2]
8]:
[5
('C', 30)
8]:
Now, let’s loop over the items of the zip_list. We know that it’s
a list of tuples, so let’s print these tuples:
[6
1 # loop over the zip_list
2]:
2 for el in zip_list:
3 print(el)
[6
('A', 10)
2]:
('B', 20)
('C', 30)
In cell 63, we unpack the current tuple of the loop as: for
d1_el, d2_el in zip_list . Then we use these variables in the
string.format() function as: "{0} is from d1 - {1} is from
d2".format(d1_el, d2_el) .
Example:
Check the corresponding elements (at the same index) of two
lists. And print them if they are equal. In other words, we want to
print the common elements at the same index in both lists.
[6
1 # define two lists
4]:
2 l1 = ['a', 'B', 'C', 'd', 'e', 'F']
3 l2 = ['A', 'B', 'c', 'd', 'E', 'F']
4
5 # loop over the zip of them
6 for e1, e2 in zip(l1, l2):
7 # check it the items are the same
8 if e1 == e2:
9 print(e1)
[6
B
4]:
d
F
In cell 64, we loop over the zip of the two lists, l1 and l2 . We
unpack the tuple items in the for loop as e1 and e2 . Then in line
8, we check if their values are the same. And we print one of them
if they have the same value.
Remainder:
Dictionaries are not ordered types. We cannot expect its items
to be ordered. Because, Dictionaries do not use implicit integer
indexing. We will get an error if we try to access the elements of
the dictionary with an index.
[6
1 # try to access the dict element with index
8]:
2 asciis[0]
[6
KeyError: 0
8]:
Convert a List of Tuples to a Dictionary:
We saw that items() method returns the elements of a
Dictionary as a List of Tuples. Now let's do the opposite. This time,
we have a list of tuples and we want convert it to a dictionary. Let’s
start by defining a list of tuples first:
[6
1 # define a list of tuples
9]:
2 months_days = [
3 ('January', 31),
4 ('February', 28),
5 ('March', 31),
6 ('April', 30)
7 ]
8
9 months_days
[6 [('January', 31), ('February', 28), ('March', 31),
9]: ('April', 30)]
1: Monday
2: Tuesday
3: Wednesday
4: Thursday
5: Friday
6: Saturday
7: Sunday
[7
1 # define a list for the day names
1]:
day_names = ['Monday', 'Tuesday', 'Wednesday',
2
'Thursday', 'Friday', 'Saturday', 'Sunday']
3
4 # define a range for the numbers
5 numbers = range(1, 8)
6
7 # zip numbers and day_names
8 days = zip(numbers, day_names)
9
10 # print the zip object
11 print(days)
[7
<zip object at 0x000001E215A28080>
1]:
Example:
We want to store the students’ names and grades in a
dictionary. A tuple of (FirstName, LastName) will be the key
and Grade will be the value. Here is the dictionary we want to
achieve:
[7
1 student_grades = {
4]:
2 ('Musa', 'Arda'): 'AA',
3 ('Bruce', 'Wayne'): 'DC',
4 ('Clark', 'Kent'): 'FF',
5 ('Peter', 'Parker'): 'FD'
6 }
[75
1 # create 3 lists
]:
2 names = ['Musa', 'Bruce', 'Clark', 'Peter']
3 lastnames = ['Arda', 'Wayne', 'Kent', 'Parker']
4 grades = ['AA', 'DC', 'FF', 'FD']
5
6 # create an empty dict
7 student_grades = {}
8
9 # loop over 3 list simultaneously -> zip()
for name, lastname, grade in zip(names, lastnames,
10
grades):
11 student_grades[(name, lastname)] = grade
12
13 # print the dictionary
14 student_grades
[75
{('Musa', 'Arda'): 'AA',
]:
('Bruce', 'Wayne'): 'DC',
('Clark', 'Kent'): 'FF',
('Peter', 'Parker'): 'FD'}
For example:
For sort operations Python has two built-in functions: sort()
and sorted() . Here are the syntax for them:
sort()
list.sort(reverse=True|False, key=myFunc)
sorted()
sorted(list, key=myFunc, reverse=True|False)
In cell 83, we redefine the cars tuple. And in cell 84, we call
the sorted() function. This time the key parameter is a lambda
function as: key = lambda e: len(e) . This lambda function takes
one argument as e and returns its length as len(e) .
[8
1 # sort()
5]:
2
3 # convert the tuple to a list
4 cars_list = list(cars)
5
6 # call the sort() method
7 cars_list.sort(key=lambda e: len(e))
8
9 print(cars_list)
[8
5]: ['VW', 'BMW', 'Audi', 'Porsche', 'Mercedes']
Question:
So far, we didn't pass any key parameters into sorted() or
sort() . How did they do the sorting?
Answer:
They use standard sorting:
Example:
Sort the elements of the tuple numbers which is (2, 1, 3, 7,
6, 5, 4) .
[8
1 numbers = (2, 1, 3, 7, 6, 5, 4)
6]:
2 numbers
[8
(2, 1, 3, 7, 6, 5, 4)
6]:
[8
1 # default -> sorts numeric
7]:
2 sorted(numbers)
[8
[1, 2, 3, 4, 5, 6, 7]
7]:
In cell 87, we call the sorted() function with the default value
for key parameter. Since the elements are numbers, it performs
numeric sort on them. We can do the same sorting with a lambda
function. This lambda function will take a parameter and it will
return that parameter. That’s all.
[8
1 # lambda
8]:
# sorted(numbers) = sorted(numbers, key=lambda
2
e: e)
3 sorted(numbers, key=lambda e: e)
[8
[1, 2, 3, 4, 5, 6, 7]
8]:
The lambda function for the key parameter in cell 88, has no
effect on the sort. Why? Because it just returns whatever the
parameter value e is: lambda e: e . And that’s exactly the same
condition as the default sort.
Example:
Sort the elements of the list letters which is ['c', 'f', 'b',
'a', 'e', 'd'] .
[8
1 # define the list
9]:
2 letters = ['c', 'f', 'b', 'a', 'e', 'd']
3
4 # default sort
5 letters.sort()
6
7 print(letters)
[8
['a', 'b', 'c', 'd', 'e', 'f']
9]:
OceanofPDF.com
QUIZ - Tuple
Now it’s time to solve the quiz for this chapter. You can
download the quiz file, QUIZ_Tuple.ipynb, from the Github
Repository of this book. It is in the folder named 16_Tuple. Here
are the questions for this chapter:
QUIZ - Tuple:
Q1:
Define a function named create_tuple.
It will create a tuple of numbers from 1 to 10.
Then it will add numbers from 11 to 20 to this tuple.
And it will return the final form of the tuple.
Hints:
Expected Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20)
[
1 1 # Q 1:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 t = create_tuple()
8 print(t)
[
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
1
17, 18, 19, 20)
]:
Q2:
Define a function named tuple_to_string.
It will take a Tuple as parameter and convert it into a String.
Then it will return this String.
Hints:
join()
Parameter:
t = ('M', 'a', 'c', 'h', 'i', 'n', 'e', ' ', 'L', 'e', 'a', 'r', 'n', 'i', 'n', 'g')
Expected Output:
Machine Learning
[
2 1 # Q 2:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
t = ('M', 'a', 'c', 'h', 'i', 'n', 'e', ' ', 'L', 'e', 'a', 'r', 'n',
7
'i', 'n', 'g')
8 t = tuple_to_string(t)
9 print(t)
[
2 Machine Learning
]:
Q3:
Define a function named string_to_list_to_tuple.
It will take String as parameter.
First it will convert this String into a List, then it will
convert this List into a Tuple.
Finally it will return the Tuple.
Hints:
Parameter:
pythons_father = 'Guido van Rossum'
Expected Result:
('G', 'u', 'i', 'd', 'o', ' ', 'v', 'a', 'n', ' ', 'R', 'o', 's', 's', 'u', 'm')
[
3 1 # Q 3:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 pythons_father = 'Guido van Rossum'
father_tuple =
8
string_to_list_to_tuple(pythons_father)
9 print(father_tuple)
[
('G', 'u', 'i', 'd', 'o', ' ', 'v', 'a', 'n', ' ', 'R', 'o', 's', 's',
3
'u', 'm')
]:
Q4:
Define a function named how_many_instances.
It will take a Tuple and an index (int) as parameter.
Function will first find the element at that index.
Then it will return the number of instances of the element in
the Tuple.
Hints:
count()
Parameters:
t = (5, 2, 3, 3, 4, 2, 1, 3, 4, 5, 2, 1, 2)
i = 1
how_many_instances(t, i)
Expected Output:
4
[
4 1 # Q 4:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 t = (5, 2, 3, 3, 4, 2, 1, 3, 4, 5, 2, 1, 2)
8 i = 1
9 count = how_many_instances(t, i)
10 print(count)
[
4 4
]:
Q5:
Slicing Operations in Tuples are exactly the same as in Lists
and Strings.
Our Tuple is: tup = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
Find the items in tup, based on slicing and indexing:
[
5 1 # Q 5:
]:
2
3 tup = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
4
5 # 1. items from position 4 (inc) to position 7 (inc)
6 # position 4 -> index 3 (index starts 0)
# position 7 -> index 6 (since it is included the
7
end index will be 7)
8 # ---- your solution here ----
9
10 # 2. the first 5 items
11 # ---- your solution here ----
12
13 # 3. items from position 6 (inc)
14 # ---- your solution here ----
15
16 # 4. all items
17 # ---- your solution here ----
18
19 # 5. 2nd item from the last
20 # ---- your solution here ----
21
22 # 6. last 4 items
23 # -4 , -3 , -2, -1
24 # ---- your solution here ----
25
26 # 7. items from position 2 to 8, with step size 2
27 # ---- your solution here ----
28
29 # 8. all items with step size 3
30 # ---- your solution here ----
31
# 9. items from position 9 to position 3 (exc) but
32
in reverse order
33 # reverse order -> -step size
34 # ---- your solution here ----
35
36 # 10. print the tup in reverse order
37 # ---- your solution here ----
38
# 11. print the tup in reverse order with step size
39
2
40 # ---- your solution here ----
41
# 12. all the elements excluding the last one, with
42
negative index
43 # ---- your solution here ----
[
5 ('d', 'e', 'f', 'g')
]:
('a', 'b', 'c', 'd', 'e')
('f', 'g', 'h', 'i', 'j')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
i
('g', 'h', 'i', 'j')
('b', 'd', 'f', 'h')
('a', 'd', 'g', 'j')
('i', 'h', 'g', 'f', 'e', 'd')
('j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a')
('j', 'h', 'f', 'd', 'b')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')
Q6:
Define a function named change_tuple_ending.
It will take a List as parameter.
The elements of this list will be Tuples.
Each Tuple may be in different length, so the size of the
elements in the list is not fixed.
The function will replace the last item in each Tuple element
in the list with its square.
Hints:
Parameter:
tuple_liste = [(2,5,8), (4,3), (1,7,9,6), (5,)]
Expected Output:
tuple_list: [(2, 5, 8), (4, 3), (1, 7, 9, 6), (5,)]
new_tuple_list: [(4, 25, 64), (16, 9), (1, 49, 81, 36), (25,)]
[
7 1 # Q 7:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 tuple_list = [(2,5,8), (4,3), (1,7,9,6), (5,)]
8
new_tuple_list =
9
replace_tuple_with_sqaure(tuple_list)
10
11 print('tuple_list:', tuple_list)
12 print('new_tuple_list:', new_tuple_list)
[
7 tuple_list: [(2, 5, 8), (4, 3), (1, 7, 9, 6), (5,)]
]:
new_tuple_list: [(4, 25, 64), (16, 9), (1, 49, 81,
36), (25,)]
Q8:
Define a function named movie_characters.
It will take 4 lists as parameters.
4 lists are:
List 1 -> Actor/Actress Name
List 2 -> Movie Title
List 3 -> Release Year
List 4 -> Character Name
It will use the Tuple 1 as the Key and Tuple 2 as the Value
and create a Dictionary.
And it will return this dictionary.
Hints:
zip()
Parameters:
actors = ['Marlon Brando', 'Heath Ledger', 'Natalie Portman',
'Emma Stone']
characters = ['Don Vito Corleone', 'Joker', 'The Swan Queen',
'Mia']
movies = ['The Godfather', 'The Dark Knight', 'Black Swan',
'La La Land']
years = [1972, 2008, 2010, 2016]
Expected Output:
{('Marlon Brando', 'Don Vito Corleone'): ('The Godfather',
1972),
('Heath Ledger', 'Joker'): ('The Dark Knight', 2008),
('Natalie Portman', 'The Swan Queen'): ('Black Swan', 2010),
('Emma Stone', 'Mia'): ('La La Land', 2016)}
[
8 1 # Q 8:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 # lists:
actors = ['Marlon Brando', 'Heath Ledger',
8
'Natalie Portman', 'Emma Stone']
characters = ['Don Vito Corleone', 'Joker', 'The
9
Swan Queen', 'Mia']
movies = ['The Godfather', 'The Dark Knight',
10
'Black Swan', 'La La Land']
11 years = [1972, 2008, 2010, 2016]
12
13 # call the functions
movie_dictionary = movie_characters(actors,
14
characters, movies, years)
15 movie_dictionary
[
{('Marlon Brando', 'Don Vito Corleone'): ('The
8
Godfather', 1972),
]:
('Heath Ledger', 'Joker'): ('The Dark Knight',
2008),
('Natalie Portman', 'The Swan Queen'): ('Black
Swan', 2010),
('Emma Stone', 'Mia'): ('La La Land', 2016)}
Q9:
Define a function named sort_tuple_of_tuples.
It will take a Tuple as the parameter.
The elements inside this Tuple are also Tuples.
Namely, the parameter is a Tuple of Tuples.
Each Tuple element contains 2 items.
The function will sort the main Tuple and return a sorted
Tuple.
Sort Rule is going to be the second element in the inner
Tuples.
Hints:
lambda
Parameter:
tuple_of_tuples = (('a', 12), ('e', 8), ('b', 16), ('c', 22))
Expected Output:
[('e', 8), ('a', 12), ('b', 16), ('c', 22)]
[
9 1 # Q 9:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
tuple_of_tuples = (('a', 12), ('e', 8), ('b', 16), ('c',
7
22))
8
sorted_tuple =
9
sort_tuple_of_tuples(tuple_of_tuples)
10 sorted_tuple
[
9 [('e', 8), ('a', 12), ('b', 16), ('c', 22)]
]:
Q10:
Define a function named number_of_occurences.
It will take Tuple of integers and a number as parameters.
It will return the number of occurrences of that number in the
Tuple.
It will use the function defined in Q4 (how_many_instances)
to find the count. (It will not do the counting itself.)
If the number does not exist in the Tuple, it will return:
'Number not in Tuple!'.
Hints:
function returning a function
raise
index()
Parameters:
tup = (5, 2, 3, 3, 4, 2, 1, 3, 4, 5, 2, 1, 2)
num = 2
Expected Output:
4
[10]
1 # Q 10:
:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 tup = (5, 2, 3, 3, 4, 2, 1, 3, 4, 5, 2, 1, 2)
8 num = 2
9 number_of_occurences(tup, num)
[10]
4
:
OceanofPDF.com
SOLUTIONS - Tuple
Here are the solutions for the quiz for Chapter 16 - Tuple.
SOLUTIONS - Tuple:
S1:
[
1 1 # S 1:
]:
2
3 def create_tuple():
4 # create the tuple
5 tup = (1,2,3,4,5,6,7,8,9,10)
6
7 # add items
8 for i in range(11, 21):
9 # create a new temp tuple via addition
10 # (i,) -> tuple with one element
11 new_tup = tup + (i,)
12
13 # reassign the new tuple
14 tup = new_tup
15
16 return tup
17
18
19 # call the function you defined
20 t = create_tuple()
21 print(t)
[
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
1
17, 18, 19, 20)
]:
S2:
[
2 1 # S 2:
]:
2
3 def tuple_to_string(tup):
4 # join all the items of tup
5 text = ''.join(tup)
6
7 return text
8
9
10 # call the function you defined
t = ('M', 'a', 'c', 'h', 'i', 'n', 'e', ' ', 'L', 'e', 'a', 'r',
11
'n', 'i', 'n', 'g')
12 t = tuple_to_string(t)
13 print(t)
[
2 Machine Learning
]:
S3:
[
3 1 # S 3:
]:
2
3 def string_to_list_to_tuple(text):
4 # first String -> List
5 a_list = list(text)
6
7 # now List -> Tuple
8 tup = tuple(a_list)
9
10 return tup
11
12
13 # call the function you defined
14 pythons_father = 'Guido van Rossum'
father_tuple =
15
string_to_list_to_tuple(pythons_father)
16 print(father_tuple)
[
('G', 'u', 'i', 'd', 'o', ' ', 'v', 'a', 'n', ' ', 'R', 'o', 's',
3
's', 'u', 'm')
]:
S4:
[
4 1 # S 4:
]:
2
3 def how_many_instances(tup, index):
4 # get the element at index
5 el = tup[index]
6
7 # count()
8 return tup.count(el)
9
10
11 # call the function you defined
12 t = (5, 2, 3, 3, 4, 2, 1, 3, 4, 5, 2, 1, 2)
13 i = 1
14 count = how_many_instances(t, i)
15 print(count)
[
4 4
]:
S5:
[
5 1 # S 5:
]:
2
3 tup = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
4
5 # 1. items from position 4 (inc) to position 7 (inc)
6 # position 4 -> index 3 (index starts 0)
# position 7 -> index 6 (since it is included the
7
end index will be 7)
8 print(tup[3:7])
9
10 # 2. the first 5 items
11 print(tup[:5])
12
13 # 3. items from position 6 (inc)
14 print(tup[5:])
15
16 # 4. all items
17 print(tup[::])
18
19 # 5. 2nd item from the last
20 print(tup[-2])
21
22 # 6. last 4 items
23 # -4 , -3 , -2, -1
24 print(tup[-4:])
25
26 # 7. items from position 2 to 8, with step size 2
27 print(tup[1:8:2])
28
29 # 8. all items with step size 3
30 print(tup[::3])
31
# 9. items from position 9 to position 3 (exc) but
32
in reverse order
33 # reverse order -> -step size
34 print(tup[8:2:-1])
35
36 # 10. print the tup in reverse order
37 print(tup[::-1])
38
# 11. print the tup in reverse order with step size
39
2
40 print(tup[::-2])
41
# 12. all the elements excluding the last one, with
42
negative index
43 print(tup[:-1])
[
5 ('d', 'e', 'f', 'g')
]:
('a', 'b', 'c', 'd', 'e')
('f', 'g', 'h', 'i', 'j')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
i
('g', 'h', 'i', 'j')
('b', 'd', 'f', 'h')
('a', 'd', 'g', 'j')
('i', 'h', 'g', 'f', 'e', 'd')
('j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a')
('j', 'h', 'f', 'd', 'b')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')
S6:
[
6 1 # S 6:
]:
2
3 def change_tuple_ending(a_list):
4
5 for index, tup in enumerate(a_list):
6 # get the tuple items up to the last one
7 items_up_to_last = tup[:-1]
8
9 # last element -> square it
10 last_item = tup[-1]**2
11
12 # concat them -> override tup
13 tup = items_up_to_last + (last_item,)
14
# mutate the list -> change the current
15
element
16 a_list[index] = tup
17
18
19 # call the function you defined
20 tuple_list = [(2,5,8), (4,3), (1,7,9,6), (5,)]
print('tuple_list before passing to function:',
21
tuple_list)
22
23 change_tuple_ending(tuple_list)
print('tuple_list after passing to function:',
24
tuple_list)
[
tuple_list before passing to function: [(2, 5, 8),
6
(4, 3), (1, 7, 9, 6), (5,)]
]:
tuple_list after passing to function: [(2, 5, 64), (4,
9), (1, 7, 9, 36), (25,)]
S7:
[ 1 # S 7:
7
]:
2
3 def replace_tuple_with_sqaure(a_list):
4 # new_list
5 new_list = a_list.copy()
6
7 for index, tup in enumerate(new_list):
8 new_tup = tuple()
9
10 # loop over the tup items
11 for t in tup:
12 new_tup += (t**2,)
13
14 # mutate the list
15 new_list[index] = new_tup
16
17 return new_list
18
19
20 # call the function you defined
21 tuple_list = [(2,5,8), (4,3), (1,7,9,6), (5,)]
22
new_tuple_list =
23
replace_tuple_with_sqaure(tuple_list)
24
25 print('tuple_list:', tuple_list)
26 print('new_tuple_list:', new_tuple_list)
[
7 tuple_list: [(2, 5, 8), (4, 3), (1, 7, 9, 6), (5,)]
]:
new_tuple_list: [(4, 25, 64), (16, 9), (1, 49, 81,
36), (25,)]
S8:
[
8 1 # S 8:
]:
2
def movie_characters(actors, characters, movies,
3
years):
4 # create an empty dict
5 dictionary = {}
6
7 # loop over the 4 lists -> zip()
for actor, character, movie, year in zip(actors,
8
characters, movies, years):
9 # create and add tuples into dictionary
dictionary[(actor, character)] = (movie,
10
year)
11
12 return dictionary
13
14
15 # call the function you defined
16 # lists:
actors = ['Marlon Brando', 'Heath Ledger',
17
'Natalie Portman', 'Emma Stone']
characters = ['Don Vito Corleone', 'Joker', 'The
18
Swan Queen', 'Mia']
movies = ['The Godfather', 'The Dark Knight',
19
'Black Swan', 'La La Land']
20 years = [1972, 2008, 2010, 2016]
21
22 # call the functions
movie_dictionary = movie_characters(actors,
23
characters, movies, years)
24 movie_dictionary
[
{('Marlon Brando', 'Don Vito Corleone'): ('The
8
Godfather', 1972),
]:
('Heath Ledger', 'Joker'): ('The Dark Knight',
2008),
('Natalie Portman', 'The Swan Queen'): ('Black
Swan', 2010),
('Emma Stone', 'Mia'): ('La La Land', 2016)}
S9:
[
9 1 # S 9:
]:
2
3 def sort_tuple_of_tuples(tuple_of_tuples):
4 # let's say we have a tuple x
5 # x -> ('c', 22)
6 # x[1] -> 22
return sorted(tuple_of_tuples, key = lambda
7
x: x[1])
8
9
10 # call the function you defined
tuple_of_tuples = (('a', 12), ('e', 8), ('b', 16), ('c',
11
22))
12
sorted_tuple =
13
sort_tuple_of_tuples(tuple_of_tuples)
14 sorted_tuple
[
9 [('e', 8), ('a', 12), ('b', 16), ('c', 22)]
]:
S10:
[1
1 # S 10:
0]:
2
3 def number_of_occurences(tup, num):
4
5 # check if the num is in tup
6 if not num in tup:
7 raise Exception('Number not in Tuple!')
8 else:
9
10 # call the function in Q4
11 # how_many_instances(tup, index)
12
13 # tuple.index(el) -> first found index
14 index = tup.index(num)
15
16 # return the function
17 return how_many_instances(tup, index)
18
19
20 # call the function you defined
21 tup = (5, 2, 3, 3, 4, 2, 1, 3, 4, 5, 2, 1, 2)
22 num = 2
23 number_of_occurences(tup, num)
[1
4
0]:
OceanofPDF.com
17. Set
Set Creation
So far, we covered Lists, Dictionaries and Tuples. Now we will
learn another built-in data type in Python which are the Sets.
A Set:
[
5 1 # set({}) -> empty set
]:
2 an_empty_set = set({})
3 type(an_empty_set)
[
5 set
]:
[
6 1 # a set with elements
]:
2 letters = set({'A', 'B', 'C', 'D'})
3 letters
[
6 {'A', 'B', 'C', 'D'}
]:
Sets cannot have two items with the same value. Even if you
pass duplicate items, Python will only use one of them and ignore
the rest.
[
7 1 # Same element in a set -> only once
]:
2 grades = ['A', 'A', 'B', 'C', 'B', 'C']
3 print(grades)
[
7 ['A', 'A', 'B', 'C', 'B', 'C']
]:
In cell 8, the grades list have duplicate items in it. But after
we convert it to a set, the duplicates items are ignored. Every
element occurs only once in the set.
[
9 1 # String -> Set
]:
2 sentence = 'sentence'
3 set(sentence)
[
9 {'c', 'e', 'n', 's', 't'}
]:
In cell 9, we convert a string to a set. And in the output, you
can see a set with the unique items.
Since set object does not have index structure, you cannot
access its elements with indices. Let’s try and see what happens:
[1
1 # try to access the elements of a Set with index
0]:
2 grades_set[0]
[1
TypeError: 'set' object is not subscriptable
0]:
Set Methods
In Python, we have a set of built-in methods that you can use
on Sets. Let’s see some of them.
intersection() :
Returns a set which contains the items that exist in both set A ,
and set B . It’s syntax is: A.intersection(B) .
[1
1 grades = ['A', 'A', 'B', 'C', 'B', 'C']
1]:
2 grades = set(grades)
3 grades
[1
{'A', 'B', 'C'}
1]:
[1
1 letters = ['A', 'L', 'T', 'B', 'F']
2]:
2 letters = set(letters)
3 letters
[1
{'A', 'B', 'F', 'L', 'T'}
2]:
[1
1 # A.intersection(B)
3]:
2 grades.intersection(letters)
[1
{'A', 'B'}
3]:
In cell 13, we get the common items which exist in both sets,
grades and letters . The syntax is:
grades.intersection(letters) . And the intersection is the set of
{'A', 'B'} .
union() :
Returns a set containing the union of sets, A and B . It’s syntax
is: A.union(B) .
[1
1 # get the union of grades and letters
4]:
2 grades.union(letters)
[1
{'A', 'B', 'C', 'F', 'L', 'T'}
4]:
In cell 14, we get the union of the sets grades and letters .
And the result is the set of {'A', 'B', 'C', 'F', 'L', 'T'} .
difference() :
Returns a set containing the difference between two sets, A
and B . It’s syntax is: A.difference(B) .
[1
1 # get the difference of grades from letters
5]:
2 grades.difference(letters)
[1
{'C'}
5]:
[1
1 # get the difference of letters from grades
6]:
2 letters.difference(grades)
[1
{'F', 'L', 'T'}
6]:
issubset():
Checks if a set ( A ) is the subset of the other one ( B ). It’s
syntax is: A.issubset(B) . If all elements of A are in B , then we
say ‘A is a subset of B’.
[1
1 # if grades is a subset of letters
7]:
2 grades.issubset(letters)
[1
False
7]:
In cell 17, we check if grades is a subset of letters as:
grades.issubset(letters) . And it returns False . Because the
letter ‘C’ exists in grades but it does not exist in letters . So
grades is not a subset of letters .
[1
1 # define a new set
8]:
2 grades_small = {'A', 'B'}
[1
1 # if grades_small is a subset of letters
9]:
2 grades_small.issubset(letters)
[1
True
9]:
[2
1 # if letters is a superset of grades_small
2]:
2 letters.issuperset(grades_small)
[2
True
2]:
Set Operations
add() :
Adds an element to the set. It’s syntax is: set.add(element) .
Let’s add some elements to the grades set:
[2
1 # add 'F' to grades
4]:
2 grades.add('F')
3 grades
[2
{'A', 'B', 'C', 'F'}
4]:
[2
1 # add 'T' to grades
5]:
2 grades.add('T')
3 grades
[2
{'A', 'B', 'C', 'F', 'T'}
5]:
In cell 24, we add the element of ‘F’ to the grades set. And
in cell 25, we add the letter of ‘T’ . Let’s see what happens if we
try to add the same element:
[2
1 # Python will ignore the same element
6]:
2 grades.add('F')
3 grades
[2
{'A', 'B', 'C', 'F', 'T'}
6]:
As you see in cell 26, Python ignores the letter ‘F’ which we
try to add. Because this element already exists in the set and sets
cannot include duplicate elements.
remove() :
Removes the specified element from the set. It’s syntax is:
set.remove(element) .
[2
1 # set.remove(<element>)
7]:
2 grades.remove('C')
3 grades
[2
{'A', 'B', 'F', 'T'}
7]:
In cell 27, we remove the element ‘C’ from the grades set.
Let’s remove one more element:
[2
1 # remove 'F'
8]:
2 grades.remove('F')
3 grades
[2
{'A', 'B', 'T'}
8]:
In cell 29, we loop over the set named letters . We call the
current element as letter at each iteration and print it in line 2.
The loop structure is the same as we did with other types. Let’s
loop over the grades set too:
[3
1 for g in grades:
0]:
2 print(g)
[3
B
0]:
T
A
Assignment in Sets:
In sets, assignment is aliasing. Remember, aliasing is nothing
but giving another name to the same object.
[3
1 # create a set
1]:
2 a = {2, 5, 7}
3
4 # assignment -> aliasing
5 b = a
6
7 print('a:', a)
8 print('b:', b)
[3
a: {2, 5, 7}
1]:
b: {2, 5, 7}
[3
1 # identity check
4]:
2 a is b
[3
True
4]:
[3
1 # equality check
5]:
2 a == b
[3
True
5]:
[3
1 a.issubset(b)
6]:
[3
True
6]:
[3
1 a.issuperset(b)
7]:
[3
True
7]:
Now let’s add a new element to a and see that it has been also
added to the variable b :
[3
1 # add a new element to a
8]:
2 a.add('YYYY')
[3
1 print('a:', a)
9]:
2 print('b:', b)
[3
a: {2, 'NEW', 5, 'YYYY', 7}
9]:
b: {2, 'NEW', 5, 'YYYY', 7}
update() :
Updates the set with another set or any other type that can be
converted to a set. With update() method, you can add either a
single element or multiple elements.
[4
1 # define a set of fruits
0]:
2 fruits = {'Apple', 'Orange', 'Melon'}
3 fruits
[4
{'Apple', 'Melon', 'Orange'}
0]:
[4
1 # define a list
1]:
2 more_fruits = ['Cherry', 'Banana']
3 more_fruits
[4
['Cherry', 'Banana']
1]:
OceanofPDF.com
QUIZ - Set
Now it’s time to solve the quiz for this chapter. You can
download the quiz file, QUIZ_Set.ipynb, from the Github
Repository of this book. It is in the folder named 17_Set. Here are
the questions for this chapter:
QUIZ - Set:
Q1:
Define a function named create_set_and_add.
It will take a list as parameter.
First it will create a set containing the elements below:
"Apple", "Banana", "Cherry", "Orange"
Then it will the items of the parameter list into this set.
And finally it will return the final set.
Hints:
{}
add()
Parameter:
list_to_add = ['Cranberry', 'Grape', 'Pineapple', 'Mango']
Expected Output:
(the order of items may change, because sets are unordered
collections)
{'Mango', 'Apple', 'Cranberry', 'Banana', 'Cherry', 'Grape',
'Pineapple', 'Orange'}
[
1 1 # Q 1:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
list_to_add = ['Cranberry', 'Grape', 'Pineapple',
7
'Mango']
8 all_fruits = create_set_and_add(list_to_add)
9 print(all_fruits)
[
{'Pineapple', 'Apple', 'Mango', 'Orange', 'Grape',
1
'Cherry', 'Cranberry', 'Banana'}
]:
Q2:
Define a function named create_set_and_add_all_at_once.
It will take a list as parameter.
First it will create a set containing the elements below:
"Apple", "Banana", "Cherry", "Orange"
Then it will the items of the parameter list into this set (all at
once).
And finally it will return the final set.
Hints:
set()
update()
Parameter:
list_to_add = ['Cranberry', 'Grape', 'Pineapple', 'Mango']
Expected Output:
(the order of items may change, because sets are unordered
collections)
{'Mango', 'Apple', 'Cranberry', 'Banana', 'Cherry', 'Grape',
'Pineapple', 'Orange'}
[2
1 # Q 2:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
list_to_add = ['Cranberry', 'Grape', 'Pineapple',
7
'Mango', 'Another Fruit']
all_fruits =
8
create_set_and_add_all_at_once(list_to_add)
9 print(all_fruits)
[2 {'Pineapple', 'Apple', 'Mango', 'Another Fruit',
]: 'Orange', 'Grape', 'Cherry', 'Cranberry', 'Banana'}
Q3:
Define a function named same_elements.
It will take two Sets as parameters.
The function will return the same elements (intersection) of
both sets in a List.
And this list is going to be sorted in ascending order.
Hints:
no loops
intersection()
sorted()
Parameters:
set_1 = {10, 20, 30, 40, 50, 60}
set_2 = {20, 40, 60, 80, 90, 100}
Expected Output:
[20, 40, 60]
[
3 1 # Q 3:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 set_1 = {10, 20, 30, 40, 50, 60}
8 set_2 = {20, 40, 60, 80, 90, 100}
9 intersection = same_elements(set_1, set_2)
10 print(intersection)
[
3 [20, 40, 60]
]:
Q4:
Define a function named all_elements.
It will take two Sets as parameters.
The function will return all the elements (union) of both sets
in a List.
And this list is going to be sorted in ascending order.
Hints:
no loops
union()
sort()
Parameters:
set_1 = {10, 20, 30, 40, 50, 60}
set_2 = {20, 40, 60, 80, 90, 100}
Expected Output:
[100, 90, 80, 60, 50, 40, 30, 20, 10]
[
4 1 # Q 4:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 set_1 = {10, 20, 30, 40, 50, 60}
8 set_2 = {20, 40, 60, 80, 90, 100}
9 union = all_elements(set_1, set_2)
10 print(union)
[
4 [10, 20, 30, 40, 50, 60, 80, 90, 100]
]:
Q5:
Define a function named get_difference.
It will take two lists (list_1, list_2) as parameters.
The function will return the Set of elements which are in
list_1 but not in list_2.
Hints:
no loops
set()
difference()
Parameters:
l_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
l_2 = [2, 4, 6, 8]
Expected Output:
{1, 3, 5, 7, 9}
[
5 1 # Q 5:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 l_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
8 l_2 = [2, 4, 6, 8]
9 diff = get_difference(l_1, l_2)
10 print(diff)
[
5 {1, 3, 5, 7, 9}
]:
Q6:
Define a function named is_completely_different.
It will check if two sets (parameters) are completely different
or not.
Completely different means they have no elements in
common.
The function will also check if the two parameters are Set or
not.
If any of them is not a Set it will raise an Exception as
"Parameters must be of Set type."
Hints:
no loops
completely different: isdisjoint()
isinstance()
raise Exception()
Parameters:
set_1 = {20, 10, 40, 30, 50}
set_2 = {60, 80, 70, 100, 90}
Expected Output:
'They are completely different.'
----------------------------------------
Parameters:
set_1 = {20, 10, 40, 30, 50, 60}
set_2 = {60, 80, 70, 90, 40, 10}
Expected Output:
'They are not completely different.'
[
6 1 # Q 6:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 set_1 = {20, 10, 40, 30, 50}
8 set_2 = {60, 80, 70, 100, 90}
9 print(is_completely_different(set_1, set_2))
10
11 set_1 = {20, 10, 40, 30, 50, 60}
12 set_2 = {60, 80, 70, 90, 40, 10}
13 print(is_completely_different(set_1, set_2))
[
6 They are completely different.
]:
They are not completely different.
Q7:
Define a function named is_completely_different_2.
It will check if two sets (parameters) are completely different
or not.
Completely different means they have no elements in
common.
Parameters:
set_1 = {20, 10, 40, 30, 50}
set_2 = {60, 80, 70, 100, 90}
Expected Output:
'They are completely different.'
----------------------------------------
Parameters:
set_1 = {20, 10, 40, 30, 50, 60}
set_2 = {60, 80, 70, 90, 40, 10}
Expected Output:
'They are not completely different: {40, 10, 60}'
[
7 1 # Q 7:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 set_1 = {20, 10, 40, 30, 50}
8 set_2 = {60, 80, 70, 100, 90}
9 print(is_completely_different_2(set_1, set_2))
10
11 set_1 = {20, 10, 40, 30, 50, 60}
12 set_2 = {60, 80, 70, 90, 40, 10}
13 print(is_completely_different_2(set_1, set_2))
[
7 They are completely different.
]:
They are not completely different: {40, 10, 60}
Q8:
Define a function named copy_and_clear.
It will take a Set as parameter.
And it will clear (remove all elements) this Set but copy its
elements into another Set.
Finally it will return this new Set.
Hints:
no loops
copy()
clear()
remember Pass by Reference
Parameter:
set1 = {'A', 'B', 'C', 'D', 'E'}
Expected Output:
(the order of items may change, because sets are unordered
collections)
before the function call -> set1: {'E', 'C', 'D', 'B', 'A'}
after the function call -> set1: set()
set1_copy: {'D', 'B', 'E', 'A', 'C'}
[
8 1 # Q 8:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 set1 = {'A', 'B', 'C', 'D', 'E'}
8 print('before the function call -> set1:', set1)
9 set1_copy = copy_and_clear(set1)
10 print('after the function call -> set1:', set1)
11 print('set1_copy:', set1_copy)
[
before the function call -> set1: {'E', 'C', 'D', 'A',
8
'B'}
]:
after the function call -> set1: set()
set1_copy: {'E', 'C', 'A', 'B', 'D'}
Q9:
Define a function named remove_common_elements.
It will take two Sets (set_1, set_2) as parameters.
The function will remove the elements of set_1 which are
also in set_2.
And this operation will change the original sets.
In other words, it will mutate the parameter.
Hints:
no loops
difference_update()
Parameters:
set_1 = {'a', 'b', 'c', 'd', 'e', 'f'}
set_2 = {'d', 'b', 'e', 'f', 'h', 'g'}
Expected Output:
(the order of items may change, because sets are unordered
collections)
before the function -> set_1: {'c', 'a', 'b', 'f', 'd', 'e'}
after the function -> set_1: {'c', 'a'}
[
9 1 # Q 9:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 set_1 = {'a', 'b', 'c', 'd', 'e', 'f'}
8 set_2 = {'d', 'b', 'e', 'f', 'h', 'g'}
9 print('before the function -> set_1:', set_1)
10 remove_common_elements(set_1, set_2)
11 print('after the function -> set_1:', set_1)
[
before the function -> set_1: {'d', 'b', 'c', 'a', 'e',
9
'f'}
]:
after the function -> set_1: {'c', 'a'}
Q10:
Define a function named which_one_is_superset.
It will take two Sets (set_1, set_2) as parameters.
If set_1 is the superset of set_2 it will return: "set_1: {....} is
superset of set_2: {....}"
It the opposite is true it will return: "set_2: {....} is superset
of set_1: {....}"
Here "{....}" are set elements.
Hints:
no loop
issuperset()
OceanofPDF.com
SOLUTIONS - Set
Here are the solutions for the quiz for Chapter 17 - Set.
SOLUTIONS - Set:
S1:
[
1 1 # S 1:
]:
2
3 def create_set_and_add(a_list):
4 # first create the set
fruits = {"Apple", "Banana", "Cherry",
5
"Orange"}
6
7 # loop over the list -> add elements
8 for e in a_list:
9 fruits.add(e)
10
11 return fruits
12
13
14 # call the function you defined
list_to_add = ['Cranberry', 'Grape', 'Pineapple',
15
'Mango']
16 all_fruits = create_set_and_add(list_to_add)
17 print(all_fruits)
[
{'Pineapple', 'Apple', 'Mango', 'Orange', 'Grape',
1
'Cherry', 'Cranberry', 'Banana'}
]:
S2:
[2
1 # S 2:
]:
2
3 def create_set_and_add_all_at_once(a_list):
4 # first create the set
fruits = {"Apple", "Banana", "Cherry",
5
"Orange"}
6
7 # add all elements -> update()
8 fruits.update(a_list)
9
10 return fruits
11
12
13 # call the function you defined
list_to_add = ['Cranberry', 'Grape', 'Pineapple',
14
'Mango', 'Another Fruit']
all_fruits =
15
create_set_and_add_all_at_once(list_to_add)
16 print(all_fruits)
[2 {'Pineapple', 'Apple', 'Mango', 'Another Fruit',
]: 'Orange', 'Grape', 'Cherry', 'Cranberry', 'Banana'}
S3:
[
3 1 # S 3:
]:
2
3 def same_elements(set_1, set_2):
4 # get intersection -> common
5 # set
6 intersection = set_1.intersection(set_2)
7
8 # pass the set -> sorted() -> return a list
9 intersection = sorted(intersection)
10
11 return intersection
12
13
14 # call the function you defined
15 set_1 = {10, 20, 30, 40, 50, 60}
16 set_2 = {20, 40, 60, 80, 90, 100}
17 intersection = same_elements(set_1, set_2)
18 print(intersection)
[
3 [20, 40, 60]
]:
S4:
[
4 1 # S 4:
]:
2
3 def all_elements(set_1, set_2):
4 # get the union -> all elements
5 # set
6 union = set_1.union(set_2)
7
8 # sort() is a list function
9 union_list = list(union)
10
11 # pass the set -> sort() -> return a list
12 # sort() -> mutates
13 union_list.sort()
14
15 return union_list
16
17
18 # call the function you defined
19 set_1 = {10, 20, 30, 40, 50, 60}
20 set_2 = {20, 40, 60, 80, 90, 100}
21 union = all_elements(set_1, set_2)
22 print(union)
[
4 [10, 20, 30, 40, 50, 60, 80, 90, 100]
]:
S5:
[
5 1 # S 5:
]:
2
3 def get_difference(list_1, list_2):
4 # first -> convert Lists into Sets
5 set_1 = set(list_1)
6 set_2 = set(list_2)
7
8 # get difference
9 diff = set_1.difference(set_2)
10
11 return diff
12
13
14 # call the function you defined
15 l_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
16 l_2 = [2, 4, 6, 8]
17 diff = get_difference(l_1, l_2)
18 print(diff)
[
5 {1, 3, 5, 7, 9}
]:
S6:
[
6 1 # S 6:
]:
2
3 def is_completely_different(set_1, set_2):
4 # check if both are Set type
if not isinstance(set_1, set) or not
5
isinstance(set_2, set):
raise Exception("Parameters must be of Set
6
type.")
7 # they are both Set type
8 else:
9 # check if completely different
10 if set_1.isdisjoint(set_2):
11 return 'They are completely different.'
12 else:
return 'They are not completely
13
different.'
14
15
16 # call the function you defined
17 set_1 = {20, 10, 40, 30, 50}
18 set_2 = {60, 80, 70, 100, 90}
19 print(is_completely_different(set_1, set_2))
20
21 set_1 = {20, 10, 40, 30, 50, 60}
22 set_2 = {60, 80, 70, 90, 40, 10}
23 print(is_completely_different(set_1, set_2))
[
6 They are completely different.
]:
They are not completely different.
S7:
[
7 1 # S 7:
]:
2
3 def is_completely_different_2(set_1, set_2):
4 # check if both are Set type
if not isinstance(set_1, set) or not
5
isinstance(set_2, set):
raise Exception("Parameters must be of Set
6
type.")
7 # they are both Set type
8 else:
9 # check if completely different
10 if set_1.isdisjoint(set_2):
11 return 'They are completely different.'
12 else:
13 # set
common_elements =
14
set_1.intersection(set_2)
15
return 'They are not completely different:
16
' + str(common_elements)
17
18
19 # call the function you defined
20 set_1 = {20, 10, 40, 30, 50}
21 set_2 = {60, 80, 70, 100, 90}
22 print(is_completely_different_2(set_1, set_2))
23
24 set_1 = {20, 10, 40, 30, 50, 60}
25 set_2 = {60, 80, 70, 90, 40, 10}
26 print(is_completely_different_2(set_1, set_2))
[
7 They are completely different.
]:
They are not completely different: {40, 10, 60}
S8:
[
8 1 # S 8:
]:
2
3 # Sets pass by reference
4
5 def copy_and_clear(a_set):
6 # copy the set
7 a_set_copy = a_set.copy()
8
9 # clear the original set
10 a_set.clear()
11
12 return a_set_copy
13
14
15 # call the function you defined
16 set1 = {'A', 'B', 'C', 'D', 'E'}
17 print('before the function call -> set1:', set1)
18 set1_copy = copy_and_clear(set1)
19 print('after the function call -> set1:', set1)
20 print('set1_copy:', set1_copy)
[
before the function call -> set1: {'E', 'C', 'D', 'A',
8
'B'}
]:
after the function call -> set1: set()
set1_copy: {'E', 'C', 'A', 'B', 'D'}
S9:
[
9 1 # S 9:
]:
2
3 def remove_common_elements(set_1, set_2):
4 # update the parameter in place
# difference_update() removes the common
5
elements
6 set_1.difference_update(set_2)
7
8
9 # call the function you defined
10 set_1 = {'a', 'b', 'c', 'd', 'e', 'f'}
11 set_2 = {'d', 'b', 'e', 'f', 'h', 'g'}
12 print('before the function -> set_1:', set_1)
13 remove_common_elements(set_1, set_2)
14 print('after the function -> set_1:', set_1)
[
before the function -> set_1: {'d', 'b', 'c', 'a', 'e',
9
'f'}
]:
after the function -> set_1: {'c', 'a'}
S10:
[1
1 # S 10:
0]:
2
3 def which_one_is_superset(set_1, set_2):
4 if set_1.issuperset(set_2):
return "set_1: {0} is superset of set_2:
5
{1}".format(set_1, set_2)
6 else:
return "set_2: {0} is superset of set_1:
7
{1}.".format(set_2, set_1)
8
9
10 # call the function you defined
11 set_1 = {'a', 'b', 'c', 'd'}
12 set_2 = {'d', 'b', 'e', 'f', 'a', 'c'}
13 print(which_one_is_superset(set_1, set_2))
14
15 set_1 = {'d', 'b', 'e', 'f', 'a', 'c'}
16 set_2 = {'a', 'b', 'c', 'd'}
17 print(which_one_is_superset(set_1, set_2))
[1 set_2: {'d', 'b', 'c', 'a', 'e', 'f'} is superset of
0]: set_1: {'b', 'd', 'a', 'c'}.
set_1: {'d', 'b', 'c', 'a', 'e', 'f'} is superset of
set_2: {'b', 'd', 'a', 'c'}
OceanofPDF.com
18. Comprehension
What is Comprehension?
Comprehension is an easy way to loop over sequences. It offers
a shorter syntax for almost any operation you do with the loops.
This chapter is devoted to Comprehensions. You will learn List,
Dictionary and Set Comprehensions, how to set Nested
Comprehensions and how to use Conditional Statements in
Comprehensions.
List Comprehension:
Let’s assume, you want to create a new list based on the values
of an existing list. You can achieve this with loops and slicing in
some cases. But there is a better way in Python: List
Comprehension. In the image below you can see an example for
the syntax of a list comprehension:
Example:
Create a list that contains the squares of numbers from 1 to 10.
First, we will solve this question with the classical method, a for
loop. Then we will do the same operation with a comprehension.
[
1 1 # classical method -> loop
]:
2
3 # define an empty list
4 squares = []
5
6 # loop over the range
7 for i in range(1, 11):
8 squares.append(i**2)
9
10 print(squares)
[
1 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
]:
Example:
Create a list that contains the cubes of numbers from 1 to 7.
[
3 1 # classical method - loops
]:
2
3 # define an empty list
4 cubes = list()
5
6 # loop over the range
7 for k in range(1, 7):
8 cubes.append(k**3)
9
10 cubes
[
3 [1, 8, 27, 64, 125, 216]
]:
Example:
Convert the letters of 'lorem ipsum' to uppercases and
append them to a list.
[
5 1 # define the string variable
]:
2 text = 'lorem ipsum'
[
6 1 # classical method -> loops
]:
2
3 # define an empty list
4 capitals = []
5
6 # loop over the letters
7 for letter in text:
8 capitals.append(letter.upper())
9
10 capitals
[
6 ['L', 'O', 'R', 'E', 'M', ' ', 'I', 'P', 'S', 'U', 'M']
]:
Dictionary Comprehension:
Dictionary Comprehension is very similar to List
Comprehension. The main difference is that, it returns a
Dictionary instead of a List. Dictionary Comprehensions are
defined with a pair curly brackets {} .
Example:
We have two lists: Programming Languages and Release Years:
In cell 9, we loop over both lists with the help of the zip()
function as: zip(languages, years) . The items that zip()
function returns are tuples. And we deconstruct these tuples into
two variables: lang and year . Then we add them into the
lang_year dict as: lang_year[lang] = year .
[1
1 # Comprehension
0]:
2 lang_year_comp = {lang: year
3 for lang, year in zip(languages, years)}
4
5 lang_year_comp
[1 {'Python': 1989, 'Java': 1995, 'JavaScript': 1995,
0]: 'C#': 2000}
Set Comprehension:
The last type of comprehension which we cover is the Set
Comprehension. Its syntax is very similar to the List
Comprehension. The difference is that, it returns a Set instead of a
List. Set Comprehensions are defined with a pair curly brackets {} .
Example:
Let's create a set containing the letters of 'pepper'.
[1
1 # define the variable
1]:
2 pepper = 'pepper'
[1
1 # classical method -> loop
2]:
2
3 # define an empty set
4 letters = set()
5
6 # loop over the items
7 for l in pepper:
8 letters.add(l)
9
10 letters
[1
{'e', 'p', 'r'}
2]:
In cell 12, we loop over the elements of the pepper string and
we add them to the letters set. Let’s do it with a Set
Comprehension:
[1
1 # comprehension
3]:
2 letters_comp = {l for l in pepper}
3 letters_comp
4 letters
[1
{'e', 'p', 'r'}
3]:
Nested Comprehensions
We use nested comprehensions like we use nested loops. The
idea is almost the same. Let’s see with examples:
Example:
We have two lists and we want to create a new list of Tuples.
The Tuple elements will be the pairs of the items from these lists.
Here are the lists and the expected output:
[1
1 # lists
4]:
2 letters = ['A', 'B']
3 numbers = [1, 2, 3]
[
('A', 1),
('A', 2),
('A', 3),
...
]
Let’s start with the classical method, the loops. We will set
nested loops over the lists:
[1
1 # classical method -> loops
5]:
2
3 # define an empty list
4 result = []
5
6 # nested loops on the lists
7 for letter in letters:
8 for number in numbers:
9 tup = (letter, number)
10 result.append(tup)
11
12 print(result)
[1 [('A', 1), ('A', 2), ('A', 3), ('B', 1), ('B', 2), ('B',
5]: 3)]
In cell 15, the outer for loop is on letters and the inner one is
on numbers . We create a tuple of a letter and a number in line
9. Then we append this tuple to the result list.
[1
1 # nested comprehension
6]:
2 result_comp = [(letter, number)
3 for letter in letters
4 for number in numbers]
5 result_comp
[1
[('A', 1), ('A', 2), ('A', 3), ('B', 1), ('B', 2), ('B', 3)]
6]:
Example:
We want to create a dictionary for numbers from 1 to 10. The
number will be the key and a list of all the numbers which are less
than or equal to that number will be the value. Here is the
dictionary:
smallers = {
1: [1],
2: [1, 2],
3: [1, 2, 3],
4: [1, 2, 3, 4],
5: [1, 2, 3, 4, 5],
6: [1, 2, 3, 4, 5, 6],
7: [1, 2, 3, 4, 5, 6, 7],
8: [1, 2, 3, 4, 5, 6, 7, 8],
9: [1, 2, 3, 4, 5, 6, 7, 8, 9],
10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}
Example:
Let's find the odd numbers from 1 to 20.
[1
1 # classical method -> loop
9]:
2
3 # empty list
4 odds = []
5
6 for i in range(1, 21):
7 if i % 2 == 1:
8 odds.append(i)
9
10 odds
[1
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
9]:
Rule of Thumb:
if-else structure in Comprehension is exactly the same as in
loops.
[2
1 # comprehension
0]:
2 odds_comp = [i
3 for i in range(1, 21)
4 if i % 2 == 1]
5
6 odds_comp
[2
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
0]:
Example:
We want to create a dictionary for numbers from 2 to 20. The
number will be the key and the value will be a list. This list is
going to be the positive factors of that number. Here is the expected
output:
factors = {
2: [2],
3: [3],
4: [2, 4],
5: [5],
6: [2, 3, 6]
7: [7],
8: [2, 4, 8],
9: [3, 9],
10: [2, 5, 10],
...
}
In cell 21, we set two loops. The first one iterates over the
range(2, 21) and the inner one iterates over range(2, i+1) . The
loop variable of the outer one is i , and the loop variable of the
inner one is j . In line 9, we check if i is divisible by j . If it is
divisible, then we check if i exists in the factors dictionary. If it’s
the first time that i occurs in the dictionary we add it as:
factors[i] = [j] . Be careful, here the value is a list: [j] . And if the
number i already exists in the dict, then we update the value list in
line 14 as: factors[i].append(j) .
[2
1 # comprehension
2]:
2 factors_comp = {
3 i: [j for j in range(2, i+1) if i % j == 0]
4 for i in range(2, 21)
5 }
6
7 factors_comp
[2
{2: [2],
2]:
3: [3],
4: [2, 4],
5: [5],
6: [2, 3, 6],
7: [7],
8: [2, 4, 8],
9: [3, 9],
10: [2, 5, 10],
…
}
In cell 22, you see the dictionary comprehension for the same
question. We write it in separate lines to make it easy to read. Here
is the comprehension:
{
i: [j for j in range(2, i+1) if i % j == 0]
for i in range(2, 21)
}
Here the output of the outer comprehension is: i: [j for j in
range(2, i+1) if i % j == 0] . And the collection is: for i in
range(2, 21) .
The inner comprehension is a List Comprehension which is: [j
for j in range(2, i+1) if i % j == 0] . Here, j is the output, for
j in range(2, i+1) is the collection and if i % j == 0 is the
condition.
Example:
We want to create a dictionary for even numbers from 2 to 20.
The number will be the key and the value will be a list. This list is
going to be the positive factors of that number. Here is the expected
output:
factors_of_evens = {
2: [2],
4: [2, 4],
6: [2, 3, 6],
8: [2, 4, 8],
10: [2, 5, 10],
12: [2, 3, 4, 6, 12],
14: [2, 7, 14],
16: [2, 4, 8, 16],
18: [2, 3, 6, 9, 18],
20: [2, 4, 5, 10, 20]
}
Important Note:
Try to use Comprehensions instead of loops whenever possible,
because they provide better performance. And once you get used to
them, they are cleaner and easier to read and maintain.
OceanofPDF.com
QUIZ - Comprehension
Now it’s time to solve the quiz for this chapter. You can
download the quiz file, QUIZ_Comprehension.ipynb, from the
Github Repository of this book. It is in the folder named
18_Comprehension. Here are the questions for this chapter:
QUIZ - Comprehension:
Q1:
Define two functions named get_squares and
get_squares_comp.
They will give the list of squares of numbers from 1 to 10
(exc.).
The first function will use for loops and the second one will
use Comprehension.
Expected Output:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[
1 1 # Q 1:
]:
2
3 # for loop
4 # ---- your solution here ----
5
6
7 # call the function you defined
8 squares = get_squares()
9 print(squares)
10
11
12 # comprehension
13 # ---- your solution here ----
14
15
16 # call the function you defined
17 sqrs = get_squares_comp()
18 print(sqrs)
[
1 [1, 4, 9, 16, 25, 36, 49, 64, 81]
]:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
Q2:
Define two functions named get_sentences and
get_sentences_comp.
They will take a paragraph string as parameter.
And they will return the sentences as list.
The first function will use for loops and second one will use
Comprehension.
paragraph = "Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Ut enim ad minim veniam, quis nostrud
exercitation ullamco. Duis aute irure dolor in reprehenderit
in voluptate velit esse. Excepteur sint occaecat cupidatat non
proident."
Expected Output:
['Lorem ipsum dolor sit amet, consectetur adipiscing elit', 'Ut
enim ad minim veniam, quis nostrud exercitation ullamco',
'Duis aute irure dolor in reprehenderit in voluptate velit
esse', 'Excepteur sint occaecat cupidatat non proident.']
[
2 1 # Q 2:
]:
2
3 paragraph = "Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Ut enim ad minim
veniam, quis nostrud exercitation ullamco. Duis
aute irure dolor in reprehenderit in voluptate velit
esse. Excepteur sint occaecat cupidatat non
proident."
4
5
6 # for loop
7 # ---- your solution here ----
8
9
10 # call the function you defined
11 sentences = get_sentences(paragraph)
12 print(sentences)
13
14
15
16 # comprehension
17 # ---- your solution here ----
18
19
20 # call the function you defined
sentences_comp =
21
get_sentences_comp(paragraph)
22 print(sentences_comp)
['Lorem ipsum dolor sit amet, consectetur
[ adipiscing elit', 'Ut enim ad minim veniam, quis
2 nostrud exercitation ullamco', 'Duis aute irure
]: dolor in reprehenderit in voluptate velit esse',
'Excepteur sint occaecat cupidatat non proident.']
['Lorem ipsum dolor sit amet, consectetur
adipiscing elit', 'Ut enim ad minim veniam, quis
nostrud exercitation ullamco', 'Duis aute irure
dolor in reprehenderit in voluptate velit esse',
'Excepteur sint occaecat cupidatat non proident.']
Q3:
Use the functions in Q2 and this time define two functions
named get_words and get_words_comp.
These functions will return the words in the paragraph.
The first one will use for loop and the second one will use
Comprehension.
paragraph = "Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Ut enim ad minim veniam, quis nostrud
exercitation ullamco. Duis aute irure dolor in reprehenderit
in voluptate velit esse. Excepteur sint occaecat cupidatat non
proident."
Expected Output:
['Lorem', 'ipsum', 'dolor', 'sit', 'amet,', 'consectetur',
'adipiscing', 'elit.', 'Ut', 'enim', 'ad', 'minim', 'veniam,', 'quis',
'nostrud', 'exercitation', 'ullamco.', 'Duis', 'aute', 'irure',
'dolor', 'in', 'reprehenderit', 'in', 'voluptate', 'velit', 'esse.',
'Excepteur', 'sint', 'occaecat', 'cupidatat', 'non', 'proident.']
[
3 1 # Q 3:
]:
2
paragraph = "Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Ut enim ad minim
veniam, quis nostrud exercitation ullamco. Duis
3
aute irure dolor in reprehenderit in voluptate velit
esse. Excepteur sint occaecat cupidatat non
proident."
4
5
6 # for loop
7 # ---- your solution here ----
8
9
10 # call the function you defined
11 words = get_words(paragraph)
12 print(words)
13
14
15
16 # comprehension
17 # ---- your solution here ----
18
19
20 # call the function you defined
21 words_comp = get_words_comp(paragraph)
22 print(words_comp)
['Lorem', 'ipsum', 'dolor', 'sit', 'amet,',
'consectetur', 'adipiscing', 'elit', 'Ut', 'enim', 'ad',
[ 'minim', 'veniam,', 'quis', 'nostrud', 'exercitation',
3 'ullamco', 'Duis', 'aute', 'irure', 'dolor', 'in',
]: 'reprehenderit', 'in', 'voluptate', 'velit', 'esse',
'Excepteur', 'sint', 'occaecat', 'cupidatat', 'non',
'proident.']
['Lorem', 'ipsum', 'dolor', 'sit', 'amet,',
'consectetur', 'adipiscing', 'elit', 'Ut', 'enim', 'ad',
'minim', 'veniam,', 'quis', 'nostrud', 'exercitation',
'ullamco', 'Duis', 'aute', 'irure', 'dolor', 'in',
'reprehenderit', 'in', 'voluptate', 'velit', 'esse',
'Excepteur', 'sint', 'occaecat', 'cupidatat', 'non',
'proident.']
Q4:
Use the functions in Q2 and this time define two functions
named get_words_starting_with_vowels and
get_words_starting_with_vowels_comp.
These functions will return the words in the paragraph.
The first one will use for loop and the second one will use
Comprehension.
paragraph = "Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Ut enim ad minim veniam, quis nostrud
exercitation ullamco. Duis aute irure dolor in reprehenderit
in voluptate velit esse. Excepteur sint occaecat cupidatat non
proident."
Expected Output:
['ipsum', 'amet,', 'adipiscing', 'elit.', 'Ut', 'enim', 'ad',
'exercitation', 'ullamco.', 'aute', 'irure', 'in', 'in', 'esse.',
'Excepteur', 'occaecat']
[
4 1 # Q 4:
]:
2
paragraph = "Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Ut enim ad minim
veniam, quis nostrud exercitation ullamco. Duis
3
aute irure dolor in reprehenderit in voluptate velit
esse. Excepteur sint occaecat cupidatat non
proident."
4
5
6 # for loop
7 # ---- your solution here ----
8
9
10 # call the function you defined
vowel_words =
11
get_words_starting_with_vowels(paragraph)
12 print(vowel_words)
13
14
15
16 # comprehension
17 # ---- your solution here ----
18
19
20 # call the function you defined
vowels_comp =
21 get_words_starting_with_vowels_comp(paragraph
)
22 print(vowels_comp)
[ ['ipsum', 'amet,', 'adipiscing', 'elit', 'Ut', 'enim',
4 'ad', 'exercitation', 'ullamco', 'aute', 'irure', 'in',
]: 'in', 'esse', 'Excepteur', 'occaecat']
['ipsum', 'amet,', 'adipiscing', 'elit', 'Ut', 'enim',
'ad', 'exercitation', 'ullamco', 'aute', 'irure', 'in',
'in', 'esse', 'Excepteur', 'occaecat']
Q5:
Define a function named dict_of_squares.
It will return a dictionary of squares of numbers from 0 to 20
(inc.).
Key will be the number and the value will be the square of
this number:
{number: square}.
Hints:
no loops
use comprehension only
Expected Output:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81,
10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225, 16: 256,
17: 289, 18: 324, 19: 361, 20: 400}
[
5 1 # Q 5:
]:
2
3 # ---- your solution here ----
4
5
6
7 # call the function you defined
8 sqr_dict = dict_of_squares()
9 print(sqr_dict)
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8:
[
64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14:
5
196, 15: 225, 16: 256, 17: 289, 18: 324, 19: 361, 20:
]:
400}
Q6:
Define two functions named numbers_and_names and
numbers_and_names_comp.
They will take a dictionary as parameter.
The dictionary is:
{1: ['Spiderman', 24],
2: ['Wonder Woman', 100],
3: ['Batman', 45],
4: ['Superman', 70]}
They will return a new dictionary containing only the
numbers as the key and the name as value.
Expected Output:
{1: 'Spiderman', 2: 'Wonder Woman', 3: 'Batman', 4:
'Superman'}
[
6 1 # Q 6:
]:
2
3 heroes = {1: ['Spiderman', 24],
4 2: ['Wonder Woman', 100],
5 3: ['Batman', 45],
6 4: ['Superman', 70]}
7
8
9 # for loop
10 # ---- your solution here ----
11
12
13 # call the function you defined
14 print(numbers_and_names(heroes))
15
16
17
18 # comprehension
19 # ---- your solution here ----
20
21
22 # call the function you defined
23 print(numbers_and_names_comp(heroes))
[
{1: 'Spiderman', 2: 'Wonder Woman', 3: 'Batman',
6
4: 'Superman'}
]:
{1: 'Spiderman', 2: 'Wonder Woman', 3: 'Batman',
4: 'Superman'}
Q7:
Define a function named odd_numbered_names.
It will take a dictionary as parameter.
The dictionary is:
{1: ['Spiderman', 24],
2: ['Wonder Woman', 100],
3: ['Batman', 45],
4: ['Superman', 70]}
It will return a new dictionary containing only the odd
numbers as the key and the name as value.
Hints:
no loops
only with comprehension
Expected Output:
{1: 'Spiderman', 3: 'Batman'}
[
7 1 # Q 7:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 heroes = {1: ['Spiderman', 24],
8 2: ['Wonder Woman', 100],
9 3: ['Batman', 45],
10 4: ['Superman', 70]}
11
12 names_dict = odd_numbered_names(heroes)
13 print(names_dict)
[
7 {1: 'Spiderman', 3: 'Batman'}
]:
Q8:
Define a function named word_lengths.
It will take a text as parameter.
The function will return two lists.
The first list is going to be the words in the text.
And the second list will contain the lengths of these words.
If the word is one ('the', 'in', 'as', 'at') it will ignore the word.
Hints:
Parameter:
"It takes all the running you can do, to keep in the same
place. If you want to get somewhere else, you must run at
least twice as fast as that!"
Expected Output:
['It', 'takes', 'all', 'running', 'you', 'can', 'do,', 'to', 'keep',
'same', 'place.', 'If', 'you', 'want', 'to', 'get', 'somewhere',
'else,', 'you', 'must', 'run', 'least', 'twice', 'fast', 'that!']
[2, 5, 3, 7, 3, 3, 3, 2, 4, 4, 6, 2, 3, 4, 2, 3, 9, 5, 3, 4, 3, 5, 5,
4, 5]
[8]
1 # Q 8:
:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 # Alice's Adventures in Wonderland
alice_in_wonderland = """It takes all the
8
running you can do, to keep in the same place.
If you want to get somewhere else, you must run
9
at least twice as fast as that!"""
10
# call the function and unpack the returning
11
Tuple
words, lengths =
12
word_lengths(alice_in_wonderland)
13
14 # now print them
15 print(words)
16 print(lengths)
['It', 'takes', 'all', 'running', 'you', 'can', 'do,',
[8] 'to', 'keep', 'same', 'place.', 'If', 'you', 'want',
: 'to', 'get', 'somewhere', 'else,', 'you', 'must',
'run', 'least', 'twice', 'fast', 'that!']
[2, 5, 3, 7, 3, 3, 3, 2, 4, 4, 6, 2, 3, 4, 2, 3, 9, 5,
3, 4, 3, 5, 5, 4, 5]
Q9:
Define a function named only_positives.
It will take a list of numbers (integers and floats) as
parameter.
It will take only the positive numbers and cast them into
integers and return the final list.
Hints:
no loops
use comprehension
Parameter:
[12.8, -27.2, -34.5, 58.4, -82.0, 66.6, 14.9]
Expected Output:
[12, 58, 66, 14]
[
9 1 # Q 9:
]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
numbers = [12.8, -27.2, -34.5, 58.4, -82.0, 66.6,
7
14.9]
8 positives = only_positives(numbers)
9 print(positives)
[
9 [12, 58, 66, 14]
]:
Q10:
Define a function named flatten.
It will take a list as parameter.
This list will be containing lists as elements.
Namely it is a nested list.
The function will flatten the list and return a final list which
contains only the individual elements. The final list not a
nested list anymore.
Hints:
no loops
comprehension only
Parameter:
list_of_lists = [[8,6], [3,1,4], [2,0,9], [5]]
Expected Output:
[8, 6, 3, 1, 4, 2, 0, 9, 5]
[1
1 # Q 10:
0]:
2
3 # ---- your solution here ----
4
5
6 # call the function you defined
7 list_of_lists = [[8,6], [3,1,4], [2,0,9], [5]]
8 print(flatten(list_of_lists))
[1
[8, 6, 3, 1, 4, 2, 0, 9, 5]
0]:
OceanofPDF.com
SOLUTIONS - Comprehension
Here are the solutions for the quiz for Chapter 18 -
Comprehension.
SOLUTIONS - Comprehension:
S1:
[
1 1 # S 1:
]:
2
3 # for loop
4 def get_squares():
5 squares = []
6 for i in range(1, 10):
7 squares.append(i**2)
8 return squares
9
10
11 # call the function you defined
12 squares = get_squares()
13 print(squares)
14
15
16 # comprehension
17 def get_squares_comp():
18 squares = [x**2 for x in range(1, 10)]
19 return squares
20
21
22 # call the function you defined
23 sqrs = get_squares_comp()
24 print(sqrs)
[
1 [1, 4, 9, 16, 25, 36, 49, 64, 81]
]:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
S2:
[
2 1 # S 2:
]:
2
paragraph = "Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Ut enim ad minim
veniam, quis nostrud exercitation ullamco. Duis
3
aute irure dolor in reprehenderit in voluptate
velit esse. Excepteur sint occaecat cupidatat non
proident."
4
5
6 # for loop
7 def get_sentences(paragraph):
8 sentences = []
9 for sentence in paragraph.split('. '):
10 sentences.append(sentence)
11 return sentences
12
13
14 # call the function you defined
15 sentences = get_sentences(paragraph)
16 print(sentences)
17
18
19
20 # comprehension
21 def get_sentences_comp(paragraph):
# sentences = [sentence for sentence in
22
paragraph]
23 #return sentences
return [sentence for sentence in
24
paragraph.split('. ')]
25
26
27 # call the function you defined
sentences_comp =
28
get_sentences_comp(paragraph)
29 print(sentences_comp)
['Lorem ipsum dolor sit amet, consectetur
[ adipiscing elit', 'Ut enim ad minim veniam, quis
2 nostrud exercitation ullamco', 'Duis aute irure
]: dolor in reprehenderit in voluptate velit esse',
'Excepteur sint occaecat cupidatat non proident.']
['Lorem ipsum dolor sit amet, consectetur
adipiscing elit', 'Ut enim ad minim veniam, quis
nostrud exercitation ullamco', 'Duis aute irure
dolor in reprehenderit in voluptate velit esse',
'Excepteur sint occaecat cupidatat non proident.']
S3:
[
3 1 # S 3:
]:
2
paragraph = "Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Ut enim ad minim
veniam, quis nostrud exercitation ullamco. Duis
3
aute irure dolor in reprehenderit in voluptate
velit esse. Excepteur sint occaecat cupidatat non
proident."
4
5
6 # for loop
7 def get_words(paragraph):
8 words = []
9
10 # get sentences of paragraph
11 sentences = get_sentences_comp(paragraph)
12
13 for sentence in sentences:
14 for word in sentence.split():
15 words.append(word)
16
17 return words
18
19
20 # call the function you defined
21 words = get_words(paragraph)
22 print(words)
23
24
25
26 # comprehension
27 def get_words_comp(paragraph):
28
29 # get sentences of paragraph
30 sentences = get_sentences_comp(paragraph)
31
32 words = [word
33 for sentence in sentences
34 for word in sentence.split()]
35
36 return words
37
38
39 # call the function you defined
40 words_comp = get_words_comp(paragraph)
41 print(words_comp)
['Lorem', 'ipsum', 'dolor', 'sit', 'amet,',
'consectetur', 'adipiscing', 'elit', 'Ut', 'enim', 'ad',
[ 'minim', 'veniam,', 'quis', 'nostrud', 'exercitation',
3 'ullamco', 'Duis', 'aute', 'irure', 'dolor', 'in',
]: 'reprehenderit', 'in', 'voluptate', 'velit', 'esse',
'Excepteur', 'sint', 'occaecat', 'cupidatat', 'non',
'proident.']
['Lorem', 'ipsum', 'dolor', 'sit', 'amet,',
'consectetur', 'adipiscing', 'elit', 'Ut', 'enim', 'ad',
'minim', 'veniam,', 'quis', 'nostrud', 'exercitation',
'ullamco', 'Duis', 'aute', 'irure', 'dolor', 'in',
'reprehenderit', 'in', 'voluptate', 'velit', 'esse',
'Excepteur', 'sint', 'occaecat', 'cupidatat', 'non',
'proident.']
S4:
[
4 1 # S 4:
]:
2
paragraph = "Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Ut enim ad minim
veniam, quis nostrud exercitation ullamco. Duis
3
aute irure dolor in reprehenderit in voluptate
velit esse. Excepteur sint occaecat cupidatat non
proident."
4
5
6 # for loop
7 def get_words_starting_with_vowels(paragraph):
8
9 vowels = ['a', 'e', 'i', 'o', 'u']
10
11 words_of_vowels = []
12
13 # get sentences of paragraph
14 sentences = get_sentences_comp(paragraph)
15
16 for sentence in sentences:
17 for word in sentence.split():
18 if word[0].lower() in vowels:
19 words_of_vowels.append(word)
20
21 return words_of_vowels
22
23
24 # call the function you defined
vowel_words =
25
get_words_starting_with_vowels(paragraph)
26 print(vowel_words)
27
28
29
30 # comprehension
def
31 get_words_starting_with_vowels_comp(paragrap
h):
32
33 vowels = ['a', 'e', 'i', 'o', 'u']
34
35 # get sentences of paragraph
36 sentences = get_sentences_comp(paragraph)
37
38 words_of_vowels = [word
39 for sentence in sentences
40 for word in sentence.split()
41 if word[0].lower() in vowels]
42
43 return words_of_vowels
44
45
46 # call the function you defined
vowels_comp =
47 get_words_starting_with_vowels_comp(paragrap
h)
48 print(vowels_comp)
[ ['ipsum', 'amet,', 'adipiscing', 'elit', 'Ut', 'enim',
4 'ad', 'exercitation', 'ullamco', 'aute', 'irure', 'in',
]: 'in', 'esse', 'Excepteur', 'occaecat']
['ipsum', 'amet,', 'adipiscing', 'elit', 'Ut', 'enim',
'ad', 'exercitation', 'ullamco', 'aute', 'irure', 'in',
'in', 'esse', 'Excepteur', 'occaecat']
S5:
[
5 1 # S 5:
]:
2
3 def dict_of_squares():
4
squares = {num: num**2 for num in range(0,
5
21)}
6
7 return squares
8
9
10
11 # call the function you defined
12 sqr_dict = dict_of_squares()
13 print(sqr_dict)
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49,
[
8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169,
5
14: 196, 15: 225, 16: 256, 17: 289, 18: 324, 19:
]:
361, 20: 400}
S6:
[
6 1 # S 6:
]:
2
3 heroes = {1: ['Spiderman', 24],
4 2: ['Wonder Woman', 100],
5 3: ['Batman', 45],
6 4: ['Superman', 70]}
7
8
9 # for loop
10 def numbers_and_names(heroes):
11
12 heroe_dict = {}
13
14 for num, name_and_age in heroes.items():
15 heroe_dict[num] = name_and_age[0]
16
17 return heroe_dict
18
19
20 # call the function you defined
21 print(numbers_and_names(heroes))
22
23
24
25 # comprehension
26 def numbers_and_names_comp(heroes):
27
28 heroe_dict = {num: name_and_age[0]
for num, name_and_age in
29
heroes.items()}
30
31 return heroe_dict
32
33
34 # call the function you defined
35 print(numbers_and_names_comp(heroes))
[
{1: 'Spiderman', 2: 'Wonder Woman', 3: 'Batman',
6
4: 'Superman'}
]:
{1: 'Spiderman', 2: 'Wonder Woman', 3: 'Batman',
4: 'Superman'}
S7:
[
7 1 # S 7:
]:
2
3 def odd_numbered_names(heroes):
4
5 heroe_dict_odds = {key: value[0]
6 for key, value in heroes.items()
7 if key % 2 == 1}
8
9 return heroe_dict_odds
10
11
12
13 # call the function you defined
14 heroes = {1: ['Spiderman', 24],
15 2: ['Wonder Woman', 100],
16 3: ['Batman', 45],
17 4: ['Superman', 70]}
18
19 names_dict = odd_numbered_names(heroes)
20 print(names_dict)
[
7 {1: 'Spiderman', 3: 'Batman'}
]:
S8:
[8
1 # S 8:
]:
2
3 def word_lengths(text):
4
5 # get the words first
6 words = text.split()
7
8 # forbidden words
9 forbiddens = ('the', 'in', 'as', 'at')
10
11 # comprehensions
12 word_list = [word
13 for word in words
14 if not word in forbiddens]
15
16 lengths = [len(word)
17 for word in words
18 if not word in forbiddens]
19
20 return (word_list, lengths)
21
22
23
24 # call the function you defined
25 # Alice's Adventures in Wonderland
alice_in_wonderland = """It takes all the running
26
you can do, to keep in the same place.
If you want to get somewhere else, you must run
27
at least twice as fast as that!"""
28
# call the function and unpack the returning
29
Tuple
words, lengths =
30
word_lengths(alice_in_wonderland)
31
32 # now print them
33 print(words)
34 print(lengths)
['It', 'takes', 'all', 'running', 'you', 'can', 'do,', 'to',
[8 'keep', 'same', 'place.', 'If', 'you', 'want', 'to',
]: 'get', 'somewhere', 'else,', 'you', 'must', 'run',
'least', 'twice', 'fast', 'that!']
[2, 5, 3, 7, 3, 3, 3, 2, 4, 4, 6, 2, 3, 4, 2, 3, 9, 5,
3, 4, 3, 5, 5, 4, 5]
S9:
[
9 1 # S 9:
]:
2
3 def only_positives(a_list):
4
5 return [int(x)
6 for x in a_list
7 if x > 0]
8
9
10
11 # call the function you defined
numbers = [12.8, -27.2, -34.5, 58.4, -82.0, 66.6,
12
14.9]
13 positives = only_positives(numbers)
14 print(positives)
[
9 [12, 58, 66, 14]
]:
S10:
[1
1 # S 10:
0]:
2
3 def flatten(a_list):
4
5 return [j
6 for i in a_list
7 for j in i]
8
9
10
11 # call the function you defined
12 list_of_lists = [[8,6], [3,1,4], [2,0,9], [5]]
13 print(flatten(list_of_lists))
[1
[8, 6, 3, 1, 4, 2, 0, 9, 5]
0]:
OceanofPDF.com
19. Project 3 - Snake Game
Preparation
This is our final project in this book. It will cover all the topics
what we have seen so far. We will build our own snake game with
Python. We will use built-in turtle , time and random modules
in this project. Here is an image of our snake game.
Figure 19-1: The snake game
Functions
Main Screen:
Now let’ define a function, set_screen , to set up the main
screen. It will use the global variable window , and assign
turtle.Screen object to it. It will also set the title, background
color, with and height of the screen.
[
3 1 # function to set up the main screen
]:
2
3 def set_screen():
4 """Sets the main screen."""
5
6 global window
7
8 window = turtle.Screen()
9 window.title('Hands-On Snake')
10 window.bgcolor('white smoke')
11 window.setup(width=WIDTH, height=HEIGHT)
12 window.tracer(0) # False
The window.tracer(0) method in line 12 is used to turn turtle
animation on or off and set a delay for update drawings. We don’t
want to have automatic screen updates, so we turn them off by
setting tracer() to 0. 0 means False. We will update the screen
manually.
Events:
We will play our snake game with the keyboard. So we need to
listen the keyboard events. The keyboard event which we are
interested in is: onkeypress(fun, key) . This function takes two
arguments:
[
4 1 # function to listen screen events
]:
2
3 def listen_events():
4 window.listen()
5 window.onkeypress(set_up_direction, 'Up')
6 window.onkeypress(set_down_direction, 'Down')
7 window.onkeypress(set_left_direction, 'Left')
8 window.onkeypress(set_right_direction, 'Right')
Head:
Now let’s define a function to create the head of the snake.
[
6 1 # create the head
]:
2
3 def create_head(is_initial=True):
4 """Creates the snake head."""
5
6 global head, snake
7
8 # create the head
9 head = turtle.Turtle()
10 head.shape(shapes[1]) # 20 x 20
11 head.speed(0)
12 head.penup()
13
14 # start at a higher position
15 if is_initial:
16 head.goto(0, 200)
17
18 snake.append(head)
Score:
To be able to write the score on the screen we need a new turtle
object with pen. We will call this turtle as the pen . We will create
two separate function for score: create_score and
update_score .
The create_score function will instantiate the pen
turtle and set its color.
The update_score function will update the score text
on the screen.
[
7 1 # create the score
]:
2
3 def create_score():
4
5 global pen
6
7 # create the pen turtle
8 pen = turtle.Turtle()
9 pen.penup()
10 pen.hideturtle()
11 pen.goto(0, Y_RANGE - 2 * TURTLE_SIZE)
12 pen.color('black')
13
14 # initialize the score
15 update_score(0)
[
9 1 # reset screen fn
]:
2
3 def reset():
4
5 # hide the segments of snake
6 for t in snake:
7 t.goto(40000, 4000)
8
9 # clear the snake list
10 snake.clear()
11
12 # create a new head
13 create_head(is_initial=False)
14
15 # reset the score
16 update_score(0, is_reset=True)
In the reset function, we loop over all the parts in the snake
list in line 6. And we hide all the parts by sending them to a very
far (x, y) position on the screen. The reason for hiding is that,
unfortunately we cannot destroy the turtles we create in the current
kernel session. That’s why we hide them.
In line 10, we clear the snake list. In Python you can remove
all the items of a List with the clear() function.
In line 13, we create a new head by calling the create_head()
function. And in line 16, we update the score to 0, which means a
new game will start.
Delay:
To be able to see the snake moving, we need some delay. We
use the time module for this. The time module has a method
called sleep() . The time.sleep(duration) function suspends
execution of the current program for a given number of seconds. In
other words, Python waits for that amount of seconds before
executing the next line.
[1
1 # delay function
0]:
2
3 def delay(duration):
4 time.sleep(duration)
Collisions:
The game will be over if the snake collides either to its own
body or to any of the four borders of the screen. Let’s define the
functions for collisions.
[1
1 # function for border collisions
1]:
2
3 def check_border_collisions():
4
# if the head position (x, y) is out the ranges
5
(X_RANGE, Y_RANGE) -> we collide
6
7 x = head.xcor()
8 y = head.ycor()
9
if x <= -X_RANGE or x >= X_RANGE or y
10
<= -Y_RANGE or y >= Y_RANGE:
11 # set direction
12 global direction
13 direction = 'stop'
14
15 # reset screen after 1 second
16 delay(1)
17 reset()
Move:
To be able to move the snake, we have to move all of its
segments. We will define three separate move functions. The first
one is the main move() function, the second one is the
move_head() and the third one is the move_segments() . Let’s
define them:
[1
1 # move function
3]:
2
3 def move():
4 if window._RUNNING:
5 # move only if the direction is not stop
6 if direction != 'stop':
7 # move the segments
8 move_segments()
9
10 # move the head
11 move_head()
Food:
We need to feed the snake. Each food it eats will become a
body segment for it. We will have different functions for the food.
The first one will be add_food() which will create a food on
the screen. A food is nothing but a turtle object.
The second function will be move_food() . It will move the
food turtle to a random position on the screen.
The third one will be eat_food() function. We will define
what ‘eating’ means and the actions after eating the food.
Let’s define these functions:
[1
1 # create the food object
6]:
2
3 def add_food():
4
5 if window._RUNNING:
6 global food
7
# create a turtle -> single -> Singleton
8
Pattern
9 if food == None:
10 food = turtle.Turtle()
11 food.shape(get_shape())
12 food.shapesize(0.5, 0.5)
13 food.speed(0)
14 food.penup()
15
16 # color
17 food.color(get_color())
18
19 # move the food
20 move_food(food)
Body Segments:
Now that the snake eats the food, let’s define functions to add
body segments to it. We will have two separate functions:
create_segment and get_last_segment_position .
[1
1 # function to create segment
9]:
2
3 def create_segment():
4 """Creates a new segment for snake."""
5
6 global snake
7
8 # create a segment
9 segment = turtle.Turtle()
10 segment.shape(shapes[1])
11 segment.speed(0)
12 segment.color(food_color)
13 segment.penup()
14
15 # position the segment
16 x, y = get_last_segment_position()
17 segment.goto(x, y)
18
19 # add this segment into snake list
20 snake.append(segment)
[2
1 # last segment position
0]:
2
3 def get_last_segment_position():
4
5 # last element -> snake[-1]
6 x = snake[-1].xcor()
7 y = snake[-1].ycor()
8
9 # direction
# if direction is up -> same x, y is
10
TURTLE_SIZE less
11 if direction == 'up':
12 y = y - TURTLE_SIZE
13
# if direction is up -> same x, y is
14
TURTLE_SIZE more
15 elif direction == 'down':
16 y = y + TURTLE_SIZE
17
# if direction is right -> same y, x is
18
TURTLE_SIZE less
19 elif direction == 'right':
20 x = x - TURTLE_SIZE
21
# if direction is left -> same y, x is
22
TURTLE_SIZE more
23 elif direction == 'left':
24 x = x + TURTLE_SIZE
25
26 return (x, y)
In cell 20, we define the get_last_segment_position
function. The last segment of the snake is the last element in the
list. We get it as: snake[-1] . We get its x and y coordinates in
lines 6 and 7. Then we check the current direction. We need to
know, in which direction the snake is currently moving.
Let’s say, it’s moving to the ‘right’ . This is the condition in
line 19. Then the y coordinate of the new segment will be the same
as the last one. That’s why we do not touch the y coordinate. But
the x coordinate will be x - TURTLE_SIZE . Because the new
segment will come after the last one. That’s why we have a minus
sign for the ‘right’ direction.
In line 26, the function returns a tuple of the new values of (x,
y) .
Shape:
Every time the snake eats the food, we want the food to be of a
new random shape. Remember, we have the global variable
shapes . It is a dictionary and it keeps the numbers and
corresponding shapes. We define a function to return a random
shape each time we call it.
[2
1 # get a random shape
1]:
2
3 def get_shape():
4 # get a random index from the shapes
5 index = random.randint(0, len(shapes)-1)
6 # return the item at that index
7 return shapes[index]
Color:
[2
1 # get a random color
2]:
2
3 def get_color():
4
5 global food_color
6
7 index = random.randint(0, len(colors)-1)
8 color = colors[index]
9
10 food_color = color
11
12 return color
Every time the snake eats the food, we want the food to be of a
new random color. And we define a function for it. The
get_color() function creates a random index by using the length
of the colors dictionary as: index = random.randint(0,
len(colors)-1) . And it gets the color at this index as: color =
colors[index] .
In line 10, it assigns this color to the global food_color
because the new segment will be using this global variable. Finally
it returns the color .
Screen:
We need to update the screen, because we turned off automatic
screen updates. Let’s define the update_screen() function:
[2
1 # function to update screen
3]:
2
3 def update_screen():
4
5 while window._RUNNING:
6
7 # side collisions
8 check_border_collisions()
9
10 # body collisions
11 check_body_collisions()
12
13 # move the head
14 move()
15
16 # delay
17 delay(delay_time)
18
19 # create the food
20 add_food()
21
22 # eat the food
23 eat_food()
24
25 # get rid of update error
26 window.update()
Main:
Now that we have all the functions defined, we need one final
function to call them. This is going to be the main() function. In
general, main() functions are used to start the program execution.
That’s what we will do here. We will call the necessary functions
inside our main() function. And as soon as we call the main()
function, our snake game will start.
[2
1 # main function to call other functions
4]:
2
3 def main():
4
5 # set the screen
6 set_screen()
7
8 # listen keyboard events
9 listen_events()
10
11 # create head
12 create_head()
13
14 # create score
15 create_score()
16
17 # update screen
18 update_screen()
In cell 24, we define the main function. Its purpose is to call the
necessary function to set and start the game. It calls set_screen(),
listen_events(), create_head(), create_score() and
update_screen() functions in this order. Now let’s call the
main() function and play the game.
[2
1 # call the main function to start the game
5]:
2 main()
As soon as we run the cell 25, and call the main() function
Python will start the game. You can start playing it now. To start a
new game just press any of the direction buttons on the keyboard.
Final Warning:
Since our game is built on the turtle module, the last line
should always be turtle.mainloop() . You will start playing the
game by calling the main() function. When you decide to close
the game, run this line of code. You should only run it before you
close the game. You shouldn’t run it while playing the game. It’s
the terminator line. That’s why, it is the last cell in our project:
[2
1 # the last line
6]:
2 turtle.mainloop()
OceanofPDF.com
20. Assignment 3 - Snake Game
We finished our third project, Project 3 - Snake Game. Now it’s
your turn to recreate the same project. This chapter is the
assignment on the Project 3 - Snake Game.
To complete the assignment, follow the instructions where you
see a #TODO statement. You can also download the assignment
Jupyter file in the Github repository of the book. You can find the
solutions in the previous chapter.
The Assignment
You will create your own Snake Game with Python. You can
play it once you finish it. It will be a fun way of using what you
learned in this book. You will use built-in turtle , time and
random modules in this assignment. Here is an image of the snake
game that you will build:
Figure 20-1: The snake game
Global Variables:
Next, we will define the global variables, which we will be
using throughout the project:
[
2 1 # global variable
]:
2
3 window = None
4 snake = []
5 head = None
6
7 direction = 'down'
8 delay_time = 0.1
9
10 food = None
11 food_color = ''
12
13 TURTLE_SIZE = 20
14 WIDTH = 600
15 HEIGHT = 600
16
17 X_RANGE = (WIDTH - TURTLE_SIZE) / 2
18 Y_RANGE = (HEIGHT - TURTLE_SIZE) / 2
19
20 pen = None
21 score = 0
22 high_score = 0
23
24 #TODO - define shapes dictionary
25
26 #TODO - define colors dictionary
Functions
Main Screen:
Now let’ define a function, set_screen , to set up the main
screen. It will use the global variable window , and assign
turtle.Screen object to it. It will also set the title, background
color, with and height of the screen.
[
3 1 # function to set up the main screen
]:
2
3 def set_screen():
4 """Sets the main screen."""
5
6 global window
7
8 #TODO - create the window (Screen)
9 # and give it;
10 # title, background, width-height,
11 # and set the tracer.
Events:
We will play our snake game with the keyboard. So we need to
listen the keyboard events. The keyboard event which we are
interested in is: onkeypress(fun, key) . This function takes two
arguments:
fun : the name of the function to execute when this event
occurs
key : the string name of the key (e.g. “Down”)
[
4 1 # function to listen screen events
]:
2
3 def listen_events():
4 window.listen()
#TODO - listen Up, Down, Left and Right key
5
press events
Head:
Now let’s define a function to create the head of the snake.
[
6 1 # create the head
]:
2
3 def create_head(is_initial=True):
4 """Creates the snake head."""
5
6 global head, snake
7
8 # create the head
9
#TODO - create the head turtle, give it a shape
10
and a color.
11 #TODO - set the position for the head
12 #TODO - append the head into snake list
Score:
To be able to write the score on the screen we need a new turtle
object with pen. We will call this turtle as the pen . We will create
two separate function for score: create_score and
update_score .
[
7 1 # create the score
]:
2
3 def create_score():
4
5 global pen
6
7 # create the pen turtle
#TODO - create the pen turtle and place it on
8
screen
9
10 # initialize the score
#TODO - call the update_score function to
11
initialize the pen
In the reset function, we loop over all the parts in the snake
list in line 6. And we hide all the parts by sending them to a very
far (x, y) position on the screen. The reason for hiding is that,
unfortunately we cannot destroy the turtles we create in the current
kernel session. That’s why we hide them.
In line 10, we clear the snake list. In Python you can remove
all the items of a List with the clear() function.
In line 13, we create a new head by calling the create_head()
function. And in line 16, we update the score to 0, which means a
new game will start.
Delay:
To be able to see the snake moving, we need some delay. We
use the time module for this. The time module has a method
called sleep() . The time.sleep(duration) function suspends
execution of the current program for a given number of seconds. In
other words, Python waits for that amount of seconds before
executing the next line.
[1
1 # delay function
0]:
2
3 def delay(duration):
4 time.sleep(duration)
Collisions:
The game will be over if the snake collides either to its own
body or to any of the four borders of the screen. Let’s define the
functions for collisions.
[1
1 # function for border collisions
1]:
2
3 def check_border_collisions():
4
# if the head position (x, y) is out the ranges
5
(X_RANGE, Y_RANGE) -> we collide
6
#TODO - get x and y coordinates of the head
7
turtle
8
9 if #TODO decide the collision :
10
11 # set direction
#TODO - set the global direction variable to
12
'stop'
13
14 # reset screen after 1 second
15 #TODO - call delay function with 1 seconds
16 #TODO - call reset function
Move:
To be able to move the snake, we have to move all of its
segments. We will define three separate move functions. The first
one is the main move() function, the second one is the
move_head() and the third one is the move_segments() . Let’s
define them:
[1
1 # move function
3]:
2
3 def move():
4 if window._RUNNING:
5 # move only if the direction is not stop
6 if direction != 'stop':
7 # move the segments
8 #TODO - call move_segments function
9
10 # move the head
11 #TODO - call move_head function
Food:
We need to feed the snake. Each food it eats will become a
body segment for it. We will have different functions for the food.
The first one will be add_food() which will create a food on
the screen. A food is nothing but a turtle object.
The second function will be move_food() . It will move the
food turtle to a random position on the screen.
The third one will be eat_food() function. We will define
what ‘eating’ means and the actions after eating the food.
Let’s define these functions:
[1
1 # create the food object
6]:
2
3 def add_food():
4
5 if window._RUNNING:
6 global food
7
# create a turtle -> single -> Singleton
8
Pattern
9 if food == None:
10
#TODO - create the food the turtle and
11
give it a random shape
12
13 # color
14 #TODO - give food turtle a random color
15
16 # move the food
#TODO - call move_food function with
17
the food turtle
Body Segments:
Now that the snake eats the food, let’s define functions to add
body segments to it. We will have two separate functions:
create_segment and get_last_segment_position .
[1
1 # function to create segment
9]:
2
3 def create_segment():
4 """Creates a new segment for snake."""
5
6 global snake
7
8 # create a segment
#TODO - create the segment turtle with
9
appropriate shape
10
#TODO - set the color of the segment turtle to
11
global food_color
12
13 # position the segment
x, y = #TODO - call get_last_segment_position
14
function
15 segment.goto(x, y)
16
17 # add this segment into snake list
#TODO - append the segment to global snake
18
list
[2
1 # last segment position
0]:
2
3 def get_last_segment_position():
4
5 # last element -> snake[-1]
#TODO - get the x and y coordinates of the last
6
segment in the snake
7
8 # direction
# if direction is up -> same x, y is
9
TURTLE_SIZE less
10 if direction == 'up':
11 y = y - TURTLE_SIZE
12
# if direction is up -> same x, y is
13
TURTLE_SIZE more
14 elif direction == 'down':
15 y = #TODO - assign the new y value
16
# if direction is right -> same y, x is
17
TURTLE_SIZE less
18 elif direction == 'right':
19 x = #TODO - assign the new x value
20
# if direction is left -> same y, x is
21
TURTLE_SIZE more
22 elif direction == 'left':
23 x = #TODO - assign the new x value
24
25 return #TODO - return a tuple of x and y
Shape:
Every time the snake eats the food, we want the food to be of a
new random shape. Remember, we have the global variable
shapes . It is a dictionary and it keeps the numbers and
corresponding shapes. We define a function to return a random
shape each time we call it.
[2
1 # get a random shape
1]:
2
3 def get_shape():
4 # get a random index from the shapes
5 index = random.randint(0, len(shapes)-1)
6 # return the item at that index
7 return shapes[index]
Color:
[2
1 # get a random color
2]:
2
3 def get_color():
4
5 global food_color
6
index = #TODO - get a random integer between
7
0 and the length of colors -1
8 color = colors[index]
9
10 food_color = color
11
12 return color
Every time the snake eats the food, we want the food to be of a
new random color. And we define a function for it. The
get_color() function creates a random index by using the length
of the colors dictionary as: index = random.randint(0,
len(colors)-1) . And it gets the color at this index as: color =
colors[index] .
In line 10, it assigns this color to the global food_color
because the new segment will be using this global variable. Finally
it returns the color .
Screen:
We need to update the screen, because we turned off automatic
screen updates. Let’s define the update_screen() function:
[2
1 # function to update screen
3]:
2
3 def update_screen():
4
5 while window._RUNNING:
6
7 # side collisions
#TODO - call check_border_collisions
8
function
9
10 # body collisions
#TODO - call check_body_collisions
11
function
12
13 # move the head
14 #TODO - call move function
15
16 # delay
#TODO - call delay function with global
17
delay_time
18
19 # create the food
20 #TODO - call add_food function
21
22 # eat the food
23 #TODO - call eat_food function
24
25 # get rid of upate error
26 window.update()
Main:
Now that we have all the functions defined, we need one final
function to call them. This is going to be the main() function. In
general, main() functions are used to start the program execution.
That’s what we will do here. We will call the necessary functions
inside our main() function. And as soon as we call the main()
function, our snake game will start.
[2
1 # main function to call other functions
4]:
2
3 def main():
4
5 # set the screen
6 set_screen()
7
8 # listen keyboard events
9 listen_events()
10
11 # create head
12 create_head()
13
14 # create score
15 create_score()
16
17 # update screen
18 update_screen()
In cell 24, we define the main function. Its purpose is to call the
necessary function to set and start the game. It calls set_screen(),
listen_events(), create_head(), create_score() and
update_screen() functions in this order. Now let’s call the
main() function and play the game.
[2
1 # call the main function to start the game
5]:
2 main()
As soon as we run the cell 25, and call the main() function
Python will start the game. You can start playing it now. To start a
new game just press any of the direction buttons on the keyboard.
Figure 20-2: The snake game completed and ready to play
Final Warning:
Since our game is built on the turtle module, the last line
should always be turtle.mainloop() . You will start playing the
game by calling the main() function. When you decide to close
the game, run this line of code. You should only run it before you
close the game. You shouldn’t run it while playing the game. It’s
the terminator line. That’s why, it is the last cell in our project:
[2
1 # the last line
6]:
2 turtle.mainloop()
OceanofPDF.com
21. Final Exam
The Final Exam includes the topics we have covered in this
book. It is a multiple choice exam with 20 questions. Each
question is 5 points, which makes 100 points in total. You
should get at least 70 points to be successful. The duration is
120 minutes. Here is the number of questions in the
respective chapters:
Topic Questions
First Program 1
Variables 1
Functions I 1
Conditional Statements 2
Functions II 2
Loops 2
Strings 2
List 2
Dictionary 2
Tuple 2
Set 1
Comprehension 2
Total 20
If you want to solve the final exam in Jupyter environment,
you can download the notebook file for the final exam,
Final_Exam.ipynb, from the Github Repository of this book.
It is in the folder named 21_Final_Exam. Here are the
questions for the Final Exam:
OceanofPDF.com
QUESTIONS:
Q1:
OceanofPDF.com
ANSWERS OF THE FINAL EXAM QUESTIONS:
Question Answer
Q1 B
Q2 D
Q3 D
Q4 A
Q5 C
Q6 A
Q7 D
Q8 B
Q9 C
Q10 D
Q11 B
Q12 A
Q13 D
Q14 B
Q15 C
Q16 A
Q17 D
Q18 A
Q19 B
Q20 C
OceanofPDF.com
22. Conclusion
This is the last chapter in this book. We have covered almost all
the fundamental concepts in Python and we covered them in great
detail. We started with setting our development environment. Then
we defined our First program and said ‘Hello World’ to Python.
Then we moved on with Variables, Functions and we finished our
First Project. We learned Conditional Statements and we moved on
with the second part of the Functions. Then we learned Loops and
Strings. After Strings we built the Second Project. Then came the
Lists, Dictionaries, Tuples, Sets and Comprehensions. And we
finished the third Project after these topics. Finally you got the
Final Exam to test yourself.
Dear reader!
Good bye.
Musa Arda
[1]
Anaconda Installation:
https://docs.anaconda.com/anaconda/install/index.html
[2]
Jupyter Notebook Security: https://jupyter-
notebook.readthedocs.io/en/stable/security.html
[3]
The JupyterLab Interface:
https://jupyterlab.readthedocs.io/en/stable/user/interface.html
[4]
Python Virtual Environments:
https://docs.python.org/3/tutorial/venv.html
[5]
Central Repository for Python Packages: https://pypi.org/
[6]
The Turtle Module official documentation:
https://docs.python.org/3/library/turtle.html
[7]
String Methods documentation:
https://docs.python.org/3/library/stdtypes.html#string-methods
OceanofPDF.com