Python Essentials 1
Python Essentials 1
by
First published in the USA in 2023 by the Open Education and Development Group
ISBN: 979-8-9877622-1-9
All rights reserved. This book may not be copied or reproduced, in whole or in part,
without the express written permission of the Open Education and Development Group.
While the authors and publisher have taken every precaution in the preparation of this
book, they assume no responsibility for any errors or omissions. Furthermore, authors
and publisher assume no liability for any damages that result from the use of the
information contained within this book.
Image credits
Portrait of Guido Van Rossum at the Dropbox headquarters in 2014
CC BY-SA 4.0, Photograph by Daniel Stroud.
Cover Design
Konrad Papka
The Open Education and Development Group and its subsidiaries, including the
OpenEDG Python Institute, is an independent organization with no affiliated links to any
other organization, including the Python Software Foundation.
All the code examples in the book have been tested on Python 3.4, 3.6, 3.7, 3.8, and 3.9,
and should work with any subsequent versions of Python 3.x.
Appendices
Appendix A: LAB Hints
Appendix B: LAB Sample Solutions
Appendix C: Answers
Appendix D: PCEP Exam Syllabus
Welcome to Python Essentials 1
LEARN PYTHON – THE LANGUAGE OF
TODAY AND TOMORROW
This course is the first in a two-course Python Essentials series. It covers everything
you need to know to start designing, writing, running, debugging, and improving Python
programs at the foundational level. It also fully prepares you for the PCEP – Certified
Entry-Level Python Programmer certification exam from the Python Institute.
INTRODUCTION
Python is one of the fastest growing programming languages in the world, and is used in
almost every sector and industry, from gaming, to medicine, to nuclear physics. It is
essential for any would-be programmer to have at least a foundational knowledge of
Python.
Luckily, Python is also one of the easiest programming languages to learn. With its focus
on real-world words and syntax, a beginner learner of Python can start writing simple
programs within minutes
Additionally, it prepares you to take the PCEP Python Certified Entry-Level Python
Programmer exam, which can be taken through the OpenEDG testing platform
TestNow™.
At the end of this book, you will find the complete syllabus for the PCEP Python Certified
Entry-Level Python Programmer exam
Learning Tools
Edube
The material found in this book may also be accessed online at www.edube.org. Here it is
possible to take other courses such as JavaScript Essentials, or C/C++ Essentials, and
progress to the intermediate and advances Python courses. Furthermore, through the
Edube platform, you can purchase exam vouchers and schedule an exam.
Sandbox
The Edube educational platform offers an interactive programming sandbox, where you
can try out the code examples shown in this book. The Sandbox becomes available as
soon as you create an account on Edube.
Answers
Throughout this book you will find quizzes and exercises. You can find the answers, hints,
and sample solutions and the back of the book in the Appendices.
About the course
Welcome to Python Essentials 1! This course has been designed and developed by the
OpenEDG Python Institute in partnership with the Cisco Networking Academy.
The course has been created for anyone and everyone who wants to learn Python and
modern programming techniques. It will particularly appeal to:
• learners looking to gain fundamental skills and knowledge for an entry-level job
role as a software developer, data analyst, or tester;
During the course you will have access to hands-on practice materials, labs, quizzes,
assessments, and tests to learn how to utilize the skills and knowledge gained from
studying the resources and performing coding tasks, and interact with some real-life
programming challenges and situations.
Syllabus
In this course you will learn:
• how to use the most important elements of the Python Standard Library;
Module 1
Introduction to Python and computer programming;
Module 2
Data types, variables, basic input-output operations, and basic operators;
Module 3
Boolean values, conditional execution, loops, lists and list processing, logical and bitwise
operations;
Module 4
Functions, tuples, dictionaries, exceptions, and data processing.
Prepare for the PCEP-30-0x exam
Dive into programming, learn Python from scratch, and prepare for the PCEP –
Certified Entry-Level Python Programmer certification
Python Essentials 1 is aligned with the PCEP – Certified Entry-Level Python Programmer
certification, a professional credential that demonstrates the holder's understanding of the
Python language syntax and semantics, as well as their proficiency in using the most
essential elements of the language, tools, and resources to design, develop, and refactor
simple Python programs.
The certification holder knows the syntax of the Python language to a degree that allows
them to work with variables, operators, control flow mechanisms, and functions, as well
as understands the fundamentals of the Python data type system, exception handling,
troubleshooting, debugging, and the runtime environment.
Becoming PCEP certified will help you stand out from other candidates and get your
foot in the door.
For more information about the PCEP – Certified Entry-Level Python Programmer
certification, please visit www.PythonInstitute.org.
MODULE 1
INTRODUCTION TO
PYTHON AND
COMPUTER
PROGRAMMING
• What Python is, how it is positioned among other programming languages, and
what distinguishes the different versions of Python
SECTION 1.1 –
INTRODUCTION TO
PROGRAMMING
Hello there, and welcome to Module
One! We will start off by learning about
some of the universal concepts of
programming, such as instruction list,
source file, language elements,
compilation and interpretation. Ready?
Let's start!
Python Essentials 1
Computers are able to perform very complex tasks, but this ability is not innate. A
computer's nature is quite different.
It can execute only extremely simple operations. For example, a computer cannot
understand the value of a complicated mathematical function by itself, although this isn't
beyond the realms of possibility in the near future.
Contemporary computers can only evaluate the results of very fundamental operations,
like adding or dividing, but they can do it very fast, and can repeat these actions virtually
any number of times.
Imagine that you want to know the average speed you've reached during a long journey.
You know the distance, you know the time, you need the speed.
Naturally, the computer will be able to compute this, but the computer is not aware of
such things as distance, speed, or time. Therefore, it is necessary to instruct the
computer to:
• divide the former value by the latter and store the result in the memory;
These four simple actions form a program. Of course, these examples are not
formalized, and they are very far from what the computer can understand, but they are
good enough to be translated into a language the computer can accept.
10
Module 1
Another language you use each day is your mother tongue, which you use to manifest
your will and to ponder reality. Computers have their own language, too, called machine
language, which is very rudimentary.
The commands it recognizes are very simple. We can imagine that the computer
responds to orders like "take that number, divide by another and save the result".
No computer is currently capable of creating a new language. However, that may change
soon. Just as people use a number of very different languages, machines have many
different languages, too. The difference, though, is that human languages developed
naturally.
Moreover, they are still evolving, and new words are created every day as old words
disappear. These languages are called natural languages.
An alphabet: a set of symbols used to build words of a certain language (e.g. the Latin
alphabet for English, the Cyrillic alphabet for Russian, Kanji for Japanese, and so on)
A lexis: (aka a dictionary) a set of words the language offers its users (e.g. the word
"computer" comes from the English language dictionary, while "cmoptrue" doesn't; the
word "chat" is present both in English and French dictionaries, but their meanings are
different)
A syntax: a set of rules (formal or informal, written or felt intuitively) used to determine if a
certain string of words forms a valid sentence (e.g. "I am a python" is a syntactically
correct phrase, while "I a python am" isn't)
11
Python Essentials 1
Semantics: a set of rules determining if a certain phrase makes sense (e.g. "I ate a
doughnut" makes sense, but "A doughnut ate me" doesn't)
Unfortunately, this mother tongue is a far cry from a human mother tongue. We both (
computers and humans) need something else, a common language for computers and
humans, or a bridge between the two different worlds.
We need a language in which humans can write their programs and a language that
computers may use to execute the programs, one that is far more complex than machine
language and yet far simpler than natural language.
Such languages are often called high-level programming languages. They are at least
somewhat similar to natural ones in that they use symbols, words and conventions
readable to humans. These languages enable humans to express commands to
computers that are much more complex than those offered by ILs.
• lexically – each programming language has its dictionary and you need to master
it; thankfully, it's much simpler and smaller than the dictionary of any natural
language;
12
Module 1
• syntactically – each language has its rules and they must be obeyed;
Unfortunately, a programmer can also make mistakes with each of the above four
senses. Each of them can cause the program to become completely useless.
Let's assume that you've successfully written a program. How do we persuade the
computer to execute it? You have to render your program into machine language. Luckily,
the translation can be done by a computer itself, making the whole process fast and
efficient.
There are very few languages that can be both compiled and interpreted. Usually, a
programming language is projected with this factor in its constructors' minds – will it be
compiled or interpreted?
13
Python Essentials 1
Note: it has to be pure text, without any decorations like different fonts, colors,
embedded images or other media. Now you have to invoke the interpreter and let it read
your source file.
The interpreter reads the source code in a way that is common in Western culture: from
top to bottom and from left to right. There are some exceptions – they'll be covered later
in the course.
First of all, the interpreter checks if all subsequent lines are correct (using the four
aspects covered earlier).
If the compiler finds an error, it finishes its work immediately. The only result in this case
is an error message.
The interpreter will inform you where the error is located and what caused it. However,
these messages may be misleading, as the interpreter isn't able to follow your exact
intentions, and may detect errors at some distance from their real causes.
For example, if you try to use an entity of an unknown name, it will cause an error, but the
error will be discovered in the place where it tries to use the entity, not where the new
entity's name was introduced.
In other words, the actual reason is usually located a little earlier in the code, for
example, in the place where you had to inform the interpreter that you were going to use
the entity of the name.
If the line looks good, the interpreter tries to execute it (note: each line is usually
executed separately, so the trio "read-check-execute" can be repeated many times –
more times than the actual number of lines in the source file, as some parts of the code
may be executed more than once).
It is also possible that a significant part of the code may be executed successfully before
the interpreter finds an error. This is normal behavior in this execution model.
You may ask now: which is better? The "compiling" model or the "interpreting" model?
There is no obvious answer. If there had been, one of these models would have ceased
to exist a long time ago. Both of them have their advantages and their disadvantages.
14
Module 1
15
Python Essentials 1
Compilation Interpretation
16
Module 1
• If you want to program in Python, you'll need the Python interpreter. You won't be
able to run your code without it. Fortunately, Python is free. This is one of its most
important advantages.
17
SECTION 1.2 –
INTRODUCTION TO
PYTHON
Here, we will learn a little bit about the
history of Python, different Python
versions and implementations, and the
impact that Python has had on modern-
day programming. Let's begin.
Python Essentials 1
What is Python?
Python is a widely-used, interpreted, object-oriented, and high-level programming
language with dynamic semantics, used for general-purpose programming.
And while you may know the python as a large snake, the name of the Python
programming language comes from an old BBC television comedy sketch series called
Monty Python's Flying Circus.
At the height of its success, the Monty Python team were performing their sketches to live
audiences across the world, including at the Hollywood Bowl.
Since Monty Python is considered one of the two fundamental nutrients to a programmer
(the other being pizza), Python's creator named the language in honor of the TV show.
20
Module 1
There are not many languages whose authors are known by name. Python was created
by Guido van Rossum, born in 1956 in Haarlem, the Netherlands. Of course, Guido van
Rossum did not develop and evolve all the Python components himself.
The speed with which Python has spread around the world is a result of the continuous
work of thousands (very often anonymous) programmers, testers, users (many of them
aren't IT specialists) and enthusiasts, but it must be said that the very first idea (the seed
from which Python sprouted) came to one head – Guido's.
21
Python Essentials 1
Python goals
In 1999, Guido van Rossum defined his goals for Python:
• an easy and intuitive language just as powerful as those of the major competitors;
About 20 years later, it is clear that all these intentions have been fulfilled. Some sources
say that Python is the most popular programming language in the world, while others
claim it's the second or the third.
Either way, it still occupies a high rank in the top ten of the PYPL PopularitY of
Programming Language and the TIOBE Programming Community Index.
Python isn't a young language anymore. It is mature and trustworthy. It's not a one-hit
wonder. It's a bright star in the programming firmament, and time spent learning Python is
a very good investment.
22
Module 1
Why Python?
How does it happen that programmers, young and old, experienced and novice, want to
use it? How did it happen that large companies adopted Python and implemented their
flagship products using it?
There are many reasons – we've listed some of them already, but let's enumerate them
again in a more practical manner:
• it's easy to learn – the time needed to learn Python is shorter than for many other
languages; this means that it's possible to start the actual programming faster;
• it's easy to teach – the teaching workload is smaller than that needed by other
languages; this means that the teacher can put more emphasis on general
(language-independent) programming techniques, not wasting energy on exotic
tricks, strange exceptions and incomprehensible rules;
• it's easy to use for writing new software – it's often possible to write code faster
when using Python;
• it's easy to understand – it's also often easier to understand someone else's code
faster if it is written in Python;
• it's easy to obtain, install and deploy – Python is free, open and multiplatform; not
all languages can boast that.
Python rivals
Python has two direct competitors, with comparable properties and predispositions.
These are:
The former is more traditional and more conservative than Python, and resembles some
of the old languages derived from the classic C programming language.
In contrast, the latter is more innovative and more full of fresh ideas than Python. Python
itself lies somewhere between these two creations.
23
Python Essentials 1
The Internet is full of forums with infinite discussions on the superiority of one of these
three over the others, should you wish to learn more about each of them.
Many developing tools are implemented in Python. More and more everyday-use
applications are being written in Python. Lots of scientists have abandoned expensive
proprietary tools and switched to Python. Lots of IT project testers have started using
Python to carry out repeatable test procedures. The list is long.
24
Module 1
Python 2 is an older version of the original Python. Its development has since been
intentionally stalled, although that doesn't mean that there are no updates to it. On the
contrary, the updates are issued on a regular basis, but they are not intended to modify
the language in any significant way. They rather fix any freshly discovered bugs and
security holes. Python 2's development path has reached a dead end already, but Python
2 itself is still very much alive.
Python 3 is the newer (or to be more precise, the current) version of the language.
It's going through its own evolutionary path, creating its own standards and habits.
These two versions of Python aren't compatible with each other. Python 2 scripts won't
run in a Python 3 environment and vice versa, so if you want the old Python 2 code to be
run by a Python 3 interpreter, the only possible solution is to rewrite it, not from scratch,
of course, as large parts of the code may remain untouched, but you do have to revise all
the code to find all possible incompatibilities. Unfortunately, this process cannot be fully
automatized.
It's too hard, too time-consuming, too expensive, and too risky to migrate an old Python 2
application to a new platform, and it's even possible that rewriting the code will introduce
new bugs into it. It's easier, and more sensible, to leave these systems alone and to
improve the existing interpreter, instead of trying to work inside the already functioning
source code.
If you're modifying an old existing Python solution, then it's highly likely that it was coded
in Python 2. This is the reason why Python 2 is still in use. There are too many existing
Python 2 applications to discard it altogether.
NOTE
If you're going to start a new Python project, you should use Python 3, and this is the
version of Python that will be used during this course.
25
Python Essentials 1
All the code samples you will find during the course have been tested against Python 3.4,
Python 3.6, Python 3.7, Python 3.8, and Python 3.9.
Python implementations
In addition to Python 2 and Python 3, there is more than one version of each.
According to the Python wiki page, an implementation of Python refers to "a program or
environment, which provides support for the execution of programs written in the Python
language, as represented by the CPython reference implementation."
But, first things first. There are the Pythons which are maintained by the people gathered
around the PSF (Python Software Foundation), a community that aims to develop,
improve, expand, and popularize Python and its environment. The PSF's president is
Guido von Rossum himself, and for this reason, these Pythons are called canonical.
They are also considered to be reference Pythons, as any other implementation of the
language should follow all standards established by the PSF.
Guido van Rossum used the "C" programming language to implement the very first
version of his language and this decision is still in force. All Pythons coming from the PSF
are written in the "C" language. There are many reasons for this approach. One of them
(probably the most important) is that thanks to it, Python may be easily ported and
migrated to all platforms with the ability to compile and run "C" language programs
(virtually all platforms have this feature, which opens up many expansion opportunities for
Python).
This is why the PSF implementation is often referred to as CPython. This is the most
influential Python among all the Pythons in the world.
26
Module 1
How are these two contradictions reconciled? One solution is to write your mathematical
ideas using Python, and when you're absolutely sure that your code is correct and
produces valid results, you can translate it into "C". Certainly, "C" will run much faster
than pure Python.
This is what Cython is intended to do – to automatically translate the Python code (clean
and clear, but not too swift) into "C" code (complicated and talkative, but agile).
Jython can communicate with existing Java infrastructure more effectively. This is why
some projects find it useful and necessary.
Note: the current Jython implementation follows Python 2 standards. There is no Jython
conforming to Python 3, so far.
27
Python Essentials 1
The PyPy logo is a rebus. Can you solve it? It means: a Python within a Python. In other
words, it represents a Python environment written in Python-like language named
RPython (Restricted Python). It
is actually a subset of Python.
This doesn't make PyPy any less important or less serious than CPython, of course.
There are many more different Pythons in the world. You'll find them if you look, but this
course will focus on CPython.
MicroPython is an efficient
open source software
implementation of Python 3
that is optimized to run on
microcontrollers. It includes
a small subset of the Python
Standard Library, but it is largely packed with a large number of features such as
interactive prompt or arbitrary precision integers, as well as modules that give the
programmer access to low-level hardware.
Originally created by Damien George, an Australian programmer, who in the year 2013
ran a successful campaign on Kickstarter, and released the first MicroPython version with
an STM32F4-powered development board called pyboard.
28
Module 1
In 2017, MicroPython was used to create CircuitPython, another one open source
programming language that runs on the microcontroller hardware, which is a derivative of
the MicroPython language.
29
SECTION 1.3 –
DOWNLOADING AND
INSTALLING PYTHON
Here we will talk about the ways of
obtaining, installing, and configuring
Python on your local computer. This
section is optional, as throughout the
course you will be able to launch, test,
and experiment with all your Python
programs in Edube Interactive TM, the
programming environment that we have
integrated with the learning platform and
these study resources. Still, if you can
download and install Python on your
local machine, we strongly recommend
it.
Python Essentials 1
Linux users most probably have Python already installed – this is the most likely
scenario, as Python's infrastructure is intensively used by many Linux OS components.
For example, some distributors may couple their specific tools together with the system
and many of these tools, like package managers, are often written in Python. Some parts
of graphical environments available in the Linux world may use Python, too.
python3
at the shell prompt, press Enter and wait. If you see something like this:
32
Module 1
If Python 3 is absent, then refer to your Linux documentation in order to find out how to
use your package manager to download and install a new package – the one you need is
named python3 or its name begins with that.
In this case, select Python 3. The site always offers you the latest version of it.
If you're a Windows user, start the downloaded .exe file and follow all the steps.
Leave the default settings the installer suggests for now, with one exception – look at the
checkbox named Add Python 3.x to PATH and check it.
33
Python Essentials 1
If you're a macOS user, a version of Python 2 may already have been preinstalled on
your computer, but since we will be working with Python 3, you will still need to download
and install the relevant .pkg file from the Python site.
This will be a very simple procedure, but it should be enough to convince you that the
Python environment is complete and functional.
There are many ways of utilizing Python, especially if you're going to be a Python
developer.
• an editor which will support you in writing the code (it should have some special
features, not available in simple tools); this dedicated editor will give you more than
the standard OS equipment;
• a console in which you can launch your newly written code and stop it forcibly
when it gets out of control;
• a tool named a debugger, able to launch your code step-by-step, which will allow
you to inspect it at each moment of execution.
Besides its many useful components, the Python 3 standard installation contains a very
simple but extremely useful application named IDLE.
Navigate through your OS menus, find IDLE somewhere under Python 3.x and launch it.
This is what you should see:
34
Module 1
The first step is to create a new source file and fill it with code. Click File in the IDLE
menu and choose New file.
35
Python Essentials 1
As you can see, IDLE opens a new window for you. You can use it to write and amend
your code.
This is the editor window. Its only purpose is to be a workplace in which your source
code is treated. Do not confuse the editor window with the shell window. They perform
different functions.
The editor window is currently untitled, but it's good practice to start work by naming the
source file.
Click File (in the new window), then click Save as..., select a folder for the new file (the
desktop is a good place for your first programming attempts) and chose a name for the
new file.
Note: don't set any extension for the file name you are going to use. Python needs its
files to have the .py extension, so you should rely on the dialog window's defaults. Using
the standard .py extension enables the OS to properly open these files.
Now put just one line into your newly opened and named editor window.
print("Hisssssss...")
We're not going to explain the meaning of the program right now. You'll find a detailed
discussion in the next chapter.
36
Module 1
Take a closer look at the quotation marks. These are the simplest form of quotation
marks (neutral, straight, dumb, etc.) commonly used in source files. Do not try to use
typographic quotes (curved, curly, smart, etc.), used by advanced text processors, as
Python doesn't accept them.
Save the file (File -> Save) and run the program (Run -> Run Module).
If everything goes okay and there are no mistakes in the code, the console window will
show you the effects caused by running the program.
37
Python Essentials 1
• Click File, Open, point to the file you saved previously and let IDLE read it in.
As you can see, IDLE is able to save your code and retrieve it when you need it again.
Hisssssss...
Every time you put the closing parenthesis in your program, IDLE will show the part of the
text limited with a pair of corresponding parentheses. This helps you to remember to
place them in pairs.
38
Module 1
Remove the closing parenthesis again. The code becomes erroneous. It contains a
syntax error now. IDLE should not let you run it.
Try to run the program again. IDLE will remind you to save the modified file. Follow the
instructions.
A new window appears – it says that the interpreter has encountered an EOF (end-of-file)
although (in its opinion) the code should contain some more text.
39
Python Essentials 1
print("Hisssssss...")
Let's spoil the code one more time. Remove one letter from the word print . Run the
code by pressing F5. What happens now? As you can see, Python is not able to
recognize the instruction.
You may have noticed that the error message generated for the previous error is quite
different from the first one.
40
Module 1
This is because the nature of the error is different and the error is discovered at a
different stage of interpretation.
The editor window will not provide any useful information regarding the error, but the
console windows might.
• the traceback (which is the path that the code traverses through different parts of
the program – you can ignore it for now, as it is empty in such a simple code);
• the location of the error (the name of the file containing the error, line number and
module name); note: the number may be misleading, as Python usually shows the
place where it first notices the effects of the error, not necessarily the error itself;
• the content of the erroneous line; note: IDLE's editor window doesn't show line
numbers, but it displays the current cursor location at the bottom-right corner; use it
to locate the erroneous line in a long source code;
Experiment with creating new files and running your code. Try to output a different
message to the screen, e.g. roar! , meow , or even maybe an oink! . Try to spoil
and fix your code – see what happens.
41
Python Essentials 1
42
MODULE 2
PYTHON DATA TYPES,
VARIABLES,
OPERATORS, AND
BASIC I/O OPERATIONS
As we're going to show you some fundamental concepts and terms, these snippets of
code won't be all that serious or complex.
Run the following code. If everything goes okay here, you'll see the line of text in the
console window.
Alternatively, launch IDLE, create a new Python source file, fill it with this code, name the
file and save it. Now run it. If everything goes okay, you'll see the text contained within the
quotation marks in the IDLE console window. The code you have run should look familiar.
You saw something very similar when we led you through the setting up of the IDLE
environment.
1 print("Hello, world!")
2
Now we'll spend some time showing and explaining to you what you're actually seeing,
and why it looks like this.
As you can see, this first program consists of the following parts:
• an opening parenthesis;
• a quotation mark;
• a closing parenthesis.
46
Module 2
The word print that you can see here is a function name. That doesn't mean that
wherever the word appears it is always a function name. The meaning of the word comes
from the context in which the word has been used.
You've probably encountered the term function many times before, during math classes.
You can probably also list several names of mathematical functions, like sine or log.
Python functions, however, are more flexible, and can contain more content than their
mathematical siblings.
A function (in this context) is a separate part of the computer code able to:
• cause some effect (e.g. send text to the terminal, create a file, draw an image, play
a sound, etc.); this is something completely unheard of in the world of mathematics;
• evaluate a value (e.g. the square root of a value or the length of a given text) and
return it as the function's result; this is what makes Python functions the relatives
of mathematical concepts.
• From modules: they may come from one or more of Python's add-ons named
modules; some of the modules come with Python, others may require separate
installation – whatever the case, they all need to be explicitly connected with your
code (we'll show you how to do that soon);
• From your code: you can write them yourself, placing as many functions as you
want and need inside your program to make it simpler, clearer and more elegant.
47
Python Essentials 1
The name of the function should be significant (the name of the print function is self-
evident).
Of course, if you're going to make use of any already existing function, you have no
influence on its name, but when you start writing your own functions, you should consider
carefully your choice of names.
48
Module 2
• an effect;
• a result.
Mathematical functions usually take one argument. For example, sin(x) takes an x, which
is the measure of an angle.
Python functions, on the other hand, are more versatile. Depending on the individual
needs, they may accept any number of arguments ‒ as many as necessary to perform
their tasks. Note: When we said any number, that includes zero ‒ some Python functions
don't need any argument.
print("Hello, World!")
If you want to deliver one or more arguments to a function, you place them inside the
parentheses. If you're going to use a function which doesn't take any argument, you still
have to have the parentheses.
Note: to distinguish ordinary words from function names, place a pair of empty
parentheses after their names, even if the corresponding function wants one or more
arguments. This is a standard convention.
print("Hello, World!")
49
Python Essentials 1
As you can see, the string is delimited with quotes ‒ in fact, the quotes make the string
‒ they cut out a part of the code and assign a different meaning to it.
You can imagine that the quotes say something like: the text between us is not code. It
isn't intended to be executed, and you should take it as is.
Almost anything you put inside the quotes will be taken literally, not as code, but as data.
Try to play with this particular string ‒ modify it, enter some new content, delete some of
the existing content.
There's more than one way to specify a string inside Python's code, but for now, though,
this one is enough.
So far, you have learned about two important parts of the code: the function and the
string. We've talked about them in terms of syntax, but now it's time to discuss them in
terms of semantics.
50
Module 2
We'll discuss this in more depth soon, but let's just shed a little light on it right now.
function_name(argument)
Let's see:
• First, Python checks if the name specified is legal (it browses its internal data in
order to find an existing function of the name; if this search fails, Python aborts the
code)
• second, Python checks if the function's requirements for the number of arguments
allows you to invoke the function in this way (e.g. if a specific function demands
exactly two arguments, any invocation delivering only one argument will be
considered erroneous, and will abort the code's execution)
• third, Python leaves your code for a moment and jumps into the function you want
to invoke; of course, it takes your argument(s) too and passes it/them to the
function;
• fourth, the function executes its code, causes the desired effect (if any), evaluates
the desired result(s) (if any) and finishes its task;
• finally, Python returns to your code (to the place just after the invocation) and
resumes its execution.
51
Python Essentials 1
• Use the print() function to print the line Hello, Python! to the screen.
Use double quotes around the string.
• Having done that, use the print() function again, but this time print your first
name.
• Remove the double quotes and run your code. Watch Python's reaction. What kind
of error is thrown?
• Then, remove the parentheses, put back the double quotes, and run your code
again. What kind of error is thrown this time?
• Experiment as much as you can. Change double quotes to single quotes, use
multiple print() functions on the same line, and then on different lines. See
what happens.
52
Module 2
• takes its arguments (it may accept more than one argument and may also accept
less than one argument)
• converts them into human-readable form if needed (as you may suspect, strings
don't require this action, as the string is already readable)
• and sends the resulting data to the output device (usually the console); in other
words, anything you put into the print() function will appear on your screen.
No wonder then, that from now on, you'll utilize print() very intensively to see the
results of your operations and evaluations.
Any. We'll show you soon that print() is able to operate with virtually all types of
data offered by Python. Strings, numbers, characters, logical values, objects ‒ any of
these may be successfully passed to print() .
53
Python Essentials 1
2.1.6 Instructions
You have already seen a computer program that contains one function invocation. A
function invocation is one of many possible kinds of Python instruction.
Of course, any complex program usually contains many more instructions than one. The
question is: how do you couple more than one instruction into the Python code?
Python's syntax is quite specific in this area. Unlike most programming languages,
Python requires that there cannot be more than one instruction in a line.
A line can be empty (i.e. it may contain no instruction at all) but it must not contain two,
three or more instructions. This is strictly prohibited.
Note: Python makes one exception to this rule ‒ it allows one instruction to spread across
more than one line (which may be helpful when your code contains complex
constructions).
Let's expand the code a bit. Run it and note what you see.
1 print("The itsy bitsy spider climbed up the waterspout.")
2 print("Down came the rain and washed the spider out.")
3
• the program invokes the print() function twice, and you can see two
separate lines in the console ‒ this means that print() begins its output from a
new line each time it starts its execution; you can change this behavior, but you can
also use it to your advantage;
• each print() invocation contains a different string, as its argument, and the
console content reflects it ‒ this means that the instructions in the code are
executed in the same order in which they have been placed in the source file; no
subsequent instruction is executed until the previous one is completed (there are
some exceptions to this rule, but you can ignore them for now.)
54
Module 2
We've changed the example a bit ‒ we've added one empty print() function
invocation. We call it empty because we haven't delivered any arguments to the function.
What happens?
1 print("The itsy bitsy spider climbed up the waterspout.")
2 print()
3 print("Down came the rain and washed the spider out.")
4
As you can see, the empty print() invocation is not as empty as you may have
expected ‒ it does output an empty line, or (this interpretation is also correct) it outputs a
newline.
This is not the only way to produce a newline in the output console. We're now going to
show you another way.
55
Python Essentials 1
There are two very subtle changes ‒ we've inserted a strange pair of characters inside
the rhyme. They look like this: \n .
1 print("The itsy bitsy spider\nclimbed up the waterspout.")
2 print()
3 print("Down came the rain\nand washed the spider out.")
4
Interestingly, while you can see two characters, Python sees one.
The backslash ( \ ) has a very special meaning when used inside strings ‒ this is called
the escape character.
The word escape should be understood specifically ‒ it means that the series of
characters in the string escapes for the moment (a very short moment) to introduce a
special inclusion.
In other words, the backslash doesn't mean anything in itself, but is only a kind of
announcement, that the next character after the backslash has a different meaning too.
The letter n placed after the backslash comes from the word newline.
Both the backslash and the n form a special symbol named a newline character,
which urges the console to start a new output line.
Run the code. Your console should now look like this:
As you can see, two newlines appear in the nursery rhyme, in the places where the \n
have been used.
56
Module 2
1. If you want to put just one backslash inside a string, don't forget its escaping nature ‒
you have to double it. For example, an invocation like this will cause an error:
print("\")
print("\\")
2. Not all escape pairs (the backslash coupled with another character) mean something.
Experiment with your code, run it, and see what happens.
1 print("The itsy bitsy spider\nclimbed up the waterspout.")
2 print()
3 print("Down came the rain\nand washed the spider out.")
4
57
Python Essentials 1
There is one print() function invocation, but it contains three arguments. All of
them are strings.
The arguments are separated by commas. We've surrounded them with spaces to
make them more visible, but it's not really necessary, and we won't be doing it anymore.
In this case, the commas separating the arguments play a completely different role than
the comma inside the string. The former is a part of Python's syntax, while the latter is
intended to be shown in the console.
If you look at the code again, you'll see that there are no spaces inside the strings.
The spaces, removed from the strings, have appeared again. Can you explain why?
• a print() function invoked with more than one argument outputs them all on
one line;
• the print() function puts a space between the outputted arguments on its
own initiative.
58
Module 2
You should be able to predict the output without running the code.
1 print("My name is", "Python.")
2 print("Monty Python.")
3
The way in which we are passing the arguments into the print() function is the most
common in Python, and is called the positional way. This name comes from the fact that
the meaning of the argument is dictated by its position (e.g. the second argument will be
outputted after the first, not the other way round).
Run the code and check if the output matches your predictions.
59
Python Essentials 1
We aren't going to explain it in depth right now. We plan to do this when we talk about
functions. For now, we simply want to show you how it works. Feel free to use it in your
own programs.
The mechanism is called keyword arguments. The name stems from the fact that the
meaning of these arguments is taken not from its location (position) but from the special
word (keyword) used to identify them.
The print() function has two keyword arguments that you can use for your
purposes. The first is called end .
• any keyword arguments have to be put after the last positional argument (this is
very important)
In our example, we have made use of the end keyword argument, and set it to a string
containing one space.
As you can see, the end keyword argument determines the characters the
print() function sends to the output once it reaches the end of its positional
arguments.
60
Module 2
The default behavior reflects the situation where the end keyword argument is
implicitly used in the following way: end="\n" .
If you look carefully, you'll see that we've used the end argument, but the string
assigned to it is empty (it contains no characters at all).
As the end argument has been set to nothing, the print() function outputs
nothing too, once its positional arguments have been exhausted.
The string assigned to the end keyword argument can be of any length. Experiment
with it if you want.
We said previously that the print() function separates its outputted arguments with
spaces. This behavior can be changed, too.
The keyword argument that can do this is named sep (as in separator).
My-name-is-Monty-Python.
The print() function now uses a dash, instead of a space, to separate the outputted
arguments.
Note: the sep argument's value may be an empty string, too. Try it for yourself.
Both keyword arguments may be mixed in one invocation, just like here.
61
Python Essentials 1
The example doesn't make much sense, but it visibly presents the interactions between
end and sep .
Now that you understand the print() function, you're ready to consider how to store
and process data in Python.
62
Module 2
Expected output
Programming***Essentials***in...Python
Code
1 print("Programming","Essentials","in")
2 print("Python")
3
63
Python Essentials 1
Try to:
• duplicate the arrow, placing both arrows side by side; note: a string may be
multiplied by using the following trick: "string" * 2 will produce
"stringstring" (we'll tell you more about it soon)
• remove any of the quotes, and look carefully at Python's response; pay attention to
where Python sees an error ‒ is this the place where the error really exists?
• change any of the print words into something else, differing only in case (e.g.
Print ) ‒ what happens now?
• replace some of the quotes with apostrophes; watch what happens carefully.
Code
1
2
3 print(" *")
4 print(" * *")
5 print(" * *")
6 print(" * *")
7 print("*** ***")
8 print(" * *")
9 print(" * *")
10 print(" *****")
11
64
Module 2
2. Built-in functions, contrary to user-defined functions, are always available and don't
have to be imported. Python 3.8 comes with 69 built-in functions. You can find their full
list provided in alphabetical order in the Python Standard Library.
3. To call a function (this process is known as function invocation or function call), you
need to use the function name followed by parentheses. You can pass arguments into a
function by placing them inside the parentheses. You must separate arguments with a
comma, e.g. print("Hello,", "world!") . An "empty" print() function
outputs an empty line to the screen.
4. Python strings are delimited with quotes, e.g. "I am a string" (double quotes),
or 'I am a string, too' (single quotes).
6. In Python strings the backslash ( \ ) is a special character which announces that the
next character has a different meaning, e.g. \n (the newline character) starts a new
output line.
7. Positional arguments are the ones whose meaning is dictated by their position, e.g.
the second argument is outputted after the first, the third is outputted after the second,
etc.
8. Keyword arguments are the ones whose meaning is not dictated by their location, but
by a special word (keyword) used to identify them.
9. The end and sep parameters can be used for formatting the output of the
print() function. The sep parameter specifies the separator between the
outputted arguments, e.g. print("H", "E", "L", "L", "O", sep="-") ,
whereas the end parameter specifies what to print at the end of the print statement.
65
Python Essentials 1
print('Greg\'s book.')
print("'Greg's book.'")
print('"Greg\'s book."')
print("Greg\'s book.")
print('"Greg's book."')
Check Answers
66
SECTION 2.2 – PYTHON
LITERALS
Now it's time to talk about Python literals.
Python Essentials 1
123
Can you guess what value it represents? Of course you can ‒ it's one hundred twenty
three.
Does it represent any value? Maybe. It can be the symbol of the speed of light, for
example. It can also be the constant of integration. Or even the length of a hypotenuse in
the sense of a Pythagorean theorem. There are many possibilities.
You cannot choose the right one without some additional knowledge.
You use literals to encode data and to put them into your code. We're now going to
show you some conventions you have to obey when using Python.
Let's start with a simple experiment ‒ take a look at the following snippet.
1 print("2")
2 print(2)
3
The first line looks familiar. The second seems to be erroneous due to the visible lack of
quotes.
68
Module 2
The print() function presents them in exactly the same way ‒ this example is
obvious, as their human-readable representation is also the same. Internally, in the
computer's memory, these two values are stored in completely different ways ‒ the string
exists as just a string ‒ a series of letters.
The number is converted into machine representation (a set of bits). The print()
function is able to show them both in a form readable to humans.
We're now going to be spending some time discussing numeric literals and their internal
life.
69
Python Essentials 1
2.2.2 Integers
You may already know a little about how computers perform calculations on numbers.
Perhaps you've heard of the binary system, and know that it's the system computers
use for storing numbers, and that those computers can perform any operation upon them.
We won't explore the intricacies of positional numeric systems here, but we will say that
the numbers handled by modern computers are of two types:
• integers, that is, those which are devoid of the fractional part;
• and floating-point numbers (or simply floats), that contain (or are able to contain)
the fractional part.
This definition is not entirely accurate, but quite sufficient for now. The distinction is very
important, and the boundary between these two types of numbers is very strict. Both of
these kinds of numbers differ significantly in how they're stored in a computer memory
and in the range of acceptable values.
The characteristic of the numeric value which determines its kind, range, and application,
is called the type.
If you encode a literal and place it inside Python code, the form of the literal determines
the representation (type) Python will use to store it in the memory.
For now, let's leave the floating-point numbers aside (we'll come back to them soon) and
consider the question of how Python recognizes integers.
The process is almost like how you would write them with a pencil on paper – it's simply a
string of digits that make up the number. But there's a reservation ‒ you must not interject
any characters that are not digits inside the number.
Take, for example, the number eleven million one hundred eleven thousand one hundred
eleven. If you took a pencil in your hand right now, you would write the number like this:
11,111,111 , or like this: 11.111.111 , or even like this: 11 111 111 .
It's clear that this provision makes it easier to read, especially when the number consists
of many digits. However, Python doesn't accept things like these. It's prohibited. What
Python does allow, though, is the use of underscores in numeric literals.*
Therefore, you can write this number either like this: 11111111 , or like this: 11_
111_111 .
70
Module 2
NOTE Python 3.6 has introduced underscores in numeric literals, allowing for the
placement of single underscores between digits and after base specifiers for improved
readability. This feature is not available in older versions of Python.
And how do we code negative numbers in Python? As usual ‒ by adding a minus. You
can write: -11111111 , or -11_111_111 .
Positive numbers do not need to be preceded by the plus sign, but it's permissible, if you
wish to do it. The following lines describe the same number: +11111111 and
11111111 .
The second convention allows us to use hexadecimal numbers. Such numbers should
be preceded by the prefix 0x or 0X (zero-x).
71
Python Essentials 1
2.2.3 Floats
Now it's time to talk about another type, which is designed to represent and to store the
numbers that (as a mathematician would say) have a non-empty decimal fraction.
They are the numbers that have (or may have) a fractional part after the decimal point,
and although such a definition is very poor, it's certainly sufficient for what we wish to
discuss.
Whenever we use a term like two and a half or minus zero point four, we think of
numbers which the computer considers floating-point numbers:
2.5
-0.4
Note: two and a half looks normal when you write it in a program, although if your native
language prefers to use a comma instead of a point in the number, you should ensure
that your number doesn't contain any commas at all.
Python will not accept that, or (in very rare but possible cases) may misunderstand your
intentions, as the comma itself has its own reserved meaning in Python.
If you want to use just a value of two and a half, you should write it as shown previously.
Note once again: there is a point between 2 and 5, not a comma.
As you can probably imagine, the value of zero point four could be written in Python as:
0.4
But don't forget this simple rule ‒ you can omit zero when it is the only digit in front of or
after the decimal point.
.4
4.
72
Module 2
4
4.0
You may think that they are exactly the same, but Python sees them in a completely
different way.
On the other hand, it's not only points that make a float. You can also use the letter e .
When you want to use any numbers that are very large or very small, you can use
scientific notation.
Take, for example, the speed of light, expressed in meters per second. Written directly it
would look like this: 300000000 .
To avoid writing out so many zeros, physics textbooks use an abbreviated form, which
you have probably already seen: 3 x 108 .
In Python, the same effect is achieved in a slightly different way ‒ take a look:
3E8
The letter E (you can also use the lower-case letter e ‒ it comes from the word
exponent) is a concise record of the phrase times ten to the power of.
Note:
Coding floats
Let's see how this convention is used to record numbers that are very small (in the sense
of their absolute value, which is close to zero).
73
Python Essentials 1
A physical constant called Planck's constant (and denoted as h), according to the
textbooks, has the value of: 6.62607 x 10-34.
If you would like to use it in a program, you should write it this way:
6.62607E-34
Note: the fact that you've chosen one of the possible forms of coding float values doesn't
mean that Python will present it the same way.
For example, let's say you've decided to use the following float literal:
0.0000000000000000000001
1e-22
Python always chooses the more economical form of the number's presentation, and
you should take this into consideration when creating literals.
74
Module 2
2.2.4 Strings
Strings are used when you need to process text (like names of all kinds, addresses,
novels, etc.), not numbers.
You already know a bit about them, e.g. that strings need quotes the way floats need
points.
However, there is a catch. The catch is how to encode a quote inside a string which is
already delimited by quotes.
The first is based on the concept we already know of the escape character, which you
should remember is played by the backslash. The backslash can escape quotes too. A
quote preceded by a backslash changes its meaning ‒ it's not a delimiter, but just a
quote. This will work as intended:
1 print("I like \"Monty Python\"")
2
Note: there are two escaped quotes inside the string ‒ can you see them both?
The second solution may be a bit surprising. Python can use an apostrophe instead of
a quote. Either of these characters may delimit strings, but you must be consistent.
If you open a string with a quote, you have to close it with a quote.
If you start a string with an apostrophe, you have to end it with an apostrophe.
75
Python Essentials 1
Coding strings
Now, the next question is: how do you embed an apostrophe into a string placed between
apostrophes?
You should already know the answer, or to be precise, two possible answers.
Do you know how to do it? Check in the answers section to see if you were right:
As you can see, the backslash is a very powerful tool ‒ it can escape not only quotes, but
also apostrophes.
We've shown it already, but we want to emphasize this phenomenon once more: a string
can be empty ‒ it may contain no characters at all.
''
""
76
Module 2
True
False
You cannot change anything ‒ you have to take these symbols as they are, including
case-sensitivity.
77
Python Essentials 1
Expected output
"I'm"
""learning""
"""Python"""
Check Hint
78
Module 2
2. The binary system is a system of numbers that employs 2 as the base. Therefore, a
binary number is made up of 0s and 1s only, e.g. 1010 is 10 in decimal.
Octal and hexadecimal numeration systems, similarly, employ 8 and 16 as their bases
respectively. The hexadecimal system uses the decimal numbers and six extra letters.
3. Integers (or simply ints) are one of the numerical types supported by Python. They
are numbers written without a fractional component, e.g. 256, or -1 (negative integers).
4. Floating-point numbers (or simply floats) are another one of the numerical types
supported by Python. They are numbers that contain (or are able to contain) a fractional
component, e.g. 1.27.
5. To encode an apostrophe or a quote inside a string, you can either use the escape
character, e.g. 'I\'m happy.', or open and close the string using an opposite set of
symbols to the ones you wish to encode, e.g. "I'm happy." to encode an apostrophe,
and 'He said "Python", not "typhoon"' to encode a (double) quote.
6. Boolean values are the two constant objects True and False used to represent truth
values (in numeric contexts 1 is True, while 0 is False.
EXTRA
There is one more, special literal that is used in Python: the None literal. This literal is a
NoneType object, and it is used to represent the absence of a value. We'll tell you more
about it soon.
79
Python Essentials 1
1011
Check Answers
80
SECTION 2.3 –
OPERATORS: DATA
MANIPULATION TOOLS
In this section, we will talk about Python
operators.
Python Essentials 1
You should see the number four. Feel free to experiment with other operators.
Without taking this too seriously, you've just discovered that Python can be used as a
calculator. Not a very handy one, and definitely not a pocket one, but a calculator
nonetheless.
Taking it more seriously, we are now entering the province of operators and
expressions.
82
Module 2
For example, just as in arithmetic, the + (plus) sign is the operator which is able to add
two numbers, giving the result of the addition.
Not all Python operators are as obvious as the plus sign, though, so let's go through
some of the operators available in Python, and we'll explain which rules govern their use,
and how to interpret the operations they perform.
We'll begin with the operators which are associated with the most widely recognizable
arithmetic operations:
//
**
The order of their appearance is not accidental. We'll talk more about it once we've gone
through them all.
Remember: Data and operators when connected together form expressions. The
simplest expression is a literal itself.
Exponentiation
Look at the following example:
1 print(2 ** 3)
2 print(2 ** 3.)
3 print(2. ** 3)
4 print(2. ** 3.)
5
83
Python Essentials 1
Note: we've surrounded the double asterisks with spaces in our examples. It's not
compulsory, but it improves the readability of the code.
The examples show a very important feature of virtually all Python numerical operators.
Run the code and look carefully at the results it produces. Can you see any regularity
here?
Remember: It's possible to formulate the following rules based on this result:
Multiplication
An * (asterisk) sign is a multiplication operator.
Run the following code and check if our integer vs. float rule is still working.
1 print(2 * 3)
2 print(2 * 3.)
3 print(2. * 3)
4 print(2. * 3.)
5
Division
A / (slash) sign is a division operator.
The value in front of the slash is a dividend, the value behind the slash, a divisor.
84
Module 2
The result produced by the division operator is always a float, regardless of whether
or not the result seems to be a float at first glance: 1 / 2 , or if it looks like a pure
integer: 2 / 1 .
Is this a problem? Yes, it is. It happens sometimes that you really need a division that
provides an integer value, not a float.
• its result lacks the fractional part ‒ it's absent (for integers), or is always equal to
zero (for floats); this means that the results are always rounded;
As you can see, integer by integer division gives an integer result. All other cases
produce floats.
85
Python Essentials 1
The result of integer division is always rounded to the nearest integer value that is less
than the real (not rounded) result.
Look at the following code and try to predict the results once again:
1 print(-6 // 4)
2 print(6. // -4)
3
Note: some of the values are negative. This will obviously affect the result. But how?
The result is two negative twos. The real (not rounded) result is -1.5 in both cases.
However, the results are the subjects of rounding. The rounding goes toward the
lesser integer value, and the lesser integer value is -2 , hence: -2 and -2.0 .
NOTE
Integer division can also be called floor division. You will definitely come across this
term in the future.
Remainder (modulo)
The next operator is quite a peculiar one, because it has no equivalent among traditional
arithmetic operators.
Its graphical representation in Python is the % (percent) sign, which may look a bit
confusing.
Try to think of it as a slash (division operator) accompanied by two funny little circles.
The result of the operator is a remainder left after the integer division.
In other words, it's the value left over after dividing one value by another to produce an
integer quotient.
Take a look at the snippet ‒ try to predict its result and then run it:
1 print(14 % 4)
2
86
Module 2
What is the result? Check the answers section to see if you are right.
Addition
The addition operator is the + (plus) sign, which is fully in line with mathematical
standards.
The result should be nothing surprising. Run the code to check it.
This is a great opportunity to present a very important distinction between unary and
binary operators.
87
Python Essentials 1
In subtracting applications, the minus operator expects two arguments: the left (a
minuend in arithmetical terms) and right (a subtrahend).
For this reason, the subtraction operator is considered to be one of the binary operators,
just like the addition, multiplication and division operators.
But the minus operator may be used in a different (unary) way ‒ take a look at the last
line of the snippet:
1 print(-4 – 4)
2 print(4. – 8)
3 print(-1.1)
4
By the way: there is also a unary + operator. You can use it like this:
1 print(+2)
2
The operator preserves the sign of its only argument – the right one.
Although such a construction is syntactically correct, using it doesn't make much sense,
and it would be hard to find a good rationale for doing so.
Take a look at the previous snippet ‒ can you guess its output?
88
Module 2
Also, you will very often find more than one operator in one expression, and then things
are no longer so simple.
2 + 3 * 5
You surely remember that you should first multiply 3 by 5 and, keeping the 15 in your
memory, then add it to 2, thus getting the result of 17.
The phenomenon that causes some operators to act before others is known as the
hierarchy of priorities.
Python precisely defines the priorities of all operators, and assumes that operators of a
higher priority perform their operations before the operators of a lower priority.
So, if you know that * has a higher priority than + , the computation of the final result
should be obvious.
Most of Python's operators have left-sided binding, which means that the calculation of
the expression is conducted from left to right.
1 print(9 % 6 % 2)
2
• from right to left: first 6 % 2 gives 0 , and then 9 % 0 causes a fatal error.
89
Python Essentials 1
1 print(9 % 6 % 2)
2
The result should be 1 . This operator has left-sided binding. But there's one
interesting exception.
• 2 ** 2 → 4 ; 4 ** 3 → 64
• 2 ** 3 → 8 ; 2 ** 8 → 256
The result clearly shows that the exponentiation operator uses right-sided binding.
This has an interesting effect. If the exponentiation operator uses right-sided binding, can
you guess the output of the following snippet?
1 print(-3 ** 2)
2 print(-2 ** 3)
3 print(-(3 ** 2))
4
List of priorities
Since you're new to Python operators, we don't want to present the complete list of
operator priorities right now.
Instead, we'll show you a truncated form, and we'll expand it consistently as we introduce
new operators.
90
Module 2
Priority Operator
1 **
3 * , / , // , %
4 + , - binary
Note: we've enumerated the operators in order from the highest (1) to the lowest (4)
priorities.
Try to work through the following expression:
1 print(2 * 3 % 5)
2
Both operators ( * and % ) have the same priority, so the result can be guessed only
when you know the binding direction. What do you think? What is the result?
Try to compute the value that's printed to the console. What's the result of the
print() function?
91
Python Essentials 1
Key takeaways
1. An expression is a combination of values (or variables, operators, calls to functions ‒
you will learn about them soon) which evaluates to a certain value, e.g. 1 + 2 .
2. Operators are special symbols or keywords which are able to operate on the values
and perform (mathematical) operations, e.g. the * operator multiplies two values:
x * y .
• then the unary + and – (note: a unary operator to the right of the exponentiation
operator binds more strongly, for example 4 ** -1 equals 0.25 )
• then: * , / , and % ,
92
Module 2
Check Answers
93
SECTION 2.4 –
VARIABLES
This part of the course focuses on
variables – we will learn what they are,
how to use them, and what the rules are
that govern them. Ready?
Python Essentials 1
You already know that you can do some arithmetic operations with these numbers: add,
subtract, etc. You'll be doing that many times.
But it's quite a normal question to ask how to store the results of these operations, in
order to use them in other operations, and so on.
How do you save the intermediate results, and use them again to produce subsequent
ones?
Python will help you with that. It offers special "boxes" (or "containers" as we may call
them) for that purpose, and these boxes are called variables ‒ the name itself suggests
that the content of these containers can be varied in (almost) any way.
• a name;
Variables do not appear in a program automatically. As a developer, you must decide how
many and which variables to use in your programs.
96
Module 2
• upper- and lower-case letters are treated as different (a little differently than in the
real world – Alice and ALICE are the same first names, but in Python they are two
different variable names, and consequently, two different variables);
• the name of the variable must not be any of Python's reserved words (the keywords
– we'll explain more about this soon).
Python does not impose restrictions on the length of variable names, but that doesn't
mean that a long variable name is always better than a short one.
Here are some correct, but not always convenient variable names:
• MyVariable
• i
• l
• t34
• Exchange_Rate
• counter
• days_to_christmas
• TheNameIsTooLongAndHardlyReadable
• _
• Adiós_Señora
• sûr_la_mer
• Einbahnstraße
97
Python Essentials 1
• переменная
Python lets you use not only Latin letters but also characters specific to languages that
use other alphabets.
The PEP 8 -- Style Guide for Python Code recommends the following naming convention
for variables and functions in Python:
• function names follow the same convention as variable names (e.g. fun, my_
function )
• it's also possible to use mixed case (e.g. myVariable ), but only in contexts
where that's already the prevailing style, to retain backward compatibility with the
adopted convention.
Keywords
Take a look at the list of words that play a very special role in every Python program.
They are called keywords or (more precisely) reserved keywords. They are reserved
because you mustn't use them as names: neither for your variables, nor functions, nor
any other named entities you want to create.
The meaning of the reserved word is predefined, and mustn't be changed in any way.
98
Module 2
Fortunately, due to the fact that Python is case-sensitive, you can modify any of these
words by changing the case of any letter, thus creating a new word, which is not reserved
anymore.
import
You mustn't have a variable named in such a way ‒ it is prohibited. But you can do this
instead:
Import
These words might be a mystery to you now, but you'll soon learn the meaning of them.
99
Python Essentials 1
Anything.
You can use a variable to store any value of any of the already presented kinds, and
many more of the ones we haven't shown you yet.
The value of a variable is what you have put into it. It can vary as often as you need or
want. It can be an integer one moment, and a float a moment later, eventually becoming
a string.
Let's talk now about two important things ‒ how variables are created, and how to put
values inside them (or rather ‒ how to give or pass values to them).
REMEMBER
A variable comes into existence as a result of assigning a value to it. Unlike in other
languages, you don't need to declare it in any special way.
If you assign any value to a non-existent variable, the variable will be automatically
created. You don't need to do anything else.
The creation (in other words, its syntax) is extremely simple: just use the name of the
desired variable, then the equal sign ( = ) and the value you want to put into the
variable.
• The first of them creates a variable named var , and assigns a literal with an
integer value equal to 1 .
• The second prints the value of the newly created variable to the console.
As you can see, print() has yet another side to it ‒ it can handle variables too. Do
you know what the output of the snippet will be? Run the code to check.
100
Module 2
However, you're not allowed to use a variable which doesn't exist (in other words, a
variable that was not assigned a value).
Do you know why? We've tried to use a variable named Var , which doesn't have any
value (note: var and Var are different entities, and have nothing in common as far
as Python's concerned).
REMEMBER
You can use the print() function and combine text and variables using the +
operator to output strings and variables. For example:
1 var = "3.8.5"
2 print("Python version: " + var)
3
101
Python Essentials 1
The equal sign is in fact an assignment operator. Although this may sound strange, the
operator has a simple syntax and unambiguous interpretation.
It assigns the value of its right argument to the left, while the right argument may be an
arbitrarily complex expression involving literals, operators and already defined variables.
1
2
The first line of the snippet creates a new variable named var and assigns 1 to it.
The third line assigns the same variable with the new value taken from the variable
itself, summed with 1 . Seeing a record like that, a mathematician would probably
protest ‒ no value may be equal to itself plus one. This is a contradiction. But Python
treats the sign = not as equal to, but as assign a value.
Take the current value of the variable var , add 1 to it and store the result in the
variable var .
102
Module 2
In effect, the value of variable var has been incremented by one, which has nothing
to do with comparing the variable with any value.
Do you know what the output of the following snippet will be?
1 var = 100
2 var = 200 + 300
3 print(var)
4
Check
103
Python Essentials 1
The square of the hypotenuse is equal to the sum of the squares of the other two sides.
The following code evaluates the length of the hypotenuse (i.e. the longest side of a right-
angled triangle, the one opposite of the right angle) using the Pythagorean theorem:
1 a = 3.0
2 b = 4.0
3 c = (a ** 2 + b ** 2) ** 0.5
4 print("c =", c)
5
Note: we need to make use of the ** operator to evaluate the square root as:
√(x) = x(½)
and
c = √a2 + b2
104
Module 2
LAB 5 Variables
Here is a short story:
Once upon a time in Appleland, John had three apples, Mary had five apples, and Adam
had six apples. They were all very happy and lived for a long time. End of story.
• assign values to the variables. The values must be equal to the numbers of fruit
possessed by John, Mary, and Adam respectively;
• having stored the numbers in the variables, print the variables on one line, and
separate each of them with a comma;
• now create a new variable named total_apples equal to the addition of the
three previous variables;
• experiment with your code: create new variables, assign different values to them,
and perform various arithmetic operations on them (e.g. + , - , * , / , // ,
etc.). Try to print a string and an integer together on one line, e.g.
"Total number of apples:" and total_apples .
1
105
Python Essentials 1
You may use an expression like this if you can't fall asleep and you're trying to deal with it
using some good, old-fashioned methods:
1 sheep = sheep + 1
2
Python offers you a shortened way of writing operations like these, which can be coded
as follows:
1 x *= 2
2 sheep += 1
3
Take a look at the following examples. Make sure you understand them all.
106
Module 2
i = i + 2 * j i += 2 * j
x = x ** 2 x **= 2
107
Python Essentials 1
Bearing in mind that 1 mile is equal to approximately 1.61 kilometers, complete the
program so that it converts:
• miles to kilometers;
• kilometers to miles.
Do not change anything in the existing code. Write your code in the places indicated by
### . Test your program with the data we've provided in the source code.
Pay particular attention to what is going on inside the print() function. Analyze how
we provide multiple arguments to the function, and how we output the expected data.
Note that some of the arguments inside the print() function are strings (e.g.
"miles is" , whereas some other are variables (e.g. miles ).
TIP
There's one more interesting thing happening there. Can you see another function inside
the print() function? It's the round() function. Its job is to round the outputted
result to the number of decimal places specified in the parentheses, and return a float
(inside the round() function you can find the variable name, a comma, and the
number of decimal places we're aiming for). We're going to talk about functions very
soon, so don't worry that everything may not be fully clear yet. We just want to spark your
curiosity.
After completing the lab, open the Sandbox, and experiment some more. Try to write
different converters, e.g. a USD to EUR converter, a temperature converter, etc. ‒ let
your imagination fly! Try to output the results by combining strings and variables. Try to
use and experiment with the round() function to round your results to one, two, or
three decimal places. Check out what happens if you don't provide any number of digits.
Remember to test your programs.
Expected output
108
Module 2
Code
1 kilometers = 12.25
2 miles = 7.38
3
4 miles_to_kilometers = ###
5 kilometers_to_miles = ###
6
7 print(miles, "miles is", round(miles_to_kilometers, 2),
"kilometers")
8 print(kilometers, "kilometers is", round(kilometers_to_miles,
2), "miles")
9
109
Python Essentials 1
3x3 – 2x2 + 3x – 1
Remember that classical algebraic notation likes to omit the multiplication operator ‒ you
need to use it explicitly. Note how we change data type to make sure that x is of type
float .
Keep your code clean and readable, and test it using the data we've provided, each time
assigning it to the x variable (by hardcoding it). Don't be discouraged by any initial
failures. Be persistent and inquisitive.
Sample input
x = 0
x = 1
x = -1
Sample output
y = -1.0
y = 3.0
y = -9.0
Code
1 x = # Hardcode your test data here.
2 x = float(x)
3 # Write your code here.
4 print("y =", y)
5
110
Module 2
Each variable must have a unique name ‒ an identifier. A legal identifier name must be a
non-empty sequence of characters, must begin with the underscore( _ ), or a letter, and
it cannot be a Python keyword. The first character may be followed by underscores,
letters, and digits. Identifiers in Python are case-sensitive.
You can also use compound assignment operators (shortcut operators) to modify
values assigned to variables, for example: var += 1 , or var /= 5 * 2 .
You can assign new values to already existing variables using the assignment operator or
one of the compound operators, for example:
1 var = 2
2 print(var)
3
4 var = 3
5 print(var)
6
7 var += 1
8 print(var)
9
You can combine text and variables using the + operator, and use the print()
function to output strings and variables, for example:
1 var = "007"
2 print("Agent " + var)
3
111
Python Essentials 1
• 3
• 2
• 5
Question 2: Which of the following variable names are illegal in Python? (Select three
answers)
• my_var
• m
• 101
• averylongVariablename
• m101
• m 101
• Del
• del
• 1
• 11
• 2
112
Module 2
2 b = 3
3 a /= 2 * b
4 print(a)
5
• 1.0
• 1
• 9
• 6
• 6.0
Check Answers
113
SECTION 2.5 –
COMMENTS
In this section, we want to share with you
a few comments on comments. You will
learn here why it is important to
document your code, and why you
should leave comments. You will also
learn how to do it, and when comments
are considered good practice. Let's go!
Python Essentials 1
A remark inserted into the program, which is omitted at runtime, is called a comment.
How do you leave this kind of comment in the source code? It has to be done in a way
that won't force Python to interpret it as part of the code.
In Python, a comment is a piece of text that begins with a # (hash) sign and extends to
the end of the line.
If you want a comment that spans several lines, you have to put a hash in front of them
all. Just like here:
1 # This program evaluates the hypotenuse c.
2 # a and b are the lengths of the legs.
3 a = 3.0
4 b = 4.0
5 c = (a ** 2 + b ** 2) ** 0.5 # We use ** instead of a square
root.
6 print("c =", c)
7
For example, if a particular variable is designed to store an area of some unique square,
the name square_area will obviously be better than aunt_jane .
116
Module 2
This is often done during the testing of a program, in order to isolate the place where an
error might be hidden.
TIP
If you'd like to quickly comment or uncomment multiple lines of code, select the line(s)
you wish to modify and use the following keyboard shortcut: CTRL + / (Windows) or CMD
+ / (Mac OS). It's a very useful trick, isn't it? Now experiment with the following code.
1 # uncomment_me = 1
2 # uncomment_me_too = 3
3 # uncomment_me_also = 5
4
5 print(uncomment_me, uncomment_me_too, uncomment_me_also,
sep="\n")
6
117
Python Essentials 1
LAB 8 Comments
The code contains comments. Try to improve it: add or remove comments where you find
it appropriate (yes, sometimes removing a comment can make the code more readable),
and change variable names where you think this will improve code comprehension.
NOTE
Comments are very important. They are used not only to make your programs easier to
understand, but also to disable those pieces of code that are currently not needed
(e.g. when you need to test some parts of your code only, and ignore others). Good
programmers describe each important piece of code, and give self-commenting names
to variables, as sometimes it is simply much better to leave information in the code.
It's good to use readable variable names, and sometimes it's better to divide your code
into named pieces (e.g. functions). In some situations, it's a good idea to write the steps
of computations in a clearer way.
One more thing: it may happen that a comment contains a wrong or incorrect piece of
information ‒ you should never do that on purpose!
Code
1 #this program computes the number of seconds in a given
number of hours
2 # this program was written two days ago
3
4 a = 2 # number of hours
5 seconds = 3600 # number of seconds in 1 hour
6
7 print("Hours: ", a) #printing the number of hours
8 # print("Seconds in Hours: ", a * seconds) # printing the
number of seconds in a given number of hours
9
10 #here we should also print "Goodbye", but a programmer didn't
have time to write any code
11 #this is the end of the program that computes the number of
seconds in 3 hours
12
118
Module 2
If you want to place a comment that spans several lines, you need to place # in front of
them all. Moreover, you can use a comment to mark a piece of code that is not needed at
the moment (see the last line of the following snippet), for example:
1 # This program prints
2 # an introduction to the screen.
3 print("Hello!") # Invoking the print() function
4 # print("I'm Python.")
5
Whenever possible and justified, you should give self-commenting names to variables,
e.g. if you're using two variables to store the length and width of something, the variable
names length and width may be a better choice than myvar1 and myvar2 .
It's important to use comments to make programs easier to understand, and to use
readable and meaningful variable names in code. However, it's equally important not to
use variable names that are confusing, or leave comments that contain wrong or
incorrect information!
Comments can be important when you are reading your own code after some time (trust
us, developers do forget what their own code does), and when others are reading your
code (they can help them understand what your programs do and how they do it more
quickly).
119
Python Essentials 1
Question 2: What will happen when you run the following code?
1 # This is
2 a multiline
3 comment. #
4
5 print("Hello!")
6
Check Answers
120
SECTION 2.6 –
INTERACTION WITH THE
USER
In this section, you will learn how to talk
to a computer: you will familiarize
yourself with the input() function,
perform type conversions, and learn how
to use string operators.
Python Essentials 1
print() has no usable result. The meaning of the new function is to return a very
usable result.
The function is named input() . The name of the function says everything.
The input() function is able to read data entered by the user and to return the same
data to the running program.
The program can manipulate the data, making the code truly interactive.
Virtually all programs read and process data. A program which doesn't get a user's input
is a deaf program.
• The program prompts the user to input some data from the console (most likely
using a keyboard, although it is also possible to input data using voice or image)
• the input() function is invoked without arguments (this is the simplest way of
using the function); the function will switch the console to input mode; you'll see a
blinking cursor, and you'll be able to input some keystrokes, finishing off by hitting
the Enter key; all the inputted data will be sent to your program through the
function's result;
• note: you need to assign the result to a variable; this is crucial ‒ missing out this
step will cause the entered data to be lost;
122
Module 2
• then we use the print() function to output the data we get, with some
additional remarks.
Run the code and let the function show you what it can do for you.
1 print("Tell me anything...")
2 anything = input()
3 print("Hmm...", anything, "... Really?")
4
123
Python Essentials 1
NOTE
• the input() function is invoked with one argument ‒ it's a string containing a
message;
• the message will be displayed on the console before the user is given an
opportunity to enter anything;
This variant of the input() invocation simplifies the code and makes it clearer.
124
Module 2
A string containing all the characters the user enters from the keyboard. It is not an
integer or a float.
This means that you mustn't use it as an argument of any arithmetic operation, e.g.
you can't use this data to square it, divide it by anything, or divide anything by it.
1 anything = input("Enter a number: ")
2 something = anything ** 2.0
3 print(anything, "to the power of 2 is", something)
4
125
Python Essentials 1
What happens? Python should have given you the following output:
The last line of the sentence explains everything ‒ you tried to apply the ** operator to
'str' (string) accompanied with 'float' .
This is prohibited.
This should be obvious – can you predict the value of "to be or not to be"
raised to the power of 2 ?
Have we fallen into a deadlock? Is there a solution to this issue? Of course there is.
126
Module 2
• the int() function takes one argument (e.g. a string: int(string) ) and
tries to convert it into an integer; if it fails, the whole program will fail too (there is a
workaround for this situation, but we'll show you this a little later);
• the float() function takes one argument (e.g. a string: float(string) ) and
tries to convert it into a float (the rest is the same).
This is very simple and very effective. Moreover, you can invoke any of the functions by
passing the input() results directly to them. There's no need to use any variable as
an intermediate storage.
Can you imagine how the string entered by the user flows from input() into
print() ?
Try to run the modified code. Don't forget to enter a valid number.
Check some different values, small and big, negative and positive. Zero is a good input,
too.
1 anything = float(input("Enter a number: "))
2 something = anything ** 2.0
3 print(anything, "to the power of 2 is", something)
4
127
Python Essentials 1
You'll eventually be able to write complete programs, accepting data in the form of
numbers, processing them and displaying the results.
Of course, these programs will be very primitive and not very usable, as they cannot
make decisions, and consequently are not able to react differently to different situations.
This is not really a problem, though; we'll show you how to overcome it soon.
Our next example refers to the earlier program to find the length of a hypotenuse. Let's
run it and make it able to read the lengths of the legs from the console.
The program asks the user for the lengths of both legs, evaluates the hypotenuse and
prints the result. Run it and try to input some negative values.
The program, unfortunately, doesn't react to this obvious error. Let's ignore this weakness
for now. We'll come back to it soon.
Note that in the program that you can see, the hypo variable is used for only one
purpose ‒ to save the calculated value between the execution of the adjoining line of
code.
As the print() function accepts an expression as its argument, you can remove the
variable from the code.
128
Module 2
We want to show you that they have a second function. They are able to do something
more than just add and multiply.
We've seen them in action where their arguments are numbers (floats or integers, it
doesn't matter).
Now we're going to show you that they can handle strings, too, albeit in a very specific
way.
The + (plus) sign, when applied to two strings, becomes a concatenation operator:
1 string + string
2
It simply concatenates (glues) two strings into one. Of course, like its arithmetic sibling, it
can be used more than once in one expression, and in such a context it behaves
according to left-sided binding.
In contrast to its arithmetic sibling, the concatenation operator is not commutative, i.e.
"ab" + "ba" is not the same as "ba" + "ab" .
Don't forget ‒ if you want the + sign to be a concatenator, not an adder, you must
ensure that both its arguments are strings.
Note: using + to concatenate strings lets you construct the output in a more precise
way than with a pure print() function, even if enriched with the end= and
sep= keyword arguments.
Run the code and see if the output matches your predictions.
129
Python Essentials 1
Replication
The * (asterisk) sign, when applied to a string and number (or a number and string, as
it remains commutative in this position) becomes a replication operator:
1 string * number
2 number * string
3
It replicates the string the same number of times specified by the number.
For example:
This simple program "draws" a rectangle, making use of an old operator ( + ) in a new
role:
1 print("+" + 10 * "-" + "+")
2 print(("|" + " " * 10 + "|\n") * 5, end="")
3 print("+" + 10 * "-" + "+")
4
Note the way in which we've used the parentheses in the second line of the code.
130
Module 2
str()
You already know how to use the int() and float() functions to convert a string
into a number.
This type of conversion is not a one-way street. You can also convert a number into a
string, which is way easier and safer ‒ this kind of operation is always possible.
To be honest, it can do much more than just transform numbers into strings, but that can
wait for later.
We've modified it a bit to show you how the str() function works. Thanks to this, we
can pass the whole result to the print() function as one string, forgetting about
the commas.
You already know the basic data types, and a set of fundamental operators. You know
how to organize the output and how to get data from the user. These are very strong
foundations for Module 3. But before we move on to the next module, let's do a few labs,
and recap all that you've learned in this section.
131
Python Essentials 1
You may not be able to protect the code from a user who wants to divide by zero. That's
okay, don't worry about it for now.
We won't show you any test data ‒ that would be too simple.
Code
1 # input a float value for variable a here
2 # input a float value for variable b here
3
4 # output the result of addition here
5 # output the result of subtraction here
6 # output the result of multiplication here
7 # output the result of division here
8
9 print("\nThat's all, folks!")
10
Check Hint
132
Module 2
The result should be assigned to y . Be careful ‒ watch the operators and keep their
priorities in mind. Don't hesitate to use as many parentheses as you need.
You can use additional variables to shorten the expression (but it's not necessary). Test
your code carefully.
Sample input:
Expected output:
y = 0.6000000000000001
Sample input:
10
Expected output:
y = 0.09901951266867294
Sample input:
100
133
Python Essentials 1
Expected output:
y = 0.009999000199950014
Sample input:
-5
Expected output:
y = -0.19258202567760344
Code
1 x = float(input("Enter value for x: "))
2
3 # Write your code here.
4
5 print("y =", y)
6
134
Module 2
For example, if an event starts at 12:17 and lasts 59 minutes, it will end at 13:16.
Don't worry about any imperfections in your code ‒ it's okay if it accepts an invalid time ‒
the most important thing is that the code produces valid results for valid input data.
Test your code carefully. Hint: using the % operator may be the key to success.
Sample input:
12
17
59
Expected output:
13:16
Sample input:
23
58
642
Expected output:
10:40
Sample input:
0
1
2939
135
Python Essentials 1
Expected output:
1:0
Code
1 hour = int(input("Starting time (hours): "))
2 mins = int(input("Starting time (minutes): "))
3 dura = int(input("Event duration (minutes): "))
4
5 # Write your code here.
6
Check Hint
136
Module 2
The input() function comes with an optional parameter: the prompt string. It allows
you to write a message before the user input, e.g.:
1 name = input("Enter your name: ")
2 print("Hello, " + name + ". Nice to meet you!")
3
When the input() function is called, the program's flow is stopped, the prompt
symbol keeps blinking (it prompts the user to take action when the console is switched to
input mode) until the user has entered an input and/or pressed the Enter key.
NOTE
You can test the functionality of the input() function in its full scope locally on your
machine. For resource optimization reasons, we have limited the maximum program
execution time in Edube to a few seconds. Go to the Sandbox, copy-paste the previous
snippet, run the program, and do nothing ‒ just wait a few seconds to see what happens.
Your program should be stopped automatically after a short moment. Now open IDLE,
and run the same program there ‒ can you see the difference?
Tip: this feature of the input() function can be used to prompt the user to end a
program. Look at the following code:
1 name = input("Enter your name: ")
2 print("Hello, " + name + ". Nice to meet you!")
3 print("\nPress Enter to end the program.")
4 input()
5 print("THE END.")
6
The result of the input() function is a string. You can add strings to each other using
the concatenation ( + ) operator. Check out this code:
1 num_1 = input("Enter the first number: ") # Enter 12
2 num_2 = input("Enter the second number: ") # Enter 21
3 print(num_1 + num_2) # the program returns 1221
137
Python Essentials 1
138
Module 2
Check Answers
139
MODULE 3
BOOLEAN VALUES,
CONDITIONAL
EXECUTION, LOOPS,
LISTS AND LIST
PROCESSING,
LOGICAL AND BITWISE
OPERATIONS
Key Learning Areas
• Boolean values to compare difference values and control the execution paths using
the if and if-else instructions
• The utilization of loops (while and for) and how to control their behavior using the
break and continue instructions
• The concept of lists and list processing, including the iteration provided by the for
loop, and slicing
A computer executes the program and provides the answers. The program must be
able to react according to the received answers.
You will never get a response like Let me think...., I don't know, or Probably yes, but I
don't know for sure.
To ask questions, Python uses a set of very special operators. Let's go through them
one after another, illustrating their effects on some simple examples.
144
Module 3
It is a binary operator with left-sided binding. It needs two arguments and checks if
they are equal.
145
Python Essentials 1
3.1.3 Exercises
Now let's ask a few questions. Try to guess the answers.
2 == 2
2 == 2.
1 == 2
146
Module 3
3.1.4 Operators
Look at the equality following comparison – what is the result of this operation?
var == 0
Note that we cannot find the answer if we do not know what value is currently stored in
the variable var .
If the variable has been changed many times during the execution of your program, or its
initial value is entered from the console, the answer to this question can be given only by
Python and only at runtime.
Now imagine a programmer who suffers from insomnia, and has to count black and white
sheep separately as long as there are exactly twice as many black sheep as white ones.
black_sheep == 2 * white_sheep
Due to the low priority of the == operator, the question shall be treated as equivalent to
this one:
black_sheep == (2 * white_sheep)
So, let's practice your understanding of the == operator now – can you guess the
output of the code?
1 var = 0 # Assigning 0 to var
2 print(var == 0)
3
147
Python Essentials 1
Now take a look at the following inequality comparison – can you guess the result of this
operation?
1 var = 0 # Assigning 0 to var
2 print(var != 0)
3
4 var = 1 # Assigning 1 to var
5 print(var != 0)
6
If you want to know if there are more black sheep than white ones, you can write it as
follows:
1 black_sheep > white_sheep # Greater than
2
148
Module 3
Both of these operators (strict and non-strict), as well as the two others discussed in the
next section, are binary operators with left-sided binding, and their priority is greater
than that shown by == and != .
If we want to find out whether or not we have to wear a warm hat, we ask the following
question:
1 centigrade_outside >= 0.0 # Greater than or equal to
2
We're going to check if there's a risk of being fined by the highway police (the first
question is strict, the second isn't).
149
Python Essentials 1
There are at least two possibilities: first, you can memorize it (store it in a variable) and
make use of it later. How do you do that? Well, you use an arbitrary variable like this:
The content of the variable will tell you the answer to the question asked.
The second possibility is more convenient and far more common: you can use the
answer you get to make a decision about the future of the program.
You need a special instruction for this purpose, and we'll discuss it very soon.
Now we need to update our priority table, and put all the new operators into it. It now
looks as follows:
Priority Operator
1 + , - unary
2 **
3 * , / , // , %
4 + , - binary
6 == , !=
150
Module 3
Don't create any if blocks (we're going to talk about them very soon). Test your code
using the data we've provided for you.
Sample input:
55
Expected output:
False
Sample input:
99
Expected output:
False
Sample input:
100
Expected output:
True
Sample input:
101
151
Python Essentials 1
Expected output:
True
Sample input:
-5
Expected output:
False
Sample input:
+123
Expected output:
True
152
Module 3
It's just like in real life: you do certain things or you don't when a specific condition is met
or not, e.g. you go for a walk if the weather is good, or stay home if it's wet and cold.
To make such decisions, Python offers a special instruction. Due to its nature and its
application, it's called a conditional instruction (or conditional statement).
There are several variants of it. We'll start with the simplest, increasing the difficulty
slowly.
The first form of a conditional statement is written very informally but figuratively:
if true_or_not:
do_this_if_true
This conditional statement consists of the following, strictly necessary, elements in this
and this order only:
• the if keyword;
153
Python Essentials 1
• If the true_or_not expression represents the truth (i.e. its value is not equal
to zero), the indented statement(s) will be executed;
• if the true_or_not expression does not represent the truth (i.e. its value is
equal to zero), the indented statement(s) will be omitted (ignored), and the next
executed instruction will be the one after the original indentation level.
As you can see, having lunch is not a conditional activity and doesn't depend on the
weather.
Knowing what conditions influence our behavior, and assuming that we have the
parameterless functions go_for_a_walk() and have_lunch() , we can write the
following snippet:
1 if the_weather_is_good:
2 go_for_a_walk()
3 have_lunch()
4
You can read it as: if sheep_counter is greater than or equal to 120, then fall asleep
and dream (i.e. execute the sleep_and_dream function.)
We've said that conditionally executed statements have to be indented. This creates
a very legible structure, clearly demonstrating all possible execution paths in the code.
154
Module 3
As you can see, making a bed, taking a shower and falling asleep and dreaming are all
executed conditionally – when sheep_counter reaches the desired limit.
Now we're going to discuss another variant of the conditional statement, which also
allows you to perform an additional action when the condition is not met.
Note: there is not a word about what will happen if the weather is bad. We only know that
we won't go outdoors, but what we could do instead is not known. We may want to plan
something in case of bad weather, too.
We can say, for example: If the weather is good, we will go for a walk, otherwise we will
go to a theater.
Now we know what we'll do if the conditions are met, and we know what we'll do if not
everything goes our way. In other words, we have a "Plan B".
Python allows us to express such alternative plans. This is done with a second, slightly
more complex form of the conditional statement, the if-else statement:
1 if true_or_false_condition:
2 perform_if_condition_true
3 else:
4 perform_if_condition_false
5
155
Python Essentials 1
The part of the code which begins with else says what to do if the condition specified
for the if is not met (note the colon after the word).
• if the condition evaluates to True (its value is not equal to zero), the
perform_if_condition_true statement is executed, and the conditional
statement comes to an end;
If the weather is good, we'll go for a walk. Otherwise, we'll go to a theater. No matter if the
weather is good or bad, we'll have lunch afterwards (after the walk or after going to the
theater).
Everything we've said about indentation works in the same manner inside the else
branch:
1 if the_weather_is_good:
2 go_for_a_walk()
3 have_fun()
4 else:
5 go_to_a_theater()
6 enjoy_the_movie()
7 have_lunch()
8
156
Module 3
First, consider the case where the instruction placed after the if is another if .
Read what we have planned for this Sunday. If the weather is fine, we'll go for a walk. If
we find a nice restaurant, we'll have lunch there. Otherwise, we'll eat a sandwich. If the
weather is poor, we'll go to the theater. If there are no tickets, we'll go shopping in the
nearest mall.
Let's write the same in Python. Consider carefully the code here:
1 if the_weather_is_good:
2 if nice_restaurant_is_found:
3 have_lunch()
4 else:
5 eat_a_sandwich()
6 else:
7 if tickets_are_available:
8 go_to_the_theater()
9 else:
10 go_shopping()
11
• this use of the if statement is known as nesting; remember that every else
refers to the if which lies at the same indentation level; you need to know this
to determine how the if s and else s pair up;
• consider how the indentation improves readability, and makes the code easier to
understand and trace.
elif is used to check more than just one condition, and to stop when the first
statement which is true is found.
157
Python Essentials 1
Our next example resembles nesting, but the similarities are very slight. Again, we'll
change our plans and express them as follows: If the weather is fine, we'll go for a walk,
otherwise if we get tickets, we'll go to the theater, otherwise if there are free tables at the
restaurant, we'll go for lunch; if all else fails, we'll stay home and play chess.
Have you noticed how many times we've used the word otherwise? This is the stage
where the elif keyword plays its role.
Notice again how the indentation improves the readability of the code.
• else is always the last branch of the cascade, regardless of whether you've
used elif or not;
• if there is an else branch in the cascade, only one of all the branches is
executed;
• if there is no else branch, it's possible that none of the available branches is
executed.
This may sound a little puzzling, but hopefully some simple examples will help shed more
light.
158
Module 3
All the programs solve the same problem – they find the largest of several numbers
and print it out.
Example 1:
We'll start with the simplest case – how to identify the larger of two numbers:
1 # Read two numbers
2 number1 = int(input("Enter the first number: "))
3 number2 = int(input("Enter the second number: "))
4
5 # Choose the larger number
6 if number1 > number2:
7 larger_number = number1
8 else:
9 larger_number = number2
10
11 # Print the result
12 print("The larger number is:", larger_number)
13
This snippet should be clear – it reads two integer values, compares them, and finds
which is the larger.
Example 2:
Now we're going to show you one intriguing fact. Python has an interesting feature – look
at the following code:
1 # Read two numbers
2 number1 = int(input("Enter the first number: "))
3 number2 = int(input("Enter the second number: "))
4
5 # Choose the larger number
6 if number1 > number2: larger_number = number1
7 else: larger_number = number2
159
Python Essentials 1
8
9 # Print the result
10 print("The larger number is:", larger_number)
11
Note: if any of the if-elif-else branches contains just one instruction, you may
code it in a more comprehensive form (you don't need to make an indented line after the
keyword, but just continue the line after the colon).
This style, however, may be misleading, and we're not going to use it in our future
programs, but it's definitely worth knowing if you want to read and understand someone
else's programs.
Example 3:
It's time to complicate the code – let's find the largest of three numbers. Will it enlarge the
code? A bit.
We assume that the first value is the largest. Then we verify this hypothesis with the two
remaining values.
160
Module 3
largest_number
17 # and update the largest_number if needed.
18 if number3 > largest_number:
19 largest_number = number3
20
21 # Print the result
22 print("The largest number is:", largest_number)
23
This method is significantly simpler than trying to find the largest number all at once, by
comparing all possible pairs of numbers (i.e. first with second, second with third, third
with first). Try to rebuild the code for yourself.
161
Python Essentials 1
You already know the scheme, so extending the size of the problem will not be
particularly complex.
But what happens if we ask you to write a program that finds the largest of two hundred
numbers? Can you imagine the code?
You'll need two hundred variables. If two hundred variables isn't bad enough, try to
imagine searching for the largest of a million numbers.
Imagine a code that contains 199 conditional statements and two hundred invocations of
the input() function. Luckily, you don't need to deal with that. There's a simpler
approach.
We'll ignore the requirements of Python syntax for now, and try to analyze the problem
without thinking about the real programming. In other words, we'll try to write the
algorithm, and when we're happy with it, we'll implement it.
In this case, we'll use a kind of notation which is not an actual programming language (it
can be neither compiled nor executed), but it is formalized, concise and readable. It's
called pseudocode.
162
Module 3
Firstly, we can simplify the program if, at the very beginning of the code, we assign the
variable largest_number with a value which will be smaller than any of the entered
numbers. We'll use -999999999 for that purpose.
Secondly, we assume that our algorithm will not know in advance how many numbers will
be delivered to the program. We expect that the user will enter as many numbers as
she/he wants – the algorithm will work well with one hundred and with one thousand
numbers. How do we do that?
We make a deal with the user: when the value -1 is entered, it will be a sign that there
are no more data and the program should end its work.
Otherwise, if the entered value is not equal to -1, the program will read another number,
and so on.
The trick is based on the assumption that any part of the code can be performed more
than once – precisely, as many times as needed.
Performing a certain part of the code more than once is called a loop. The meaning of
this term is probably obvious to you.
Lines 02 through 08 make a loop. We'll pass through them as many times as
needed to review all the entered values.
Can you use a similar structure in a program written in Python? Yes, you can.
163
Python Essentials 1
EXTRA
Python often comes with a lot of built-in functions that will do the work for you. For
example, to find the largest number of all, you can use a Python built-in function called
max() . You can use it with multiple arguments. Analyze the following code:
1 # Read three numbers.
2 number1 = int(input("Enter the first number: "))
3 number2 = int(input("Enter the second number: "))
4 number3 = int(input("Enter the third number: "))
5
6 # Check which one of the numbers is the greatest
7 # and pass it to the largest_number variable.
8
9 largest_number = max(number1, number2, number3)
10
11 # Print the result.
12 print("The largest number is:", largest_number)
13
By the same fashion, you can use the min() function to return the lowest number. You
can rebuild this code and experiment with it in the Sandbox.
We're going to talk about these (and many other) functions soon. For the time being, our
focus will be on conditional execution and loops to let you gain more confidence in
programming and teach you the skills that will let you fully understand and apply the two
concepts in your code. So, for now, we're not taking any shortcuts.
164
Module 3
Imagine that your computer program loves these plants. Whenever it receives an input in
the form of the word Spathiphyllum , it involuntarily shouts to the console the
following string: "Spathiphyllum is the best plant ever!"
Write a program that utilizes the concept of conditional execution, takes a string as input,
and:
Test your code using the data we've provided for you. And get yourself a Spathiphyllum,
too!
Sample input:
spathiphyllum
Expected output:
Sample input:
pelargonium
Expected output:
165
Python Essentials 1
Sample input:
Spathiphyllum
Expected output:
166
Module 3
• if the citizen's income was not higher than 85,528 thalers, the tax was equal to 18%
of the income minus 556 thalers and 2 cents (this was what they called tax relief)
• if the income was higher than this amount, the tax was equal to 14,839 thalers and
2 cents, plus 32% of the surplus over 85,528 thalers.
• Next, it should print the calculated tax, rounded to full thalers. There's a function
named round() which will do the rounding for you – you'll find it in the following
skeleton code.
Note: this happy country never returned any money to its citizens. If the calculated tax
was less than zero, it would only mean no tax at all (the tax was equal to zero). Take this
into consideration during your calculations.
Look at the code – it only reads one input value and outputs a result, so you need to
complete it with some smart calculations.
Sample input:
10000
Expected output:
Sample input:
100000
167
Python Essentials 1
Expected output:
Sample input:
1000
Expected output:
Sample input:
-100
Expected output:
Code
1 income = float(input("Enter the annual income: "))
2
3 if income < 85528:
4 tax = income * 0.18 – 556.02
5 # Write the rest of your code here.
6
7 tax = round(tax, 0)
8 print("The tax is:", tax, "thalers")
9
168
Module 3
Since the introduction of the Gregorian calendar (in 1582), the following rule is used to
determine the kind of year:
• otherwise, if the year number isn't divisible by 100, it's a leap year;
• otherwise, if the year number isn't divisible by 400, it's a common year;
Look at the following code – it only reads a year number, and needs to be completed with
the instructions implementing the test we've just described.
The code should output one of two possible messages, which are Leap year or
Common year , depending on the value entered.
It would be good to verify if the entered year falls into the Gregorian era, and output a
warning otherwise: Not within the Gregorian calendar period . Tip: use the
!= and % operators.
Sample input:
2000
Expected output:
Leap year
Sample input:
2015
Expected output:
Common year
169
Python Essentials 1
Sample input:
1999
Expected output:
Common year
Sample input:
1996
Expected output:
Leap year
Sample input:
1580
Expected output:
Code
1 year = int(input("Enter a year: "))
2
3 if year < 1582:
4 print("Not within the Gregorian calendar period")
5 else:
6 # Write the if-elif-elif-else block here.
7
170
Module 3
2. When you want to execute some code only if a certain condition is met, you can use a
conditional statement:
171
Python Essentials 1
3 if x == 10: # condition
4 print("x is equal to 10") # Executed if the condition is
True.
5
172
Module 3
If the condition for if is False , the program checks the conditions of the
subsequent elif blocks – the first elif block that is True is executed. If all the
conditions are False , the else block will be executed.
173
Python Essentials 1
174
Module 3
x = 5
y = 10
z = 8
print(x > y)
print(y > z)
x, y, z = 5, 10, 8
print(x > z)
print((y – 5) == x)
x, y, z = 5, 10, 8
x, y, z = z, y, x
print(x > z)
print((y – 5) == x)
175
Python Essentials 1
x = 10
if x == 10:
print(x == 10)
if x > 5:
print(x > 5)
if x < 10:
print(x < 10)
else:
print("else")
x = "1"
if x == 1:
print("one")
elif x == "1":
if int(x) > 1:
print("two")
elif int(x) < 1:
print("three")
else:
print("four")
if int(x) == 1:
print("five")
else:
print("six")
176
Module 3
x = 1
y = 1.0
z = "1"
if x == y:
print("one")
if y == int(z):
print("two")
elif x == y:
print("three")
else:
print("four")
Check Answers
177
SECTION 3.2 – LOOPS IN
PYTHON
Here you will learn about loops in
Python, and specifically – the while
and for loops. You will learn how to
create (and avoid falling into) infinite
loops, how to exit loops, and skip
particular loop iterations. Ready?
Python Essentials 1
Note that this record also declares that if there is nothing to do, nothing at all will happen.
while
instruction
If you notice some similarities to the if instruction, that's quite all right. Indeed, the
syntactic difference is only one: you use the word while instead of the word if .
The semantic difference is more important: when the condition is met, if performs its
statements only once; while repeats the execution as long as the condition
evaluates to True .
Note: all the rules regarding indentation are applicable here, too. We'll show you this
soon.
while conditional_expression:
instruction_one
instruction_two
instruction_three
:
:
instruction_n
180
Module 3
• if you want to execute more than one statement inside one while loop, you
must (as with if ) indent all the instructions in the same way;
• an instruction or set of instructions executed inside the while loop is called the
loop's body;
• if the condition is False (equal to zero) as early as when it is tested for the first
time, the body is not executed even once (note the analogy of not having to do
anything if there is nothing to do);
• the body should be able to change the condition's value, because if the condition is
True at the beginning, the body might run continuously to infinity – notice that
doing a thing usually decreases the number of things to do).
181
Python Essentials 1
This loop will infinitely print "I'm stuck inside a loop." on the screen.
NOTE
If you want to get the best learning experience from seeing how an infinite loop behaves,
launch IDLE, create a New File, copy-paste the previous code, save your file, and run the
program. What you will see is the never-ending sequence of
"I'm stuck inside a loop." strings printed to the Python console window. To
terminate your program, just press Ctrl-C (or Ctrl-Break on some computers). This will
cause a KeyboardInterrupt exception and let your program get out of the loop.
We'll talk about it later in the course.
Let's go back to the sketch of the algorithm we showed you recently. We're going to show
you how to use this newly learned loop to find the largest number from a large set of
entered data.
Analyze the program carefully. See where the loop starts (line 8). Locate the loop's body
and find out how the body is exited:
1 # Store the current largest number here.
2 largest_number = -999999999
3
4 # Input the first value.
5 number = int(input("Enter a number or type -1 to stop: "))
6
7 # If the number is not equal to -1, continue.
8 while number != -1:
9 # Is number larger than largest_number?
10 if number > largest_number:
11 # Yes, update largest_number.
12 largest_number = number
182
Module 3
Check how this code implements the algorithm we showed you earlier.
183
Python Essentials 1
Try to recall how Python interprets the truth of a condition, and note that these two forms
are equivalent:
184
Module 3
The condition that checks if a number is odd can be coded in these equivalent forms, too:
This code is intended to print the string "Inside the loop." and the value stored
in the counter variable during a given loop exactly five times. Once the condition has
not been met (the counter variable has reached 0 ), the loop is exited, and the
message "Outside the loop." as well as the value stored in counter is
printed.
But there's one thing that can be written more compactly – the condition of the while
loop.
REMEMBER Don't feel obliged to code your programs in a way that is always the
shortest and the most compact. Readability may be a more important factor. Keep your
code ready for a new programmer.
185
Python Essentials 1
Your task is to help the magician complete the code in such a way so that the code:
• will check whether the number entered by the user is the same as the number
picked by the magician. If the number chosen by the user is different than the
magician's secret number, the user should see the message
"Ha ha! You're stuck in my loop!" and be prompted to enter a number
again. If the number entered by the user matches the number picked by the
magician, the number should be printed to the screen, and the magician should say
the following words: "Well done, muggle! You are free now."
By the way, look at the print() function. The way we've used it here is called multi-
line printing. You can use triple quotes to print strings on multiple lines in order to make
text easier to read, or create a special text-based design. Experiment with it.
Code
1 secret_number = 777
2
3 print(
4 """
5 +================================+
6 | Welcome to my game, muggle! |
7 | Enter an integer number |
8 | and guess what number I've |
9 | picked for you. |
10 | So, what is the secret number? |
11 +================================+
186
Module 3
12 """)
13
Check Hint
187
Python Essentials 1
It would be nice if somebody could do this boring counting for you. Is that possible?
Of course it is – there's a special loop for these kinds of tasks, and it is named for .
Actually, the for loop is designed to do more complicated tasks – it can "browse"
large collections of data item by item. We'll show you how to do that soon, but right
now we're going to present a simpler variant of its application.
Take a look at the snippet:
1 for i in range(100):
2 # do_something()
3 pass
4
There are some new elements. Let us tell you about them:
• the for keyword opens the for loop; note – there's no condition after it; you don't
have to think about conditions, as they're checked internally, without any
intervention;
• any variable after the for keyword is the control variable of the loop; it counts
the loop's turns, and does it automatically;
• the in keyword introduces a syntax element describing the range of possible
values being assigned to the control variable;
• the range() function (this is a very special function) is responsible for
generating all the desired values of the control variable; in our example, the function
will create (we can even say that it will feed the loop with) subsequent values from
the following set: 0, 1, 2 .. 97, 98, 99; note: in this case, the range() function
188
Module 3
starts its job from 0 and finishes it one step (one integer number) before the value of
its argument;
• note the pass keyword inside the loop body – it does nothing at all; it's an empty
instruction – we put it here because the for loop's syntax demands at least one
instruction inside the body (by the way – if , elif , else and while
express the same thing)
Our next examples will be a bit more modest in the number of loop repetitions.
Take a look at the following snippet. Can you predict its output?
1 for i in range(10):
2 print("The value of i is currently", i)
3
In this case, the first argument determines the initial (first) value of the control variable.
The last argument shows the first value the control variable will not be assigned.
Note: the range() function accepts only integers as its arguments, and generates
sequences of integers.
Can you guess the output of the program? Run it to check if you were right now, too.
The first value shown is 2 (taken from the range() 's first argument.)
The last is 7 (although the range() 's second argument is 8 ).
189
Python Essentials 1
The third argument is an increment – it's a value added to control the variable at every
loop turn (as you may suspect, the default value of the increment is 1).
Can you tell us how many lines will appear in the console and what values they will
contain?
You should be able to see the following lines in the console window:
Do you know why? The first argument passed to the range() function tells us what
the starting number of the sequence is (hence 2 in the output). The second argument
tells the function where to stop the sequence (the function generates numbers up to the
number indicated by the second argument, but does not include it). Finally, the third
argument indicates the step, which actually means the difference between each number
in the sequence of numbers generated by the function.
Note: if the set generated by the range() function is empty, the loop won't execute its
body at all.
190
Module 3
Note: the set generated by the range() has to be sorted in ascending order.
There's no way to force the range() to create a set in a different form when the
range() function accepts exactly two arguments. This means that the range() 's
second argument must be greater than the first.
Let's have a look at a short program whose task is to write some of the first powers of
two:
1 power = 1
2 for expo in range(16):
3 print("2 to the power of", expo, "is", power)
4 power *= 2
5
The expo variable is used as a control variable for the loop, and indicates the current
value of the exponent. The exponentiation itself is replaced by multiplying by two. Since
20 is equal to 1, then 2 × 1 is equal to 21, 2 × 21 is equal to 22, and so on. What is the
greatest exponent for which our program still prints the result?
Run the code and check if the output matches your expectations.
191
Python Essentials 1
The word Mississippi is also used for a slightly different purpose: to count mississippily.
If you're not familiar with the phrase, we're here to explain to you what it means: it's used
to count seconds.
The idea behind it is that adding the word Mississippi to a number when counting
seconds aloud makes them sound closer to clock-time, and therefore "one Mississippi,
two Mississippi, three Mississippi" will take approximately an actual three seconds of
time! It's often used by children playing hide-and-seek to make sure the seeker does an
honest count.
Your task is very simple here: write a program that uses a for loop to "count
mississippily" to five. Having counted to five, the program should print to the screen the
final message "Ready or not, here I come!"
Note that the code contains two elements which may not be fully clear to you at this
moment: the import time statement, and the sleep() method. We're going to
talk about them soon.
For the time being, we'd just like you to know that we've imported the time module and
used the sleep() method to suspend the execution of each subsequent print()
function inside the for loop for one second, so that the message outputted to the
console resembles an actual counting. Don't worry – you'll soon learn more about
modules and methods.
192
Module 3
Expected output:
1 Mississippi
2 Mississippi
3 Mississippi
4 Mississippi
5 Mississippi
Code
1 import time
2
3 # Write a for loop that counts to five.
4 # Body of the loop – print the loop iteration number and
the word "Mississippi".
5 # Body of the loop – use: time.sleep(1)
6
7 # Write a print function with the final message.
8
Check Hint
193
Python Essentials 1
• it appears that it's unnecessary to continue the loop as a whole; you should refrain
from further execution of the loop's body and go further;
• it appears that you need to start the next turn of the loop without completing the
execution of the current turn.
Python provides two special instructions for the implementation of both these tasks. Let's
say for the sake of accuracy that their existence in the language is not necessary – an
experienced programmer is able to code any algorithm without these instructions. Such
additions, which don't improve the language's expressive power, but only simplify the
developer's work, are sometimes called syntactic candy, or syntactic sugar.
• break – exits the loop immediately, and unconditionally ends the loop's
operation; the program begins to execute the nearest instruction after the loop's
body;
• continue – behaves as if the program has suddenly reached the end of the
body; the next turn is started and the condition expression is tested immediately.
Now we'll show you two simple examples to illustrate how the two instructions work. Look
at the following code. Run the program and analyze the output. Modify the code and
experiment.
1 # break – example
2
3 print("The break instruction:")
4 for i in range(1, 6):
5 if i == 3:
6 break
7 print("Inside the loop.", i)
8 print("Outside the loop.")
9
10
194
Module 3
11 # continue – example
12
13 print("\nThe continue instruction:")
14 for i in range(1, 6):
15 if i == 3:
16 continue
17 print("Inside the loop.", i)
18 print("Outside the loop.")
19
Analyze the code, and judge whether and how you would use either of them.
195
Python Essentials 1
1 largest_number = -99999999
2 counter = 0
3
4 number = int(input("Enter a number or type -1 to end program:
"))
5
6 while number != -1:
7 if number == -1:
8 continue
9 counter += 1
10
11 if number > largest_number:
12 largest_number = number
13 number = int(input("Enter a number or type -1 to end the
program: "))
14
15 if counter:
16 print("The largest number is", largest_number)
17 else:
18 print("You haven't entered any number.")
19
Look carefully, the user enters the first number before the program enters the while
loop. The subsequent number is entered when the program is already in the loop.
Again – run the program, test it, and experiment with it.
196
Module 3
Design a program that uses a while loop and continuously asks the user to enter a
word unless the user enters "chupacabra" as the secret exit word, in which case the
message "You've successfully left the loop." should be printed to the
screen, and the loop should terminate.
Don't print any of the words entered by the user. Use the concept of conditional execution
and the break statement.
1
Check Hint
197
Python Essentials 1
Your task here is very special: you must design a vowel eater! Write a program that uses:
• a for loop;
• use conditional execution and the continue statement to "eat" the following
vowels A, E, I, O, U from the inputted word;
• print the uneaten letters to the screen, each one of them on a separate line.
Test your program with the data we've provided for you.
Sample input:
Gregory
Expected output:
G
R
G
R
Y
198
Module 3
Sample input:
abstemious
Expected output:
B
S
T
M
S
Sample input:
IOUEA
Expected output:
Code
1 # Prompt the user to enter a word
2 # and assign it to the user_word variable.
3
4 for letter in user_word:
5 # Complete the body of the for loop.
6
Check Hint
199
Python Essentials 1
• a for loop;
• use conditional execution and the continue statement to "eat" the following
vowels A, E, I, O, U from the inputted word;
• assign the uneaten letters to the word_without_vowels variable and print the
variable to the screen.
Test your program with the data we've provided for you.
Sample input:
Gregory
Expected output:
GRGRY
200
Module 3
Sample input:
abstemious
Expected output:
BSTMS
Sample input:
IOUEA
Expected output:
Code
1 word_without_vowels = ""
2
3 # Prompt the user to enter a word
4 # and assign it to the user_word variable.
5
6
7 for letter in user_word:
8 # Complete the body of the loop.
9
10 # Print the word assigned to word_without_vowels.
11
Check Hint
201
Python Essentials 1
We'll show you how it works – try to judge for yourself if it's usable and whether you can
live without it or not.
In other words, try to convince yourself if the feature is valuable and useful, or is just
syntactic sugar.
Take a look at the following snippet. There's something strange at the end – the else
keyword.
As you may have suspected, loops may have the else branch too, like if s.
The loop's else branch is always executed once, regardless of whether the loop
has entered its body or not.
Can you guess the output? Run the program to check if you were right.
1 i = 1
2 while i < 5:
3 print(i)
4 i += 1
5 else:
6 print("else:", i)
7
Modify the snippet a bit so that the loop has no chance to execute its body even once:
The while 's condition is False at the beginning – can you see it?
Run and test the program, and check whether the else branch has been executed or
not.
202
Module 3
The loop's body won't be executed here at all. Note: we've assigned the i variable
before the loop.
When the loop's body isn't executed, the control variable retains the value it had before
the loop.
Note: if the control variable doesn't exist before the loop starts, it won't exist when
the execution reaches the else branch.
Soon we'll tell you about some other kinds of variables. Our current variables can only
store one value at a time, but there are variables that can do much more – they can
store as many values as you want. But let's do some labs, first.
203
Python Essentials 1
Their pyramid is a bit weird, as it is actually a pyramid-shaped wall – it's flat. The pyramid
is stacked according to one simple principle: each lower layer contains one block more
than the layer above.
Your task is to write a program which reads the number of blocks the builders have, and
outputs the height of the pyramid that can be built using these blocks.
Note: the height is measured by the number of fully completed layers – if the builders
don't have a sufficient number of blocks and cannot complete the next layer, they finish
their work immediately.
Expected output:
Sample input:
20
204
Module 3
Expected output:
Sample input:
1000
Expected output:
Sample input:
Expected output:
Code
1 blocks = int(input("Enter the number of blocks: "))
2
3 #
4 # Write your code here.
5 #
6
7 print("The height of the pyramid:", height)
8
Check Hint
205
Python Essentials 1
if c0 ≠ 1 , go back to point 2.
The hypothesis says that regardless of the initial value of c0 , it will always go to 1.
Of course, it's an extremely complex task to use a computer in order to prove the
hypothesis for any natural number (it may even require artificial intelligence), but you can
use Python to check some individual numbers. Maybe you'll even find the one which
would disprove the hypothesis.
Write a program which reads one natural number and executes these steps as long as
c0 remains different from 1. We also want you to count the steps needed to achieve
the goal. Your code should output all the intermediate values of c0 , too.
Hint: the most important part of the problem is how to transform Collatz's idea into a
while loop – this is the key to success.
Sample input:
15
206
Module 3
Expected output:
46
46
70
35
106
53
160
80
40
20
10
5
16
8
4
2
1
steps = 17
Sample input:
16
Expected output:
8
4
2
1
steps = 4
Sample input:
1023
207
Python Essentials 1
Expected output:
3070
1535
4606
2303
6910
3455
10366
5183
15550
7775
23326
11663
34990
17495
52486
26243
78730
39365
118096
59048
29524
14762
7381
22144
11072
5536
2768
1384
692
346
173
173
260
130
65
208
Module 3
196
98
49
148
74
37
37
56
28
14
7
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1
2
Check Hint
209
Python Essentials 1
for loop executes a set of statements many times; it's used to iterate over a
sequence (e.g. a list, a dictionary, a tuple, or a set – you will learn about them soon) or
other iterable objects (e.g. strings). You can use the for loop to iterate over a
sequence of numbers using the built-in range function. Look at the following examples:
1 # Example 1
2 word = "Python"
3 for letter in word:
4 print(letter, end="*")
5
6 # Example 2
7 for i in range(1, 10):
8 if i % 2 == 0:
9 print(i)
10
2. You can use the break and continue statements to change the flow of a loop:
210
Module 3
4 break
5 print(letter, end="")
6
You use continue to skip the current iteration, and continue with the next iteration,
e.g.:
1 text = "pyxpyxpyx"
2 for letter in text:
3 if letter == "x":
4 continue
5 print(letter, end="")
6
3. The while and for loops can also have an else clause in Python. The
else clause executes after the loop finishes its execution as long as it has not been
terminated by break , e.g.:
1 n = 0
2
3 while n != 3:
4 print(n)
5 n += 1
6 else:
7 print(n, "else")
8
9 print()
10
11 for i in range(0, 3):
12 print(i)
13 else:
14 print(i, "else")
15
211
Python Essentials 1
• stop is an optional parameter specifying the end of the sequence generated (it
is not included),
Example code:
1 for i in range(3):
2 print(i, end=" ") # Outputs: 0 1 2
3
4 for i in range(6, 1, -2):
5 print(i, end=" ") # Outputs: 6, 4, 2
6
212
Module 3
Question 2: Create a while loop that counts from 0 to 10, and prints odd numbers to
the screen. Use the following skeleton:
x = 1
while x < 11:
# Line of code.
# Line of code.
# Line of code.
Question 3: Create a program with a for loop and a break statement. The
program should iterate over characters in an email address, exit the loop when it reaches
the @ symbol, and print the part before @ on one line. Use the following skeleton:
for ch in "[email protected]":
if ch == "@":
# Line of code.
# Line of code.
213
Python Essentials 1
Question 4: Create a program with a for loop and a continue statement. The
program should iterate over a string of digits, replace each 0 with x , and print the
modified string to the screen. Use the following skeleton:
n = 3
while n > 0:
print(n + 1)
n -= 1
else:
print(n)
n = range(4)
for num in n:
print(num – 1)
else:
print(num)
Check Answers
214
SECTION 3.3 – LOGIC
AND BIT OPERATIONS IN
PYTHON
In this section, you will learn about
logical and bitwise operators in Python,
and such concepts as the truth table and
bit shifting.
Python Essentials 1
If we have some free time, and the weather is good, we will go for a walk.
We've used the conjunction and , which means that going for a walk depends on the
simultaneous fulfilment of these two conditions. In the language of logic, such a
connection of conditions is called a conjunction. And now another example:
If you are in the mall or I am in the mall, one of us will buy a gift for Mom.
The appearance of the word or means that the purchase depends on at least one of
these conditions. In logic, such a compound is called a disjunction.
It's clear that Python must have operators to build conjunctions and disjunctions. Without
them, the expressive power of the language would be substantially weakened. They're
called logical operators.
The result provided by the and operator can be determined on the basis of the truth
table.
216
Module 3
The or operator
A disjunction operator is the word or. It's a binary operator with a lower priority than
and (just like + compared to). Its truth table is as follows:
Argument A Argument B A or B
This operator is written as the word not, and its priority is very high: the same as the
unary + and - . Its truth table is simple:
False True
True False
217
Python Essentials 1
Note how the parentheses have been used to code the expressions ‒ we put them there
to improve readability.
We should add that none of these two-argument operators can be used in the
abbreviated form known as op= . This exception is worth remembering.
218
Module 3
The result of their operations is one of these values: False or True. This means that this
snippet will assign the value True to the j variable if i is not zero; otherwise, it
will be False .
1 i = 1
2 j = not not i
3
219
Python Essentials 1
They cover all the operations we mentioned before in the logical context, and one
additional operator. This is the xor (as in exclusive or) operator, and is denoted as ^
(caret).
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
0 1
1 0
220
Module 3
Let us add an important remark: the arguments of these operators must be integers; we
must not use floats here.
The difference in the operation of the logical and bit operators is important: the logical
operators do not penetrate into the bit level of its argument. They're only interested
in the final integer value.
Bitwise operators are stricter: they deal with every bit separately. If we assume that the
integer variable occupies 64 bits (which is common in modern computer systems), you
can imagine the bitwise operation as a 64-fold evaluation of the logical operator for each
pair of bits of the arguments. This analogy is obviously imperfect, as in the real world all
these 64 operations are performed at the same time (simultaneously).
If we assume that the integers are stored with 32 bits, the bitwise image of the two
variables will be as follows:
i: 00000000000000000000000000001111
j: 00000000000000000000000000010110
We are dealing with a logical conjunction here. Let's trace the course of the calculations.
Both variables i and j are not zeros, so will be deemed to represent True. Consulting the
truth table for the and operator, we can see that the result will be True. No other
operations are performed.
221
Python Essentials 1
1 log: True
2
The & operator will operate with each pair of corresponding bits separately, producing
the values of the relevant bits of the result. Therefore, the result will be as follows:
i 00000000000000000000000000001111
j 00000000000000000000000000010110
Let's look at the negation operators now. First the logical one:
1 logneg = not i
2
The logneg variable will be set to False ‒ nothing more needs to be done.
It may be a bit surprising: the bitneg variable value is -16 . This may seem
strange, but isn't at all. If you wish to learn more, you should check out the binary
numeral system and the rules governing two's complement numbers.
i 00000000000000000000000000001111
bitneg = ~i
11111111111111111111111111110000
222
Module 3
Each of these two-argument operators can be used in abbreviated form. These are the
examples of their equivalent notations:
x = x & y x &= y
x = x | y x |= y
x = x ^ y x ^= y
223
Python Essentials 1
flag_register = 0x1234
The variable stores the information about various aspects of system operation. Each bit
of the variable stores one yes/no value. You've also been told that only one of these
bits is yours ‒ the third (remember that bits are numbered from zero, and bit number zero
is the lowest one, while the highest is number 31). The remaining bits are not allowed to
change, because they're intended to store other data. Here's your bit marked with the
letter x:
flag_register = 0000000000000000000000000000x000
1. Check the state of your bit ‒ you want to find out the value of your bit; comparing the
whole variable to zero will not do anything, because the remaining bits can have
completely unpredictable values, but you can use the following conjunction property:
1 x & 1 = x
2 x & 0 = 0
3
If you apply the & operation to the flag_register variable along with the following bit
image:
00000000000000000000000000001000
(note the 1 at your bit's position) as the result, you obtain one of the following bit
strings:
224
Module 3
Such a sequence of zeros and ones, whose task is to grab the value or to change the
selected bits, is called a bit mask.
Let's build a bit mask to detect the state of your bit. It should point to the third bit. That
bit has the weight of 23 = 8. A suitable mask could be created by the following
declaration:
1 the_mask = 8
2
You can also make a sequence of instructions depending on the state of your bit. Here it
is:
1 if flag_register & the_mask:
2 # My bit is set.
3 else:
4 # My bit is reset.
5
2. Reset your bit ‒ you assign a zero to the bit while all the other bits must remain
unchanged; let's use the same property of the conjunction as before, but let's use a
slightly different mask ‒ exactly as follows:
11111111111111111111111111110111
Note that the mask was created as a result of the negation of all the bits of the_mask
variable. Resetting the bit is simple, and looks like this (choose the one you like more):
1 flag_register = flag_register & ~the_mask
2 flag_register &= ~the_mask
3
3. Set your bit ‒ you assign a 1 to your bit, while all the remaining bits must remain
unchanged; use the following disjunction property:
1 x | 1 = 1
2 x | 0 = x
3
You're now ready to set your bit with one of the following instructions:
225
Python Essentials 1
4. Negate your bit ‒ you replace a 1 with a 0 and a 0 with a 1. You can use an
interesting property of the xor operator:
1 x ^ 1 = ~x
2 x ^ 0 = x
3
226
Module 3
You already apply this operation very often and quite unconsciously. How do you multiply
any number by ten? Take a look:
12345 × 10 = 123450
As you can see, multiplying by ten is in fact a shift of all the digits to the left and filling
the resulting gap with zero.
12340 ÷ 10 = 1234
The same kind of operation is performed by the computer, but with one difference: as two
is the base for binary numbers (not 10), shifting a value one bit to the left thus
corresponds to multiplying it by two; respectively, shifting one bit to the right is like
dividing by two (notice that the rightmost bit is lost).
The shift operators in Python are a pair of digraphs: << and >> , clearly
suggesting in which direction the shift will act.
The left argument of these operators is an integer value whose bits are shifted. The
right argument determines the size of the shift.
The priority of these operators is very high. You'll see them in the updated table of
priorities, which we'll show you at the end of this section.
227
Python Essentials 1
17 68 8
Note:
And here is the updated priority table, containing all the operators introduced so far:
Priority Operator
1 ~ , + , -
2 ** unary
3 * , / , // , %
4 + , -
5 << , >>
7 == , !=
binary
8 &
9 |
= , += , -= , *= , /= , %= , &= , ^= , |= ,
10
>>= , <<=
228
Module 3
• not → returns false if the result is true, and returns true if the result is false, e.g.
not True is False .
2. You can use bitwise operators to manipulate single bits of data. The following sample
data:
will be used to illustrate the meaning of bitwise operators in Python. Analyze the following
examples:
• & does a bitwise and, e.g. x & y = 0 , which is 0000 0000 in binary
• >> does a bitwise right shift, e.g. y >> 1 = 8 , which is 0000 1000 in binary
• << does a bitwise left shift, e.g. y << 3 = , which is 1000 0000 in binary
* -16 (decimal from signed 2's complement) -- read more about the Two's complement
operation at https://en.wikipedia.org/wiki/Two%27s_complement.
229
Python Essentials 1
Check Answers
230
SECTION 3.4 – LISTS
Here you will learn about Python lists
and how to perform various operations
on them. You will learn how to index,
update, and delete list elements, how to
perform slices, and how to use some of
the most important list functions and
methods.
Python Essentials 1
var1 = int(input())
var2 = int(input())
var3 = int(input())
var4 = int(input())
var5 = int(input())
var6 = int(input())
:
:
If you don't think that this is a complicated task, then take a piece of paper and write a
program that:
• prints them in order from the smallest to the largest (NB, this kind of processing is
called sorting).
You should find that you don't even have enough paper to complete the task.
So far, you've learned how to declare variables that are able to store exactly one given
value at a time. Such variables are sometimes called scalars by analogy with
mathematics. All the variables you've used so far are actually scalars.
Think of how convenient it would be to declare a variable that could store more than
one value. For example, a hundred, or a thousand or even ten thousand. It would still be
one and the same variable, but very wide and capacious. Sounds appealing? Perhaps,
but how would it handle such a container full of different values? How would it choose
just the one you need?
What if you could just number them? And then say: give me the value number 2; assign
the value number 15; increase the value number 10000.
232
Module 3
We'll show you how to declare such multi-value variables. We'll do this with the
example we just suggested. We'll write a program that sorts a sequence of numbers.
We won't be particularly ambitious ‒ we'll assume that there are exactly five numbers.
Let's create a variable called numbers ; it's assigned with not just one number, but is
filled with a list consisting of five values (note: the list starts with an open square
bracket and ends with a closed square bracket; the space between the brackets is
filled with five numbers separated by commas).
numbers = [10, 5, 7, 2, 1]
Let's say the same thing using adequate terminology: numbers is a list consisting of
five values, all of them numbers. We can also say that this statement creates a list of
length equal to five (as in there are five elements inside it).
The elements inside a list may have different types. Some of them may be integers,
others floats, and yet others may be lists.
Python has adopted a convention stating that the elements in a list are always
numbered starting from zero. This means that the item stored at the beginning of the
list will have the number zero. Since there are five elements in our list, the last of them is
assigned the number four. Don't forget this.
You'll soon get used to it, and it'll become second nature.
Before we go any further in our discussion, we have to state the following: our list is a
collection of elements, but each element is a scalar.
233
Python Essentials 1
Let's assign a new value of 111 to the first element in the list. We do it this way:
1 numbers = [10, 5, 7, 2, 1]
2 print("Original list contents:", numbers) # Printing
original list contents.
3
4 numbers[0] = 111
5 print("New list contents: ", numbers) # Current list
contents.
6
And now we want the value of the fifth element to be copied to the second element ‒
can you guess how to do it?
1 numbers = [10, 5, 7, 2, 1]
2 print("Original list contents:", numbers) # Printing
original list contents.
3
4 numbers[0] = 111
5 print("\nPrevious list contents:", numbers) # Printing
previous list contents.
6
7 numbers[1] = numbers[4] # Copying value of the fifth element
to the second.
8 print("New list contents:", numbers) # Printing current list
contents.
9
The value inside the brackets which selects one element of the list is called an index,
while the operation of selecting an element from the list is known as indexing.
We're going to use the print() function to print the list content each time we make
the changes. This will help us follow each step more carefully and see what's going on
after a particular list modification.
Note: all the indices used so far are literals. Their values are fixed at runtime, but any
expression can be the index, too. This opens up lots of possibilities.
234
Module 3
Assuming that all of the previous operations have been completed successfully, the
snippet will send 111 to the console.
1 numbers = [10, 5, 7, 2, 1]
2 print("Original list contents:", numbers) # Printing
original list contents.
3
4 numbers[0] = 111
5 print("\nPrevious list contents:", numbers) # Printing
previous list contents.
6
7 numbers[1] = numbers[4] # Copying value of the fifth element
to the second.
8 print("Previous list contents:", numbers) # Printing
previous list contents.
9
10 print("\nList length:", len(numbers)) # Printing the list's
length.
11
As you can see, the list may also be printed as a whole – just like here:
1 print(numbers) # Printing the whole list.
2
As you've probably noticed before, Python decorates the output in a way that suggests
that all the presented values form a list. The output from the example snippet looks like
this:
[111, 1, 7, 2, 1]
235
Python Essentials 1
If you want to check the list's current length, you can use a function named len() (its
name comes from length).
The function takes the list's name as an argument, and returns the number of
elements currently stored inside the list (in other words ‒ the list's length).
Look at the last line of the previous code, run the program and check what value it will
print to the console. Can you guess?
236
Module 3
You have to point to the element to be removed ‒ it'll vanish from the list, and the list's
length will be reduced by one.
Look at the following snippet. Can you guess what output it will produce? Run the
program and check.
1 del numbers[1]
2 print(len(numbers))
3 print(numbers)
4
You can't access an element which doesn't exist ‒ you can neither get its value nor
assign it a value. Both of these instructions will cause runtime errors now:
1 print(numbers[4])
2 numbers[4] = 1
3
Add the previous snippet after the last line of code, run the program and check what
happens.
1 numbers = [10, 5, 7, 2, 1]
2 print("Original list content:", numbers) # Printing original
list content.
3
4 numbers[0] = 111
5 print("\nPrevious list content:", numbers) # Printing
previous list content.
6
7 numbers[1] = numbers[4] # Copying value of the fifth element
to the second.
8 print("Previous list content:", numbers) # Printing previous
list content.
9
10 print("\nList's length:", len(numbers)) # Printing previous
list length.
237
Python Essentials 1
11
12 ###
13
14 del numbers[1] # Removing the second element from the list.
15 print("New list's length:", len(numbers)) # Printing new
list length.
16 print("\nNew list content:", numbers) # Printing current
list content.
17
18 ###
19
Note: we've removed one of the list's elements ‒ there are only four elements in the list
now. This means that element number four doesn't exist.
238
Module 3
The example snippet will output 1 . Run the program and check.
Similarly, the element with an index equal to -2 is the one before last in the list.
1 numbers = [111, 7, 2, 1]
2 print(numbers[-2])
3
The last accessible element in our list is numbers[-4] (the first one) ‒ don't try to go
any further!
239
Python Essentials 1
• write a line of code that prompts the user to replace the middle number in the list
with an integer number entered by the user (Step 1)
• write a line of code that removes the last element from the list (Step 2)
• write a line of code that prints the length of the existing list (Step 3).
Code
1 hat_list = [1, 2, 3, 4, 5] # This is an existing list of
numbers hidden in the hat.
2
3 # Step 1: write a line of code that prompts the user
4 # to replace the middle number with an integer number entered
by the user.
5
6 # Step 2: write a line of code that removes the last element
from the list.
7
8 # Step 3: write a line of code that prints the length of the
existing list.
9
10 print(hat_list)
11
Check Hint
240
Module 3
A function doesn't belong to any data ‒ it gets data, it may create new data and it
(generally) produces a result.
A method does all these things, but is also able to change the state of a selected
entity.
A method is owned by the data it works for, while a function is owned by the whole
code.
This also means that invoking a method requires some specification of the data from
which the method is invoked.
It may sound puzzling here, but we'll deal with it in depth when we delve into object-
oriented programming.
result = function(arg)
result = data.method(arg)
NOTE : the name of the method is preceded by the name of the data which owns the
method. Next, you add a dot, followed by the method name, and a pair of parenthesis
enclosing the arguments.
The method will behave like a function, but can do something more ‒ it can change the
internal state of the data from which it has been invoked.
You may ask: why are we talking about methods, not about lists?
241
Python Essentials 1
This is an essential issue right now, as we're going to show you how to add new
elements to an existing list. This can be done with methods owned by all the lists, not by
functions.
242
Module 3
list.append(value)
The insert() method is a bit smarter ‒ it can add a new element at any place in
the list, not only at the end.
list.insert(location, value)
• the first shows the required location of the element to be inserted; note: all the
existing elements that occupy locations to the right of the new element (including
the one at the indicated position) are shifted to the right, in order to make space for
the new element;
Look at the following code. See how we use the append() and insert()
methods. Pay attention to what happens after using insert() : the former first
element is now the second, the second the third, and so on.
1 numbers = [111, 7, 2, 1]
2 print(len(numbers))
3 print(numbers)
4
5 ###
6
7 numbers.append(4)
8
9 print(len(numbers))
243
Python Essentials 1
10 print(numbers)
11
12 ###
13
14 numbers.insert(0, 222)
15 print(len(numbers))
16 print(numbers)
17
18 #
19
Print the final list content to the screen and see what happens. This snippet inserts 333
into the list, making it the second element. The former second element becomes the
third, the third the fourth, and so on.
You can start a list's life by making it empty (this is done with an empty pair of square
brackets) and then adding new elements to it as needed.
Take a look at the following snippet. Try to guess its output after the for loop
execution. Run the program to check if you were right.
1 my_list = [] # Creating an empty list.
2
3 for i in range(5):
4 my_list.append(i + 1)
5
6 print(my_list)
7
It'll be a sequence of consecutive integer numbers from 1 (you then add one to all the
appended values) to 5 .
244
Module 3
4 my_list.insert(0, i + 1)
5
6 print(my_list)
7
What happens now? Run the program and check if this time you're right, too.
You should get the same sequence, but in reverse order (this is the merit of using the
insert() method).
245
Python Essentials 1
Let's assume that you want to calculate the sum of all the values stored in the my_
list list.
You need a variable whose sum will be stored and initially assigned a value of 0 ‒ its
name will be total. (Note: we're not going to name it sum as Python uses the same
name for one of its built-in functions: sum() . Using the same name would generally
be considered bad practice.) Then you add to it all the elements of the list using the
for loop. Take a look at the previous snippet.
• note the way in which the len() function has been employed ‒ it makes the
code independent of any possible changes in the list's contents.
246
Module 3
2 total = 0
3
4 for i in my_list:
5 total += i
6
7 print(total)
8
• the for instruction specifies the variable used to browse the list ( i here)
followed by the in keyword and the name of the list being processed ( my_list
here)
• the i variable is assigned the values of all the subsequent list's elements, and
the process occurs as many times as there are elements in the list;
• this means that you use the i variable as a copy of the elements' values, and you
don't need to use indices;
247
Python Essentials 1
Imagine that you need to rearrange the elements of a list, i.e. reverse the order of the
elements: the first and the fifth as well as the second and fourth elements will be
swapped. The third one will remain untouched.
If you do something like this, you would lose the value previously stored in
variable_2 . Changing the order of the assignments will not help. You need a third
variable that serves as an auxiliary storage.
Python offers a more convenient way of doing the swap – take a look:
1 variable_1 = 1
2 variable_2 = 2
3
4 variable_1, variable_2 = variable_2, variable_1
5
248
Module 3
Now you can easily swap the list's elements to reverse their order:
1 my_list = [10, 1, 8, 3, 5]
2
3 my_list[0], my_list[4] = my_list[4], my_list[0]
4 my_list[1], my_list[3] = my_list[3], my_list[1]
5
6 print(my_list)
7
[5, 3, 8, 1, 10]
Will it still be acceptable with a list containing 100 elements? No, it won't.
Can you use the for loop to do the same thing automatically, irrespective of the list's
length? Yes, you can.
Note:
• we've assigned the length variable with the current list's length (this makes our
code a bit clearer and shorter)
• we've launched the for loop to run through its body length // 2 times (this
works well for lists with both even and odd lengths, because when the list contains
an odd number of elements, the middle one remains untouched)
• we've swapped the ith element (from the beginning of the list) with the one with an
index equal to (length – i – 1) (from the end of the list); in our example, for
i equal to 0 the (length – i – 1) gives 4 ; for i equal to 1 , it
gives 3 ‒ this is exactly what we needed.
Lists are extremely useful, and you'll encounter them very often.
249
Python Essentials 1
The band underwent many line-up changes, culminating in 1962 with the line-up of John
Lennon, Paul McCartney, George Harrison, and Richard Starkey (better known as Ringo
Starr).
Write a program that reflects these changes and lets you practice with the concept of
lists. Your task is to:
• step 2: use the append() method to add the following members of the band to
the list: John Lennon , Paul McCartney , and George Harrison ;
• step 3: use the for loop and the append() method to prompt the user to add
the following members of the band to the list: Stu Sutcliffe , and
Pete Best ;
• step 5: use the insert() method to add Ringo Starr to the beginning of
the list.
By the way, are you a Beatles fan? (The Beatles is one of Greg's favorite bands. But wait.
..who's Greg...?)
Code
1 # step 1
2 print("Step 1:", beatles)
3
4 # step 2
5 print("Step 2:", beatles)
6
7 # step 3
8 print("Step 3:", beatles)
9
250
Module 3
10 # step 4
11 print("Step 4:", beatles)
12
13 # step 5
14 print("Step 5:", beatles)
15
16
17 # testing list length
18 print("The Fab", len(beatles))
19
Check Hint
251
Python Essentials 1
You will learn more about nesting in Module 3.7 ‒ for the time being, we just want you to
be aware that something like this is possible, too.
252
Module 3
Again, you will learn more about this in Module 3.6 ‒ don't worry. For the time being just
try to experiment with this code and check how changing it affects the output.
6. The len() function may be used to check the list's length, e.g.:
1 my_list = ["white", "purple", "blue", "yellow", "green"]
2 print(len(my_list)) # outputs 5
3
4 del my_list[2]
5 print(len(my_list)) # outputs 4
6
253
3.4 SECTION QUIZ
Question 1: What is the output of the following snippet?
1 lst = [1, 2, 3, 4, 5]
2 lst.insert(1, 6)
3 del lst[0]
4 lst.append(1)
5
6 print(lst)
7
Check Answers
SECTION 3.5 – SORTING
SIMPLE LISTS: THE
BUBBLE SORT ALGORITHM
In this section, you will learn how to sort
simple lists using the Bubble sort
algorithm.
Python Essentials 1
In the following sections, we'll sort the list in increasing order, so that the numbers will be
ordered from the smallest to the largest.
We'll try to use the following approach: we'll take the first and the second elements and
compare them; if we determine that they're in the wrong order (i.e. the first is greater than
the second), we'll swap them round; if their order is valid, we'll do nothing. A glance at our
list confirms the latter ‒ the elements 01 and 02 are in the proper order, as in 8 < 10 .
Now look at the second and the third elements. They're in the wrong positions. We have
to swap them:
8 6 10 2 4
We go further, and look at the third and the fourth elements. Again, this is not what it's
supposed to be like. We have to swap them:
8 6 2 10 4
Now we check the fourth and the fifth elements. Yes, they too are in the wrong positions.
Another swap occurs:
8 6 2 4 10
The first pass through the list is already finished. We're still far from finishing our job, but
something curious has happened in the meantime. The largest element, 10 , has
256
Module 3
already gone to the end of the list. Note that this is the desired place for it. All the
remaining elements form a picturesque mess, but this one is already in place.
Now, for a moment, try to imagine the list in a slightly different way ‒ namely, like this:
10
Look ‒ 10 is at the top. We could say that it floated up from the bottom to the surface,
just like the bubble in a glass of champagne. The sorting method derives its name from
the same observation ‒ it's called a bubble sort.
Now we start with the second pass through the list. We look at the first and second
elements – a swap is necessary:
6 8 2 4 10
Time for the second and third elements: we have to swap them too:
6 2 8 4 10
Now the third and fourth elements, and the second pass is finished, as 8 is already in
place:
6 2 4 8 10
We start the next pass immediately. Watch the first and the second elements carefully –
another swap is needed:
2 6 4 8 10
Now 6 needs to go into place. We swap the second and the third elements:
2 4 6 8 10
The list is already sorted. We have nothing more to do. This is exactly what we want.
As you can see, the essence of this algorithm is simple: we compare the adjacent
elements, and by swapping some of them, we achieve our goal.
257
Python Essentials 1
Let's code into Python all the actions performed during a single pass through the list, and
then we'll consider how many passes we actually need in order to perform it. We haven't
explained this so far, and we'll do that a little later.
258
Module 3
We solve this issue in the following way: we introduce another variable; its task is to
observe if any swap has been done during the pass or not; if there is no swap, then the
list is already sorted, and nothing more has to be done. We create a variable named
swapped , and we assign a value of False to it, to indicate that there are no swaps.
Otherwise, it will be assigned True .
1 my_list = [8, 10, 6, 2, 4] # list to sort
2
3 for i in range(len(my_list) – 1): # we need (5 – 1)
comparisons
4 if my_list[i] > my_list[i + 1]: # compare adjacent
elements
5 my_list[i], my_list[i + 1] = my_list[i + 1], my_
list[i] # If we end up here, we have to swap the elements.
6
You should be able to read and understand this program without any problems:
1 my_list = [8, 10, 6, 2, 4] # list to sort
2 swapped = True # It's a little fake, we need it to enter the
while loop.
3
4 while swapped:
5 swapped = False # no swaps so far
6 for i in range(len(my_list) – 1):
7 if my_list[i] > my_list[i + 1]:
8 swapped = True # a swap occurred!
9 my_list[i], my_list[i + 1] = my_list[i + 1], my_
list[i]
10
11 print(my_list)
12
259
Python Essentials 1
Python, however, has its own sorting mechanisms. No one needs to write their own sorts,
as there is a sufficient number of ready-to-use tools.
We explained this sorting system to you because it's important to learn how to process a
list's contents, and to show you how real sorting may work.
If you want Python to sort your list, you can do it like this:
1 my_list = [8, 10, 6, 2, 4]
2 my_list.sort()
3 print(my_list)
4
It is as simple as that.
260
Module 3
[2, 4, 6, 8, 10]
As you can see, all the lists have a method named sort() , which sorts them as fast
as possible. You've already learned about some of the list methods before, and you're
going to learn more about others very soon.
261
Python Essentials 1
2. There is also a list method called reverse() , which you can use to reverse the list,
e.g.:
1 lst = [5, 3, 1, 2, 4]
2 print(lst)
3
4 lst.reverse()
5 print(lst) # outputs: [4, 2, 1, 3, 5]
6
262
Module 3
Check Answers
263
SECTION 3.6 –
OPERATIONS ON LISTS
In this section, you will learn how to
process lists using slices and the in
and not in operators. You will also
analyze a few simple programs utilizing
the concept of lists to learn how to apply
them in more challenging projects.
Python Essentials 1
We want you to memorize it ‒ it may affect your future programs, and cause severe
problems if forgotten or overlooked.
The program:
The surprising part is the fact that the program will output: [2], not [1] , which seems to
be the obvious solution.
Lists (and many other complex Python entities) are stored in different ways than ordinary
(scalar) variables.
• the name of a list is the name of a memory location where the list is stored.
Read these two lines once more ‒ the difference is essential for understanding what we
are going to talk about next.
The assignment: list_2 = list_1 copies the name of the array, not its contents. In
effect, the two names ( list_1 and list_2 ) identify the same location in the
computer memory. Modifying one of them affects the other, and vice versa.
266
Module 3
A slice is an element of Python syntax that allows you to make a brand new copy of a
list, or parts of a list.
This is exactly what you need. Take a look at the following snippet:
1 list_1 = [1]
2 list_2 = list_1[:]
3 list_1[0] = 2
4 print(list_2)
5
The inconspicuous part of the code described as [:] is able to produce a brand new
list.
my_list[start:end-1]
As you can see, it resembles indexing, but the colon inside makes a big difference.
A slice of this form makes a new (target) list, taking elements from the source list ‒
the elements of the indices from start to end – 1 .
Note: not to end but to end – 1 . An element with an index equal to end is the
first element which does not take part in the slicing.
Using negative values for both start and end is possible (just like in indexing).
267
Python Essentials 1
The new_list list will have end – start (3 – 1 = 2) elements ‒ the ones with
indices equal to 1 and 2 (but not 3 ).
Run the following code to see how Python copies the entire list, and some fragment of a
list. Feel free to experiment!
1 # Copying the entire list.
2 list_1 = [1]
3 list_2 = list_1[:]
4 list_1[0] = 2
5 print(list_2)
6
7 # Copying some part of the list.
8 my_list = [10, 8, 6, 4, 2]
9 new_list = my_list[1:3]
10 print(new_list)
11
268
Module 3
my_list[start:end]
To repeat:
• end is the index of the first element not included in the slice.
[8, 6, 4]
If the start specifies an element lying further than the one described by the end
(from the list's beginning), the slice will be empty:
my_list = [10, 8, 6, 4, 2]
new_list = my_list[-1:1]
print(new_list)
[]
If you omit the start in your slice, it is assumed that you want to get a slice beginning
at the element with index 0 .
269
Python Essentials 1
my_list[:end]
my_list[0:end]
Similarly, if you omit the end in your slice, it is assumed that you want the slice to end
at the element with the index len(my_list) .
my_list[start:]
my_list[start:len(my_list)]
270
Module 3
As we've said before, omitting both start and end makes a copy of the whole
list:
1 my_list = [10, 8, 6, 4, 2]
2 new_list = my_list[:]
3 print(new_list)
4
Note: in this case, the slice doesn't produce any new list!
Removing the slice from the code changes its meaning dramatically.
Take a look:
1 my_list = [10, 8, 6, 4, 2]
2 del my_list
3 print(my_list)
4
The del instruction will delete the list itself, not its content.
271
Python Essentials 1
The print() function invocation from the last line of the code will then cause a
runtime error.
272
Module 3
The first of them ( in ) checks if a given element (its left argument) is currently stored
somewhere inside the list (the right argument) ‒ the operator returns True in this
case.
The second ( not in ) checks if a given element (its left argument) is absent in a list ‒
the operator returns True in this case.
Look at the following code. The snippet shows both operators in action. Can you guess
its output? Run the program to check if you were right.
1 my_list = [0, 3, 12, 8, 2]
2
3 print(5 in my_list)
4 print(5 not in my_list)
5 print(12 in my_list)
6
273
Python Essentials 1
The first of them tries to find the greater value in the list. Look at the following code.
1 my_list = [17, 3, 11, 5, 1, 9, 7, 15, 13]
2 largest = my_list[0]
3
4 for i in range(1, len(my_list)):
5 if my_list[i] > largest:
6 largest = my_list[i]
7
8 print(largest)
9
The concept is rather simple ‒ we temporarily assume that the first element is the largest
one, and check the hypothesis against all the remaining elements in the list.
The code may be rewritten to make use of the newly introduced form of the for loop:
1 my_list = [17, 3, 11, 5, 1, 9, 7, 15, 13]
2 largest = my_list[0]
3
4 for i in my_list:
5 if i > largest:
6 largest = i
7
8 print(largest)
9
This program performs one unnecessary comparison, when the first element is compared
with itself, but this isn't a problem at all.
274
Module 3
4 for i in my_list[1:]:
5 if i > largest:
6 largest = i
7
8 print(largest)
9
The question is: which of these two actions consumes more computer resources ‒ just
one comparison, or slicing almost all of a list's elements?
Note:
• the current status of the search is stored in the found variable ( True/False )
Let's assume that you've chosen the following numbers in the lottery: 3 , 7 , 11 ,
42 , 34 , 49 .
275
Python Essentials 1
Note:
276
Module 3
Your task is to write a program which removes all the number repetitions from the list.
The goal is to have a list in which all the numbers appear not more than once.
Note: assume that the source list is hard-coded inside the code ‒ you don't have to enter
it from the keyboard. Of course, you can improve the code and add a part that can carry
out a conversation with the user and obtain all the data from her/him.
Hint: we encourage you to create a new list as a temporary work area ‒ you don't need to
update the list in situ.
We've provided no test data, as that would be too easy. You can use our skeleton
instead.
Code
1 my_list = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
2 #
3 # Write your code here.
4 #
5 print("The list with unique elements only:")
6 print(my_list)
7
Check Hint
277
Python Essentials 1
2. If you want to copy a list or part of the list, you can do it by performing slicing:
1 colors = ['red', 'green', 'orange']
2
3 copy_whole_colors = colors[:] # copy the entire list
4 copy_part_colors = colors[0:2] # copy part of the list
5
3. You can use negative indices to perform slices, too. For example:
1 sample_list = ["A", "B", "C", "D", "E"]
2 new_list = sample_list[2:-1]
3 print(new_list) # outputs: ['C', 'D']
4
4. The start and end parameters are optional when performing a slice:
list[start:end] , e.g.:
1 my_list = [1, 2, 3, 4, 5]
2 slice_one = my_list[2: ]
3 slice_two = my_list[ :2]
4 slice_three = my_list[-2: ]
5
6 print(slice_one) # outputs: [3, 4, 5]
7 print(slice_two) # outputs: [1, 2]
8 print(slice_three) # outputs: [4, 5]
9
278
Module 3
6. You can test if some items exist in a list or not using the keywords in and not in, e.g.:
1 my_list = ["A", "B", 1, 2]
2
3 print("A" in my_list) # outputs: True
4 print("C" not in my_list) # outputs: True
5 print(2 not in my_list) # outputs: False
6
279
Python Essentials 1
280
Module 3
2 list_2 = list_1[:]
3 list_3 = list_2[:]
4
5 del list_1[0]
6 del list_2[0]
7
8 print(list_3)
9
Question 5: Insert in or not in instead of ??? so that the code outputs the
expected result.
1 my_list = [1, 2, "in", True, "ABC"]
2
3 print(1 ??? my_list) # outputs True
4 print("A" ??? my_list) # outputs True
5 print(3 ??? my_list) # outputs True
6 print(False ??? my_list) # outputs False
7
Check Answers
281
SECTION 3.7 – LISTS IN
ADVANCED
APPLICATIONS
In this section, you will learn about
arrays, nested lists (matrices), and list
comprehensions.
Python Essentials 1
We often find such arrays in our lives. Probably the best example of this is a
chessboard.
A chessboard is composed of rows and columns. There are eight rows and eight
columns. Each column is marked with the letters A through H. Each line is marked with a
number from one to eight.
The location of each field is identified by letter-digit pairs. Thus, we know that the bottom
left corner of the board (the one with the white rook) is A1, while the opposite corner is
H8.
Let's assume that we're able to use the selected numbers to represent any chess piece.
We can also assume that every row on the chessboard is a list.
It builds a list containing eight elements representing the second row of the chessboard ‒
the one filled with pawns (assume that WHITE_PAWN is a predefined symbol
representing a white pawn).
List comprehensions
The same effect may be achieved by means of a list comprehension, the special syntax
used by Python in order to fill massive lists.
284
Module 3
the clause specifying how many times the data occurs inside the list
( for i in range(8) ).
Example #1:
1 squares = [x ** 2 for x in range(10)]
2
The snippet produces a ten-element list filled with squares of ten integer numbers starting
from zero (0, 1, 4, 9, 16, 25, 36, 49, 64, 81)
Example #2:
1 twos = [2 ** i for i in range(8)]
2
The snippet creates an eight-element array containing the first eight powers of two (1, 2,
4, 8, 16, 32, 64, 128)
Example #3:
1 odds = [x for x in squares if x % 2 != 0 ]
2
The snippet makes a list with only the odd elements of the squares list.
285
Python Essentials 1
So, if we want to create a list of lists representing the whole chessboard, it may be done
in the following way:
1 board = []
2
3 for i in range(8):
4 row = [EMPTY for i in range(8)]
5 board.append(row)
6
Note:
• the inner part of the loop creates a row consisting of eight elements (each of them
equal to EMPTY ) and appends it to the board list;
This model perfectly mimics the real chessboard, which is in fact an eight-element list of
elements, all being single rows. Let's summarize our observations:
• the elements of the rows are fields, eight of them per row;
• the elements of the chessboard are rows, eight of them per chessboard.
The board variable is now a two-dimensional array. It's also called, by analogy to
algebraic terms, a matrix.
As list comprehensions can be nested, we can shorten the board creation in the
following way:
1 board = [[EMPTY for i in range(8)] for j in range(8)]
2
The inner part creates a row, and the outer part builds a list of rows.
Access to the selected field of the board requires two indices ‒ the first selects the row;
the second ‒ the field number inside the row, which is de facto a column number.
286
Module 3
Take a look at the chessboard. Every field contains a pair of indices which should be
given to access the field's contents:
Glancing at the previous figure, let's set some chess pieces on the board. First, let's add
all the rooks:
1 board[0][0] = ROOK
2 board[0][7] = ROOK
3 board[7][0] = ROOK
4 board[7][7] = ROOK
5
287
Python Essentials 1
Imagine that you're developing a piece of software for an automatic weather station. The
device records the air temperature on an hourly basis and does it throughout the month.
This gives you a total of 24 × 31 = 744 values. Let's try to design a list capable of storing
all these results.
First, you have to decide which data type would be adequate for this application. In this
case, a float would be best, since this thermometer is able to measure the
temperature with an accuracy of 0.1ºC.
Then you take an arbitrary decision that the rows will record the readings every hour on
the hour (so the row will have 24 elements) and each of the rows will be assigned to one
day of the month (let's assume that each month has 31 days, so you need 31 rows).
Here's the appropriate pair of comprehensions ( h is for hour, d for day):
1 temps = [[0.0 for h in range(24)] for d in range(31)]
2
The whole matrix is filled with zeros now. You can assume that it's updated automatically
using special hardware agents. The thing you have to do is to wait for the matrix to be
filled with measurements.
Now it's time to determine the monthly average noon temperature. Add up all 31 readings
recorded at noon and divide the sum by 31. You can assume that the midnight
temperature is stored first. Here's the relevant code:
1 temps = [[0.0 for h in range(24)] for d in range(31)]
2 #
3 # The matrix is magically updated here.
4 #
5
6 total = 0.0
7
288
Module 3
Note: the day variable used by the for loop is not a scalar ‒ each pass through the
temps matrix assigns it with the subsequent rows of the matrix; hence, it's a list. It has
to be indexed with 11 to access the temperature value measured at noon.
Now find the highest temperature during the whole month ‒ see the code:
1 temps = [[0.0 for h in range(24)] for d in range(31)]
2 #
3 # The matrix is magically updated here.
4 #
5
6 highest = -100.0
7
8 for day in temps:
9 for temp in day:
10 if temp > highest:
11 highest = temp
12
13 print("The highest temperature was:", highest)
14
NOTE
• the day variable iterates through all the rows in the temps matrix;
• the temp variable iterates through all the measurements taken in one day.
Now count the days when the temperature at noon was at least 20ºC:
1 temps = [[0.0 for h in range(24)] for d in range(31)]
2 #
3 # The matrix is magically updated here.
4 #
289
Python Essentials 1
5
6 hot_days = 0
7
8 for day in temps:
9 if day[11] > 20.0:
10 hot_days += 1
11
12 print(hot_days, "days were hot.")
13
Python does not limit the depth of list-in-list inclusion. Here you can see an example of a
three-dimensional array:
1 rooms = [[[False for r in range(20)] for f in range(15)] for
t in range(3)]
2
Imagine a hotel. It's a huge hotel consisting of three buildings, 15 floors each. There are
20 rooms on each floor. For this, you need an array which can collect and process
information on the occupied/free rooms.
First step ‒ the type of the array's elements. In this case, a Boolean value ( True/
False ) would fit.
Step two ‒ calm analysis of the situation. Summarize the available information: three
buildings, 15 floors, 20 rooms.
The first index ( 0 through 2 ) selects one of the buildings; the second ( 0 through
14 ) selects the floor, the third ( 0 through 19 ) selects the room number. All rooms
are initially free.
Now you can book a room for two newlyweds: in the second building, on the tenth floor,
room 14:
1 rooms[1][9][13] = True
2
290
Module 3
and release the second room on the fifth floor located in the first building:
1 rooms[0][4][1] = False
2
Check if there are any vacancies on the 15th floor of the third building:
1 vacancy = 0
2
3 for room_number in range(20):
4 if not rooms[2][14][room_number]:
5 vacancy += 1
6
The vacancy variable contains 0 if all the rooms are occupied, or the number of
available rooms otherwise.
Congratulations! You've made it to the end of the module. Keep up the good work!
291
Python Essentials 1
Here's an example of a list comprehension ‒ the code creates a five-element list filled
with the first five natural numbers raised to the power of 3:
1 cubed = [num ** 3 for num in range(5)]
2 print(cubed) # outputs: [0, 1, 8, 27, 64]
3
2. You can use nested lists in Python to create matrices (i.e. two-dimensional lists). For
example:
292
Module 3
(4x4)
2
3 table = [[":(", ":)", ":(", ":)"],
4 [":)", ":(", ":)", ":)"],
5 [":(", ":)", ":)", ":("],
6 [":)", ":)", ":)", ":("]]
7
8 print(table)
9 print(table[0][0]) # outputs: ':('
10 print(table[0][3]) # outputs: ':)'
11
3. You can nest as many lists in lists as you want, thereby creating n-dimensional lists,
e.g. three-, four- or even sixty-four-dimensional arrays. For example:
293
Python Essentials 1
294
MODULE 4
FUNCTIONS, TUPLES,
DICTIONARIES,
EXCEPTIONS, AND
DATA PROCESSING
• The concept of passing arguments in different ways and setting their default values,
along with the mechanisms of returning the function’s results
• New data aggregates: tuples and dictionaries, and their role in data processing.
SECTION 4.1 –
FUNCTIONS
Welcome to Module 4! In this section,
you will learn how to create, use, and call
your own functions. Let's start!
Python Essentials 1
When you want some data to be printed on the console, you use print() . When you
want to read the value of a variable, you use input() , coupled with either int()
or float() .
You've also made use of some methods, which are in fact functions, but declared in a
very specific way.
Now you'll learn how to write and use your own functions. We'll write several functions
together, from the very simple to the rather complex, which will require your focus and
attention.
It often happens that a particular piece of code is repeated many times in your
program. It's repeated either literally, or with only a few minor modifications, consisting of
the use of other variables in the same algorithm. It also happens that a programmer
cannot resist simplifying their work, and begins to clone such pieces of code using the
clipboard and copy-paste operations.
It could end up as greatly frustrating when suddenly it turns out that there was an error in
the cloned code. The programmer will have a lot of drudgery to find all the places that
need corrections. There's also a high risk of the corrections causing errors.
We can now define the first condition which can help you decide when to start writing
your own functions: if a particular fragment of the code begins to appear in more
than one place, consider the possibility of isolating it in the form of a function
invoked from the points where the original code was placed before.
It may happen that the algorithm you're going to implement is so complex that your code
begins to grow in an uncontrolled manner, and suddenly you notice that you're not able to
navigate through it so easily anymore.
You can try to cope with the issue by commenting the code extensively, but soon you find
that this dramatically worsens your situation ‒ too many comments make the code
larger and harder to read. Some say that a well-written function should be viewed
entirely in one glance.
A good, attentive developer divides the code (or more accurately: the problem) into well-
isolated pieces, and encodes each of them in the form of a function.
298
Module 4
This considerably simplifies the work of the program, because each piece of code can be
encoded separately, and tested separately. The process described here is often called
decomposition.
We can now state the second condition: if a piece of code becomes so large that
reading and understating it may cause a problem, consider dividing it into
separate, smaller problems, and implement each of them in the form of a separate
function.
This decomposition continues until you get a set of short functions, easy to understand
and test.
299
Python Essentials 1
4.1.2 Decomposition
It often happens that the problem is so large and complex that it cannot be assigned to a
single developer, and a team of developers have to work on it. The problem must be
split between several developers in a way that ensures their efficient and seamless
cooperation.
It seems inconceivable that more than one programmer should write the same piece of
code at the same time, so the job has to be dispersed among all the team members.
This kind of decomposition has a different purpose to the one described previously ‒ it's
not only about sharing the work, but also about sharing the responsibility among
many developers.
Each of them writes a clearly defined and described set of functions, which when
combined into the module (we'll tell you about this a bit later) will give the final product.
This leads us directly to the third condition: if you're going to divide the work among
multiple programmers, decompose the problem to allow the product to be
implemented as a set of separately written functions packed together in different
modules.
300
Module 4
301
Python Essentials 1
302
Module 4
It's rather simple, but we only want it to be an example of transforming a repeating part
of a code into a function.
The messages sent to the console by the print() function are always the same. Of
course, there's nothing really bad in such a code, but try to imagine what you would have
to do if your boss asked you to change the message to make it more polite, e.g. to start it
with the phrase "Please" .
It seems that you'd have to spend some time changing all the occurrences of the
message (you'd use a clipboard, of course, but it wouldn't make your life much easier).
It's obvious that you'd probably make some mistakes during the amendment process,
and you (and your boss) would get a bit frustrated.
Is it possible to separate such a repeatable part of the code, name it, and make it
reusable? It would mean that a change made once in one place would be propagated
to all the places where it's used.
Of course, a code like this should work only when it's explicitly launched.
303
Python Essentials 1
• next after def goes the name of the function (the rules for naming functions are
exactly the same as for naming variables)
• after the function name, there's a place for a pair of parentheses (they contain
nothing here, but that will change soon)
• the line directly after def begins the function body ‒ a couple (at least one) of
necessarily nested instructions, which will be executed every time the function is
invoked; note: the function ends where the nesting ends, so you have to be
careful.
We're ready to define our prompting function. We'll name it message ‒ here it is:
1 def message():
2 print("Enter a value: ")
3
The function is extremely simple, but fully usable. We've named it message, but you can
label it according to your taste. Let's use it.
Note: we don't use the function at all ‒ there's no invocation of it inside the code.
We start here.
We end here.
This means that Python reads the function's definitions and remembers them, but won't
launch any of them without your permission.
304
Module 4
We've modified the code now ‒ we've inserted the function's invocation between the
start and end messages:
1 def message():
2 print("Enter a value: ")
3
4 print("We start here.")
5 message()
6 print("We end here.")
7
We start here.
Enter a value:
We end here.
305
Python Essentials 1
• when you invoke a function, Python remembers the place where it happened and
jumps into the invoked function;
• reaching the end of the function forces Python to return to the place directly after
the point of invocation.
There are two, very important, catches. Here's the first of them:
You mustn't invoke a function which is not known at the moment of invocation.
Remember – Python reads your code from top to bottom. It's not going to look ahead in
order to find a function you forgot to put in the right place ("right" means "before
invocation".)
We've inserted an error into this code ‒ can you see the difference?
1 print("We start here.")
2 message()
3 print("We end here.")
4
5
6 def message():
7 print("Enter a value: ")
306
Module 4
We've moved the function to the end of the code. Is Python able to find it when the
execution reaches the invocation?
Don't try to force Python to look for functions you didn't deliver at the right time.
Assigning a value to the name message causes Python to forget its previous role. The
function named message becomes unavailable.
Fortunately, you're free to mix your code with functions ‒ you're not obliged to put all
your functions at the top of your source file.
It may look strange, but it's completely correct, and works as intended.
307
Python Essentials 1
Let's return to our primary example, and employ the function for the right job, like here:
1 def message():
2 print("Enter a value: ")
3
4 message()
5 a = int(input())
6 message()
7 b = int(input())
8 message()
9 c = int(input())
10
Modifying the prompting message is now easy and clear – you can do it by changing the
code in just one place ‒ inside the function's body.
Try it yourself.
308
Module 4
built-in functions which are an integral part of Python (such as the print()
function). You can see a complete list of built-in Python functions at https://docs.python.
org/3/library/functions.html.
the ones that come from pre-installed modules (you'll learn about them in the Python
Essentials 2 course)
user-defined functions which are written by users for users ‒ you can write your own
functions and use them freely in your code,
the lambda functions (you'll learn about them in the Python Essentials 2 course.)
3. You can define your own function using the def keyword and the following syntax:
1 def your_function(optional parameters):
2 # the body of the function
3
You can define a function which doesn't take any arguments, e.g.:
1 def message(): # defining a function
2 print("Hello") # body of the function
3
4 message() # calling the function
5
You can define a function which takes arguments, too, just like the following one-
parameter function:
1 def hello(name): # defining a function
2 print("Hello,", name) # body of the function
3
4
309
Python Essentials 1
We'll tell you more about parametrized functions in the next section. Don't worry.
310
Module 4
a) user-defined function
b) built-in function
Question 2: What happens when you try to invoke a function before you define it?
Example:
1 hi()
2
3 def hi():
4 print("hi!")
5
Question 3: What will happen when you run the following code?
1 def hi():
2 print("hi")
3
4 hi(5)
5
Check Answers
311
SECTION 4.2 – HOW
FUNCTIONS COMMUNICATE
WITH THEIR ENVIRONMENT
In this section, you will learn about
parameterless and parameterized
functions, as well as how to write one-,
two- and three-parameter functions and
pass arguments to them. Let's begin!
Python Essentials 1
A parameter is actually a variable, but there are two important factors that make
parameters different and special:
• parameters exist only inside functions in which they have been defined, and
the only place where the parameter can be defined is a space between a pair of
parentheses in the def statement;
def function(parameter):
###
Don't forget:
Let's enrich the function with just one parameter ‒ we're going to use it to show the user
the number of a value the function asks for.
We have to rebuild the def statement ‒ this is what it looks like now:
def message(number):
###
The definition specifies that our function operates on just one parameter named
number . You can use it as an ordinary variable, but only inside the function ‒ it isn't
visible anywhere else.
314
Module 4
We've made use of the parameter. Note: we haven't assigned the parameter with any
value. Is it correct?
Yes, it is.
A value for the parameter will arrive from the function's environment.
Moreover, it behaves better. The code will produce the following output:
Enter a number: 1
Can you see how it works? The value of the argument used during invocation (1) has
been passed into the function, setting the initial value of the parameter named number.
It's legal, and possible, to have a variable named the same as a function's parameter.
315
Python Essentials 1
1 def message(number):
2 print("Enter a number:", number)
3
4 number = 1234
5 message(1)
6 print(number)
7
The parameter named number is a completely different entity from the variable named
number .
This means that the previous snippet will produce the following output:
Enter a number: 1
1234
A function can have as many parameters as you want, but the more parameters you
have, the harder it is to memorize their roles and purposes.
This also means that invoking the function will require two arguments.
The first new parameter is intended to carry the name of the desired value.
316
Module 4
Here it is:
1 def message(what, number):
2 print("Enter", what, "number", number)
3
4 message("telephone", 11)
5 message("price", 5)
6 message("number", "number")
7
Run the code, modify it, add more parameters, and see how this affects the output.
317
Python Essentials 1
You've used it already, but Python can offer a lot more. We're going to tell you about it
now.
1 def my_function(a, b, c):
2 print(a, b, c)
3
4 my_function(1, 2, 3)
5
Note: positional parameter passing is intuitively used by people in many social occasions.
For example, it may be generally accepted that when we introduce ourselves we mention
our first name(s) before our last name, e.g. "My name's John Doe."
Let's implement that social custom in Python. The following function will be responsible
for introducing somebody:
1 def introduction(first_name, last_name):
2 print("Hello, my name is", first_name, last_name)
3
4 introduction("Luke", "Skywalker")
5 introduction("Jesse", "Quick")
6 introduction("Clark", "Kent")
7
Can you guess the output? Run the code and find out if you're right.
Now imagine that the same function is being used in Hungary. In this case, the code
would look like this:
1 def introduction(first_name, last_name):
2 print("Hello, my name is", first_name, last_name)
3
4 introduction("Skywalker", "Luke")
5 introduction("Quick", "Jesse")
318
Module 4
6 introduction("Kent", "Clark")
7
Run the code to see if you're right here, too. Are you surprised?
319
Python Essentials 1
The concept is clear ‒ the values passed to the parameters are preceded by the target
parameters' names, followed by the = sign.
The position doesn't matter here ‒ each argument's value knows its destination on the
basis of the name used.
You should be able to predict the output. Run the code to check if you're right.
320
Module 4
To show you how it works, we'll use the following simple three-parameter function:
1 def adding(a, b, c):
2 print(a, "+", b, "+", c, "=", a + b + c)
3
Its purpose is to evaluate and present the sum of all its arguments.
will output:
1 + 2 + 3 = 6
Of course, you can replace such an invocation with a purely keyword variant, like this:
1 adding(c = 1, a = 2, b = 3)
2
2 + 3 + 1 = 6
321
Python Essentials 1
• the argument ( 3 ) for the a parameter is passed using the positional way;
3 + 2 + 1 = 6
Be careful, and beware of mistakes. If you try to pass more than one value to one
argument, all you'll get is a runtime error.
Look at the following invocation – it seems that we've tried to set a twice:
1 adding(3, a = 1, b = 2)
2
Python's response:
Look at the following snippet. A code like this is fully correct, but it doesn't make much
sense:
1 adding(4, 3, c = 2)
2
Everything is right, but leaving in just one keyword argument looks a bit weird ‒ what do
you think?
322
Module 4
They say that the most popular English last name is Smith. Let's try to take this into
account.
The default parameter's value is set using clear and pictorial syntax:
1 def introduction(first_name, last_name="Smith"):
2 print("Hello, my name is", first_name, last_name)
3
You only have to extend the parameter's name with the = sign, followed by the default
value.
Can you guess the output of the program? Run it and check if you're right.
And? Everything looks the same, but when you invoke the function in a way that looks a
bit suspicious at first sight, like this:
1 introduction("Henry")
2
or this:
1 introduction(first_name="William")
2
there will be no error, and both invocations will succeed, while the console will show the
following output:
Test it.
323
Python Essentials 1
You can go further if it's useful. Both parameters have their default values now, look at
the following code:
1 def introduction(first_name="John", last_name="Smith"):
2 print("Hello, my name is", first_name, last_name)
3
If you use one keyword argument, the remaining one will take the default value:
1 introduction(last_name="Hopkins")
2
Test it.
Congratulations ‒ you have just learned some basic techniques for communicating with
functions.
324
Module 4
• positional argument passing in which the order of arguments passed matters (Ex.
1)
325
Python Essentials 1
1 Ex. 1
2 def subtra(a, b):
3 print(a – b)
4
5 subtra(5, 2) # outputs: 3
6 subtra(2, 5) # outputs: -3
7
8
9 Ex. 2
10 def subtra(a, b):
11 print(a – b)
12
13 subtra(a=5, b=2) # outputs: 3
14 subtra(b=2, a=5) # outputs: 3
15
16 Ex. 3
17 def subtra(a, b):
18 print(a – b)
19
20 subtra(5, b=2) # outputs: 3
21 subtra(5, 2) # outputs: 3
22
326
Module 4
3. You can use the keyword argument-passing technique to pre-define a value for a
given argument:
1 def name(first_name, last_name="Smith"):
2 print(first_name, last_name)
3
4 name("Andy") # outputs: Andy Smith
5 name("Betty", "Johnson") # outputs: Betty Johnson (the
keyword argument replaced by "Johnson")
6
327
Python Essentials 1
Check Answers
328
SECTION 4.3 –
RETURNING A RESULT
FROM A FUNCTION
In this part of the course, you will learn
about the effects and results of functions,
the return expression, and the
None value. You will also learn how to
pass lists as function arguments, how to
return lists as function results, and how
to assign function results to variables.
Let's go!
Python Essentials 1
To get functions to return a value (but not only for this purpose) you use the return
instruction.
This word gives you a full picture of its capabilities. Note: it's a Python keyword.
The return instruction has two different variants ‒ let's consider them separately.
happy_new_year()
the function causes a little noise ‒ the output will look like this:
Three...
Two...
One...
Happy New Year!
330
Module 4
happy_new_year(False)
will modify the function's behavior ‒ the return instruction will cause its termination just
before the wishes ‒ this is the updated output:
Three...
Two...
One...
• moreover, the function will evaluate the expression's value and will return it
(hence the name once again) as the function's result.
331
Python Essentials 1
123
The return instruction, enriched with the expression (the expression is very simple
here), "transports" the expression's value to the place where the function has been
invoked.
Note: we're not being too polite here — the function returns a value, and we ignore it (we
don't use it in ay way):
1 def boring_function():
2 print("'Boredom Mode' ON.")
3 return 123
4
5 print("This lesson is interesting!")
6 boring_function()
7 print("This lesson is boring!")
8
332
Module 4
The only disadvantage is that the result has been irretrievably lost.
Don't forget:
• you are always allowed to ignore the function's result, and be satisfied with the
function's effect (if the function has any)
• if a function is intended to return a useful result, it must contain the second variant
of the return instruction.
Wait a minute ‒ does this mean that there are useless results, too? Yes, in some sense.
333
Python Essentials 1
Its data doesn't represent any reasonable value ‒ actually, it's not a value at all; hence, it
mustn't take part in any expressions.
print(None + 2)
NOTE
None is a keyword.
There are only two kinds of circumstances when None can be safely used:
Don't forget this: if a function doesn't return a certain value using a return expression
clause, it is assumed that it implicitly returns None .
334
Module 4
It's obvious that the strange_function function returns True when its argument
is even.
True
None
Don't be surprised next time you see None as a function result ‒ it may be the
symptom of a subtle mistake inside the function.
335
Python Essentials 1
Of course it may! Any entity recognizable by Python can play the role of a function
argument, although it has to be assured that the function is able to cope with it.
So, if you pass a list to a function, the function has to handle it like a list.
will return 12 as a result, but you should expect problems if you invoke it in this risky
way:
1 print(list_sum(5))
2
This is caused by the fact that a single integer value mustn't be iterated through by
the for loop.
336
Module 4
1 def strange_list_fun(n):
2 strange_list = []
3
4 for i in range(0, n):
5 strange_list.insert(0, i)
6
7 return strange_list
8
9 print(strange_list_fun(5))
10
[4, 3, 2, 1, 0]
Let's dive a little deeper into the issues connected with variables in functions. This is
essential for creating effective and safe functions.
337
Python Essentials 1
The seed of the function is already sown in the following skeleton code.
Note: we've also prepared a short testing code, which you can use to test your function.
The code uses two lists ‒ one with the test data, and the other containing the expected
results. The code will tell you if any of your results are invalid.
Code
1 def is_year_leap(year):
2 #
3 # Write your code here.
4 #
5
6 test_data = [1900, 2000, 2016, 1987]
7 test_results = [False, True, True, False]
8 for i in range(len(test_data)):
9 yr = test_data[i]
10 print(yr,"->",end="")
11 result = is_year_leap(yr)
12 if result == test_results[i]:
13 print("OK")
14 else:
15 print("Failed")
16
Check Hint
338
Module 4
The initial part of the function is ready. Now, convince the function to return None if its
arguments don't make sense.
Of course, you can (and should) use the previously written and tested function (LAB 26).
It may be very helpful. We encourage you to use a list filled with the months' lengths. You
can create it inside the function ‒ this trick will significantly shorten the code.
Code
1 def is_year_leap(year):
2 #
3 # Your code from the previous LAB.
4 #
5
6 def days_in_month(year, month):
7 #
8 # Write your new code here.
9 #
10
11 test_years = [1900, 2000, 2016, 1987]
12 test_months = [2, 2, 1, 11]
13 test_results = [28, 29, 31, 30]
14 for i in range(len(test_years)):
15 yr = test_years[i]
16 mo = test_months[i]
17 print(yr, mo, "->", end="")
18 result = days_in_month(yr, mo)
339
Python Essentials 1
19 if result == test_results[i]:
20 print("OK")
21 else:
22 print("Failed")
23
Check Hint
340
Module 4
Use the previously written and tested functions. Add your own test cases to the code.
Code
1 def is_year_leap(year):
2 #
3 # Your code from the previous LAB.
4 #
5
6 def days_in_month(year, month):
7 #
8 # Your code from the previous lab.
9 #
10
11 def day_of_year(year, month, day):
12 #
13 # Write your new code here.
14 #
15
16 print(day_of_year(2000, 12, 31))
17
Check Hint
341
Python Essentials 1
Complicated? Not at all. For example, 8 isn't a prime number, as you can divide it by 2
and 4 (we can't use divisors equal to 1 and 8, as the definition prohibits this).
On the other hand, 7 is a prime number, as we can't find any legal divisors for it.
The function:
• is called is_prime ;
Hint: try to divide the argument by all subsequent values (starting from 2) and check the
remainder ‒ if it's zero, your number cannot be a prime; think carefully about when you
should stop the process.
If you need to know the square root of any value, you can utilize the ** operator.
Remember: the square root of x is the same as x0.5 .
Run your code and check whether your output is the same as ours.
Expected output:
2 3 5 7 11 13 17 19
Code
1 def is_prime(num):
2 #
3 # Write your code here.
4 #
5
6 for i in range(1, 20):
7 if is_prime(i + 1):
8 print(i + 1, end=" ")
342
Module 4
9 print()
10
Check Hint
343
Python Essentials 1
In the USA, it is shown as the number of miles traveled by a car using one gallon of fuel.
Your task is to write a pair of functions converting l/100km into mpg, and vice versa.
The functions:
Complete the following code and run it to check whether your output is the same as ours.
Expected output:
60.31143162393162
31.36194444444444
23.52145833333333
3.9007393587617467
7.490910297239916
10.009131205673757
Code
1 def liters_100km_to_miles_gallon(liters):
2 #
3 # Write your code here.
4 #
5
6 def miles_gallon_to_liters_100km(miles):
7 #
8 # Write your code here.
9 #
344
Module 4
10
11 print(liters_100km_to_miles_gallon(3.9))
12 print(liters_100km_to_miles_gallon(7.5))
13 print(liters_100km_to_miles_gallon(10.))
14 print(miles_gallon_to_liters_100km(60.3))
15 print(miles_gallon_to_liters_100km(31.4))
16 print(miles_gallon_to_liters_100km(23.5))
17
Check Hint
345
Python Essentials 1
346
Module 4
347
Python Essentials 1
348
Module 4
Check Answers
349
SECTION 4.4 – SCOPES
IN PYTHON
In this part of the course, you will learn
about scopes in Python, and the
global keyword. By the end of the
section you will be able to distinguish
between local and global variables, and
know how to utilize the mechanism of
namespaces in your programs.
Python Essentials 1
The scope of a name (e.g. a variable name) is the part of a code where the name is
properly recognizable.
For example, the scope of a function's parameter is the function itself. The parameter is
inaccessible outside the function.
Let's check it. Look at the following code. What will happen when you run it?
1 def scope_test():
2 x = 123
3
4
5 scope_test()
6 print(x)
7
The program will fail when run. The error message will read:
This is to be expected.
We're going to conduct some experiments with you to show you how Python constructs
scopes, and how you can use these to your benefit.
Let's start by checking whether or not a variable created outside any function is visible
inside the functions. In other words, does a variable's name propagate into a function's
body?
352
Module 4
The answer is: a variable existing outside a function has scope inside the function's
body.
This rule has a very important exception. Let's try to find it.
The result has changed, too ‒ the code produces a slightly different output now:
What's happened?
• the var variable created inside the function is not the same as when defined
outside it ‒ it seems that there two different variables of the same name;
• moreover, the function's variable shadows the variable coming from the outside
world.
A variable existing outside a function has scope inside the function's body,
excluding those which define a variable of the same name.
It also means that the scope of a variable existing outside a function is supported
only when getting its value (reading). Assigning a value forces the creation of the
function's own variable.
Make sure you understand this well and carry out your own experiments.
353
Python Essentials 1
There's a special Python method which can extend a variable's scope in a way which
includes the function's body (even if you want not only to read the values, but also to
modify them).
Using this keyword inside a function with the name (or names separated with commas) of
a variable (or variables), forces Python to refrain from creating a new variable inside the
function ‒ the one accessible from outside will be used instead.
In other words, this name becomes global (it has global scope, and it doesn't matter
whether it's the subject of read or assign).
354
Module 4
This should be sufficient evidence to show that the global keyword does what it
promises.
355
Python Essentials 1
The following code should teach you something. As you can see, the function changes
the value of its parameter. Does the change affect the argument?
1 def my_function(n):
2 print("I got", n)
3 n += 1
4 print("I have", n)
5
6
7 var = 1
8 my_function(var)
9 print(var)
10
I got 1
I have 2
1
This also means that a function receives the argument's value, not the argument itself.
This is true for scalars.
Is it worth checking how it works with lists (do you recall the peculiarities of assigning list
slices versus assigning lists as a whole?).
356
Module 4
We don't change the value of the parameter my_list_1 (we already know it will not
affect the argument), but instead modify the list identified by it.
357
Python Essentials 1
Let's try:
• if the argument is a list, then changing the value of the corresponding parameter
doesn't affect the list (remember: variables containing lists are stored in a different
way than scalars)
• but if you change a list identified by the parameter (note: the list, not the
parameter!), the list will reflect the change.
It's time to write some example functions. You'll do that in the next section.
358
Module 4
Example 1:
1 var = 2
2
3
4 def mult_by_var(x):
5 return x * var
6
7
8 print(mult_by_var(7)) # outputs: 14
9
Example 2:
1 def mult(x):
2 var = 5
3 return x * var
4
5
6 print(mult(7)) # outputs: 35
7
Example 3:
1 def mult(x):
2 var = 7
3 return x * var
4
5
6 var = 3
7 print(mult(7)) # outputs: 49
8
2. A variable that exists inside a function has scope inside the function body (Example 4),
e.g.:
359
Python Essentials 1
Example 4:
1 def adding(x):
2 var = 7
3 return x + var
4
5
6 print(adding(4)) # outputs: 11
7 print(var) # NameError
8
3. You can use the global keyword followed by a variable name to make the
variable's scope global, e.g.:
1 var = 2
2 print(var) # outputs: 2
3
4
5 def return_var():
6 global var
7 var = 5
8 return var
9
10
11 print(return_var()) # outputs: 5
12 print(var) # outputs: 5
13
360
Module 4
361
Python Essentials 1
13
Check Answers
362
SECTION 4.5 – CREATING
MULTI-PARAMETER
FUNCTIONS
Here we will analyze the following
examples of multi-parameter functions:
BMI calculator, Unit Converter, Triangle
Tester, Triangle Area Calculator,
Factorial, Fibonacci, and recursive
functions.
Python Essentials 1
It seems that this new function will have two parameters. Its name will be bmi , but if
you prefer any other name, use it instead.
19.283746556473833
The function fulfils our expectations, but it's a bit simple ‒ it assumes that the values of
both parameters are always meaningful. It's definitely worth checking if they're
trustworthy.
Let's check them both and return None if any of them looks suspicious.
364
Module 4
First, the test invocation ensures that the protection works properly ‒ the output is:
None
Second, take a look at the way the backslash ( \ ) symbol is used. If you use it in
Python code and end a line with it, it will tell Python to continue the line of code in the
next line of code.
It can be particularly useful when you have to deal with long lines of code and you'd like
to improve code readability.
Okay, but there's something we omitted too easily ‒ the imperial measurements. This
function is not too useful for people accustomed to pounds, feet, and inches.
We can write two simple functions to convert imperial units to metric ones. Let's start
with pounds.
365
Python Essentials 1
0.45359237
And now it's time for feet and inches: 1 ft = 0.3048 m , and
1 in = 2.54 cm = 0.0254 m .
It looks as expected.
Note: we wanted to name the second parameter just in , not inch , but we couldn't.
Do you know why?
1.8288000000000002
It's quite possible that sometimes you may want to use just feet without inches. Will
Python help you? Of course it will.
366
Module 4
Now the inch parameter has its default value equal to 0.0 .
1.8288000000000002
Finally, the code is able to answer the question: what is the BMI of a person 5'7" tall and
weighing 176 lbs?
27.565214082533313
367
Python Essentials 1
We know from school that the sum of two arbitrary sides has to be longer than the third
side.
It won't be a hard challenge. The function will have three parameters ‒ one for each
side.
It will return True if the sides can build a triangle, and False otherwise. In this
case, is_a_triangle is a good name for such a function.
Look at the following code. You can find our function there. Run the program.
368
Module 4
True
False
We've negated the condition (reversed the relational operators and replaced or s with
and s, receiving a universal expression for testing triangles).
Let's install the function in a larger program. It'll ask the user for three values and make
use of the function.
369
Python Essentials 1
In the second step, we'll try to ensure that a certain triangle is a right-angle triangle.
c2 = a2 + b2
370
Module 4
Look at how we test the relationship between the hypotenuse and the remaining sides ‒
we choose the longest side, and apply the Pythagorean theorem to check if everything
is right. This requires three checks in total.
We're going use the exponentiation operator to find the square root ‒ it may seem
strange, but it works:
We try it with a right-angle triangle as a half of a square with one side equal to 1. This
means that its area should be equal to 0.5.
371
Python Essentials 1
0.49999999999999983
It's very close to 0.5, but it isn't exactly 0.5. What does it mean? Is it an error?
No, it isn't. This is the specifics of floating-point calculations. We'll tell you more about
it soon.
372
Module 4
It's marked with an exclamation mark, and is equal to the product of all natural
numbers from one up to its argument.
Let's write our code. We'll create a function and call it factorial_function . Here is
the code:
1 def factorial_function(n):
2 if n < 0:
3 return None
4 if n < 2:
5 return 1
6
7 product = 1
8 for i in range(2, n + 1):
9 product *= i
10 return product
11
12
13 for n in range(1, 6): # testing
14 print(n, factorial_function(n))
15
Notice how we mirror step-by-step the mathematical definition, and how we use the for
loop to find the product.
We add a simple testing code, and these are the results we get:
1 1
2 2
3 6
4 24
5 120
373
Python Essentials 1
They are a sequence of integer numbers built using a very simple rule:
Let's create our fib function and test it. Here it is:
1 def fib(n):
2 if n < 1:
3 return None
4 if n < 3:
5 return 1
6
7 elem_1 = elem_2 = 1
8 the_sum = 0
9 for i in range(3, n + 1):
10 the_sum = elem_1 + elem_2
11 elem_1, elem_2 = elem_2, the_sum
12 return the_sum
13
14
15 for n in range(1, 10): # testing
16 print(n, "->", fib(n))
17
Analyze the for loop body carefully, and find out how we move the elem_1 and
elem_2 variables through the subsequent Fibonacci numbers.
374
Module 4
1 -> 1
2 -> 1
3 -> 2
4 -> 3
5 -> 5
6 -> 8
7 -> 13
8 -> 21
9 -> 34
375
Python Essentials 1
4.5.5 Recursion
There's one more thing we want to show you to make everything complete − it's
recursion.
This term may describe many different concepts, but one of them is especially interesting
− the one referring to computer programming.
These two cases seem to be the best to illustrate the phenomenon − factorials and
Fibonacci numbers. Especially the latter.
The definition of the ith number refers to the i-1 number, and so on, till you reach the first
two.
Can it be used in the code? Yes, it can. It can also make the code shorter and clearer.
The second version of our fib() function makes direct use of this definition:
1 def fib(n):
2 if n < 1:
3 return None
4 if n < 3:
5 return 1
6 return fib(n – 1) + fib(n – 2)
7
Yes, there is a little risk indeed. If you forget to consider the conditions which can
stop the chain of recursive invocations, the program may enter an infinite loop. You
have to be careful.
n! = 1 × 2 × 3 × ... × n-1 × n
376
Module 4
n! = (n-1)! × n
Here it is:
1 def factorial_function(n):
2 if n < 0:
3 return None
4 if n < 2:
5 return 1
6 return n * factorial_function(n – 1)
7
Our short functional journey is almost over. The next section will take care of two curious
Python data types: tuples and dictionaries.
1 def fib(n):
2 if n < 1:
3 return None
4 if n < 3:
5 return 1
6
7 elem_1 = elem_2 = 1
8 the_sum = 0
9 for i in range(3, n + 1):
10 the_sum = elem_1 + elem_2
11 elem_1, elem_2 = elem_2, the_sum
12 return the_sum
13
377
Python Essentials 1
14
15 for n in range(1, 10):
16 print(n, "->", fib(n))
17
378
Module 4
2. You can use recursive functions in Python to write clean, elegant code, and divide it
into smaller, organized chunks. On the other hand, you need to be very careful as it
might be easy to make a mistake and create a function which never terminates. You
also need to remember that recursive calls consume a lot of memory, and therefore
may sometimes be inefficient.
When using recursion, you need to take all its advantages and disadvantages into
consideration.
The factorial function is a classic example of how the concept of recursion can be put in
practice:
1 # Recursive implementation of the factorial function.
2
3 def factorial(n):
4 if n == 1: # The base case (termination condition.)
5 return 1
6 else:
7 return n * factorial(n – 1)
8
9
10 print(factorial(4)) # 4 * 3 * 2 * 1 = 24
11
379
Python Essentials 1
Check Answers
380
SECTION 4.6 – TUPLES
AND DICTIONARIES
In this section, you will learn about
sequence types and the concept of
mutability. You will learn what tuples and
dictionaries are, and how you can use
them to store and process data values.
Let's go!
Python Essentials 1
A sequence type is a type of data in Python which is able to store more than one
value (or less than one, as a sequence may be empty), and these values can be
sequentially (hence the name) browsed, element by element.
As the for loop is a tool especially designed to iterate through sequences, we can
express the definition as: a sequence is data which can be scanned by the for
loop.
You've encountered one Python sequence so far − the list. The list is a classic example of
a Python sequence, although there are some other sequences worth mentioning, and
we're going to present them to you now.
The second notion − mutability − is a property of any Python data that describes its
readiness to be freely changed during program execution. There are two kinds of Python
data: mutable and immutable.
Mutable data can be freely updated at any time − we call such an operation in situ.
Mutable data can be freely updated at any time − we call such an operation in situ.
In situ is a Latin phrase that translates as literally in position. For example, the following
instruction modifies the data in situ:
1 list.append(1)
2
Imagine that a list can only be assigned and read over. You would be able neither to
append an element to it, nor remove any element from it. This means that appending an
element to the end of the list would require the recreation of the list from scratch.
You would have to build a completely new list, consisting of the all elements of the
already existing list, plus the new element.
The data type we want to tell you about now is a tuple. A tuple is an immutable
sequence type. It can behave like a list, but it can't be modified in situ.
382
Module 4
4.6.2 Tuples
The first and the clearest distinction between lists and tuples is the syntax used to create
them – tuples prefer to use parenthesis, whereas lists like to see brackets, although it's
also possible to create a tuple just from a set of values separated by commas.
(1, 2, 4, 8)
(1.0, 0.5, 0.25, 0.125)
Note: each tuple element may be of a different type (floating-point, integer, or any
other not-as-yet-introduced kind of data).
383
Python Essentials 1
If you want to create a one-element tuple, you have to take into consideration the fact
that, due to syntax reasons (a tuple has to be distinguishable from an ordinary, single
value), you must end the value with a comma:
one_element_tuple_1 = (1, )
one_element_tuple_2 = 1.,
Removing the commas won't spoil the program in any syntactical sense, but you will
instead get two single variables, not tuples.
384
Module 4
The program should produce the following output − run it and check:
1
1000
(10, 100, 1000)
(1, 10)
1
10
100
1000
The similarities may be misleading − don't try to modify a tuple's contents! It's not a
list!
All of these instructions (except the topmost one) will cause a runtime error:
1 my_tuple = (1, 10, 100, 1000)
2
3 my_tuple.append(10000)
4 del my_tuple[0]
5 my_tuple[1] = -10
6
This is the message that Python will give you in the console window:
• the len() function accepts tuples, and returns the number of elements
contained inside;
• the + operator can join tuples together (we've shown you this already)
385
Python Essentials 1
4 t2 = my_tuple * 3
5
6 print(len(t2))
7 print(t1)
8 print(t2)
9 print(10 in my_tuple)
10 print(-10 not in my_tuple)
11
9
(1, 10, 100, 1000, 10000)
(1, 10, 100, 1, 10, 100, 1, 10, 100)
True
True
One of the most useful tuple properties is their ability to appear on the left side of the
assignment operator. You saw this phenomenon some time ago, when it was necessary
to find an elegant tool to swap two variables' values.
It shows three tuples interacting − in effect, the values stored in them "circulate" − t1
becomes t2 , t2 becomes t3 , and t3 becomes t1 .
386
Module 4
Note: the example presents one more important fact: a tuple's elements can be
variables, not only literals. Moreover, they can be expressions if they're on the right side
of the assignment operator.
387
Python Essentials 1
4.6.3 Dictionaries
The dictionary is another Python data structure. It's not a sequence type (but can be
easily adapted to sequence processing) and it is mutable.
To explain what the Python dictionary actually is, it is important to understand that it is
literally a dictionary.
In the first example, the dictionary uses keys and values which are both strings. In the
second one, the keys are strings, but the values are integers. The reverse layout (keys →
numbers, values → strings) is also possible, as well as number-number combinations.
The list of pairs is surrounded by curly braces, while the pairs themselves are
separated by commas, and the keys and values by colons.
The first of our dictionaries is a very simple English-French dictionary. The second − a
very tiny telephone directory.
388
Module 4
The Python dictionary works in the same way as a bilingual dictionary. For example,
you have an English word (e.g. cat) and need its French equivalent. You browse the
dictionary in order to find the word (you may use different techniques to do that − it
doesn't matter) and eventually you get it. Next, you check the French counterpart and it is
(most probably) the word "chat".
In Python's world, the word you look for is named a key . The word you get from the
dictionary is called a value .
• each key must be unique − it's not possible to have more than one key of the same
value;
• a key may be any immutable type of object: it can be a number (integer or float),
or even a string, but not a list;
• the len() function works for dictionaries, too − it returns the number of key-
value elements in the dictionary;
The dictionary as a whole can be printed with a single print() invocation. The
snippet may produce the following output:
Have you noticed anything surprising? The order of the printed pairs is different than in
the initial assignment. What does that mean?
First of all, it's a confirmation that dictionaries are not lists – they don't preserve the
order of their data, as the order is completely meaningless (unlike in real, paper
389
Python Essentials 1
dictionaries). The order in which a dictionary stores its data is completely out of your
control, and your expectations. That's normal.
NOTE
In Python 3.6x dictionaries have become ordered collections by default. Your results may
vary depending on what Python version you're using.
If you want to get any of the values, you have to deliver a valid key value:
1 print(dictionary['cat'])
2 print(phone_numbers['Suzy'])
3
Note:
chat
5557654321
390
Module 4
And now the most important news: you mustn't use a non-existent key. Trying
something like this:
print(phone_numbers['president'])
Fortunately, there's a simple way to avoid such a situation. The in operator, together
with its companion, not in , can salvage this situation.
NOTE
When you write a big or lengthy expression, it may be a good idea to keep it vertically
aligned. This is how you can make your code more readable and more programmer-
friendly, e.g.:
1 # Example 1:
2 dictionary = {
3 "cat": "chat",
4 "dog": "chien",
5 "horse": "cheval"
391
Python Essentials 1
6 }
7 # Example 2:
8 phone_numbers = {'boss': 5551234567,
9 'Suzy': 22657854310
10 }
11
392
Module 4
No and yes.
No, because a dictionary is not a sequence type − the for loop is useless with it.
Yes, because there are simple and very effective tools that can adapt any dictionary to
the for loop requirements (in other words, building an intermediate link between the
dictionary and a temporary sequence entity).
The first of them is a method named keys() , possessed by each dictionary. The
method returns an iterable object consisting of all the keys gathered within the
dictionary. Having a group of keys enables you to access the whole dictionary in an
easy and handy way.
Let's now have a look at a dictionary method called items() . The method returns
tuples (this is the first example where tuples are something more than just an example of
themselves) where each tuple is a key-value pair.
Note the way in which the tuple has been used as a for loop variable.
393
Python Essentials 1
We're going to replace the value "chat" with "minou" , which is not very accurate,
but it will work well with our example.
Look:
1 dictionary = {"cat": "chat", "dog": "chien", "horse":
"cheval"}
2
3 dictionary['cat'] = 'minou'
4 print(dictionary)
5
Do you want it sorted? Just enrich the for loop to get such a form:
1 for key in sorted(dictionary.keys()):
2
394
Module 4
The sorted() function will do its best — the output will look like this:
Note the way in which the tuple has been used as a for loop variable.
There is also a method called values() , which works similarly to keys() , but
returns values.
395
Python Essentials 1
As the dictionary is not able to automatically find a key for a given value, the role of this
method is rather limited.
cheval
chien
chat
We're going to replace the value "chat" with "minou" , which is not very accurate,
but will work well with our example.
Look:
1 dictionary = {"cat": "chat", "dog": "chien", "horse":
"cheval"}
2
3 dictionary['cat'] = 'minou'
4 print(dictionary)
5
Note: this is very different behavior compared to lists, which don't allow you to assign
values to non-existing indices.
Let's add a new pair of words to the dictionary − a bit weird, but still valid:
1 dictionary = {"cat": "chat", "dog": "chien", "horse":
"cheval"}
396
Module 4
2
3 dictionary['swan'] = 'cygne'
4 print(dictionary)
5
NOTE
You can also insert an item to a dictionary by using the update() method, e.g.:
1 dictionary = {"cat": "chat", "dog": "chien", "horse":
"cheval"}
2
3 dictionary.update({"duck": "canard"})
4 print(dictionary)
5
Removing a key
Can you guess how to remove a key from a dictionary?
Note: removing a key will always cause the removal of the associated value. Values
cannot exist without their keys.
397
Python Essentials 1
EXTRA
To remove the last item in a dictionary, you can use the popitem() method:
1 dictionary = {"cat": "chat", "dog": "chien", "horse":
"cheval"}
2
3 dictionary.popitem()
4 print(dictionary) # outputs: {'cat': 'chat', 'dog': 'chien'}
5
In the older versions of Python, i.e. before 3.6.7, the popitem() method removes a
random item from a dictionary.
398
Module 4
• the program should ask for the student's name, followed by her/his single score;
• entering an empty name finishes the inputting of the data (note 1: entering an empty
score will raise the ValueError exception, but don't worry about that now, you'll see
how to handle such cases when we talk about exceptions in the second part of the
Python Essentials course series)
• a list of all names, together with the evaluated average score, should be then
emitted.
399
Python Essentials 1
• line 1: create an empty dictionary for the input data; the student's name is used as a
key, while all the associated scores are stored in a tuple (the tuple may be a
dictionary value – that's not a problem at all)
• line 3: enter an "infinite" loop (don't worry, it'll break at the right moment)
• line 8: ask for one of the student's scores (an integer from the range 0-10)
• line 9-10: if the score entered is not within the range from 0 to 10, leave the loop;
• line 12-13: if the student's name is already in the dictionary, lengthen the associated
tuple with the new score (note the += operator)
• line 14-15: if this is a new student (unknown to the dictionary), create a new entry –
its value is a one-element tuple containing the entered score;
• line 18-19: initialize the data needed to evaluate the average (sum and counter)
• line 20-22: we iterate through the tuple, taking all the subsequent scores and
updating the sum, together with the counter;
• line 23: evaluate and print the student's name and average score.
400
Module 4
401
Python Essentials 1
Each tuple element may be of a different type (i.e. integers, strings, booleans, etc.). What
is more, tuples can contain other tuples or lists (and the other way round).
If you remove the comma, you will tell Python to create a variable, not a tuple:
1 my_tuple_1 = 1,
2 print(type(my_tuple_1)) # outputs: <class 'tuple'=""></
class>
3
4 my_tuple_2 = 1 # This is not a tuple.
5 print(type(my_tuple_2)) # outputs: <class 'int'=""></class>
6
402
Module 4
5. Tuples are immutable, which means you cannot change their elements (you cannot
append tuples, or modify, or remove tuple elements). The following snippet will cause an
exception:
1 my_tuple = (1, 2.0, "string", [3, 4], (5, ), True)
2 my_tuple[2] = "guitar" # The TypeError exception will be
raised.
3
6. You can loop through a tuple elements (Example 1), check if a specific element is
(not)present in a tuple (Example 2), use the len() function to check how many
elements there are in a tuple (Example 3), or even join/multiply tuples (Example 4):
1 # Example 1
2 tuple_1 = (1, 2, 3)
3 for elem in tuple_1:
4 print(elem)
5
6 # Example 2
7 tuple_2 = (1, 2, 3, 4)
8 print(5 in tuple_2)
9 print(5 not in tuple_2)
10
11 # Example 3
12 tuple_2 = (1, 2, 3, 4)
13 print(len(tuple_3))
14 print(5 not in tuple_2)
15
16 # Example 4
403
Python Essentials 1
EXTRA
You can also create a tuple using a Python built-in function called tuple() . This is
particularly useful when you want to convert a certain iterable (e.g. a list, range, string,
etc.) to a tuple:
1 my_tuple = tuple((1, 2, "string"))
2 print(my_tuple)
3
4 my_list = [2, 4, 6]
5 print(my_list) # outputs: [2, 4, 6]
6 print(type(my_list)) # outputs: <class 'list'=""></class>
7 tup = tuple(my_list)
8 print(tup) # outputs: (2, 4, 6)
9 print(type(tup)) # outputs: <class 'tuple'=""></class>
10
By the same fashion, when you want to convert an iterable to a list, you can use a Python
built-in function called list() :
1 tup = 1, 2, 3,
2 my_list = list(tup)
3 print(type(my_list)) # outputs: <class 'list'=""></class>
4
404
Module 4
Each dictionary is a set of key: value pairs. You can create it by using the following
syntax:
1 my_dictionary = {
2 key1: value1,
3 key2: value2,
4 key3: value3,
5 }
6
2. If you want to access a dictionary item, you can do so by making a reference to its key
inside a pair of square brackets (ex. 1) or by using the get() method (ex. 2):
1 pol_eng_dictionary = {
2 "kwiat": "flower",
3 "woda": "water",
4 "gleba": "soil"
5 }
6
7 item_1 = pol_eng_dictionary["gleba"] # ex. 1
8 print(item_1) # outputs: soil
9
10 item_2 = pol_eng_dictionary.get("woda") # ex. 2
11 print(item_2) # outputs: water
12
3. If you want to change the value associated with a specific key, you can do so by
referring to the item's key name in the following way:
1 pol_eng_dictionary = {
2 "zamek": "castle",
3 "woda": "water",
4 "gleba": "soil"
5 }
6
7 pol_eng_dictionary["zamek"] = "lock"
405
Python Essentials 1
8 item = pol_eng_dictionary["zamek"]
9 print(item) # outputs: lock
10
4. To add or remove a key (and the associated value), use the following syntax:
1 phonebook = {} # an empty dictionary
2
3 phonebook["Adam"] = 3456783958 # create/add a key-value pair
4 print(phonebook) # outputs: {'Adam': 3456783958}
5
6 del phonebook["Adam"]
7 print(phonebook) # outputs: {}
8
You can also insert an item into a dictionary by using the update() method, and
remove the last element by using the popitem() method, e.g.:
1 pol_eng_dictionary = {"kwiat": "flower"}
2
3 pol_eng_dictionary.update({"gleba": "soil"})
4 print(pol_eng_dictionary) # outputs: {'kwiat': 'flower',
'gleba': 'soil'}
5
6 pol_eng_dictionary.popitem()
7 print(pol_eng_dictionary) # outputs: {'kwiat': 'flower'}
8
5. You can use the for loop to loop through a dictionary, e.g.:
1 pol_eng_dictionary = {
2 "zamek": "castle",
3 "woda": "water",
4 "gleba": "soil"
5 }
6
7 for item in pol_eng_dictionary:
8 print(item)
9
10 # woda
406
Module 4
11 # gleba
12
6. If you want to loop through a dictionary's keys and values, you can use the
items() method, e.g.:
1 pol_eng_dictionary = {
2 "zamek": "castle",
3 "woda": "water",
4 "gleba": "soil"
5 }
6
7 for key, value in pol_eng_dictionary.items():
8 print("Pol/Eng ->", key, ":", value)
9
7. To check if a given key exists in a dictionary, you can use the in keyword:
1 pol_eng_dictionary = {
2 "zamek": "castle",
3 "woda": "water",
4 "gleba": "soil"
5 }
6
7 if "zamek" in pol_eng_dictionary:
8 print("Yes")
9 else:
10 print("No")
11
8. You can use the del keyword to remove a specific item, or delete a dictionary. To
remove all the dictionary's items, you need to use the clear() method:
1 pol_eng_dictionary = {
2 "zamek": "castle",
3 "woda": "water",
4 "gleba": "soil"
5 }
6
7 print(len(pol_eng_dictionary)) # outputs: 3
407
Python Essentials 1
408
Module 4
Question 3: Complete the code to correctly use the count() method to find the
number of duplicates of 2 in the following tuple.
1 tup = 1, 2, 3, 2, 4, 5, 6, 2, 7, 2, 8, 9
2 duplicates = # Write your code here.
3
4 print(duplicates) # outputs: 4
5
Question 4: Write a program that will "glue" the two dictionaries ( d1 and d2 )
together and create a new one ( d3 ).
1 d1 = {'Adam Smith': 'A', 'Judy Paxton': 'B+'}
2 d2 = {'Mary Louis': 'A', 'Patrick White': 'C'}
3 d3 = {}
4
5 for item in (d1, d2):
6 # Write your code here.
7
8 print(d3)
9
Question 5: Write a program that will convert the my_list list to a tuple.
1 my_list = ["car", "Ford", "flower", "Tulip"]
2
409
Python Essentials 1
Question 6: Write a program that will convert the colors tuple to a dictionary.
1 colors = (("green", "#008000"), ("blue", "#0000FF"))
2
3 # Write your code here.
4
5 print(colors_dictionary)
6
Question 7: What will happen when you run the following code?
1 my_dictionary = {"A": 1, "B": 2}
2 copy_my_dictionary = my_dictionary.copy()
3 my_dictionary.clear()
4
5 print(copy_my_dictionary)
6
Check Answers
410
SECTION 4.7 –
EXCEPTIONS
Welcome to the last section in the
course, where you will learn about the
exception handling mechanism in
Python. You will explore the topics of
errors in code, and learn what you can
do to prevent program termination. You
will also cover the subject of code testing
and debugging, and learn some tips
about how to make your code writing
process better and less error-prone. Let's
begin!
Python Essentials 1
To err is human. It's impossible to make no mistakes, and it's impossible to write error-
free code. Don't get us wrong – we don't want to convince you that writing messy and
faulty programs is a virtue. We rather want to explain that even the most careful
programmer is not able to avoid minor or major defects. It's only those who do nothing
that make no mistakes.
Paradoxically, accepting this difficult truth can make you a better programmer and may
improve your code quality.
It may happen that your code will be terminated then, and the user will be left alone with
a terse and ambiguous error message on the screen. The user will be unsatisfied, and
you should be unsatisfied, too.
We're going to show you how to protect your code from this kind of failure and how not to
provoke the user's anger.
412
Module 4
The other side of dealing with programming errors reveals itself when undesirable code
behavior is caused by mistakes you made when you were writing your program. This kind
of error is commonly called a "bug", which is a manifestation of a well-established belief
that if a program works badly, it must be caused by malicious bugs which live inside the
computer hardware and cause short circuits or other interference.
This idea is not as mad as it may look – such incidents were common in times when
computers occupied large halls, consumed kilowatts of electricity, and produced
enormous amounts of heat. Fortunately or not, these times are gone forever and the only
bugs which can spoil your code are those you sowed in the code yourself. Therefore, we
will try to show you how to find and eliminate your bugs, in other words, how to debug
your code.
Let's start the journey through the land of errors and bugs.
413
Python Essentials 1
Is there anything that can go wrong with it? The code is so brief and so compact that it
doesn't seem like we'll find any trouble there.
It seems that you already know where we are going. Yes, you're right – entering data that
is not an integer (which also includes entering nothing at all) will completely ruin the
program execution. This is what the code's user will see:
All the lines Python shows you are meaningful and important, but the last line seems to
be the most valuable. The first word in the line is the name of the exception which
causes your code to stop. It's ValueError here. The rest of the line is just a brief
explanation which more precisely specifies the cause of the occurred exception.
How do you deal with it? How do you protect your code from termination, the user from
disappointment, and yourself from the user's dissatisfaction?
The very first thought that can come to your mind is to check if the data provided by the
user is valid and to refuse to cooperate if the data is incorrect. In this case, the check can
rely on the fact that we expect the input string to contain digits only.
You should already be able to implement this check and write it yourself, shouldn't you? It
is also possible to check if the value variable's type is an int (Python has a
special means for these kinds of checks – it's an operator named is . The check itself
may look like this:
1 type(value) is int
2
414
Module 4
Please forgive us if we don't spend any more time on it now – you will find more detailed
explanations of the is operator in a course module devoted to Object-Oriented
Programming.
You may be surprised to learn that we don't want you to do any preliminary data
validation. Why? Because this is not the way Python recommends. Really.
415
Python Essentials 1
Let's stop here for a moment. Don't get us wrong – we don't want you to apply the rule in
your everyday life. Don't take anyone's car without permission in the hope that you can
be so convincing that you will avoid conviction. The rule is about something else.
Actually, the rule reads: "it's better to handle an error when it happens than to try to avoid
it".
"Okay," you may say now, 'but how should I beg for forgiveness when the program is
terminated and there is nothing left that can be done?" This is where the exception
comes on the scene.
• first, starting with the try keyword – this is the place where you put the code you
suspect is risky and may be terminated in case of error; note: this kind of error is
called an exception, while the exception occurrence is called raising – we can say
that an exception is (or was) raised;
• second, the part of the code starting with the except keyword is designed to
handle the exception; it's up to you what you want to do here: you can clean up the
mess or you can just sweep the problem under the carpet (although we would
prefer the first solution).
So, we could say that these two blocks work like this:
• the try keyword marks the place where you try to do something without
permission;
416
Module 4
• the except keyword starts a location where you can show off your apology
talents.
As you can see, this approach accepts errors (treats them as a normal part of the
program's life) instead of escalating efforts to avoid errors at all.
417
Python Essentials 1
• any part of the code placed between try and except is executed in a very
special way – any error which occurs here won't terminate program execution.
Instead, the control will immediately jump to the first line situated after the
except keyword, and no other part of the try branch is executed;
• the code in the except branch is activated only when an exception has been
encountered inside the try block. There is no way to get there by any other
means;
• when either the try block or the except block is executed successfully, the
control returns to the normal path of execution, and any code located beyond in the
source file is executed as if nothing happened.
Now we want to ask you an innocent question: is ValueError the only way the
control could fall into the except branch?
418
Module 4
Yes, you're right – the division placed inside the print() function invocation will raise
the ZeroDivisionError . As you may expect, the code's behavior will be the same
as in the previous case – the user will see the "I do not know what to do..." message,
which seems to be quite reasonable in this context, but it's also possible that you would
want to handle this kind of problem in a bit different way.
Is it possible? Of course, it is. There are at least two approaches you can implement
here.
The first of them is simple and complicated at the same time: you can just add two
separate try blocks, one including the input() function invocation where the
ValueError may be raised, and the second devoted to handling possible issues
induced by the division. Both these try blocks would have their own except branches,
and in effect you will gain full control over two different errors.
This solution is good, but it is a bit lengthy – the code becomes unnecessarily bloated.
Moreover, it's not the only danger that awaits you. Note that leaving the first try-
except block leaves a lot of uncertainty – you will have to add extra code to ensure
that the value the user has entered is safe to use in division. This is how a seemingly
simple solution becomes overly complicated.
Fortunately, Python offers a simpler way to deal with this kind of challenge.
419
Python Essentials 1
Additionally, the number of except branches is not limited – you can specify as many
or as few of them as you need, but don't forget that none of the exceptions can be
specified more than once.
But this still isn't the last Python word on exceptions. Stay tuned.
420
Module 4
8 except:
9 print('Something strange has happened here... Sorry!')
10
We've added a third except branch, but this time it has no exception name
specified – we can say it's anonymous or (what is closer to its actual role) it's the
default. You can expect that when an exception is raised and there is no except
branch dedicated to this exception, it will be handled by the default branch.
NOTE
The default except branch must be the last except branch. Always!
421
Python Essentials 1
ZeroDivisionError
This appears when you try to force Python to perform any operation which provokes
division in which the divider is zero, or is indistinguishable from zero. Note that there is
more than one Python operator which may cause this exception to raise. Can you guess
them all?
ValueError
Expect this exception when you're dealing with values which may be inappropriately used
in some context. In general, this exception is raised when a function (like int() or
float() ) receives an argument of a proper type, but its value is unacceptable.
TypeError
This exception shows up when you try to apply a data whose type cannot be accepted in
the current context. Look at the example:
1 short_list = [1]
2 one_value = short_list[0.5]
3
You're not allowed to use a float value as a list index (the same rule applies to tuples,
too). TypeError is an adequate name to describe the problem, and an adequate
exception to raise.
AttributeError
This exception arrives – among other occasions – when you try to activate a method
which doesn't exist in an item you're dealing with. For example:
1 short_list = [1]
2 short_list.append(2)
3 short_list.depend(3)
422
Module 4
The third line of our example attempts to make use of a method which isn't contained in
the lists. This is the place where AttributeError is raised.
SyntaxError
This exception is raised when the control reaches a line of code which violates Python's
grammar. It may sound strange, but some errors of this kind cannot be identified without
first running the code. This kind of behavior is typical of interpreted languages – the
interpreter always works in a hurry and has no time to scan the whole source code. It is
content with checking the code which is currently being run. An example of such a
category of issues will be presented very soon.
It's a bad idea to handle this exception in your programs. You should produce code that is
free of syntax errors, instead of masking the faults you've caused.
423
Python Essentials 1
Now we want to tell you about the second side of the never-ending struggle with errors –
the inevitable destiny of a developer's life. As you are not able to avoid making bugs in
your code, you must always be ready to seek out and destroy them. Don't bury your head
in the sand – ignoring errors won't make them disappear.
An important duty for developers is to test the newly created code, but you must not
forget that testing isn't a way to prove that the code is error-free. Paradoxically, the only
proof testing can provide is that your code contains errors. Don't think you can relax after
a successful test.
The second important aspect of software testing is strictly psychological. It's a truth
known for years that authors – even those who are reliable and self-aware – aren't able
to objectively evaluate and verify their works.
This is why each novelist needs an editor and each programmer needs a tester. Some
say – a little spitefully but truthfully – that developers test the code to show their
perfection, not to find problems that may frustrate them. Testers are free of such
dilemmas, and this is why their work is more effective and profitable.
Of course, this doesn't absolve you from being attentive and careful. Test your code as
best you can. Don't make the testers' work too easy.
Your primary duty is to ensure that you've checked all execution paths your code can
go through. Does that sound mysterious? Nothing of the kind!
424
Module 4
6 print("Below zero")
7 else:
8 print("Zero")
9
There are three independent execution paths in the code – can you see them? They are
determined by the if-elif-else statements. Of course, the execution paths can be
built by many other statements, like loops, or even try-except blocks.
If you're going to test your code fairly and you want to sleep soundly and to dream
without nightmares (nightmares about bugs can be devastating for a developer's
performance) you are obliged to prepare a test data set that will force your code to
negotiate all possible paths.
In our example, the set should contain at least three float values: one positive, one
negative, and zero.
425
Python Essentials 1
We intentionally introduced an error into the code – we hope your watchful eyes noticed it
immediately. Yes, we removed just one letter and in effect, the valid print() function
invocation turns into the obviously invalid clause prin() . There is no such function as
prin() in our program's scope, but is it really obvious for Python?
As you can see, the code finishes its execution without any obstacles.
How is that possible? Why does Python overlook such an evident developer mistake?
426
Module 4
Do you understand now why passing through all execution paths is so vital and
inevitable?
Let's assume that you complete your code and the tests you've made are successful. You
deliver your code to the testers and – fortunately! – they found some bugs in it. We're
using the word "fortunately" completely consciously. You need to accept that, firstly,
testers are the developer's best friends – don't treat the bugs they discover as an offense
or a malignancy; and, secondly, each bug the testers find is a bug that won't affect the
users. Both factors are valuable and worth your attention.
You already know that your code contains a bug or bugs (the latter is more likely). How
do you locate them and how do you fix your code?
A debugger is a specialized piece of software that can control how your program is
executed. Using the debugger, you can execute your code line-by-line, inspect all the
variables' states and change their values on demand without modifying the source code,
stop program execution when certain conditions are or aren't met, and do lots of other
useful tasks.
427
Python Essentials 1
We can say that every IDE is equipped with a more or less advanced debugger. Even
IDLE has one, although you may find its handling a bit complicated and troublesome. If
you want to make use of IDLE's integrated debugger, you should activate it using the
"Debug" entry in the main IDLE window menu bar. It's the start point for all debugger
facilities.
You can see how the debugger visualizes variables and parameter values, and note the
call stack which shows the chain of invocations leading from the currently executed
function to the interpreter level.
If you want to know more about the IDLE debugger, consult the IDLE documentation.
428
Module 4
Some other debugging techniques can be used to hunt bugs. It's possible that you aren't
able or don't want to use a debugger (the reasons may vary). Are you helpless then?
Absolutely not!
You may use one of the simplest and the oldest (but still useful) debugging tactics known
as print debugging. The name speaks for itself – you just insert several additional
print() invocations inside your code to output data which illustrates the path your
code is currently negotiating. You can output the values of the variables which may affect
the execution.
These printouts may output meaningful text like "I am here", "I entered the foo()
function", "The result is 0 ", or they may contain sequences of characters that are
legible only to you. Please don't use obscene or indecent words for the purpose, even
though you may feel a strong temptation – your reputation can be ruined in a moment if
these antics leak to the public.
429
Python Essentials 1
Try to tell someone (for example, your friend or coworker) what your code is expected to
do and how it actually behaves. Be concrete and don't omit details. Answer all questions
your helper asks. You'll likely realize the cause of the problem while telling your story, as
speaking activates these parts of your brain which remain idle during coding. If no human
can help you with the problem, use a yellow rubber duck instead. We're not kidding –
consult the Wikipedia article to learn more about this commonly used technique called
Rubber Duck Debugging.
Try to isolate the problem. You can extract the part of your code that is suspected of
being responsible for your troubles and run it separately. You can comment out parts of
the code that obscure the problem. Assign concrete values to variables instead of reading
them from the input. Test your functions by applying predictable argument values.
Analyze the code carefully. Read it aloud.
If the bug has appeared recently and didn't show up earlier, analyze all the changes
you've introduced into your code – one of them may be the reason.
Take a break, drink a cup of coffee, take your dog and go for a walk, read a good book
for a moment or two, make a phone call to your best friend – you'll be surprised how
often it helps.
430
Module 4
To make a long story short – unit testing assumes that tests are inseparable parts of the
code and preparing the test data is an inseparable part of coding. This means that when
you write a function or a set of cooperating functions, you're also obliged to create a set
of data for which your code's behavior is predictable and known.
Moreover, you should equip your code with an interface that can be used by an
automated testing environment. In this approach, any amendment made to the code
(even the least significant) should be followed by the execution of all the unit tests
accompanied by your source.
To standardize this approach and make it easier to apply, Python provides a dedicated
module named unittest . We're not going to discuss it here – it's a broad and
complex topic.
Therefore, we've prepared a separate course and certification path for this subject. It is
called "Testing Essentials with Python", and we invite you to participate in it.
431
Python Essentials 1
• syntax errors (parsing errors), which occur when the parser comes across a
statement that is incorrect. For example:
will cause a SyntaxError , and result in the following (or similar) message being
displayed in the console:
Pay attention to the arrow – it indicates the place where the Python parser has run into
trouble. In our case, it's the missing double quote. Did you notice it?
print("Hello, World!)
^
SyntaxError: EOL while scanning string literal
will cause a ZeroDivisionError exception, and result in the following (or similar)
message being displayed in the console:
432
Module 4
Pay attention to the last line of the error message – it actually tells you what happened.
There are many different types of exceptions, such as ZeroDivisionError ,
NameError , TypeError , and many more; and this part of the message informs
you of what type of exception has been raised. The preceding lines show you the context
in which the exception has occurred.
2. You can "catch" and handle exceptions in Python by using the try-except block.
So, if you have a suspicion that any particular snippet may raise an exception, you can
write the code that will gracefully handle it, and will not interrupt the program. Look at the
example:
1 while True:
2 try:
3 number = int(input("Enter an integer number: "))
4 print(number/2)
5 break
6 except:
7 print("Warning: the value entered is not a valid
number. Try again...")
8
This code asks the user for input until they enter a valid integer number. If the user enters
a value that cannot be converted to an int, the program will print
Warning: the value entered is not a valid number. Try again... ,
and ask the user to enter a number again. What happens in such a case?
• The try block/clause is executed. The user enters a wrong value, for example:
hello! .
• An exception occurs, and the rest of the try clause is skipped. The program
jumps to the except block, executes it, and then continues running after the
try-except block.
If the user enters a correct value and no exception occurs, the subsequent instructions in
the try block are executed.
3. You can handle multiple exceptions in your code block. Look at the following examples:
1 while True:
2 try:
433
Python Essentials 1
You can use multiple except blocks within one try statement, and specify particular
exception names. If one of the except branches is executed, the other branches will
be skipped. Remember: you can specify a particular built-in exception only once. Also,
don't forget that the default (or generic) exception, that is the one with no name
specified, should be placed at the bottom of the branch (use the more specific
exceptions first, and the more general last).
You can also specify and handle multiple built-in exceptions within a single except clause:
1 while True:
2 try:
3 number = int(input("Enter an int number: "))
4 print(5/number)
5 break
6 except (ValueError, ZeroDivisionError):
7 print("Wrong value or No division by zero rule
broken.")
8 except:
9 print("Sorry, something went wrong...")
10
434
Module 4
To learn more about the Python built-in exceptions, consult the official Python
documentation.
5. Last but not least, you should remember about testing and debugging your code. Use
such debugging techniques as print debugging; if possible – ask someone to read your
code and help you to find bugs in it or to improve it; try to isolate the fragment of code
that is problematic and susceptible to errors: test your functions by applying predictable
argument values, and try to handle the situations when someone enters wrong values;
comment out the parts of the code that obscure the issue. Finally, take breaks and come
back to your code after some time with a fresh pair of eyes.
435
Python Essentials 1
Question 2: What is the expected behavior of the following program if the user enters
0 ?
1 value = input("Enter a value: ")
2 print(10/value)
3
Check Answers
436
APPENDICES
Appendices
Back
LAB 9
1 a = float(input("Enter first value: "))
2 b = float(input("Enter second value: "))
3
4 # output the result of addition here
5 # output the result of subtraction here
6 # output the result of multiplication here
7 # output the result of division here
8
9 print("\nThat's all, folks!")
10
Back
LAB 11
1 hour = int(input("Starting time (hours): "))
2 mins = int(input("Starting time (minutes): "))
3 dura = int(input("Event duration (minutes): "))
4 # find the total of all minutes
5 # find the number of hours hidden in minutes and update the
hour
6 # correct minutes to fall in the (0..59) range
7 # correct hours to fall in the (0..23) range
8 print(hour, ":", mins, sep='')
9
Back
439
Python Essentials 1
LAB 16
1 secret_number = 777
2
3 print(
4 """
5 +================================+
6 | Welcome to my game, muggle! |
7 | Enter an integer number |
8 | and guess what number I've |
9 | picked for you. |
10 | So, what is the secret number? |
11 +================================+
12 """)
13
14 # Prompt the user to enter an integer number.
15
16 # Write a while loop and the rest of your code.
17
Back
LAB 17
1 import time
2
3 # Write a for loop that counts to five.
4 # Body of the loop – print the loop iteration number and
the word "Mississippi".
5 time.sleep(1)
6
7 # Write a print function with the final message.
8
Back
440
Appendices
LAB 18
1 while True:
2 # Write code in the body of the loop.
3 # Put a conditional statement.
4 # Exit the loop here.
5 # Print the message.
6
Back
LAB 19
1 user_word = input("Enter your word: ")
2 user_word = user_word.upper()
3
4 for letter in user_word:
5 # if-elif-else block, nest continue statements
6 # ...
7 # ...
8 # ...
9 else:
10 print(letter)
11
Back
LAB 20
1 word_without_vowels = ""
2
3 user_word = input("Enter your word: ")
4 user_word = user_word.upper()
5
6 for letter in user_word:
7 if letter == "A":
8 continue
9 elif letter == "E":
10 continue
441
Python Essentials 1
Back
LAB 21
1 blocks = int(input("Enter the number of blocks: "))
2
3 height = 0
4 in_layer = 1
5 while in_layer <= blocks:
6 # The body of the while loop.
7
8 print("The height of the pyramid:", height)
9
Back
LAB 22
1 c0 = int(input("Enter c0: "))
2
3 if c0 > 1:
4 steps = 0
5 # The while loop goes here.
6 if c0 %2 != 0:
7 # Write your code here.
8 else:
442
Appendices
9 cnew = c0 // 2
10 #
11 # Write your code here.
12 #
13 print("steps =",steps)
14 else:
15 print("Bad c0 value")
16
Back
LAB 23
1 hat_list = [1, 2, 3, 4, 5]
2
3 # Step 1
4 hat_list[2] = int(input("Enter an integer number: "))
5
6 # Step 2: write a line of code that removes the last element
from the list.
7
8 # Step 3: write a line of code that prints the length of the
existing list.
9
10 print(hat_list)
11
Back
LAB 24
1 # step 1:
2 Beatles = []
3 #
4
5 # step 2:
6
7 Beatles.append("John Lennon")
443
Python Essentials 1
8 #
9 #
10 #
11
12 # step 3:
13 for members in range(2):
14 #
15 #
16
17 # step 4:
18 del Beatles[-1]
19 #
20 #
21
22 # step 5:
23 #
24 #
25 #
26
Back
LAB 25
1 my_list = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
2 new_list = []
3 # Browse all numbers from the source list.
4 # If the number doesn't appear within the new list...
5 # ...append it here.
6 # Make a copy of new_list.
7 print("The list with unique elements only:")
8 print(my_list)
9
Back
444
Appendices
LAB 26
1 def is_year_leap(year):
2 if year % 4 != 0:
3 #
4 # elif statement
5 #
6 # elif statement
7 #
8 # else statement
9
10 test_data = [1900, 2000, 2016, 1987]
11 test_results = [False, True, True, False]
12 for i in range(len(test_data)):
13 yr = test_data[i]
14 print(yr,"-> ",end="")
15 result = is_year_leap(yr)
16 if result == test_results[i]:
17 print("OK")
18 else:
19 print("Failed")
20
Back
LAB 27
1 def is_year_leap(year):
2 if year % 4 != 0:
3 return False
4 elif year % 100 != 0:
5 return True
6 elif year % 400 != 0:
7 return False
8 else:
9 return True
10
11 def days_in_month(year, month):
445
Python Essentials 1
12 # if statement
13 # ...
14 days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
15 res = days[month – 1]
16 if month == 2 and is_year_leap(year):
17 res = 29
18 return res
19
20 test_years = [1900, 2000, 2016, 1987]
21 test_months = [ 2, 2, 1, 11]
22 test_results = [28, 29, 31, 30]
23 for i in range(len(test_years)):
24 yr = test_years[i]
25 mo = test_months[i]
26 print(yr,mo,"-> ",end="")
27 result = days_in_month(yr, mo)
28 if result == test_results[i]:
29 print("OK")
30 else:
31 print("Failed")
32
Back
LAB 28
1 def is_year_leap(year):
2 if year % 4 != 0:
3 return False
4 elif year % 100 != 0:
5 return True
6 elif year % 400 != 0:
7 return False
8 else:
9 return True
10
11 def days_in_month(year,month):
446
Appendices
Back
LAB 29
1 def is_prime(num):
2 # the for loop
3 # the if statement
4 return False
5 return True
6
7 for i in range(1, 20):
8 if is_prime(i + 1):
9 print(i + 1, end=" ")
447
Python Essentials 1
10 print()
11
Back
LAB 30
1 # 1 American mile = 1609.344 meters
2 # 1 American gallon = 3.785411784 liters
3
4 def liters_100km_to_miles_gallon(liters):
5 gallons = liters / 3.785411784
6 miles = 100 * 1000 / 1609.344
7 return miles / gallons
8
9 def miles_gallon_to_liters_100km(miles):
10 # ...
11 # ...
12 # ...
13
14 print(liters_100km_to_miles_gallon(3.9))
15 print(liters_100km_to_miles_gallon(7.5))
16 print(liters_100km_to_miles_gallon(10.))
17 print(miles_gallon_to_liters_100km(60.3))
18 print(miles_gallon_to_liters_100km(31.4))
19 print(miles_gallon_to_liters_100km(23.5))
20
Back
448
Appendices
Back
LAB 2
1 print("Programming","Essentials","in", sep="***", end="...")
2 print("Python")
3
Back
LAB 3
1 # Sample Solution
2
3 ###################
4 print("original version:")
5 ###################
6 print(" *")
7 print(" * *")
8 print(" * *")
9 print(" * *")
10 print("*** ***")
11 print(" * *")
12 print(" * *")
449
Python Essentials 1
13 print(" *****")
14 ###################
15 print("with fewer 'print()' invocations:")
16 ###################
17 print(" *\n * *\n * *\n * *\n*** ***")
18 print(" * *\n * *\n *****")
19 ###################
20 print("higher:")
21 ###################
22 print(" *")
23 print(" * *")
24 print(" * *")
25 print(" * *")
26 print(" * *")
27 print(" * *")
28 print(" * *")
29 print(" * *")
30 print("****** ******")
31 print(" * *")
32 print(" * *")
33 print(" * *")
34 print(" * *")
35 print(" * *")
36 print(" * *")
37 print(" *******")
38 ###################
39 print("doubled:")
40 ###################
41 print(" * "*2)
42 print(" * * "*2)
43 print(" * * "*2)
44 print(" * * "*2)
45 print(" * * "*2)
46 print(" * * "*2)
47 print(" * * "*2)
48 print(" * * "*2)
450
Appendices
49 print("****** ******"*2)
50 print(" * * "*2)
51 print(" * * "*2)
52 print(" * * "*2)
53 print(" * * "*2)
54 print(" * * "*2)
55 print(" * * "*2)
56 print(" ******* "*2)
57
Back
LAB 4
1 print("\"I'm\"\n\"\"learning\"\"\n\"\"\"Python\"\"\"")
2
Back
LAB 5
1 john = 3
2 mary = 5
3 adam = 6
4
5 print(john, mary, adam, sep=',')
6
7 total_apples = john + mary + adam
8 print(total_apples)
9
10 # peter = 12.5
11 # suzy = 2
12 # print(peter / suzy)
13 # print("Total number of apples:", total_apples)
14
Back
451
Python Essentials 1
LAB 6
1 kilometers = 12.25
2 miles = 7.38
3
4 miles_to_kilometers = miles * 1.61
5 kilometers_to_miles = kilometers / 1.61
6
7 print(miles, "miles is", round(miles_to_kilometers, 2),
"kilometers")
8 print(kilometers, "kilometers is", round(kilometers_to_miles,
2), "miles")
9
Back
LAB 7
1 x = 0
2 x = float(x)
3 y = 3 * x**3 – 2 * x**2 + 3 * x – 1
4 print("y =", y)
5
6 x = 1
7 x = float(x)
8 y = 3 * x**3 – 2 * x**2 + 3 * x – 1
9 print("y =", y)
10
11 x = -1
12 x = float(x)
13 y = 3 * x**3 – 2 * x**2 + 3 * x – 1
14 print("y =", y)
15
Back
LAB 9
1 a = float(input("Enter first value: "))
452
Appendices
Back
LAB 10
1 x = float(input("Enter value for x: "))
2 y = 1./(x + 1./(x + 1./(x + 1./x)))
3 print("y =", y)
4
Back
LAB 11
1 hour = int(input("Starting time (hours): "))
2 mins = int(input("Starting time (minutes): "))
3 dura = int(input("Event duration (minutes): "))
4 mins = mins + dura # find a total of all minutes
5 hour = hour + mins // 60 # find a number of hours hidden in
minutes and update the hour
6 mins = mins % 60 # correct minutes to fall in the (0..59)
range
7 hour = hour % 24 # correct hours to fall in the (0..23)
range
8 print(hour, ":", mins, sep='')
9
Back
453
Python Essentials 1
LAB 12
1 n = int(input("Enter a number: "))
2 print(n >= 100)
3
Back
LAB 13
1 name = input("Enter flower name: ")
2
3 if name == "Spathiphyllum":
4 print("Yes – Spathiphyllum is the best plant ever!")
5 elif name == "spathiphyllum":
6 print("No, I want a big Spathiphyllum!")
7 else:
8 print("Spathiphyllum! Not", name + "!")
9
Back
LAB 14
1 income = float(input("Enter the annual income: "))
2
3 if income < 85528:
4 tax = income * 0.18 – 556.02
5 else:
6 tax = (income – 85528) * 0.32 + 14839.02
7
8 if tax < 0.0:
9 tax = 0.0
10
11 tax = round(tax, 0)
12 print("The tax is:", tax, "thalers")
13
Back
454
Appendices
LAB 15
1 year = int(input("Enter a year: "))
2
3 if year < 1582:
4 print("Not within the Gregorian calendar period")
5 else:
6 if year % 4 != 0:
7 print("Common year")
8 elif year % 100 != 0:
9 print("Leap year")
10 elif year % 400 != 0:
11 print("Common year")
12 else:
13 print("Leap year")
14
Back
LAB 16
1 secret_number = 777
2
3 print(
4 """
5 +================================+
6 | Welcome to my game, muggle! |
7 | Enter an integer number |
8 | and guess what number I've |
9 | picked for you. |
10 | So, what is the secret number? |
11 +================================+
12 """)
13
14 user_number = int(input("Enter the number: "))
15
16 while user_number != secret_number:
17 print("Ha ha! You're stuck in my loop!")
455
Python Essentials 1
Back
LAB 17
1 import time
2
3 for second in range(1, 6):
4 print(second, "Mississippi")
5 time.sleep(1)
6
7 print("Ready or not, here I come!")
8
Back
LAB 18
1 while True:
2 word = input("You're stuck in an infinite loop!\nEnter the
secret word to leave the loop: ")
3 if word == "chupacabra":
4 break
5 print("You've successfully left the loop!")
6
Back
LAB 19
1 user_word = input("Enter your word: ")
2 user_word = user_word.upper()
3
4 for letter in user_word:
5 if letter == "A":
456
Appendices
6 continue
7 elif letter == "E":
8 continue
9 elif letter == "I":
10 continue
11 elif letter == "O":
12 continue
13 elif letter == "U":
14 continue
15 else:
16 print(letter)
17
Back
LAB 20
1 word_without_vowels = ""
2
3 user_word = input("Enter your word: ")
4 user_word = user_word.upper()
5
6 for letter in user_word:
7 if letter == "A":
8 continue
9 elif letter == "E":
10 continue
11 elif letter == "I":
12 continue
13 elif letter == "O":
14 continue
15 elif letter == "U":
16 continue
17 else:
18 word_without_vowels += letter
19
20 print(word_without_vowels)
457
Python Essentials 1
21
Back
LAB 21
1 blocks = int(input("Enter the number of blocks: "))
2
3 height = 0
4 in_layer = 1
5 while in_layer <= blocks:
6 height += 1
7 blocks -= in_layer
8 in_layer += 1
9 print("The height of the pyramid:", height)
10
Back
LAB 22
1 c0 = int(input("Enter c0: "))
2
3 if c0 > 1:
4 steps = 0
5 while c0 != 1:
6 if c0 %2 != 0:
7 cnew = 3 * c0 + 1
8 else:
9 cnew = c0 // 2
10 print(c0)
11 c0 = cnew
12 steps += 1
13 print("steps =",steps)
14 else:
15 print("Bad c0 value")
16
Back
458
Appendices
LAB 23
1 hat_list = [1, 2, 3, 4, 5]
2
3 # Step 1
4 hat_list[2] = int(input("Enter an integer number: "))
5
6 # Step 2
7 del hat_list[-1]
8
9 # Step 3
10 print(len(hat_list))
11
Back
LAB 24
1 # step 1:
2 Beatles = []
3 print("Step 1:", Beatles)
4
5 # step 2:
6
7 Beatles.append("John Lennon")
8 Beatles.append("Paul McCartney")
9 Beatles.append("George Harrison")
10 print("Step 2:", Beatles)
11
12 # step 3:
13 for members in range(2):
14 Beatles.append(input("New band member: "))
15 print("Step 3:", Beatles)
16
17 # step 4:
18 del Beatles[-1]
19 del Beatles[-1]
20 print("Step 4:", Beatles)
459
Python Essentials 1
21
22 # step 5:
23 Beatles.insert(0, "RingoStarr")
24 print("Step 5:", Beatles)
25 print("The Fab:",len(Beatles))
26
Back
LAB 25
1 my_list = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
2 new_list = []
3 for number in my_list: # Browse all numbers from the source
list.
4 if number not in new_list: # If the number doesn't appear
within the new list...
5 new_list.append(number) # ...append it here.
6 my_list = new_list[:] # Make a copy of new_list.
7 print("The list with unique elements only:")
8 print(my_list)
9
Back
LAB 26
1 def is_year_leap(year):
2 if year % 4 != 0:
3 return False
4 elif year % 100 != 0:
5 return True
6 elif year % 400 != 0:
7 return False
8 else:
9 return True
10
11 test_data = [1900, 2000, 2016, 1987]
460
Appendices
Back
LAB 27
1 def is_year_leap(year):
2 if year % 4 != 0:
3 return False
4 elif year % 100 != 0:
5 return True
6 elif year % 400 != 0:
7 return False
8 else:
9 return True
10
11 def days_in_month(year,month):
12 if year < 1582 or month < 1 or month > 12:
13 return None
14 days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
15 res = days[month – 1]
16 if month == 2 and is_year_leap(year):
17 res = 29
18 return res
19
20 test_years = [1900, 2000, 2016, 1987]
21 test_months = [ 2, 2, 1, 11]
22 test_results = [28, 29, 31, 30]
461
Python Essentials 1
23 for i in range(len(test_years)):
24 yr = test_years[i]
25 mo = test_months[i]
26 print(yr,mo,"-> ",end="")
27 result = days_in_month(yr, mo)
28 if result == test_results[i]:
29 print("OK")
30 else:
31 print("Failed")
32
Back
LAB 28
1 def is_year_leap(year):
2 if year % 4 != 0:
3 return False
4 elif year % 100 != 0:
5 return True
6 elif year % 400 != 0:
7 return False
8 else:
9 return True
10 def days_in_month(year, month):
11 if year < 1582 or month < 1 or month > 12:
12 return None
13 days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
14 res = days[month – 1]
15 if month == 2 and is_year_leap(year):
16 res = 29
17 return res
18
19 def day_of_year(year, month, day):
20 days = 0
21 for m in range(1, month):
22 md = days_in_month(year, m)
462
Appendices
23 if md == None:
24 return None
25 days += md
26 md = days_in_month(year, month)
27 if day >= 1 and day <= md:
28 return days + day
29 else:
30 return None
31
32 print(day_of_year(2000, 12, 31))
33
Back
LAB 29
1 def is_prime(num):
2 for i in range(2, int(1 + num ** 0.5)):
3 if num % i == 0:
4 return False
5 return True
6
7 for i in range(1, 20):
8 if is_prime(i + 1):
9 print(i + 1, end=" ")
10 print()
11
Back
LAB 30
1 # 1 American mile = 1609.344 meters
2 # 1 American gallon = 3.785411784 liters
3
4 def liters_100km_to_miles_gallon(liters):
5 gallons = liters / 3.785411784
6 miles = 100 * 1000 / 1609.344
463
Python Essentials 1
Back
464
Appendices
APPENDIX C: ANSWERS
Section 2.1 Quiz
Question 1
My
name
is
Bond. James Bond.
Question 2
Question 3
Line 5 will raise SyntaxError , because the ' symbol in the Greg's book.
string requires an escape character.
Back
Section 2.2.4
Sample Solution 1
1 print('I\'m Monty Python.')
2
Sample Solution 2
1 print("I'm Monty Python.")
2
Back
465
Python Essentials 1
Question 2
The first is a string, the second is a numerical literal (a float), the third is a numerical
literal (an integer), and the fourth is a boolean literal.
Question 3
Back
Section 2.3.2
3.0 – not 3 but 3.0 . The rule still works:
Back
Section 2.3.3
Operators and their bindings
-9
-8
-9
List of priorities
466
Appendices
10.0
Back
16 8.0 8
Question 2
-0.5 0.5 0 -1
Question 3
-2 2 512
Back
Section 2.4.4
Back
Section 2.4.5
500 ‒ why? Well, first, the var variable is created and assigned a value of 100 .
Then, the same variable is assigned a new value: the result of adding 200 to 300 ,
which is 500 .
Back
467
Python Essentials 1
Section 2.4.6
C = 5.0
Back
Question 2
101
m 101
del
Question 3
11
Question 4
1.0
Back
String #2
Question 2
Back
468
Appendices
55
Question 2
<class 'str'>
Back
Section 3.1.3
Question 1
True – of course, 2 is equal to 2. Python will answer True (remember this pair of
predefined literals, True and False – they're Python keywords, too).
Question 2
This question is not as easy as the first one. Luckily, Python is able to convert the integer
value into its real equivalent, and consequently, the answer is True .
Question 3
This should be easy. The answer will be (or rather, always is) False .
Back
False
True
Question 2
False
True
469
Python Essentials 1
Question 3
True
False
Question 4
True
True
else
Question 5
four
five
Question 6
one
two
Back
470
Appendices
Question 2
Question 3
for ch in "[email protected]":
if ch == "@":
break
print(ch, end="")
Question 4
Question 5
4
3
2
0
471
Python Essentials 1
Question 6
-1
0
1
2
3
Question 7
0
3
Back
False
Question 2
0 5 -5 1 1 16
Back
[6, 2, 3, 4, 5, 1]
Question 2
472
Appendices
Question 3
Question 4
[2, 3]
3
Back
Question 2
[1, 2, 3]
Question 3
Back
['C']
Question 2
['B', 'C']
473
Python Essentials 1
Question 3
[]
Question 4
Question 5
1 my_list = [1, 2, "in", True, "ABC"]
2
3 print(1 in my_list) # outputs True
4 print("A" not in my_list) # outputs True
5 print(3 not in my_list) # outputs True
6 print(False in my_list) # outputs False
7
Back
Question 2
Question 3
Back
474
Appendices
Question 2
Question 3
Question 4
Back
Question 2
True
False
None
Question 3
[0, 2, 4, 6, 8, 10]
Question 4
[1, 4, 9, 16, 25
Back
475
Python Essentials 1
Question 2
2
1
Question 3
2
3
Question 4
2
2
Back
The factorial function has no termination condition (no base case) so Python will raise an
exception ( RecursionError: maximum recursion depth exceeded )
Question 2
56
Back
476
Appendices
Question 2
The program will print 6 to the screen. The tup tuple elements have been
"unpacked" in the a , b , and c variables.
Question 3
tup = 1, 2, 3, 2, 4, 5, 6, 2, 7, 2, 8, 9
duplicates = tup.count(2)
print(duplicates) # outputs: 4
Question 4
print(d3)
Question 5
t = tuple(my_list)
print(t)
477
Python Essentials 1
Question 6
colors_dictionary = dict(colors)
print(colors_dictionary)
Question 7
Question 8
Back
Question 2
Back
478
Appendices
• keywords
• instructions
• indentation
• comments
PCEP-30-02 1.3 – Introduce literals and variables into code and use different numeral
systems
• scientific notation
• strings
• variables
• naming conventions
PCEP-30-02 1.4 – Choose operators and data types adequate to the problem
• numeric operators: ** * / % // + –
479
Python Essentials 1
• string operators: * +
• Boolean expressions
• type casting
480
Appendices
• constructing vectors
• initializing loops
• list comprehensions
• constructing strings
481
Python Essentials 1
• multi-line strings
• recursion
PCEP-30-02 4.2 – Organize interaction between the function and its environment
• BaseException
• Exception
• SystemExit
• KeyboardInterrupt
• abstract exceptions
• ArithmeticError
• LookupError
• IndexError
• KeyError
• TypeError
• ValueError
482
Appendices
483
Python Essentials 1
Now that you have completed Python Essentials 1, book an exam and take the PCEP
Certified Entry-Level Python Programmer Exam.
484