You are on page 1of 95

Unit P1:

Introduction to
programming
A SHORT INTRODUCTION TO HARDWARE,
SOFTWARE, AND ALGORITHM DEVELOPMENT
Chapter 1

This Photo by Unknown Author is licensed under CC BY-SA


Unit P1: Goals
▪ Introduction to computers and programming
o About computer hardware, software and programming
o How to write and execute your first Python program
o How to test, find and fix programming errors
o How to use pseudocode to describe an algorithm
▪ Flow Charts as a support for problem solving
o Representation
o Design steps
▪ Introduction to Python
o Tools
o Language

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 2


Computer Sciences
Introduction

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 3


Definition of Computer Science
▪ Computer Science (Informatics) is the science devoted to the study
of how to represent and manipulate information

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 4


Electronic Computer

input output
(data) (results)

(computing /
elaboration)
Politecnico di Torino, 2021/22 COMPUTER SCIENCES 5
Problems
▪ How to encode data in a format that can be understood by the
computer
▪ How to encode the commands or instructions into a sequence of
operations that composes the desired elaboration
▪ How to codify the results in a format that can be understood by
the human user

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 6


Digital information: everything becomes “bits”
Text

Data

Voice
01101100
Music

Images
Videos
Politecnico di Torino, 2021/22 COMPUTER SCIENCES 7
Hardware and Software
▪ An electronic computer is composed of two parts:
o Hardware:
Physical component, consisting of electronic devices, and mechanical, magnetic and optical
parts
o Software:
“intangible” component consisting of:
• Programs: The “instructions” for hardware
• Data: Information upon which programs operate

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 8


Hardware
▪ Hardware consists of the physical elements in a computer system.
o Examples: monitor, mouse, external storage, keyboard, ….
▪ The central processing unit (CPU) performs program control and
data processing
▪ Storage devices include memory (RAM) and secondary storage
o Hard disk
o Flash drives
o CD/DVD drives
▪ Input / output devices allow the user to interact with the
computer
o Mouse, keyboard, printer, screen…

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 9


Simplified View of a Computer’s Hardware

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 10


Software
▪ Software is typically an application program
o Microsoft Word is an example of software
o Computer Games are software
o Operating systems and device drivers are also software
▪ Software
o Software is a sequence of instructions and decisions implemented in some
language and translated to a form that can be executed or run on the
computer.
o Software operates an manages the data used by the instructions
▪ Computers execute very basic instructions in rapid succession
o The basic instructions can be grouped together to perform complex tasks

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 11


Computer Programs
▪ A computer program tells a computer the sequence of steps
needed to complete a specific task
o The program consists of a (very) large number of primitive (simple)
instructions
▪ Computers can carry out a wide range of tasks because they can
execute different programs
o Each program is designed to direct the computer to work on a specific task
▪ Programming:
o The act (and the art) of designing, implementing, and testing computer
programs

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 12


Executing a Program
▪ Program instructions and data (such as text, numbers, audio, or
video) are stored in digital format
▪ To execute a program, it must be brought into memory, where the
CPU can read it.
▪ The CPU runs the program following the written instructions.
o The program may react to user input.
▪ The instructions and the user input guide the program execution
o The CPU reads data (including user input), modifies it, and writes it back to
memory, the screen, or secondary storage.

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 13


Programming

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 14


What do we learn in this course?
▪ From the specification of a problem, to the realization of a solution
to that problem, in the form of a computer program

Building
a program

The first ENGINEERING DESIGN course at Politecnico

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 15


Algorithms 1.7

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 16


Our First Definition
▪ Algorithm
▪ An algorithm is a step by step description of how to solve a
problem

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 17


Find the biggest number

4102
8745

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 18


Design
Problem

Solution

Formal
solution

Program

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 19


Example of problem solving
▪ Problem: Compute the maximum Problem
among A and B

Informal
▪ Solution: The maximum is the solution
larger number between A and B…

Formal
▪ Formal solution: solution
1. initially: max = 0
2. if A > B then max = A; stop
3. otherwise max = B; stop

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 20


Find the biggest number

7345 1302

5901

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 21


Find the biggest number

1256 6765 6829 3847 3284 4798 3848


9185 3721 3689 4574 3985 4389 5743
4395 9239 8473 2854 3785 6648 0101
9140 7231 5672 1419 4385 6318 9810
3532 5498 6043 7542 8957 2149 3285
4903 2750 3019 2754 8018 3605 2389
2740 8993 7416 5415 9857 1624 8935
9826 7561 3746 9327 5432 5436 5078
5612 3465 7984 8764 9264 1547 8144
9194 1948 1282 5821 8975 4276 9219

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 22


