0% found this document useful (0 votes)
115 views252 pages

Pythong Tutorial Openedg1721824408970

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 252

About this eBook

While ePUB is an open standard widely employed for the


publication of electronic books, support and features may vary
from one device to another. Every effort has been made to
ensure that this book will display faithfully on all devices, but it
may be necessary to adjust the settings of your particular
device for optimum readability.
There are many examples of code employed throughout this
book. Due to the flowing nature of text in the ePUB format, this
code, which is written line by line, may not always display
correctly. For example, comments that begin with // may
overflow to the next line. If you are trying to recreate the code
that you see in the eBook and discover that it is not working as
expected, ensure that you are following the correct formatting
procedures as outlined in this eBook and in the PEP 8 Style
Guide for Python Code.
Python Essentials 1
by
The OpenEDG Python Institute
Open Education and Development Group
1013 Centre Road, Suite 405
Wilmington, DE
19805, United States

First published in the USA in 2023 by the Open Education and


Development Group

Copyright © 2023 Open Education and Development Group

ISBN: 979-8-9877622-0-2

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

Trademarks & Disclaimer


Every effort has been made by the publisher to provide
information that is accurate. Any terms in this book that are
known trademarks have been capitalized. The Open Education
and Development Group makes no claims to the accuracy of
such trademarks.
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.
No warranty of fitness is implied as to the accuracy of the
information contained within this book, although every effort
has been made to ensure it is as accurate as possible. Neither the
authors nor publisher assume liability for or responsibility to
any person or entity that suffers loss or damage as result of the
use of the information contained herein.
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.
Bulk purchase and custom book design
This book may be purchased in bulk in either ePUB or PDF
format. Additionally, it may be possible to customize the layout
of the book to suit your needs. To discuss these options, email
[email protected].
CONTENTS
WELCOME TO PYTHON ESSENTIALS 1
Learn Python – the language of today and tomorrow

INTRODUCTION
About the course
Syllabus
Prepare for the PCEP-30-0x exam

MODULE 1: INTRODUCTION TO PYTHON AND COMPUTER


PROGRAMMING
SECTION 1.1 – INTRODUCTION TO PROGRAMMING
How does a computer program work?
Compilation vs. Interpretation – Advantages and
Disadvantages

SECTION 1.2 – INTRODUCTION TO PYTHON


Python – a tool, not a reptile
Who created Python?
A hobby programming project
There is more than one Python

SECTION 1.3 – DOWNLOADING AND INSTALLING PYTHON


Begin your Python journey

MODULE 2: PYTHON DATA TYPES, VARIABLES, OPERATORS,


AND BASIC I/O OPERATIONS
SECTION 2.1 – THE “HELLO, WORLD!” PROGRAM
2.1.1 Your very first program
2.1.2 The print() function
2.1.3 Function arguments
2.1.4 Function invocation
LAB 1 Working with the print() function
2.1.5 The print() function and its effect, arguments, and
values returned
2.1.6 Instructions
2.1.7 Python escape and newline characters
2.1.8 Using multiple arguments
2.1.9 Positional arguments
2.1.10 Keyword arguments
LAB 2 The print() function and its arguments
LAB 3 Formatting the output
2.1 SECTION SUMMARY
2.1 SECTION QUIZ

SECTION 2.2 – PYTHON LITERALS


2.2.1 Literals – the data in itself
2.2.2 Integers
2.2.3 Floats
2.2.4 Strings
2.2.5 Boolean values
LAB 4 Python literals – strings
2.2 SECTION SUMMARY
2.2 SECTION QUIZ

SECTION 2.3 – OPERATORS: DATA MANIPULATION TOOLS


2.3.1 Python as a calculator
2.3.2 Basic operators
2.3.3 Operators and their priorities
2.3 SECTION SUMMARY
2.3 SECTION QUIZ

SECTION 2.4 – VARIABLES


2.4.1 Variables – data-shaped boxes
2.4.2 Variable names
2.4.3 How to create a variable
2.4.4 How to use a variable
2.4.5 How to assign a new value to an already existing variable
2.4.6 Solving simple mathematical problems
LAB 5 Variables
2.4.7 Shortcut operators
LAB 6 Variables ‒ a simple converter
LAB 7 Operators and expressions
2.4 SECTION SUMMARY
2.4 SECTION QUIZ

SECTION 2.5 – COMMENTS


2.5.1 Comments – why, when, and how?
2.5.2 Marking fragments of code
LAB 8 Comments
2.5 SECTION SUMMARY
2.5 SECTION QUIZ

SECTION 2.6 – INTERACTION WITH THE USER


2.6.1 The input() function
2.6.2 The input() function with an argument
2.6.3 The result of the input() function
2.6.4 The input() function – prohibited operations
2.6.5 Type casting (type conversions)
2.6.6 More about input() and type casting
2.6.7 String operators
2.6.8 Type conversions once again
LAB 9 Simple input and output
LAB 10 Operators and expressions
LAB 11 Operators and expressions 2
2.6 SECTION SUMMARY
2.6 SECTION QUIZ

MODULE 3: BOOLEAN VALUES, CONDITIONAL EXECUTION,


LOOPS, LISTS AND LIST PROCESSING, LOGICAL AND BITWISE
OPERATIONS
SECTION 3.1 – MAKING DECISIONS IN PYTHON
3.1.1 Questions and answers
3.1.2 Comparison: equality operator
3.1.3 Exercises
3.1.4 Operators
3.1.5 Making use of the answers
LAB 12 Variables ‒ Questions and answers
3.1.6 Conditions and conditional execution
3.1.7 Analyzing code samples
3.1.8 Pseudocode and introduction to loops
LAB 13 Comparison operators and conditional execution
LAB 14 Essentials of the if-else statement
LAB 15 Essentials of the if-elif-else statement
3.1 SECTION SUMMARY
3.1 SECTION QUIZ

SECTION 3.2 – LOOPS IN PYTHON


3.2.1 Looping your code with while
3.2.2 An infinite loop
3.2.3 The while loop: more examples
LAB 16 Guess the secret number
3.2.4 Looping your code with for
3.2.5 More about the for loop and the range() function with
three arguments
LAB 17 Essentials of the for loop – counting mississippily
3.2.5 The break and continue statements
LAB 18 The break statement – Stuck in a loop
LAB 19 The continue statement – the Ugly Vowel Eater
LAB 20 The continue statement – the Pretty Vowel Eater
3.2.6 The while loop and the else branch
3.2.7 The for loop and the else branch
LAB 21 Essentials of the while loop
LAB 22 Collatz’s hypothesis
3.2 SECTION SUMMARY
3.2 SECTION QUIZ

SECTION 3.3 – LOGIC AND BIT OPERATIONS IN PYTHON


3.3.1 Computer logic
3.3.2 Logical expressions
3.3.3 Logical values vs. single bits
3.3.4 Bitwise operators
3.3.5 How do we deal with single bits?
3.3.6 Binary left shift and binary right shift
3.3 SECTION SUMMARY
3.3 SECTION QUIZ

SECTION 3.4 – LISTS


3.4.1 Why do we need lists?
3.4.2 Indexing lists
3.4.3 Accessing list content
3.4.4 Removing elements from a list
3.4.5 Negative indices are legal
LAB 23 The basics of lists
3.4.6 Functions vs. methods
3.4.7 Adding elements to a list: append() and insert()
3.4.8 Making use of lists
3.4.9 Lists in action
LAB 24 The basics of lists ‒ the Beatles
3.4 SECTION SUMMARY
3.4 SECTION QUIZ

SECTION 3.5 – SORTING SIMPLE LISTS: THE BUBBLE SORT ALGORITHM


3.5.1 The bubble sort
3.5.2 Sorting a list
3.5.3 The bubble sort – interactive version
3.5 SECTION SUMMARY
3.5 SECTION QUIZ
SECTION 3.6 – OPERATIONS ON LISTS
3.6.1 The inner life of lists
3.6.2 Powerful slices
3.6.3 Slices – negative indices
3.6.4 The in and not in operators
3.6.5 Lists – some simple programs
LAB 25 Operating with lists ‒ basics
3.6 SECTION SUMMARY
3.6 SECTION QUIZ

SECTION 3.7 – LISTS IN ADVANCED APPLICATIONS


3.7.1 Lists in lists
3.7.2 Two-dimensional arrays
3.7.3 Multidimensional nature of lists: advanced applications
3.7 SECTION SUMMARY

MODULE 4: FUNCTIONS, TUPLES, DICTIONARIES, EXCEPTIONS,


AND DATA PROCESSING
SECTION 4.1 – FUNCTIONS
4.1.1 Why do we need functions?
4.1.2 Decomposition
4.1.3 Where do functions come from?
4.1.4 Your first function
4.1.5 How functions work
4.1 SECTION SUMMARY
4.1 SECTION QUIZ

SECTION 4.2 – HOW FUNCTIONS COMMUNICATE WITH THEIR


