You are on page 1of 28

COMPUTER SCIENCE

code 30424 a.y. 2018-19

Lesson 9
Introduction to computer programming

SEDIN
IT Education Services Center
2

The second part of the course


Is divided into 5 weeks, dedicated to programming with Python:

Today

Les 9 Les 11 Les 12 Les 13 Les 15


Les 10 Pr 5 Pr 6 Les 14 Pr 7

Lab 7 Lab 8 Lab 9 Lab 10 Lab 11


2PE
simulation

Second
partial
3

Objectives of the lesson


• Understand the basic concepts related to programming, its
objectives and its importance

• Getting to know Python, install it and type the first instructions


4

What is programming?
• It is basically teaching a machine how to perform a certain task
or solve a certain problem (may it be simple or very complex)

• To do it, the computer needs precise instructions

• To provide these instructions the programmer must possess a


series of technical skills (know the logic of programming and
the specific language) and soft skills (computational thinking,
problem solving, creative thinking)
5

Why programming at Bocconi?


• Successful companies are increasingly interested in people who are able
to understand computer programming and to apply their knowledge to
several environments and disciplines
management, marketing, sales, administration, …

• Knowledge of computer programming is not only useful to programmers,


but also for every day tasks and in support of business decisions
greater effectiveness in several fields, such as budgeting and financial control, data analysis and
interpretation

• Learning the basics may facilitate the possible approach to any


programming language
6

The programming process

Requisites Analysis

Design

Implementation

Debugging Test Execution


7

Algorithms
An algorithm is a process that allows the user to solve a problem through a
finite number of simple steps. Example: cake recipe
Beginning

• Sugar + Eggs
• Weigh the ingredients • Add flour
Ingredients
• Add butter
• Mix the ingredients • Add baking soda

• Pour in a baking pan


Mixing ingredients • Put the pan in the oven
and baking
Leave it
• Verify if cake is done Is it done?
in the oven
NO

• Get it out of the oven YES


End

Top-down approach:
from general to specific actions
8

Implementing an algorithm
Start
(click of a button)
Use Scratch to implement the algorithm

Say “Hello!”

Step forward

Make a sound

Repeat
Step back
10 times

Make a sound

Say “Yeah!”

End
9

Data management in a computer


Input
&
Output CPU Memory
devices

BUS

CPU and memory can only perform very simple operations, managing data in machine
language. Data are represented with Binary Notation. e.g.:

• A number: 10011101 = 20 + 22 + 23 + 24 + 27 = 1 + 4 + 8 + 16 + 128 = 157


• A character: 01000001 = 65 = A (ASCII code)
• A pixel: 10100011 = yellow screen pixel
10

Machine language
• In the Fifties, programs were all written with 0 and 1
• Instructions were long sequences of bits (binary digits) and corresponded
to very simple actions

• Writing a very simple program required large amounts of time and


extremely specialized technicians

To deepen this topic:


https://www.youtube.com/watch?v=bGk9W65vXNA
(the story of ENIAC, that is the ancestor of our smartphones)
11

Programming languages
• A programming language is an interface for giving a computer the
instructions needed to solve a problem

• Many different programming languages exist: they have different syntax,


but basically do the same things and share most of the logic and rules

Programming languages enables the


communication between man and machine,
like natural languages do between individuals

Very often algorithms are used to outline ideas in a pattern of instructions


before translating them into a specific programming language
12

What is a software program?


It is a sequence of statements through which a computer executes an
elaboration (ex. polynomial equations, mobile apps, autonomous driving
cars, …)

The computer receives inputs (from a keyboard, a file, …) and provides


outputs (to screen, file, network, …) by means of simple operations:
Billions of LOCs
• mathematical (add, subtract, multiply, …)
• conditional (if-then-else, …)
• loops (while, for, …) Few LOCs

You can think of programming as the process of breaking a large complex


task into smaller and smaller subtasks until the subtasks are simple enough
to be performed with one of these basic instructions
13

Examples
Software for self-driving cars

100M LOCs

Google services
2B LOCs
14

Example of a program in Python


Beginning
(description)

Import of the
needed module

Input
coefficients

Compute
discriminant

Compute solutions

Show output

End
15

Evolution of programming languages


Natural
language
Going through decades,
programming languages bla, bla,
bla, …
have evolved towards
natural language and got
far from 0 and 1 alone
High-level languages
(Java, C, Python, …)

Assembly

Machine
Language
16

Talking to the machine


Even the most sophisticated
language has to be translated CPU

into machine language (0/1)


to be understood by the
machine

Two different modalities:

Compilation: translation of the entire


Program program before it is executed

Interpretation: translation and immediate


execution of single instructions
17

What is Python
• Python is a high-level interpreted, interactive, object-oriented programming
language
• It was conceived by Guido Van Rossum in 1989 and released in 1991, with
the aim to correct the defects present in other languages and to take their
strengths. The name Python derives from the passion of Guido for the group
of British comedians Monty Python
• Since then Python has grown and has found increasing popularity, first in
Web development and, especially in recent years, in the field of data science
• It is an open source language, constantly improved by the developer
community, and completed by several libraries

Info: https://www.python.org/about/
18

Popularity of programming languages

Source: TIOBE Programming Community Index


19

What is Python used for?


• Python can easily be used for small, large, online and offline projects. Today,
the best options to use Python are web development, data analysis and
simple scripting (e.g. automating emails or browser actions)

• Python is also used in machine learning, computer vision (e.g. face


detection), robotics (e.g. Raspberry Pi), game development, web scraping,
and GUI development

• Python can be used for creating prototypes of applications, that later will be
fully developed with more complex or specific languages
20

Python success stories


• Contemporary start-ups and big corporations such as Dropbox and Instagram are using
Python to build their sites and services
• Google uses Python in its well-known search engine, in several web services and apps
• YouTube uses Python to offer several features in its web site
• Mozilla corporation (Firefox web browser) uses Python for its web services
• Data management and analysis at CERN for experiments to find the first direct evidence of
the existence of the Higgs Boson was made with Python
• ESA (European Space Agency) is heavily using Python in preparing 2020 mission to Mars
• ILM (Industrial Light & Magic) has been using Python since mid-1990s
to control and automate the production of computer-generated film
sequences (e.g. in the second Star Wars trilogy)
21

Where do we start from?


• The official reference site for download and documentation
regarding Python is www.python.org

• The latest version of


Python is available in
the Download section
22

Installing Python
Check the version of the operating system/system type to choose the right download

If Python is already installed, the existing


Mac version is updated to the latest release

Win
64

Win
32

IMPORTANT!
23

IDLE: editor and shell


IDLE (Integrated Development and Learning Environment) is the Python
programming environment, which includes two different components:

• shell (or interpreter, or console), which is used in the interactive mode


(or "command line")
• editor which is used
in the script mode

Shell

Editor
24

Opening IDLE
Opening IDLE from the operating system menu will open the shell window

For our first simple examples we will work in the shell, afterwards we will
move to the editor, that is where we really write programs
25

IDLE customization
In the shell:

Options -> Configure IDLE

We can for example enlarge the font


26

Not just IDLE


IDLE is the simplest and most basic tool for using Python, and we will use
this in our course

You can use other tools, in addition to IDLE, to develop in Python. The most
popular are:
• Anaconda: www.anaconda.com
• Pythonanywhere: www.pythonanywhere.com
• Atom : https://atom.io/
27

Files of the lesson


In the zip file 30424 ENG - 18 - Lesson 9.zip you will find this file:
• 30424 lesson 9 slides.pdf: these slides
28

Book references
Learning Python:
Chapters 1, 2.1, 2.2, 2.3

You might also like