Find the biggest number
1256 6765 6829 3847 3284 4798 3848
9185 3721 3689 4574 3985 4389 5743
4395 9239 8473 1854 3785 6648 0101
9140 9871 5672 1419 4385 6318 9870
3532 5498 6043 9542 8957 2149 3285
4903 2750 3019 2754 8018 3605 2389
4740 8993 7416 5415 9837 1624 8935
9626 7561 3946 9327 5432 5436 5078
5612 3465 7984 8764 9264 1547 8144
9194 1948 1282 5821 8975 4276 9819

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 23


Critical points
▪ Critical points in the development of a project are mainly:
o Development of an “informal” solution
o Formalization of the solution
• It allows a simpler “translation” in programming language rules

▪ The solution of a problem normally requires the


development of an algorithm

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 24


Algorithm
By “algorithm” we mean a precise (formal) description
of a finite sequence of actions that need to be
executed in order to achieve the solution to a problem

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 25


Algorithm
▪ The term algorithm refers to the precise (formal) description of a
finite sequence of actions that must be executed to find the
solution to a given problem
▪ The word derives by late latin’s “algorismus”, that derives from the
name of the Persian mathematician
Muhammad ibn Mūsa 'l-Khwārizmī (780-850), who wrote an
Algebra treaty

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 26


Introduction to Algorithms
▪ If you want a computer to perform a task, you start by writing an
algorithm
▪ An Algorithm is:
o A sequence (the order matters!) of actions to take (instructions) to
accomplish the given task for achieving a goal
o Like a ‘recipe’
▪ For complex problems, software developers study an algorithm,
then formalize it with pseudo code or flow charts, all before
attempting to write a computer program
▪ Developing algorithms is a fundamental problem solving skill
o It has uses in many fields outside of Computer Science

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 27


Algorithm: Formal Definition
▪ An algorithm describes a sequence of steps that is:
▪ Unambiguous
o No “assumptions” are required to execute the algorithm
o The algorithm uses precise instructions
▪ Executable
o The algorithm can be carried out in practice
▪ Terminating
o The algorithm will eventually come to an end, or halt

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 28


Problem Solving: Algorithm Design
▪ Algorithms are simply plans
o Detailed plans that describe the steps to solve a specific problem
▪ You already know quite a few
o Calculate the area of a circle
o Find the length of the hypotenuse of a triangle
▪ Some problems are more complex and require more steps
o Solve a 2nd degree equation
o Find the GCD of two numbers
o Calculate PI to 100 decimal places
o Calculate the trajectory of a missile

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 29


An Example: Selecting a Car
▪ Problem Statement:
o You have the choice of buying two cars.
o One is more fuel efficient than the other, but also more expensive.
o You know the price and fuel efficiency (in miles per gallon, mpg) of both
cars.
o You plan to keep the car for ten years.
o Which car is the better deal?

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 30


Developing the Algorithm
▪ Determine the inputs and outputs
▪ From the problem statement we know:
o Car 1: Purchase price, Fuel Efficiency
o Car 2: Purchase price, Fuel Efficiency
o Price per gallon = $4.00
o Annual miles driven= 15,000
o Length of time = 10 years
▪ For each car we need to calculate:
o Annual fuel consumed for each car
o Annual fuel cost for each car
o Operating cost for each car
o Total cost of each Car
▪ Then we select the car with the lowest total cost

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 31


Formalizing the solution
▪ Pseudo-code
o Pros
• Immediate
o Cons
• Algorithm description not very abstract
• More complex interpretation

▪ Flow Charts
o Pros
• More intuitive: graphical formalism
• More abstract algorithm description
o Cons
• Require learning the meaning of different blocks
• Struggle to represent complex or more abstract operations

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 32


Translating the Algorithm to pseudocode
▪ Break down the problem into smaller tasks
o ‘Calculate total cost’ for each car
o To calculate the total cost for each year we need to calculate the operating
cost
o The operating cost depends on the annual fuel cost
o The annual fuel cost is the price per gallon * the annual fuel consumed
o The annual fuel consumed is the annual miles drive / fuel efficiency
▪ Describe each subtask as pseudocode
o total cost = purchase price + operating cost

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 33


The Pseudo-code
▪ For each Car, compute the total cost
o Annual fuel consumed = annual miles driven / fuel efficiency
o Annual fuel cost = price per gallon * annual fuel consumed
o Operating cost = Length of time * annual fuel cost
o Total cost = purchase price + operating cost
▪ If total cost1 < total cost2
o Chose Car1
▪ Else
o Choose Car2

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 34