ENVIRONMENT
4.2.1 Parameterized functions
4.2.2 Positional parameter passing
4.2.3 Keyword argument passing
4.2.4 Mixing positional and keyword arguments
4.2.5 Parametrized functions – more details
4.2 SECTION SUMMARY
4.2 SECTION QUIZ

SECTION 4.3 – RETURNING A RESULT FROM A FUNCTION


4.3.1 Effects and results: the return instruction
4.3.2 A few words about None
4.3.3 Effects and results: lists and functions
LAB 26 A leap year: writing your own functions
LAB 27 How many days: writing and using your own functions
LAB 28 Day of the year: writing and using your own functions
LAB 29 Prime numbers ‒ how to find them
LAB 30 Converting fuel consumption
4.3 SECTION SUMMARY
4.3 SECTION QUIZ

SECTION 4.4 – SCOPES IN PYTHON


4.4.1 Functions and scopes
4.4.2 Functions and scopes: the global keyword
4.4.3 How the function interacts with its arguments
4.4 SECTION SUMMARY
4.4 SECTION QUIZ

SECTION 4.5 – CREATING MULTI-PARAMETER FUNCTIONS


4.5.1 Sample functions: Evaluating the BMI
4.5.2 Sample functions: Triangles
4.5.3 Sample functions: Factorials
4.5.4 Fibonacci numbers
4.5.5 Recursion
4.5 SECTION SUMMARY
4.5 SECTION QUIZ

SECTION 4.6 – TUPLES AND DICTIONARIES


4.6.1 Sequence types and mutability
4.6.2 Tuples
4.6.3 Dictionaries
4.6.4 Dictionary methods and functions
4.6.5 Tuples and dictionaries can work together
4.6 SECTION SUMMARY
4.6 SECTION QUIZ

SECTION 4.7 – EXCEPTIONS


4.7.1 Errors – the developer’s daily bread
4.7.2 When data is not what it should be
4.7.3 The try-except branch
4.7.4 The exception proves the rule
4.7.5 How to deal with more than one exception
4.7.6 The default exception and how to use it
4.7.7 Some useful exceptions
4.7.8 Why you can’t avoid testing your code
4.7.9 When Python closes its eyes
4.7.10 Tests, testing, and testers
4.7.11 print debugging
4.7.12 Some useful tips
4.7.13 Unit testing – a higher level of coding
4.7 SECTION SUMMARY
4.7 SECTION QUIZ

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

Goals of this book


This book is designed to teach you the basics of Python
programming, even if you have zero programming experience
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.
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:

aspiring programmers and learners interested in


learning programming for fun and for job-related tasks;
learners looking to gain fundamental skills and
knowledge for an entry-level job role as a software
developer, data analyst, or tester;
industry professionals wishing to explore technologies
that are connected with Python, or that utilize it as a
foundation;
team leaders, product managers, and project
managers who want to understand the terminology and
processes in the software development cycle to more
effectively manage and communicate with production
and development teams.

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:

the universal concepts of computer programming;


the syntax and semantics of the Python language;
practical skills in resolving typical implementation
challenges;
how to use the most important elements of the Python
Standard Library;
how to install your runtime environment;
how to design, develop, test, and debug simple Python
programs.

The course is divided into four modules:


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.

PCEP – Certified Entry-Level Python Programmer certification is


an interim step to the PCAP – Certified Associate in Python
Programming certification, and the starting point to launch a
career in software development, Python programming, and
related technologies.
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
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!
How does a computer program work?
A program makes a computer usable. Without a program, a
computer, even the most powerful one, is nothing more than an
object. Similarly, without a player, a piano is nothing more than
a wooden box.
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:

accept a number representing the distance;


accept a number representing the travel time;
divide the former value by the latter and store the result
in the memory;
display the result (representing the average speed) in a
readable format.

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.
Language is the keyword.

Natural languages vs. programming languages


A language is a means (and a tool) for expressing and recording
thoughts. There are many languages all around us. Some of
them require neither speaking nor writing, such as body
language; it’s possible to express your deepest feelings very
precisely without saying a word.
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.
A computer, even the most technically sophisticated, is devoid
of even a trace of intelligence. You could say that it is like a well-
trained dog – it responds only to a predetermined set of known
commands.
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”.
A complete set of known commands is called an instruction
list, sometimes abbreviated to IL. Different types of computers
may vary depending on the size of their ILs, and the
instructions could be completely different in different models.
Note: machine languages are developed by humans.
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.

What makes a language?


We can say that each language (machine or natural, it doesn’t
matter) consists of the following elements:
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)
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)

Machine language vs. high-level language


The IL is, in fact, the alphabet of a machine language. This is
the simplest and most primary set of symbols we can use to
give commands to a computer. It’s the computer’s mother
tongue.
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.
A program written in a high-level programming language is
called a source code (in contrast to the machine code executed
by computers). Similarly, the file containing the source code is
called the source file.

Compilation vs. Interpretation


Computer programming is the act of composing the selected
programming language’s elements in the order that will cause
the desired effect. The effect could be different in every specific
case – it’s up to the programmer’s imagination, knowledge and
experience.
Of course, such a composition has to be correct in many senses:

alphabetically – a program needs to be written in a


recognizable script, such as Roman, Cyrillic, etc.
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;
syntactically – each language has its rules and they must
be obeyed;
semantically – the program has to make sense.

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 two different ways of transforming a program from a
high-level programming language into machine language:

Compilation – the source program is translated once (however,


this act must be repeated each time you modify the source code)
by getting a file (e.g. an .exe file if the code is intended to be run
under MS Windows) containing the machine code. Now you can
distribute the file worldwide; the program that performs this
translation is called a compiler or translator.
Interpretation – you (or any user of the code) can translate the
source program each time it has to be run. The program
performing this kind of transformation is called an interpreter,
as it interprets the code every time it is intended to be executed.
It also means that you cannot just distribute the source code as-
is, because the end-user also needs the interpreter to execute it.
Due to some very fundamental reasons, a particular high-level
programming language is designed to fall into one of these two
categories.
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?
What does the interpreter do?
Let’s assume once more that you have written a program. Now,
it exists as a computer file: a computer program is actually a
piece of text, so the source code is usually placed in text files.
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.
Compilation vs. Interpretation – Advantages and
Disadvantages
Compilation
Advantages
The execution of the translated code is usually faster;
Only the user has to have the compiler – the end-user may
use the code without it;
The translated code is stored using machine language – as
it is very hard to understand it, your own inventions and
programming tricks are likely to remain your secret.

Disdvantages
The compilation itself may be a very time-consuming
process – you may not be able to run your code
immediately after making an amendment;
You have to have as many compilers as hardware
platforms you want your code to be run on.

Interpretation
Advantages
You can run the code as soon as you complete it – there are
no additional phases of translation;
The code is stored using programming language, not
machine language – this means that it can be run on
computers using different machine languages; you don’t
compile your code separately for each different
architecture.

Disdvantages
Don’t expect interpretation to ramp up your code to high
speed – your code will share the computer’s power with
the interpreter, so it can’t be really fast;
Both you and the end user have to have the interpreter to
run your code.

What does this all mean for you?

Python is an interpreted language. This means that it


inherits all the described advantages and disadvantages.
Of course, it adds some of its unique features to both sets.
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.

Due to historical reasons, languages designed to be utilized in


the interpretation manner are often called scripting languages,
while the source programs encoded using them are
called scripts. Okay, let’s meet Python.
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 – a tool, not a reptile
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.
Who created Python?
One of the amazing features of Python is the fact that it is
actually one person’s work. Usually, new programming
languages are developed and published by large companies
employing lots of professionals, and due to copyright rules, it is
very hard to name any of the people involved in the project.
Python is an exception.
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.
A hobby programming project
The circumstances in which Python was created are a bit
puzzling. According to Guido van Rossum:
In December 1989, I was looking for a “hobby”
programming project that would keep me occupied during
the week around Christmas. My office (…) would be
closed, but I had a home computer, and not much else on
my hands. I decided to write an interpreter for the new
scripting language I had been thinking about lately: a
descendant of ABC that would appeal to Unix/C hackers.
I chose Python as a working title for the project, being in a
slightly irreverent mood (and a big fan of Monty Python’s
Flying Circus).
— Guido van Rossum

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;
open source, so anyone can contribute to its
development;
code that is as understandable as plain English;
suitable for everyday tasks, allowing for short
development times.

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.

What makes Python so special?


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:

Perl – a scripting language originally authored by Larry


Wall;
Ruby – a scripting language originally authored by
Yukihiro Matsumoto.

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.
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.

Where can we see Python in action?


We see it every day and almost everywhere. It’s used extensively
to implement complex Internet services like search engines,
cloud storage and tools, social media and so on. Whenever you
use any of these services, you are actually very close to Python,
although you wouldn’t know it.
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.

Why not Python?


Despite Python’s growing popularity, there are still some niches
where Python is absent, or is rarely seen:

