SlideShare a Scribd company logo
1 of 95
Download to read offline
Unit P1:
Introduction to
programming
A SHORT INTRODUCTION TO HARDWARE,
SOFTWARE, AND ALGORITHM DEVELOPMENT
This Photo by Unknown Author is licensed under CC BY-SA
Chapter 1
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
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
input
(data)
output
(results)
(computing /
elaboration)
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”
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Text
Data
Voice
Music
Images
Videos
01101100
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
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Programming
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
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Building
a program
The first ENGINEERING DESIGN course at Politecnico
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
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
8745
4102
18
Design
Formal
solution
Program
Problem
Solution
Politecnico di Torino, 2021/22 COMPUTER SCIENCES 19
Example of problem solving
▪ Problem: Compute the maximum
among A and B
▪ Solution: The maximum is the
larger number between A and B…
▪ Formal 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
Formal
solution
Problem
Informal
solution
20
Find the biggest number
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
7345 1302
5901
21
Find the biggest number
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
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
22
Find the biggest number
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
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
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
…
LOAD Reg1, Mem[1000]
ADD Reg1, 10
…
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Specific for a given architecture
(microprocessor)
35
Data abstraction
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
3
‘a’
Memory
Memory
addresses
-11.564
100
101
102
103
104
105
106
107
108
x; integer;
value=3
c; character;
value=‘a’
t; real;
value=-11.564
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
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
PROGRAM prova;
// test program
CONSTANTS
pi = 3.14159
coeff = 0.19
VARIABLES
x: INTEGER
y: REAL
c: CHARACTER
BEGIN PROGRAM
x = 2;
IF (y > 2) THEN y = x * pi;
PRINT x, y;
END PROGRAM
identifiers
KEYWORDS
Pseudo-instruction
Control
flow
instruction
Specify data types
Allocate memory cells
“Running”
part
Primitive
instructions
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
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Hardware
Software
45
Cooking vs Programming
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Hardware
Software
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
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
3.5
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
structure-1
structure-2
Sequence
structure
Politecnico di Torino, 2021/22 COMPUTER SCIENCES 51
condition
T F
If-Then-Else
structure-1 structure-2
Politecnico di Torino, 2021/22 COMPUTER SCIENCES 52
condition
T F
If-Then
structure
Politecnico di Torino, 2021/22 COMPUTER SCIENCES 53
condition
T
F
While-Do
structure
Politecnico di Torino, 2021/22 COMPUTER SCIENCES 54
condition
F
T
do - while
structure
Politecnico di Torino, 2021/22 COMPUTER SCIENCES 55
▪ Two Outcomes ▪ Multiple Outcomes
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Conditional Flowcharts
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:
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
International
Branch
Hawaii/Alaska
Branch
Lower 48
Branch
57
International
Branch
Hawaii/Alaska
Branch
Lower 48
Branch
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!
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
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
3.6
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
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
1.3, 1.4,
1.5, 1.6
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
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Program source code in
hello.py
(Compile and) execute
the program:
python hello.py
Program output:
Hello, world
73
Your first program
▪ Traditional ‘Hello World’ program in Python
o print is an example of a Python statement
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
hello.py
write
• IDE
• Text editor
run
• IDE
• Console
Program output
74
Writing/typing a Python program
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
▪ 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)
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
Politecnico di Torino, 2021/22
Program source code
Project files
Debugger/Output
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
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Project folder
79
Programming with the PyCharm IDE
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
New Python file
Example main.py
(may modify or
delete)
80
Programming with the PyCharm IDE
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Run
Type program
81
Programming with the PyCharm IDE
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Run again
shortcut
Program output
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
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Python console
86
On-line IDE: https://repl.it/
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Program output
Program source
Project files
Fantastic option for quick
examples, for testing program
fragments, for avoiding to
create a new project for a small
piece of code, …
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
COMPILE-TIME ERRORS
OR SYNTAX ERRORS
▪ Spelling, capitalization, punctuation
▪ Ordering of statements, matching of
parenthesis, quotes, indentation, …
▪ No executable program is created by the
compiler
▪ Correct first error listed, then compile again
o Repeat until all errors are fixed
▪ Usually detected and highlighted by the IDE
RUN-TIME ERRORS
OR LOGIC ERRORS
▪ The program runs, but produces unintended
results
▪ The program may ‘crash’
▪ The most difficult to find and correct
o Even for experienced programmers
Politecnico di Torino, 2021/22 COMPUTER SCIENCES
Errors
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

More Related Content

Similar to Unit P1 - Programming.pdf

Computer programming all chapters
Computer programming all chaptersComputer programming all chapters
Computer programming all chapters
Ibrahim Elewah
 
L1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdfL1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdf
MMRF2
 