Which languages? – Examples
▪ High Level Language

if x > 3:
x = x + 1

▪ Assembly Language
… Specific for a given architecture
LOAD Reg1, Mem[1000] (microprocessor)
ADD Reg1, 10

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 35


Data abstraction

100
101
x; integer; 3 102
value=3
103 Memory
c; character; ‘a’ 104 addresses
value=‘a’ 105
106
t; real; 107
-11.564
value=-11.564 108

Memory

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 36


Instructions
▪ Operations supported by the programming language, that will
execute them by translating them to the machine level
o Pseudo-instructions
• Directive to the language interpreter or compiler, do not correspond to executable code
o Basic (elementary, primitive) instructions
• Operations that directly correspond to hardware operations
• Example: interaction with I/O devices, data access and modification
o Flow control instructions
• Execute complex operations by controlling the execution sequence of basic instructions

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 37


Program example
PROGRAM prova; identifiers
// test program Pseudo-instruction KEYWORDS
CONSTANTS
pi = 3.14159
coeff = 0.19 Allocate memory cells
VARIABLES
x: INTEGER Specify data types
Control
y: REAL flow
c: CHARACTER
instruction
BEGIN PROGRAM
x = 2; Primitive
IF (y > 2) THEN y = x * pi;
instructions “Running”
part
PRINT x, y;
END PROGRAM

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 38


Problem Solving with
Flow Charts

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 39


True education is to learn how to think, not what to think.

If you know how to think... then you are a free human being.
(Krishnamurti)

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 40


What will we learn in this course?
▪ To acquire the mental attitude (forma mentis) necessary to face
«problem posing» and «problem solving» tasks
o To analyze a problem, and decompose it in smaller problems
o To describe the solution of a problem, in clear non-ambiguous way
o To analyze and make explicit our reasoning steps
▪ This is useful in all scientific (and non-scientific) disciplines
▪ In particular…
o …to THINK like a computer
o …to SPEAK to a computer so that it can understand us

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 41


Algorithm
Structured visual
language

Sequential operations

Sub-operations

Repetitions

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 42


Tools for Problem Solving

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 43


The Rules
▪ I must solve the problem, and imagine I have one sheet of paper
(memory) and a pen, only
▪ Every information that I need to remember must be written on the
paper

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 44


Cooking vs Programming
Hardware
Software

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 45


Cooking vs Programming
Hardware
Software

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 46


What is “programming”?
▪ Programming means to write a “document” (source file) that
describes the solution to the problem

▪ In general, “the” solution to a given problem does not exist


o Programming consists of finding the most efficient solution to the
problem, according to certain metrics

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 47


What is “programming”?
▪ Programming is a “creative” task!
o Every problem is different from every other problem
o There are no systematic/analytic solutions or “universal” solutions!

▪ Programming is a complex operation


o A “direct” approach (from the problem directly to the final source
code) is unfeasible
o Usually, organized in several stages (refinements)

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 48


Using Flowcharts to
Develop and Refine
Algorithms 3.5

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 49


Using Flowcharts
▪ Flowcharts are an excellent tool
▪ They can help you visualize the flow of your algorithm
▪ Building the flowchart
o Link your tasks and input / output boxes in the sequence they need to be
executed
o When you need to make a decision use the diamond (a conditional
statement) with two outcomes
o Never point an arrow inside another branch

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 50


Sequence

structure-1

structure
structure-2

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 51


If-Then-Else

T condition F

structure-1 structure-2

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 52


If-Then

T condition F

structure

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 53


While-Do

condition F

structure

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 54


do - while

structure

condition
T
F
Politecnico di Torino, 2021/22 COMPUTER SCIENCES 55
Conditional Flowcharts
▪ Two Outcomes ▪ Multiple Outcomes

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 56


Shipping Cost flowchart
▪ Shipping costs are $5 inside the contiguous United States (Lower
48 states), and $10 to Hawaii and Alaska. International shipping
costs are also $10.
▪ Three Branches:

International Hawaii/Alaska Lower 48


Branch Branch Branch

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 57


Don’t Connect Branches!
▪ Shipping costs are $5 inside the United States, except that to
Hawaii and Alaska they are $10. International shipping costs are
also $10.
▪ Don’t do this!

International Hawaii/Alaska Lower 48


Branch Branch Branch

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 58


Shipping Cost Flowchart
▪ Shipping costs are $5 inside the United States, except that to
Hawaii and Alaska they are $10. International shipping costs are
also $10.

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 59