low-level programming (sometimes called “close to


metal” programming): if you want to implement an
extremely effective driver or graphical engine, you
wouldn’t use Python;
applications for mobile devices: although this territory
is still waiting to be conquered by Python, it will most
likely happen someday.
There is more than one Python
Python 2 vs. Python 3
There are two main kinds of Python, called Python 2 and
Python 3.
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.
Python 3 isn’t just a better version of Python 2 – it is a
completely different language, although it’s very similar to its
predecessor. When you look at them from a distance, they
appear to be the same, but when you look closely, though, you
notice a lot of differences.
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.
It is important to remember that there may be smaller or bigger
differences between subsequent Python 3 releases (e.g. Python
3.6 introduced ordered dictionary keys by default under the
CPython implementation) – the good news, though, is that all
the newer versions of Python 3 are backward compatible with
the previous versions of Python 3. Whenever meaningful and
important, we will always try to highlight those differences in
the course.
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.
Following 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.”
The traditional implementation of Python, called CPython, is
Guido van Rossum’s reference version of the Python computing
language, and it’s most often called just “Python”. When you
hear the name CPython, it’s most probably used to distinguish it
from other, non-traditional, alternative implementations.
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.
This component is a flipcard comprised of flippable cards
containing display image. Select the front face image to flip to
the back face of these card to display associated text.

Cython is one of a possible number of solutions to the most


painful of Python’s traits – the lack of efficiency. Large and
complex mathematical calculations may be easily coded in
Python (much easier than in “C” or any other traditional
language), but the resulting code execution may be extremely
time-consuming.
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).
Another version of Python is called Jython.
“J” is for “Java”. Imagine a Python written in Java instead of C.
This is useful, for example, if you develop large and complex
systems written entirely in Java and want to add some Python
flexibility to them. The traditional CPython may be difficult to
integrate into such an environment, as C and Java live in
completely different worlds and don’t share many common
ideas.
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.

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.
The source code of PyPy is not run in the interpretation
manner, but is instead translated into the C programming
language and then executed separately.
This is useful because if you want to test any new feature that
may be (but doesn’t have to be) introduced into mainstream
Python implementation, it’s easier to check it with PyPy than
with CPython. This is why PyPy is rather a tool for people
developing Python than for the rest of the users.
This doesn’t make PyPy any less important or less serious than
CPython, of course.
In addition, PyPy is compatible with the Python 3 language.
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.
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.
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™, 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.
Begin your Python journey
How to get Python and how to use it
There are several ways to get your own copy of Python 3,
depending on the operating system you use.
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.
If you’re a Linux user, open the terminal/console, and type:
python3

at the shell prompt, press Enter and wait. If you see something
like this:
then you don’t have to do anything else.
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.
All non-Linux users can download a copy at
https://www.python.org/downloads/.

How to download, install, and configure Python


Because the browser tells the site you’ve entered the OS you use,
the only step you have to take is to click the appropriate Python
version you want.
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.

This will make things easier.


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.

Starting your work with Python


Now that you have Python 3 installed, it’s time to check if it
works and make the very first use of it.
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.
To start your work, you need the following tools:

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.
IDLE is an acronym: Integrated Development and Learning
Environment.
Navigate through your OS menus, find IDLE somewhere under
Python 3.x and launch it. This is what you should see:
Your very first program before your first program…
It is now time to write and run your first Python 3 program. It
will be very simple, for now.
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.
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.
The line looks like this:
print(“Hisssssss…”)

You can use the clipboard to copy the text into the file.
We’re not going to explain the meaning of the program right
now. You’ll find a detailed discussion in the next chapter.
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.
In this case, the program hisses.
Try to run it once again. And once more.
Now close both windows now and return to the desktop.

How to spoil and fix your code


Now start IDLE again.

Click File, Open, point to the file you saved previously and
let IDLE read it in.
Try to run it again by pressing F5 when the editor window
is active.

As you can see, IDLE is able to save your code and retrieve it
when you need it again.
IDLE contains one additional and helpful feature.

First, remove the closing parenthesis.


Then enter the parenthesis again.

Your code should look like the one down here:


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.

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.
Watch all the windows carefully.
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.
The editor window shows clearly where it happened.
Fix the code now. It should look like this:
print(“Hisssssss…”)

Run it to see if it “hisses” again.


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.

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 message (in red) shows (in the subsequent lines):

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;
the name of the error and a short explanation.

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.
MODULE 2: PYTHON DATA TYPES, VARIABLES,
OPERATORS, AND BASIC I/O OPERATIONS
SECTION 2.1 – THE “HELLO, WORLD!” PROGRAM
Welcome to Module two! In the first section, we will learn about
the most essential elements of syntax and semantics of the
Python language, and use them to build your very first Python
program – “Hello, World!”.
2.1.1 Your very first program
It’s time to start writing some real, working Python code. It’ll
be very simple for the time being.
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:

the word print;


an opening parenthesis;
a quotation mark;
a line of text: Hello, World!;
another quotation mark;
a closing parenthesis.

Each of these plays a very important role in the code.


2.1.2 The print() function
Look at this line of code:

1 print(“Hello, World!”)
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.

Moreover, many Python functions can do these two things


together.
Where do functions come from?

From Python itself: they may come from Python itself;


the print function is one of this kind; such a function is an
added value received together with Python and its
environment (it is built-in); you don’t have to do anything
special (e.g. ask anyone for anything) if you want to make
use of it;
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.

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.
2.1.3 Function arguments
As we said before, a function may have:

an effect;
a result.

There’s also a third, very important, function component ‒


the argument(s).
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!”)

In spite of the number of needed/provided arguments, Python


functions strongly demand the presence of a pair of
parentheses ‒ opening and closing ones, respectively.
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.
The function we’re talking about here is print().
Does the print() function in our example have any
arguments?
Of course it does, but what are they?

A string as an argument of the print() function


The only argument delivered to the print() function in this
example is a string:
print(“Hello, World!”)

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.
2.1.4 Function invocation
The function name (print in this case) along with the
parentheses and argument(s), forms the function invocation.

1 print(“Hello, world!”)
2

We’ll discuss this in more depth soon, but let’s just shed a little
light on it right now.
What happens when Python encounters an invocation like this
one?
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.
LAB 1 Working with the print() function
The print() command, which is one of the easiest directives in
Python, simply prints out a line to the screen.
In your first lab:

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.

Sample Solution
2.1.5 The print() function and its effect, arguments,
and values returned
Three important questions have to be answered as soon as
possible:
1. What effect does the print() function cause?
The effect is very useful and very spectacular. The function:

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.
2. What arguments does print() expect?
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().
3. What value does the print() function return?
None. Its effect is enough.
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

Your Python console should now look like this:


The itsy bitsy spider climbed up the waterspout.
Down came the rain and washed the spider out.

This is a good opportunity to make some observations:


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.)

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.
You can see it here. Run the code.
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

If everything goes right, you should see something like this:


The itsy bitsy spider climbed up the waterspout.
Down came the rain and washed the spider out.
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.
2.1.7 Python escape and newline characters
We’ve modified the code again. Look at it carefully.
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:
The itsy bitsy spider
climbed up the waterspout.
Down came the rain
and washed the spider out.

As you can see, two newlines appear in the nursery rhyme, in


the places where the \n have been used.
This convention has two important consequences:
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(“")

while this one won’t:


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
2.1.8 Using multiple arguments
So far we have tested the print() function behavior with no
arguments, and with one argument. It’s also worth trying to
feed the print() function with more than one argument.
This is what we’re going to test now:

1 print(“The itsy bitsy spider” , “climbed up” , “the


waterspout.”)
2

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.
Run the code and see what happens.
The console should now be showing the following text:
The itsy bitsy spider climbed up the waterspout.

The spaces, removed from the strings, have appeared again. Can
you explain why?
Two conclusions emerge from this example:
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.
2.1.9 Positional arguments
Now that you know a bit about print() function customs,
we’re going to show you how to change them.
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.
2.1.10 Keyword arguments
Python offers another mechanism for the passing of
arguments, which can be helpful when you want to convince
the print() function to change its behavior a bit.
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.
Here’s a very simple example how to use a keyword argument.

1 print(“My name is”, “Python.”, end=” “)


2 print(“Monty Python.”)
3

In order to use it, it is necessary to know some rules:

a keyword argument consists of three elements:


a keyword identifying the argument (end here); an equal
sign (=); and a value assigned to that argument;
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.
Run the code to see how it works.
The console should now be showing the following text:
My name is Python. Monty Python.

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.
The default behavior reflects the situation where
the end keyword argument is implicitly used in the following
way: end=”\n”.
And now it’s time to try something more difficult.
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).
What will happen now? Run the program to find out.

1 print(“My name is “, end=””)


2 print(“Monty Python.”)
3

As the end argument has been set to nothing,


the print() function outputs nothing too, once its positional
arguments have been exhausted.
The console should now be showing the following text:
My name is Monty Python.
Note: no newlines have been sent to the output.
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).
Look at the following code and run it.

1 print(“My”, “name”, “is”, “Monty”, “Python.”, sep=“-”)


2

The sep argument delivers the following results:


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.

1 print(“My”, “name”, “is”, sep=“_”, end=“*”)