Major project report
Major project reportMajor project report
Major project report
Praveen Singh
 
An introduction to_programming_the_microchip_pic_in_ccs_c
An introduction to_programming_the_microchip_pic_in_ccs_cAn introduction to_programming_the_microchip_pic_in_ccs_c
An introduction to_programming_the_microchip_pic_in_ccs_c
Suresh Murugesan
 

Similar to Unit P1 - Programming.pdf (20)

Embedded system by owais
Embedded system by owaisEmbedded system by owais
Embedded system by owais
 
Report on PLC traning
Report on PLC traningReport on PLC traning
Report on PLC traning
 
CHAPTER 1
CHAPTER 1CHAPTER 1
CHAPTER 1
 
Algorithm,Pseudocode,Flowchart.pptx
Algorithm,Pseudocode,Flowchart.pptxAlgorithm,Pseudocode,Flowchart.pptx
Algorithm,Pseudocode,Flowchart.pptx
 
Ds03 part i algorithms by jyoti lakhani
Ds03 part i algorithms   by jyoti lakhaniDs03 part i algorithms   by jyoti lakhani
Ds03 part i algorithms by jyoti lakhani
 
report
reportreport
report
 
Computer programming all chapters
Computer programming all chaptersComputer programming all chapters
Computer programming all chapters
 
L1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdfL1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdf
 
Major project report
Major project reportMajor project report
Major project report
 
Presentation on Computer Basics and architecture.pdf
Presentation on Computer Basics and architecture.pdfPresentation on Computer Basics and architecture.pdf
Presentation on Computer Basics and architecture.pdf
 
INTRODUCTION TO INFORMATION TECHNOLOGY -SARASWATHI RAMALINGAM
INTRODUCTION TO INFORMATION TECHNOLOGY -SARASWATHI RAMALINGAMINTRODUCTION TO INFORMATION TECHNOLOGY -SARASWATHI RAMALINGAM
INTRODUCTION TO INFORMATION TECHNOLOGY -SARASWATHI RAMALINGAM
 
Presentation1
Presentation1Presentation1
Presentation1
 
GOWTHAM REPORT
GOWTHAM REPORTGOWTHAM REPORT
GOWTHAM REPORT
 
ICF 7 Booklet type.docx
ICF 7 Booklet type.docxICF 7 Booklet type.docx
ICF 7 Booklet type.docx
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
C10ComputerEngg.pptx
C10ComputerEngg.pptxC10ComputerEngg.pptx
C10ComputerEngg.pptx
 
venkat_emb
venkat_embvenkat_emb
venkat_emb
 
VTU 18CPS13/23 CPPS module-1 PPT
VTU 18CPS13/23 CPPS module-1 PPTVTU 18CPS13/23 CPPS module-1 PPT
VTU 18CPS13/23 CPPS module-1 PPT
 
An introduction to_programming_the_microchip_pic_in_ccs_c
An introduction to_programming_the_microchip_pic_in_ccs_cAn introduction to_programming_the_microchip_pic_in_ccs_c
An introduction to_programming_the_microchip_pic_in_ccs_c
 
Lesson1 Contestada
Lesson1 ContestadaLesson1 Contestada
Lesson1 Contestada
 

Recently uploaded

Conjugation, transduction and transformation
Conjugation, transduction and transformationConjugation, transduction and transformation
Conjugation, transduction and transformation
Areesha Ahmad
 
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdfPests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
PirithiRaju
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Lokesh Kothari
 
Bacterial Identification and Classifications
Bacterial Identification and ClassificationsBacterial Identification and Classifications
Bacterial Identification and Classifications
Areesha Ahmad
 

Recently uploaded (20)