PyCharm

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 60


Complex Decision Making is Hard
▪ Computer systems are used to help sort and route luggage at airports
▪ The systems:
o Scan the baggage tags
o Sorts the items
o Routes the items to conveyor belts
o Humans then place the bags on trucks
▪ In 1993 Denver built a new airport with a “state of the art” luggage
system that replaced the human operators with robotic carts
o The system failed
o The airport could not open without a luggage system
o The system was replaced (it took over a year)
o The cost was almost $1B…. (yes one billion… 1994 dollars)
o The company that designed the system went bankrupt

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 61


Exercise
▪ Fred sells bunches of flowers at the local shopping center.

▪ One day Fred’s boss, Joe, tells Fred that at any time during the day
he (Joe) will need to know:
o how many bunches of flowers have been sold
o what was the value of the most expensive bunch sold
o what was the value of the least expensive bunch sold
o what is the average value of bunches sold

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 62


Exercise

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 63


Building Test Cases 3.6

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 64


Problem Solving: Test Cases
▪ To verify the correctness of a program, you must test it
o A Test Case is a set of inputs that are used to verify whether the program output is
correct in that specific case
▪ Testing is never 100% complete, but should cover all possible behaviors
of the program
▪ Aim for complete coverage of all decision points (alternatives)
o Create separate test cases for every programming alternative (e.g.,
domestic/international)
o Create test cases for “boundary conditions”: the minimum (maximum) value, a
value just above the minimum (max), just above the minimum (max), just
above/below an program threshold, …
o Create test cases for special values (0, 1, very large numbers, …)
o Create test cases for invalid inputs (negative values, strings instead of numbers,
empty values, …)

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 65


Make a Schedule…
▪ Make a reasonable estimate of the time it will take you to:
o Design the algorithm
o Develop test cases
o Translate the algorithm to code and enter the code
o Test and debug your program
▪ Leave some extra time for unanticipated problems
▪ As you gain more experience your estimates will become more
accurate. It is better to have some extra time than to be late

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 66


Introducing Pyton 1.3, 1.4,
1.5, 1.6

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 67


The Python Language
▪ In the early 1990’s, Guido van Rossum designed what would
become the Python programming language
▪ Van Rossum was dissatisfied with the languages available
o They were optimized to write large programs that executed quickly
▪ He needed a language that could not only be used to create
programs quickly but also make them easy to modify
o It was designed to have a much simpler and cleaner syntax than other
popular languages such as Java, C and C++ (making it easier to learn)
o The Python environment featured an “all batteries included” approach,
with the availability of many commonly used functions
o Python is interpreted, making it easier to develop and test short
programs
▪ Python programs are executed by the Python interpreter
o The interpreter reads your program and executes it

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 68


https://www.python.org/

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 69


https://docs.python.org/

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 70


Programming Environments
▪ There are several ways of creating a computer program
o Using an Integrated Development Environment (IDE)
• IDLE, PyCharm, Visual Studio Code, …
o Using a text editor
• Notepad, Notepad++, Atom, vi, gedit, …

▪ You should use the method you are most comfortable with
o In this course we will use the PyCharm IDE or the repl.it on-line IDE
o Book examples use the Wing IDE or the Spyder IDE

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 71


IDE components
▪ The source code editor can help programming by:
o Listing line numbers of code
o Syntax highlighting and coloring (comments, text…)
o Auto-indent source code
o Highlight syntax errors
o Auto-completion
▪ Output window
o The output generated by the program
▪ Debugger
o Tools to help you search and correct logic errors in the program

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 72


Text editor programming
▪ You can use a simple text editor to write your source code
▪ Once saved as hello.py, you can use a console window to:
o Compile & Run the program

(Compile and) execute


the program:
python hello.py

Program source code in Program output:


hello.py Hello, world

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 73


Your first program
▪ Traditional ‘Hello World’ program in Python
o print is an example of a Python statement
• IDE
write • Text editor

hello.py

• IDE
run • Console

Program output

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 74


Writing/typing a Python program
▪ Careful about spelling - e.g., ‘print’ vs. ‘primt’
▪ PyTHon iS CaSe SeNsItiVe
▪ Spaces are important, especially at the beginning of a line
(indentation)
▪ Lines beginning with # are comments (ignored by Python)

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 75


Source Code to a Running Program
▪ The compiler reads your program and generates byte code
instructions (simple instructions for the Python Virtual machine)
o The Python Virtual machine is a program that is similar to the CPU of your
computer
o Any necessary libraries (e.g. for drawing graphics) are automatically located
and included by the virtual machine

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 76