2 print(“Monty”, “Python.”, sep=“*”, end=“*\n”)
3

The example doesn’t make much sense, but it visibly presents


the interactions between end and sep.
Can you predict the output?
Run the code and see if it matches your predictions.
Now that you understand the print() function, you’re ready to
consider how to store and process data in Python.
Without print(), you wouldn’t be able to see any results.
LAB 2 The print() function and its arguments
Modify the first line of code, using the sep and end keywords, to
match the expected output. Use the two print() functions.
Don’t change anything in the second print() invocation.
Expected output
Programming***Essentials***in…Python

Code

1 print(“Programming”,“Essentials”,“in”)
2 print(“Python”)
3

Sample Solution
LAB 3 Formatting the output
We strongly encourage you to play with the code we’ve written
for you, and make some (maybe even destructive) amendments.
Feel free to modify any part of the code, but there is one
condition ‒ learn from your mistakes and draw your own
conclusions.
Try to:

minimize the number of print() function invocations by


inserting the \n sequence into the strings;
make the arrow twice as large (but keep the proportions)
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?
do the same with some of the parentheses;
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 print(” *”)
2 print(” * *”)
3 print(” * *”)
4 print(” * *”)
5 print(“*** ***”)
6 print(” * *”)
7 print(” * *”)
8 print(” *****”)
9

Sample Solution
2.1 SECTION SUMMARY
1. The print() function is a built-in function. It prints/outputs
a specified message to the screen/console window.
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).
5. Computer programs are collections of instructions. An
instruction is a command to perform a specific task when
executed, e.g. to print a certain message to the screen.
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.
2.1 SECTION QUIZ
Question 1: What is the output of the following program?

1 print(“My\nname\nis\nBond.”, end=” “)
2 print(“James Bond.”)
3

Question 2: What is the output of the following program?

1 print(sep=“&”, “fish”, “chips”)


2

Question 3: Which of the following print() function