Feature-aligned N-BEATS with Sinkhorn divergence (ICLR '24)
Feature-aligned N-BEATS with Sinkhorn divergence (ICLR '24)Feature-aligned N-BEATS with Sinkhorn divergence (ICLR '24)
Feature-aligned N-BEATS with Sinkhorn divergence (ICLR '24)
 
Factory Acceptance Test( FAT).pptx .
Factory Acceptance Test( FAT).pptx       .Factory Acceptance Test( FAT).pptx       .
Factory Acceptance Test( FAT).pptx .
 
High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...
High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...
High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...
 
Zoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfZoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdf
 
CELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdfCELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdf
 
GBSN - Microbiology (Unit 3)
GBSN - Microbiology (Unit 3)GBSN - Microbiology (Unit 3)
GBSN - Microbiology (Unit 3)
 
9999266834 Call Girls In Noida Sector 22 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 22 (Delhi) Call Girl Service9999266834 Call Girls In Noida Sector 22 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 22 (Delhi) Call Girl Service
 
Justdial Call Girls In Indirapuram, Ghaziabad, 8800357707 Escorts Service
Justdial Call Girls In Indirapuram, Ghaziabad, 8800357707 Escorts ServiceJustdial Call Girls In Indirapuram, Ghaziabad, 8800357707 Escorts Service
Justdial Call Girls In Indirapuram, Ghaziabad, 8800357707 Escorts Service
 
Botany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfBotany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdf
 
Conjugation, transduction and transformation
Conjugation, transduction and transformationConjugation, transduction and transformation
Conjugation, transduction and transformation
 
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticsPulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
 
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdfPests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
 
Proteomics: types, protein profiling steps etc.
Proteomics: types, protein profiling steps etc.Proteomics: types, protein profiling steps etc.
Proteomics: types, protein profiling steps etc.
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdf
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
 
Bacterial Identification and Classifications
Bacterial Identification and ClassificationsBacterial Identification and Classifications
Bacterial Identification and Classifications
 
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls AgencyHire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
 
GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)
 
FAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
FAIRSpectra - Enabling the FAIRification of Spectroscopy and SpectrometryFAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
FAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
 

Unit P1 - Programming.pdf

  • 1. Unit P1: Introduction to programming A SHORT INTRODUCTION TO HARDWARE, SOFTWARE, AND ALGORITHM DEVELOPMENT This Photo by Unknown Author is licensed under CC BY-SA Chapter 1
  • 2. 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
  • 3. Computer Sciences Introduction Politecnico di Torino, 2021/22 COMPUTER SCIENCES 3
  • 4. 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
  • 5. Electronic Computer Politecnico di Torino, 2021/22 COMPUTER SCIENCES input (data) output (results) (computing / elaboration) 5
  • 6. 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
  • 7. Digital information: everything becomes “bits” Politecnico di Torino, 2021/22 COMPUTER SCIENCES Text Data Voice Music Images Videos 01101100 7
  • 8. 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
  • 9. 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
  • 10. Simplified View of a Computer’s Hardware Politecnico di Torino, 2021/22 COMPUTER SCIENCES 10
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. Politecnico di Torino, 2021/22 COMPUTER SCIENCES Programming 14
  • 15. 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 Politecnico di Torino, 2021/22 COMPUTER SCIENCES Building a program The first ENGINEERING DESIGN course at Politecnico 15
  • 16. Algorithms 1.7 Politecnico di Torino, 2021/22 COMPUTER SCIENCES 16
  • 17. 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
  • 18. Find the biggest number Politecnico di Torino, 2021/22 COMPUTER SCIENCES 8745 4102 18
  • 20. Example of problem solving ▪ Problem: Compute the maximum among A and B ▪ Solution: The maximum is the larger number between A and B… ▪ Formal 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 Formal solution Problem Informal solution 20
  • 21. Find the biggest number Politecnico di Torino, 2021/22 COMPUTER SCIENCES 7345 1302 5901 21
  • 22. Find the biggest number Politecnico di Torino, 2021/22 COMPUTER SCIENCES 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 22
  • 23. Find the biggest number Politecnico di Torino, 2021/22 COMPUTER SCIENCES 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 23
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. 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
  • 29. 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
  • 30. 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
  • 31. 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
  • 32. 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
  • 33. 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
  • 34. 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
  • 35. Which languages? – Examples ▪ High Level Language … if x > 3: x = x + 1 … ▪ Assembly Language … LOAD Reg1, Mem[1000] ADD Reg1, 10 … Politecnico di Torino, 2021/22 COMPUTER SCIENCES Specific for a given architecture (microprocessor) 35
  • 36. Data abstraction Politecnico di Torino, 2021/22 COMPUTER SCIENCES 3 ‘a’ Memory Memory addresses -11.564 100 101 102 103 104 105 106 107 108 x; integer; value=3 c; character; value=‘a’ t; real; value=-11.564 36
  • 37. 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
  • 38. Program example Politecnico di Torino, 2021/22 COMPUTER SCIENCES PROGRAM prova; // test program CONSTANTS pi = 3.14159 coeff = 0.19 VARIABLES x: INTEGER y: REAL c: CHARACTER BEGIN PROGRAM x = 2; IF (y > 2) THEN y = x * pi; PRINT x, y; END PROGRAM identifiers KEYWORDS Pseudo-instruction Control flow instruction Specify data types Allocate memory cells “Running” part Primitive instructions 38
  • 39. Problem Solving with Flow Charts Politecnico di Torino, 2021/22 COMPUTER SCIENCES 39
  • 40. 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
  • 41. 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
  • 43. Tools for Problem Solving Politecnico di Torino, 2021/22 COMPUTER SCIENCES 43
  • 44. 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
  • 45. Cooking vs Programming Politecnico di Torino, 2021/22 COMPUTER SCIENCES Hardware Software 45
  • 46. Cooking vs Programming Politecnico di Torino, 2021/22 COMPUTER SCIENCES Hardware Software 46
  • 47. 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
  • 48. 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
  • 49. Using Flowcharts to Develop and Refine Algorithms Politecnico di Torino, 2021/22 COMPUTER SCIENCES 3.5 49
  • 50. 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
  • 52. condition T F If-Then-Else structure-1 structure-2 Politecnico di Torino, 2021/22 COMPUTER SCIENCES 52
  • 53. condition T F If-Then structure Politecnico di Torino, 2021/22 COMPUTER SCIENCES 53
  • 55. condition F T do - while structure Politecnico di Torino, 2021/22 COMPUTER SCIENCES 55
  • 56. ▪ Two Outcomes ▪ Multiple Outcomes Politecnico di Torino, 2021/22 COMPUTER SCIENCES Conditional Flowcharts 56
  • 57. 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: Politecnico di Torino, 2021/22 COMPUTER SCIENCES International Branch Hawaii/Alaska Branch Lower 48 Branch 57
  • 58. International Branch Hawaii/Alaska Branch Lower 48 Branch 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! Politecnico di Torino, 2021/22 COMPUTER SCIENCES 58
  • 59. 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
  • 60. PyCharm Politecnico di Torino, 2021/22 COMPUTER SCIENCES 60
  • 61. 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
  • 62. 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
  • 63. Exercise Politecnico di Torino, 2021/22 COMPUTER SCIENCES 63
  • 64. Building Test Cases Politecnico di Torino, 2021/22 COMPUTER SCIENCES 3.6 64
  • 65. 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
  • 66. 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
  • 67. Introducing Pyton Politecnico di Torino, 2021/22 COMPUTER SCIENCES 1.3, 1.4, 1.5, 1.6 67
  • 68. 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
  • 69. https://www.python.org/ Politecnico di Torino, 2021/22 COMPUTER SCIENCES 69
  • 70. https://docs.python.org/ Politecnico di Torino, 2021/22 COMPUTER SCIENCES 70
  • 71. 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
  • 72. 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
  • 73. 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 Politecnico di Torino, 2021/22 COMPUTER SCIENCES Program source code in hello.py (Compile and) execute the program: python hello.py Program output: Hello, world 73
  • 74. Your first program ▪ Traditional ‘Hello World’ program in Python o print is an example of a Python statement Politecnico di Torino, 2021/22 COMPUTER SCIENCES hello.py write • IDE • Text editor run • IDE • Console Program output 74
  • 75. Writing/typing a Python program Politecnico di Torino, 2021/22 COMPUTER SCIENCES ▪ 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) 75
  • 76. 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
  • 77. The PyCharm IDE Politecnico di Torino, 2021/22 Program source code Project files Debugger/Output COMPUTER SCIENCES 77
  • 78. 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
  • 79. Programming with the PyCharm IDE Politecnico di Torino, 2021/22 COMPUTER SCIENCES Project folder 79
  • 80. Programming with the PyCharm IDE Politecnico di Torino, 2021/22 COMPUTER SCIENCES New Python file Example main.py (may modify or delete) 80
  • 81. Programming with the PyCharm IDE Politecnico di Torino, 2021/22 COMPUTER SCIENCES Run Type program 81
  • 82. Programming with the PyCharm IDE Politecnico di Torino, 2021/22 COMPUTER SCIENCES Run again shortcut Program output 82
  • 83. 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
  • 84. 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
  • 85. Python interactive mode Politecnico di Torino, 2021/22 COMPUTER SCIENCES 85
  • 86. Python interactive mode Politecnico di Torino, 2021/22 COMPUTER SCIENCES Python console 86
  • 87. On-line IDE: https://repl.it/ Politecnico di Torino, 2021/22 COMPUTER SCIENCES Program output Program source Project files Fantastic option for quick examples, for testing program fragments, for avoiding to create a new project for a small piece of code, … 87
  • 88. 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
  • 89. 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
  • 90. 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
  • 91. 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
  • 92. 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
  • 93. COMPILE-TIME ERRORS OR SYNTAX ERRORS ▪ Spelling, capitalization, punctuation ▪ Ordering of statements, matching of parenthesis, quotes, indentation, … ▪ No executable program is created by the compiler ▪ Correct first error listed, then compile again o Repeat until all errors are fixed ▪ Usually detected and highlighted by the IDE RUN-TIME ERRORS OR LOGIC ERRORS ▪ The program runs, but produces unintended results ▪ The program may ‘crash’ ▪ The most difficult to find and correct o Even for experienced programmers Politecnico di Torino, 2021/22 COMPUTER SCIENCES Errors 93
  • 94. 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
  • 95. 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