The PyCharm IDE

Project files Program source code

Debugger/Output

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 77


Project vs Program vs File
▪ A single Program may be very large, and will be composed of may
different files
▪ IDEs allow us to group a set of related files into a “Project”
▪ Every time we want to create a new program, we must
o Create a new Project
o Create one (or more) Python Files inside the Project

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 78


Programming with the PyCharm IDE
Project folder

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 79


Programming with the PyCharm IDE

New Python file

Example main.py
(may modify or
delete)

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 80


Programming with the PyCharm IDE

Type program

Run

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 81


Programming with the PyCharm IDE

Run again
shortcut

Program output

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 82


Organize your work
▪ Your ‘source code’ is stored in .py files
▪ Create a folder for this course
▪ Create one project folder per program inside the course folder
o A program can consist of several .py files
▪ Backup your files regularly
o To a USB flash drive (or more than one)
o To a network drive (cloud service) or external hard drive
o Do it. Really.

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 83


Python interactive mode
▪ The Python interpreter loads a file and executes all the instructions
at once
o Similar to other (compiled) languages
▪ Alternatively: in interactive mode, you can run instructions one at
a time
o It allows quick ‘test programs’ to be written
o Try and experiment instructions
o Interactive mode allows you to write python statements directly in the
console window

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 84


Python interactive mode

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 85


Python interactive mode

Python console

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 86


On-line IDE: https://repl.it/

Project files
Program output
Program source

Fantastic option for quick


examples, for testing program
fragments, for avoiding to
create a new project for a small
piece of code, …

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 87


Basic Python Syntax: Print
▪ Using the Python print() function
o A function is a collection of programming instructions (with a name) that
carry out a particular task (in this case to print a value onscreen)
o It’s code that somebody else wrote for you!

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 88


Syntax for Python Functions
▪ To use, or call, a function in Python you need to specify:
o The name of the function that you want to use
• In the previous example, the name was print
o All values (arguments) needed by the function to carry out its task
• in this case, "Hello World!"
o Arguments are enclosed in parentheses
o Multiple arguments are separated with commas.

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 89


Strings
▪ A sequence of characters enclosed in quotations marks are called a
string
o May be enclosed in 'single quotes'
o May be enclosed in "double quotes"

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 90


More examples of the print function
▪ Printing numerical values
o print(3 + 4)
o Evaluates the expression 3 + 4 and displays 7
▪ Passing multiple values to the function
o print("The answer is", 6 * 7)
o Displays The answer is 42
o Each value passed to the function is displayed, one after another, with a blank
space after each value
▪ By default the print function starts a new line after its arguments are
printed
• print("Hello")
• print("World!")
o Prints two lines of text:
• Hello
• World!

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 91


Our Second Program (printtest.py)
##
# Sample Program that demonstrates the print function
#
# Prints 7

print(3 + 4)

# Print Hello World! on two lines


print("Hello")
print("World!")

# Print multiple values with a single print function call


print("My favorite numbers are", 3 + 4, "and", 3 + 10)

# Print Hello World! on two lines


print("Goodbye")
print()
print("Hope to see you again")

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 92


Errors
COMPILE-TIME ERRORS RUN-TIME ERRORS
OR SYNTAX ERRORS OR LOGIC ERRORS

▪ Spelling, capitalization, punctuation ▪ The program runs, but produces unintended


results
▪ Ordering of statements, matching of
parenthesis, quotes, indentation, … ▪ The program may ‘crash’
▪ No executable program is created by the ▪ The most difficult to find and correct
compiler o Even for experienced programmers
▪ Correct first error listed, then compile again
o Repeat until all errors are fixed

▪ Usually detected and highlighted by the IDE

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 93


Syntax Errors
▪ Syntax error are caught by the compiler
▪ What happens if you
o Miss-capitalize a word Print("Hello World!")
o Leave out quotes print(Hello World!)
o Mismatch quotes print("Hello World!')
o Don’t match brackets print('Hello'
▪ Type each example above in the IDE
o In the program source code
o In the interactive Python console
o What error messages are generated?

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 94


Logic Errors
▪ What happens if you
o Divide by zero print(1/0)
o Misspell output print("Hello, Word!")
o Forget to output Remove line 2
▪ Programs will compile and run
o The output may not be as expected
▪ Type each example above in the IDE
o What error messages are generated?

Politecnico di Torino, 2021/22 COMPUTER SCIENCES 95

You might also like