invocations will cause a SyntaxError?
print(‘Greg's book.’)
print(“‘Greg’s book.’”)
print(‘“Greg's book.”’)
print(“Greg's book.”)
print(‘“Greg’s book.”’)

Check
SECTION 2.2 – PYTHON LITERALS
Now it’s time to talk about Python literals.
2.2.1 Literals – the data in itself
Now that you have a little knowledge of some of the powerful
features offered by the print() function, it’s time to learn
about some new issues, and one important new term ‒
the literal.
A literal is data whose values are determined by the literal
itself.
As this is a difficult concept to understand, a good example may
be helpful.
Take a look at the following set of digits:
123

Can you guess what value it represents? Of course you can ‒


it’s one hundred twenty three.
But what about this:
c

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.
And this is the clue: 123 is a literal, and c is not.
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.
Try to run it.
If everything goes okay, you’ll now see two identical lines.
What happened? What does it mean?
Through this example, you encounter two different types of
literals:

a string, which you already know,


and an integer number, something completely new.

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.
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.
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.
Octal and hexadecimal numbers
There are two additional conventions in Python that are
unknown to the world of mathematics. The first allows us to
use numbers in an octal representation.
If an integer number is preceded by an 0O or 0o prefix (zero-o), it
will be treated as an octal value. This means that the number
must contain digits taken from the [0..7] range only.
0o123 is an octal number with a (decimal) value equal to 83.
The print() function does the conversion automatically. Try
this:

1 print(0o123)
2

The second convention allows us to use hexadecimal numbers.


Such numbers should be preceded by the prefix 0x or 0X (zero-
x).
0x123 is a hexadecimal number with a (decimal) value equal
to 291. The print() function can manage these values too. Try
this:

1 print(0x123)
2
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.
In essence, you can write the value 0.4 as:
.4

For example: the value of 4.0 could be written as:


4.

This will change neither its type nor its value.

Ints vs. floats


The decimal point is essential for recognizing floating-point
numbers in Python.
Look at these two numbers:
4
4.0

You may think that they are exactly the same, but Python sees
them in a completely different way.
4 is an integer number, whereas 4.0 is a floating-
point number.
The point is what makes a float.
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.
It reads: three times ten to the power of eight.
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:

the exponent (the value after the E) has to be an integer;


the base (the value in front of the E) may be either an
integer or a float.

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).
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.
Python may sometimes choose different notation than you.
For example, let’s say you’ve decided to use the following float
literal:
0.0000000000000000000001

When you run this literal through Python:


print(0.0000000000000000000001)

this is the result:


1e-22

Python always chooses the more economical form of the


number’s presentation, and you should take this into
consideration when creating literals.
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.
This is a very typical string: “I am a string.”
However, there is a catch. The catch is how to encode a quote
inside a string which is already delimited by quotes.
Let’s assume that we want to print a very simple message
saying:
I like “Monty Python”

How do we do it without generating an error? There are two


possible solutions.
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.
This example will work too:

1 print(‘I like “Monty Python”’)


2

Note: you don’t need to do any escaping here.

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.
Try to print out a string containing the following message:
I’m Monty Python.

1 print(‘I'm Monty Python.’)


2

Do you know how to do it? Click Check to see if you were right:
Check (Sample Solutions)
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.
An empty string still remains a string:

””
2.2.5 Boolean values
To conclude with Python’s literals, there are two additional
ones.
They’re not as obvious as any of the previous ones, as they’re
used to represent a very abstract value ‒ truthfulness.
Each time you ask Python if one number is greater than
another, the question results in the creation of some specific
data ‒ a Boolean value.
The name comes from George Boole (1815-1864), the author of
the fundamental work, The Laws of Thought, which contains the
definition of Boolean algebra ‒ a part of algebra which makes
use of only two distinct values: True and False, denoted
as 1 and 0.
A programmer writes a program, and the program asks
questions. Python executes the program, and provides the
answers. The program must be able to react according to the
received answers.
Fortunately, computers know only two kinds of answers:

Yes, this is true;


No, this is false.

You’ll never get a response like: I don’t know or Probably yes,


but I don’t know for sure.
Python, then, is a binary reptile.
These two Boolean values have strict denotations in Python:
True
False

You cannot change anything ‒ you have to take these symbols


as they are, including case-sensitivity.
Challenge: What will be the output of the following snippet of
code?

1 print(True > False)


2 print(True < False)
3

Run the code to check. Can you explain the result?


LAB 4 Python literals – strings
Write a one-line piece of code, using the print() function, as
well as the newline and escape characters, to match the
expected result outputted on three lines.
Expected output
“I’m”
““learning””
“““Python”””

Hint
Sample Solution
2.2 SECTION SUMMARY
1. Literals are notations for representing some fixed values in
code. Python has various types of literals – for example, a literal
can be a number (numeric literals, e.g. 123), or a string (string
literals, e.g. “I am a literal.”).
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.
2.2 SECTION QUIZ
Question 1: What types of literals are the following two
examples?
“Hello “, “007”

Question 2: What types of literals are the following four


examples?
“1.5”, 2.0, 528, False

Question 3: What is the decimal value of the following binary


number?
1011

Check
SECTION 2.3 – OPERATORS: DATA MANIPULATION TOOLS
In this section, we will talk about Python operators.
2.3.1 Python as a calculator
Now, we’re going to show you a completely new side of
the print() function. You already know that the function is
able to show you the values of the literals passed to it by
arguments.

1 print(2+2)
2

Run the code. Can you guess the output?


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.
2.3.2 Basic operators
An operator is a symbol of the programming language, which is
able to operate on the values.
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

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:

when both ** arguments are integers, the result is an


integer, too;
when at least one ** argument is a float, the result is a
float, too.

This is an important distinction to remember.

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.
Run the following code and analyze the results.

1 print(6 / 3)
2 print(6 / 3.)
3 print(6. / 3)
4 print(6. / 3.)
5

You should see that there is an exception to the rule.


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.
Fortunately, Python can help you with that.

Integer division (floor division)


A // (double slash) sign is an integer division operator. It
differs from the standard / operator in two details:
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;
it conforms to the integer vs. float rule.

Run the following example and see the results:

1 print(6 // 3)
2 print(6 // 3.)
3 print(6. // 3)
4 print(6. // 3.)
5

As you can see, integer by integer division gives an integer result.


All other cases produce floats.
Let’s do some more advanced tests.
Look at the following snippet:

1 print(6 // 4)
2 print(6. // 4)
3

Imagine that we used / instead of // ‒ could you predict the


results?
Yes, it would be 1.5 in both cases. That’s clear.
But what results should we expect with // division?
Run the code and see for yourself.
What we get is two ones ‒ one integer and one float.
The result of integer division is always rounded to the nearest
integer value that is less than the real (not rounded) result.
This is very important: rounding always goes to the lesser
integer.
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.
Note: the operator is sometimes called modulo in other
programming languages.
Take a look at the snippet ‒ try to predict its result and then run
it:

1 print(14 % 4)
2

As you can see, the result is two. This is why:

14 // 4 gives 3 → this is the integer quotient;


3 * 4 gives 12 → as a result of quotient and divisor
multiplication;
14 – 12 gives 2 → this is the remainder.

This example is somewhat more complicated:

1 print(12 % 4.5)
2

What is the result?


Check

How not to divide


As you probably know, division by zero doesn’t work.
Do not try to:

perform a division by zero;


perform an integer division by zero;
find a remainder of a division by zero.

Addition
The addition operator is the + (plus) sign, which is fully in line
with mathematical standards.
Again, take a look at the snippet of the following program:

1 print(-4 + 4)
2 print(-4. + 8)
3

The result should be nothing surprising. Run the code to check


it.

The subtraction operator, unary and binary operators


The subtraction operator is obviously the – (minus) sign,
although you should note that this operator also has another
meaning ‒ it can change the sign of a number.
This is a great opportunity to present a very important
distinction between unary and binary operators.
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?
2.3.3 Operators and their priorities
So far, we’ve treated each operator as if it had no connection
with the others. Obviously, such an ideal and simple situation is
a rarity in real programming.
Also, you will very often find more than one operator in one
expression, and then things are no longer so simple.
Consider the following expression:
2 + 3 * 5

You probably remember from school that multiplications


precede additions.
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.

Operators and their bindings


The binding of the operator determines the order of
computations performed by some operators with equal priority,
put side by side in one expression.
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

There are two possible ways of evaluating this expression:

from left to right: first 9 % 6 gives 3, and then 3 %


2 gives 1;
from right to left: first 6 % 2 gives 0, and then 9 %
0 causes a fatal error.

Run the example and see what you get.

1 print(9 % 6 % 2)
2

The result should be 1. This operator has left-sided binding.


But there’s one interesting exception.
Repeat the experiment, but now with exponentiation.
Use this snippet of code:

1 print(2 ** 2 ** 3)
2

The two possible results are:

2 ** 2 → 4; 4 ** 3 → 64
2 ** 3 → 8; 2 ** 8 → 256
Run the code. What do you see?
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

Check

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.
Look at the following table:

PRIORITY OPERATOR
1 **
+, – (note: unary operators located next to the
2 right of the power operator bind more unary
strongly)
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?
Check

Operators and parentheses


Of course, you’re always allowed to use parentheses, which can
change the natural order of a calculation.
In accordance with the arithmetic rules, subexpressions in
parentheses are always calculated first.
You can use as many parentheses as you need, and they’re often
used to improve the readability of an expression, even if they
don’t change the order of the operations.
An example of an expression with multiple parentheses is here:

1 print((5 * ((25 % 13) + 100) / (2 * 13)) // 2)


2

Try to compute the value that’s printed to the console. What’s


the result of the print() function?
Check
2.3 SECTION SUMMARY
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.
3. Arithmetic operators in Python: + (addition), –
(subtraction), * (multiplication), / (classic division ‒ always
returns a float), % (modulus ‒ divides left operand by right
operand and returns the remainder of the operation, e.g. 5 % 2
= 1), ** (exponentiation ‒ left operand raised to the power of
right operand, e.g. 2 ** 3 = 2 * 2 * 2 =
8), // (floor/integer division ‒ returns a number resulting from
division, but rounded down to the nearest whole number, e.g. 3
// 2.0 = 1.0)
4. A unary operator is an operator with only one operand,
e.g. -1, or +3.
5. A binary operator is an operator with two operands, e.g. 4 +
5, or 12 % 5.
6. Some operators act before others – the hierarchy of
priorities:

the ** operator (exponentiation) has the highest priority;


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 %,
and finally, the lowest priority: binary + and -.

7. Subexpressions in parentheses are always calculated first,


e.g. 15 – 1 * (5 * (1 + 2)) = 0.
8. The exponentiation operator uses right-sided binding, e.g. 2
** 2 ** 3 = 256.
2.3 SECTION QUIZ
Question 1: What is the expected output of the following
snippet?

1 print((2 ** 4), (2 * 4.), (2 * 4))


2

Question 2: What is the expected output of the following


snippet?

1 print((-2 / 4), (2 / 4), (2 // 4), (-2 // 4))


2

Question 3: What is the expected output of the following


snippet?

1 print((2 % -4), (2 % 4), (2 ** 3 ** 2))


2

Check
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?
2.4.1 Variables – data-shaped boxes
It seems fairly obvious that Python should allow you to encode
literals carrying number and text values.
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.
What does every Python variable have?

a name;
a value (the content of the container)

Let’s start with the issues related to a variable’s 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.
You must also name them.
2.4.2 Variable names
If you want to give a name to a variable, you must follow some
strict rules:

the name of the variable must be composed of upper-case


or lower-case letters, digits, and the
character _ (underscore)
the name of the variable must begin with a letter;
the underscore character is a letter;
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).

Note that the same restrictions apply to function names.


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
_

These variable names are also correct:

Adiós_Señora
sûr_la_mer
Einbahnstraße
переменная

Python lets you use not only Latin letters but also characters
specific to languages that use other alphabets.
And now for some incorrect names:

10t (does not begin with a letter)


!important (does not begin with a letter)
exchange rate (contains a space).

NOTE
The PEP 8 — Style Guide for Python Code recommends the
following naming convention for variables and functions in
Python:
variable names should be lowercase, with words
separated by underscores to improve readability
(e.g. var, my_variable)
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.
[‘False’, ‘None’, ‘True’, ‘and’, ‘as’, ‘assert’,
‘break’, ‘class’, ‘continue’, ‘def’, ‘del’,
‘elif’, ‘else’, ‘except’, ‘finally’, ‘for’,
‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’,
‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’,
‘raise’, ‘return’, ‘try’, ‘while’, ‘with’,
‘yield’]

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.
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.
For example ‒ you can’t name your variable like this:
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.
2.4.3 How to create a variable
What can you put inside a variable?
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.
Take a look at the following snippet:
1 var = 1
2 print(var)
3

It consists of two simple instructions:

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.
2.4.4 How to use a variable
You’re allowed to use as many variable declarations as you need
to achieve your goal, like this:

1 var = 1
2 account_balance = 1000.0
3 client_name = ‘John Doe’
4 print(var, account_balance, client_name)
5 print(var)
6

However, you’re not allowed to use a variable which doesn’t


exist (in other words, a variable that was not assigned a value).
This example will cause an error:

1 var = 1
2 print(Var)
3

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

Can you guess the output of the snippet?


Check
2.4.5 How to assign a new value to an already existing
variable
How do you assign a new value to a variable that already exists?
In the same way. You just need to use the equal sign.
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.
Look at the code:

1 var = 1
2 print(var)
3 var = var + 1
4 print(var)
5

The code sends two lines to the console:


1
2

The first line of the snippet creates a new


variable named var and assigns 1 to it.
The statement reads: assign a value of 1 to a variable
named var.
We can say it shorter: assign 1 to var.
Some prefer to read such a statement as: var becomes 1.
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.
So how do you read such a record in the program?
Take the current value of the variable var, add 1 to it and store
the result in the variable var.
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
2.4.6 Solving simple mathematical problems
Now you should be able to construct a short program solving
simple mathematical problems such as the Pythagorean
theorem:
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)
Can you guess the output of the code?
Check
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.
Your task is to:

create the variables: john, mary, and adam;


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;
print the value stored in total_apples to the console;
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.

Sample Solution
2.4.7 Shortcut operators
It’s time for the next set of operators that make a developer’s life
easier. Very often, we want to use one and the same variable
both to the right and left sides of the = operator.
For example, if we need to calculate a series of successive values
of powers of 2, we may use a piece like this:

1 x = x * 2
2

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

Let’s try to present a general description for these operations.


If op is a two-argument operator (this is a very important
condition) and the operator is used in the following context…:
variable = variable op expression

…then it can be simplified and shown as follows:


variable op= expression

Take a look at the following examples. Make sure you


understand them all.

EXPRESSION SHORTCUT OPERATOR


i = i + 2 * j i += 2 * j

var = var / 2 var /= 2

rem = rem % 10 rem %= 10


j = j – (i + var + rem) j -= (i + var + rem)

X = X ** 2 x **= 2
LAB 6 Variables ‒ a simple converter
Miles and kilometers are units of length or distance.
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.
Experiment, draw conclusions, and learn. Be curious.
Expected output
7.38 miles is 11.88 kilometers
12.25 kilometers is 7.61 miles

Code

1 kilometers = 12.25
2 miles = 7.38
3 miles_to_kilometers = ###
4 kilometers_to_miles = ###
5 print(miles, “miles
is”, round(miles_to_kilometers, 2), “kilometers”)
6 print(kilometers, “kilometers
is”, round(kilometers_to_miles, 2), “miles”)
7

Sample Solution
LAB 7 Operators and expressions
Take a look at the following code: it reads a float value, puts it
into a variable named x, and prints the value of a variable
named y. Your task is to complete the code in order to evaluate
the following expression:
3x3 – 2x2 + 3x – 1
The result should be assigned to y.
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

Sample Solution
2.4 SECTION SUMMARY
1. A variable is a named location reserved to store values in the
memory. A variable is created or initialized automatically when
you assign a value to it for the first time. (Section 2.4.1)
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.
3. Python is a dynamically-typed language, which means you
don’t need to declare variables in it. (Section 2.4.3) To assign
values to variables, you can use a simple assignment operator in
the form of the equal (=) sign, i.e. var = 1.
4. You can also use compound assignment operators (shortcut
operators) to modify values assigned to variables, for
example: var += 1, or var /= 5 * 2.
5. 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 var = 3
4 print(var)
5 var += 1
6 print(var)
7
6. 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
2.4 SECTION QUIZ
Question 1: What is the output of the following code?

1 var = 2
2 var = 3
3 print(var)
4

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

Question 3: What is the output of the following snippet?


1 a = ‘1’
2 b = “1”
3 print(a + b)
4

1
11
2

Question 4: What is the output of the following snippet?

1 a = 6
2 b = 3
3 a /= 2 * b
4 print(a)
5

1.0
1
9
6
6.0

Check
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!
2.5.1 Comments – why, when, and how?
You may want to put in a few words addressed not to Python
but to humans, usually to explain to other readers of the code
how the tricks used in the code work, or the meanings of the
variables, and eventually, in order to keep stored information
on who the author is and when the program was written.
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.
Whenever Python encounters a comment in your program, the
comment is completely transparent to it ‒ from Python’s point
of view, this is only one space (regardless of how long the real
comment is).
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
Responsible developers describe each important piece of code,
for example, by explaining the role of the variables. Although it
must be stated that the best way of commenting variables is to
name them in an unambiguous manner.
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.
We say that the first name is self-commenting.
2.5.2 Marking fragments of code
Comments may be useful in another respect ‒ you can use them
to mark a piece of code that currently isn’t needed for whatever
reason. Look at the following example, if you uncomment the
highlighted line, this will affect the output of the code:

1 # This is a test program.


2 x = 1
3 y = 2
4 # y = y + x
5 print(x + y)
6

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 print(uncomment_me, uncomment_me_too,
uncomment_me_also, sep=”\n”)
5
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 a = 2 # number of hours
4 seconds = 3600 # number of seconds in 1 hour
5 print(“Hours: “, a) #printing the number of hours
6 # print(“Seconds in Hours: “, a * seconds) # printing
the number of seconds in a given number of hours
7 #here we should also print “Goodbye”, but a programmer
didn’t have time to write any code
8 #this is the end of the program that computes the
number of seconds in 3 hours
9
2.5 SECTION SUMMARY
1. Comments can be used to leave additional information in
code. They are omitted at runtime. The information left in the
source code is addressed to human readers. In Python, a
comment is a piece of text that begins with #. The comment
extends to the end of the line.
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

3. 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.
4. 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!
5. 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).
2.5 SECTION QUIZ
Question 1: What is the output of the following snippet?

1 # print(“String #1”)
2 print(“String #2”)
3

Question 2: What will happen when you run the following


code?

1 # This is
2 a multiline
3 comment. #
4 print(“Hello!”)
5

Check
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.
2.6.1 The input() function
We’re now going to introduce you to a completely new
function, which seems to be a mirror reflection of the good
old print() function.
Why? Well, print() sends data to the console.
The new function gets data from it.
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.
Take a look at our example:

1 print(“Tell me anything…”)
2 anything = input()
3 print(“Hmm…”, anything, “… Really?”)
4

It shows a very simple case of using the input() function.


NOTE
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;
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
2.6.2 The input() function with an argument
The input() function can do something else: it can prompt the
user without any help from print().
We’ve modified our example a bit, look at the code:

1 anything = input(“Tell me anything…”)


2 print(“Hmm…”, anything, “…Really?”)
3

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;
input() will then do its job.

This variant of the input() invocation simplifies the code and


makes it clearer.
2.6.3 The result of the input() function
We’ve said it already, but it must be unambiguously stated once
again: the result of the input() function is a string.
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
2.6.4 The input() function – prohibited operations
Look at the following code. Run it, enter any number, and
press Enter.

1 # Testing a TypeError message.


2 anything = input(“Enter a number: “)
3 something = anything ** 2.0
4 print(anything, “to the power of 2 is”, something)
5

What happens? Python should have given you the following


output:
Traceback (most recent call last):
File “.main.py”, line 4, in <module>
something = anything ** 2.0

TypeError: unsupported operand type(s) for ** or


pow(): ‘str’ and ‘float’

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?
We can’t. Python can’t, either.
Have we fallen into a deadlock? Is there a solution to this issue?
Of course there is.
2.6.5 Type casting (type conversions)
Python offers two simple functions to specify a type of data and
solve this problem ‒ here they are: int() and float().
Their names are self-commenting:

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.
We’ve implemented the idea ‒ take a look at the code.
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
2.6.6 More about input() and type casting
Having a team consisting of the trio input() – int() –
float() opens up lots of new possibilities.
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.
Check out the code ‒ this is what it looks like now:

1 leg_a = float(input(“Input first leg length: “))


2 leg_b = float(input(“Input second leg length: “))
3 hypo = (leg_a**2 + leg_b**2) ** .5
4 print(“Hypotenuse length is”, hypo)
5

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.
Just like this:

1 leg_a = float(input(“Input first leg length: “))


2 leg_b = float(input(“Input second leg length: “))
3 print(“Hypotenuse length is”, (leg_a**2 + leg_b**2)
** .5)
4
2.6.7 String operators
It’s time to return to these two arithmetic operators: + and *.
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.
You cannot mix types here.
This simple program shows the + sign in its second use:
1 fnam = input(“May I have your first name, please? “)
2 lnam = input(“May I have your last name, please? “)
3 print(“Thank you.”)
4 print(”\nYour name is “ + fnam + ” “ + lnam + “.”)
5

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.

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:

“James” * 3 gives “JamesJamesJames”


3 * “an” gives “ananan”
5 * “2” (or “2” * 5) gives “22222” (not 10!)

REMEMBER
A number less than or equal to zero produces an empty string.
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.
Try practicing to create other shapes or your own artwork!
2.6.8 Type conversions once again
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.
A function capable of doing that is called str():

1 str(number)
2

To be honest, it can do much more than just transform numbers


into strings, but that can wait for later.

The right-angle triangle again


Here is our “right-angle triangle” program again:

1 leg_a = float(input(“Input first leg length: “))


2 leg_b = float(input(“Input second leg length: “))
3 print(“Hypotenuse length is “ + str((leg_a**2 +
leg_b**2) ** .5))
4

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’ve made some serious strides on your way to Python
programming.
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.
LAB 9 Simple input and output
Your task is to complete the code in order to evaluate the results
of four basic arithmetic operations.
The results have to be printed to the console.
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.
Test your code ‒ does it produce the results you expect?
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 # output the result of addition here
4 # output the result of subtraction here
5 # output the result of multiplication here
6 # output the result of division here
7 print(”\nThat’s all, folks!”)
8

Hint
Sample Solution
LAB 10 Operators and expressions
Your task is to complete the code in order to evaluate the
following expression:

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:
1

Expected output:
y = 0.6000000000000001

Sample input:
10

Expected output:
y = 0.09901951266867294

Sample input:
100

Expected output:
y = 0.009999000199950014

Sample input:
-5

Expected output:
y = -0.19258202567760344

Code

1 x = float(input(“Enter value for x: “))


2 # Write your code here.
3 print(“y =”, y)
4

Sample Solution
LAB 11 Operators and expressions 2
Your task is to prepare a simple code able to evaluate the end
time of a period of time, given as a number of minutes (it could
be arbitrarily large). The start time is given as a pair of hours
(0..23) and minutes (0..59). The result has to be printed to the
console.
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

2939

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 # Write your code here.
5

Hint
Sample Solution
2.6 SECTION SUMMARY
The print() function sends data to the console, while
the input() function gets data from the console.
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
4

You can also multiply (* ‒ replication) strings, e.g.:

1 my_input = input(“Enter something: “) # Example input:


hello
2 print(my_input * 3) # Expected output: hellohellohello
3
2.6 SECTION QUIZ
Question 1: What is the output of the following snippet?

1 x = int(input(“Enter a number: “)) # The user enters 2


2 print(x * “5”)
3

Question 2: What is the expected output of the following


snippet?

1 x = input(“Enter a number: “) # The user enters 2


2 print(type(x))
3

Check
MODULE 3: BOOLEAN VALUES, CONDITIONAL
EXECUTION, LOOPS, LISTS AND LIST PROCESSING,
LOGICAL AND BITWISE OPERATIONS
SECTION 3.1 – MAKING DECISIONS IN PYTHON
Welcome to Module three! In the first section, we will learn
about conditional statements and how to use them to make
decisions in Python.
3.1.1 Questions and answers
A programmer writes a program and the program asks
questions.
A computer executes the program and provides the answers.
The program must be able to react according to the received
answers.
Fortunately, computers know only two kinds of answers:

yes, this is true;


no, this is false.

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.
3.1.2 Comparison: equality operator
Question: are two values equal?
To ask this question, you use the == (equal equal) operator.
Don’t forget this important distinction:

= is an assignment operator, e.g. a = b assigns a with


the value of b;
== is the question are these values equal? so a ==
b compares a and b.

It is a binary operator with left-sided binding. It needs two


arguments and checks if they are equal.
3.1.3 Exercises
Now let’s ask a few questions. Try to guess the answers.
Question 1: What is the result of the following comparison?
2 == 2

Question 2: What is the result of the following comparison?


2 == 2.

Question 3: What is the result of the following comparison?


1 == 2

Check
3.1.4 Operators
Equality: the equal to operator (==)
The == (equal to) operator compares the values of two operands.
If they are equal, the result of the comparison is True. If they
are not equal, the result of the comparison is False.
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.
The question will be as follows:
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 var = 1 # Assigning 1 to var
4 print(var == 0)
5

Run the code and check if you’re right.

Inequality: the not equal to operator (!=)


The != (not equal to) operator compares the values of two
operands, too. Here is the difference: if they are equal, the result
of the comparison is False. If they are not equal, the result of
the comparison is True.
Now take a look at the following inequality comparison – can
you guess the result of this operation?
var = 0 # Assigning 0 to var

print(var != 0)

var = 1 # Assigning 1 to var

print(var != 0)

Run the code and check if you’re right.

Comparison operators: greater than


You can also ask a comparison question using the > (greater
than) operator.
If you want to know if there are more black sheep than white
ones, you can write it as follows:
black_sheep > white_sheep # Greater than
True confirms it; False denies it.

Comparison operators: greater than or equal to


The greater than operator has another special, non-
strict variant, but it’s denoted differently than in classical
arithmetic notation: >= (greater than or equal to).
There are two subsequent signs, not one.
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:
centigrade_outside >= 0.0. # Greater than or equal
to

Comparison operators: less than/less than or equal to


As you’ve probably already guessed, the operators used in this
case are: the < (less than) operator and its non-strict
sibling: <= (less than or equal to).
Look at this simple example:
current_velocity_mph < 85 # Less than

current_velocity_mph <= 85 # Less than or equal


to

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).
3.1.5 Making use of the answers
What can you do with the answer (i.e. the result of a
comparison operation) you get from the computer?
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:
answer = number_of_lions >= number_of_lionesses

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
5 <, <=, >, >=

6 ==, !=
LAB 12 Variables ‒ Questions and answers
Using one of the comparison operators in Python, write a
simple two-line program that takes the parameter n as input,
which is an integer, and prints False if n is less than 100,
and True if n is greater than or equal to 100.
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

Expected output:
True

Sample input:
-5

Expected output:
False

Sample input:
+123

Expected output:
True

Sample Solution
3.1.6 Conditions and conditional execution
You already know how to ask Python questions, but you still
don’t know how to make reasonable use of the answers. You
have to have a mechanism which will allow you to do
something if a condition is met, and not do it if it isn’t.
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;
one or more white spaces;
an expression (a question or an answer) whose value will
be interpreted solely in terms of True (when its value is
non-zero) and False (when it is equal to zero);
a colon followed by a newline;
an indented instruction or set of instructions (at least one
instruction is absolutely required); the indentation may
be achieved in two ways – by inserting a particular
number of spaces (the recommendation is to use four
spaces of indentation), or by using the tab character;
note: if there is more than one instruction in the indented
part, the indentation should be the same in all lines; even
though it may look the same if you use tabs mixed with
spaces, it’s important to make all indentations exactly the
same – Python 3 does not allow the mixing of spaces and
tabs for indentation.

How does that statement work?

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.

In real life, we often express a desire:


if the weather is good, we’ll go for a walk
then, we’ll have lunch
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

Conditional execution: the if statement


If a certain sleepless Python developer falls asleep when he or
she counts 120 sheep, and the sleep-inducing procedure may be
implemented as a special function
named sleep_and_dream(), the whole code takes the
following shape:

1 if sheep_counter >= 120: # Evaluate a test expression


2 sleep_and_dream() # Execute if test expression is
True
3

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.
Take a look at the following code:

1 if sheep_counter >= 120:


2 make_a_bed()
3 take_a_shower()
4 sleep_and_dream()
5 feed_the_sheepdogs()
6

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.
Feeding the sheepdogs, however, is always done (i.e.
the feed_the_sheepdogs() function is not indented and does
not belong to the if block, which means it is always executed.)
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.

Conditional execution: the if-else statement


We started out with a simple phrase which read: If the weather is
good, we will go for a walk.
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

Thus, there is a new word: else – this is a keyword.


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).
The if-else execution goes as follows:

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 condition evaluates to False (it is equal to zero),
the perform_if_condition_false statement is
executed, and the conditional statement comes to an end.

The if-else statement: more conditional execution


By using this form of conditional statement, we can describe
our plans as follows:
1 if the_weather_is_good:
2 go_for_a_walk()
3 else:
4 go_to_a_theater()
5 have_lunch()
6

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

Nested if-else statements


Now let’s discuss two special cases of the conditional statement.
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

Here are two important points:

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 ifs and elses pair up;
consider how the indentation improves readability, and
makes the code easier to understand and trace.

The elif statement


The second special case introduces another new Python
keyword: elif. As you probably suspect, it’s a shorter form
of else if.
elif is used to check more than just one condition, and
to stop when the first statement which is true is found.
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.
Let’s write the same scenario using Python:

1 if the_weather_is_good:
2 go_for_a_walk()
3 elif tickets_are_available:
4 go_to_the_theater()
5 elif table_is_available:
6 go_for_lunch()
7 else:
8 play_chess_at_home()
9

The way to assemble subsequent if-elif-else statements is


sometimes called a cascade.
Notice again how the indentation improves the readability of
the code.
Some additional attention has to be paid in this case:

you mustn’t use else without a preceding if;


else is always the last branch of the cascade, regardless
of whether you’ve used elif or not;
else is an optional part of the cascade, and may be
omitted;
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.
3.1.7 Analyzing code samples
Now we’re going to show you some simple yet complete
programs. We won’t explain them in detail, because we consider
the comments (and the variable names) inside the code to be
sufficient guides.
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 # Choose the larger number
5 if number1 > number2:
6 larger_number = number1
7 else:
8 larger_number = number2
9 # Print the result
10 print(“The larger number is:”, larger_number)
11

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 # Choose the larger number
5 if number1 > number2: larger_number = number1
6 else: larger_number = number2
7 # Print the result
8 print(“The larger number is:”, larger_number)
9

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.
There are no other differences in the code.
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.
Look at 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 # We temporarily assume that the first number
6 # is the largest one.
7 # We will verify this soon.
8 largest_number = number1
9 # We check if the second number is larger than the
current largest_number
10 # and update the largest_number if needed.
11 if number2 > largest_number:
12 largest_number = number2
13 # We check if the third number is larger than the
current largest_number
14 # and update the largest_number if needed.
15 if number3 > largest_number:
16 largest_number = number3
17 # Print the result
18 print(“The largest number is:”, largest_number)
19

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.
3.1.8 Pseudocode and introduction to loops
You should now be able to write a program which finds the
largest of four, five, six, or even ten numbers.
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.
Let’s look at our pseudocode:
1 largest_number = -999999999
2 number = int(input())
3 if number == -1:
4 print(largest_number)
5 exit()
6 if number > largest_number:
7 largest_number = number
8 # Go to line 02
9

What’s happening in it?


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.
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 # Check which one of the numbers is the greatest
6 # and pass it to the largest_number variable.
7 largest_number = max(number1, number2, number3)
8 # Print the result.
9 print(“The largest number is:”, largest_number)
10

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.
LAB 13 Comparison operators and conditional execution
Spathiphyllum, more commonly known as a peace lily or white
sail plant, is one of the most popular indoor houseplants that
filters out harmful toxins from the air. Some of the toxins that it
neutralizes include benzene, formaldehyde, and ammonia.
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:

prints the sentence “Yes – Spathiphyllum is the


best plant ever!” to the screen if the inputted string
is “Spathiphyllum” (upper-case)
prints “No, I want a big Spathiphyllum!” if the
inputted string is “spathiphyllum” (lower-case)
prints “Spathiphyllum! Not [input]!” otherwise.
Note: [input] is the string taken as input.

Test your code using the data we’ve provided for you. And get
yourself a Spathiphyllum, too!
Sample input:
spathiphyllum

Expected output:
No, I want a big Spathiphyllum!

Sample input:
pelargonium

Expected output:
Spathiphyllum! Not pelargonium!

Sample input:
Spathiphyllum

Expected output:
Yes – Spathiphyllum is the best plant ever!

Sample Solution
LAB 14 Essentials of the if-else statement
Once upon a time there was a land – a land of milk and honey,
inhabited by happy and prosperous people. The people paid
taxes, of course – their happiness had limits. The most
important tax, called the Personal Income Tax (PIT for short) had
to be paid once a year, and was evaluated using the following
rule:

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.

Your task is to write a tax calculator.

It should accept one floating-point value: the income.


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.
Test your code using the data we’ve provided.
Sample input:
10000

Expected output:
The tax is: 1244.0 thalers

Sample input:
100000

Expected output:
The tax is: 19470.0 thalers

Sample input:
1000

Expected output:
The tax is: 0.0 thalers

Sample input:
-100

Expected output:
The tax is: 0.0 thalers

Code
1 income = float(input(“Enter the annual income: “))
2 if income < 85528:
3 tax = income * 0.18 – 556.02
4 # Write the rest of your code here.
5 tax = round(tax, 0)
6 print(“The tax is:”, tax, “thalers”)
7

Sample Solution
LAB 15 Essentials of the if-elif-else statement
As you surely know, due to some astronomical reasons, years
may be leap or common. The former are 366 days long, while the
latter are 365 days long.
Since the introduction of the Gregorian calendar (in 1582), the
following rule is used to determine the kind of year:

if the year number isn’t divisible by four, it’s a common


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;
otherwise, it’s a leap 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.
Test your code using the data we’ve provided.
Sample input:
2000

Expected output:
Leap year

Sample input:
2015

Expected output:
Common year

Sample input:
1999

Expected output:
Common year

Sample input:
1996

Expected output:
Leap year

Sample input:
1580

Expected output:
Not within the Gregorian calendar period

Code
1 year = int(input(“Enter a year: “))
2 if year < 1582:
3 print(“Not within the Gregorian calendar period”)
4 else:
5 # Write the if-elif-elif-else block here.
6

Sample Solution
3.1 SECTION SUMMARY
1. The comparison (otherwise known as relational) operators
are used to compare values. The following table illustrates how
the comparison operators work, assuming that x = 0, y = 1,
and z = 0:

OPERATOR DESCRIPTION EXAMPLE


x == y
returns True if operands’ values are #False
==
equal, and False otherwise x == z #
True
x != y #
returns True if operands’ values are not True
!=
equal, and False otherwise x != z #
False
x > y #
True if the left operand’s value is greater
False
> than the right operand’s value,
y > z #
and False otherwise
True

x < y #
True if the left operand’s value is less
True
< than the right operand’s value,
y < z #
and False otherwise
False
>= True if the left operand’s value is greater x >= y #
than or equal to the right operand’s False
value, and False otherwise
x >= z #
True
y >= z #
True
x <= y #
True
True if the left operand’s value is less
x <= z #
<= than or equal to the right operand’s
True
value, and False otherwise
y <= z #
False
2. When you want to execute some code only if a certain
condition is met, you can use a conditional statement:
a single if statement, e.g.:

1 x = 10
2 if x == 10: # condition
3 print(“x is equal to 10”) # Executed if the
condition is True.
4

a series of if statements, e.g.:


1 x = 10
2 if x > 5: # condition one
3 print(“x is greater than 5”) # Executed if
condition one is True.
4 if x < 10: # condition two
5 print(“x is less than 10”) # Executed if
condition two is True.
6 if x == 10: # condition three
7 print(“x is equal to 10”) # Executed if condition
three is True.
8

Each if statement is tested separately.


an if-else statement, e.g.:

1 x = 10
2 if x < 10: # condition
3 print(“x is less than 10”) # Executed if the
condition is True.
4 else:
5 print(“x is greater than or equal to 10”) #
Executed if the condition is False.
6

a series of if statements followed by an else, e.g.:


1 x = 10
2 if x > 5: # condition one
3 print(“x is greater than 5”) # Executed if
condition one is True.
4 if x < 10: # condition two
5 print(“x is less than 10”) # Executed if
condition two is True.
6 if x == 10: # condition three
7 print(“x is equal to 10”) # Executed if
condition three is True.
8

Each if is tested separately. The body of else is executed if the


last if is False.

The if-elif-else statement, e.g.:

1 x = 10
2 if x == 10: # True
3 print(“x == 10”)
4 if x > 15: # False
5 print(“x > 15”)
6 elif x > 10: # False
7 print(“x > 10”)
8 elif x > 5: # True
9 print(“x > 5”)
10 else:
11 print(“else will not be executed”)
12

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.
Nested conditional statements, e.g.:

1 x = 10
2 if x > 5: # True
3 if x == 6: # False
4 print(“nested: x == 6”)
5 elif x == 10: # True
6 print(“nested: x == 10”)
7 else:
8 print(“nested: else”)
9 else:
10 print(“else”)
11
3.1 SECTION QUIZ
Question 1: What is the output of the following snippet?
x = 5

y = 10

z = 8

print(x > y)

print(y > z)

Question 2: What is the output of the following snippet?


x, y, z = 5, 10, 8

print(x > z)

print((y – 5) == x)

Question 3: What is the output of the following snippet?


x, y, z = 5, 10, 8

x, y, z = z, y, x

print(x > z)

print((y – 5) == x)

Question 4: What is the output of the following snippet?


x = 10

if x == 10:

print(x == 10)
if x > 5:

print(x > 5)

if x < 10:

print(x < 10)

else:

print(“else”)

Question 5: What is the output of the following snippet?


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”)

Question 6: What is the output of the following snippet?


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
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?
3.2.1 Looping your code with while
Do you agree with the following statement?
while there is something to do

do it

Note that this record also declares that if there is nothing to do,
nothing at all will happen.
In general, in Python, a loop can be represented as follows:
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.
Look at the following algorithm:
while conditional_expression:

instruction_one

instruction_two

instruction_three
:

instruction_n

It is now important to remember that:

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).
3.2.2 An infinite loop
An infinite loop, also called an endless loop, is a sequence of
instructions in a program which repeat indefinitely (loop
endlessly.)
Here’s an example of a loop that is not able to finish its
execution:

1 while True:
2 print(“I’m stuck inside a loop.”)
3

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 # Input the first value.
4 number = int(input(“Enter a number or type -1 to stop:
“))
5 # If the number is not equal to -1, continue.
6 while number != -1:
7 # Is number larger than largest_number?
8 if number > largest_number:
9 # Yes, update largest_number.
10 largest_number = number
11 # Input the next number.
12 number = int(input(“Enter a number or type -1 to
stop: “))
13 # Print the largest number.
14 print(“The largest number is:”, largest_number)
15

Check how this code implements the algorithm we showed you


earlier.
3.2.3 The while loop: more examples
Let’s look at another example employing the while loop. Follow
the comments to find out the idea and the solution.

1 # A program that reads a sequence of numbers


2 # and counts how many numbers are even and how many
are odd.
3 # The program terminates when zero is entered.
4 odd_numbers = 0
5 even_numbers = 0
6 # Read the first number.
7 number = int(input(“Enter a number or type 0 to stop:
“))
8 # 0 terminates execution.
9 while number != 0:
10 # Check if the number is odd.
11 if number % 2 == 1:
12 # Increase the odd_numbers counter.
13 odd_numbers += 1
14 else:
15 # Increase the even_numbers counter.
16 even_numbers += 1
17 # Read the next number.
18 number = int(input(“Enter a number or type 0 to
stop: “))
19 # Print results.
20 print(“Odd numbers count:”, odd_numbers)
21 print(“Even numbers count:”, even_numbers)
22

Certain expressions can be simplified without changing the


program’s behavior.
Try to recall how Python interprets the truth of a condition,
and note that these two forms are equivalent:
while number != 0: and while number:.
The condition that checks if a number is odd can be coded in
these equivalent forms, too:
if number % 2 == 1: and if number % 2:.

Using a counter variable to exit a loop


Look at the following snippet:

1 counter = 5
2 while counter != 0:
3 print(“Inside the loop.”, counter)
4 counter -= 1
5 print(“Outside the loop.”, counter)
6

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.
Can you see the difference?

1 counter = 5
2 while counter:
3 print(“Inside the loop.”, counter)
4 counter -= 1
5 print(“Outside the loop.”, counter)
6
Is it more compact than previously? A bit. Is it more legible?
That’s disputable.
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.
LAB 16 Guess the secret number
A junior magician has picked a secret number. He has hidden it
in a variable named secret_number. He wants everyone who
runs his program to play the Guess the secret number game, and
guess what number he has picked for them. Those who don’t
guess the number will be stuck in an endless loop forever!
Unfortunately, he does not know how to complete the code.
Your task is to help the magician complete the code in such a
way so that the code:

will ask the user to enter an integer number;


will use a while loop;
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.”

The magician is counting on you! Don’t disappoint him.


EXTRA
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 print(
3 ”””
4 +================================+
5 | Welcome to my game, muggle! |
6 | Enter an integer number |
7 | and guess what number I’ve |
8 | picked for you. |
9 | So, what is the secret number? |
10 +================================+
11 ”””)
12

Hint
Sample Solution
3.2.4 Looping your code with for
Another kind of loop available in Python comes from the
observation that sometimes it’s more important to count the
“turns” of the loop than to check the conditions.
Imagine that a loop’s body needs to be executed exactly one
hundred times. If you would like to use the while loop to do it,
it may look like this:

1 i = 0
2 while i < 100:
3 # do_something()
4 i += 1
5

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 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?

You might also like