logo sapienza

Introduction to computer programming
2023-2024

Teacher: Paolo Franciosa
3 CFU


Goals Program Teaching resources
Lectures timetable Office hours Exams
Lectures, with content details


For comments, please contact the teacher at
paoloA_DOT_HEREfranciosaAT_SYMBOLuniroma1A_DOT_HEREit


Office hours

Each Tuesday from 10:00 to 12:00 in building CU002, fourth floor, room 9). Please fix appointments via e-mail.
Appointments in other hours can be fixed via e-mail.

back to top


Goals

The main goal of the course is to introduce students to computer programming, giving basic skills in Python programming language.

At the end of the course the students will be able to solve simple problems by coding an algorithm in Python, using a user friendly IDE.

back to top


Program

Programming in Python

Encoding of text and numbers

Development tools

back to top


Teaching resources

Suggested books

Programming tools

Virtual machine installed in via Tiburtina labs

By running virtualization software VirtualBox è it is possible to replicate on any PC the same environment available in via Tiburtina labs.
Once Virtual Box has been installed (available at https://www.virtualbox.org), it is possible to run the DSS virtual machine, that can be downloaded from https://www.massimolauria.net/informatica2021/vm.html.

More links

A tutorial on Python programming tools http://www.allendowney.com/wp/books/think-python-2e/

back to top


Exams

The exam consists of a practical programming test in Python to be taken on the computer in the same way as during the lab exercises. The test will be followed by a possible interview at the discretion of the teacher.

It is recommended to practice the test as described during the laboratory lectures. The test will be taken in via Tiburtina labs on the DSS virtual machine. It is recommended to practice using this machine by downloading the virtual machine as described above. Only the synoptic table available here in PDF version can be used during the programming test.

Register to December 15, 2023, exam test

More exercises are available here

back to top


2023-2024 lectures

September 29, 2023
14:00-18:00
October 6, 2023
14:00-18:00
October 13, 2023
14:00-18:00
October 20, 2023
14:00-18:00
October 27, 2023
14:00-18:00
November 10, 2023
14:00-18:00
November 17, 2023
14:00-18:00
December 1, 2023
14:00-18:00
register to
December 15, 2023
14:00-18:00
Exam test

For comments, please contact the teacher at
paoloA_DOT_HEREfranciosaAT_SYMBOLuniroma1A_DOT_HEREit

back to top


September 29, 2023
Topics
  • Introduction
  • Problems vs. Algorithms
  • Algorithm executors, programming languages, Von Neumann architecture
  • Examples of algorithms
  • Programming languages: compilers vs. interpreters
  • Syntax errors, semantic errors, exceptions

  • Hierarchical file systems: disks, file, directories
  • Linux shell (or Terminal in Mac OS, or Command prompt in Windows)
  • Navigating in the file system: commands ls (or dir in Windows), command cd. Absolute and relative paths
  • Interactive use of Python 3 interpreter
  • Arithmetic operators, expressions, variables, assignments
  • Simple problems that can be solved in the interactive mode
  • Function type(...)
Material

A recording of September 29, 2023 lesson is available at this Zoom link Passcode: =68MA#&7
Due to technical problems, this recording only contains the audio part.
This recording is for personal use only, and is reserved to Sapienza students. Dissemination of this link to persons outside the Statistics degree and master degree programs of Sapienza is not allowed.

back to lectures list


October 6, 2023
Topics

Calling methods and using the returned value

Defining methods: the def statement

Generating pseudo-random numbers with random.random() and random.randint(a, b)

Simple programs with input(...), print(...) and pseudo-random numbers

Conditional statement if

Relational operators

<     <=     >     >=     ==     !=

Lab activity

Activity 2

Material

A recording of October 6, 2023 lesson is available at this Zoom link Passcode: zt32&?EE
This recording is for personal use only, and is reserved to Sapienza students. Dissemination of this link to persons outside the Statistics degree and master degree programs of Sapienza is not allowed.

Sources and exercises
  1. Given three distinct float values, that represent the coefficient of a degree two equation, print its real solutions, if they exist, or print a message if they do not exist. The program must properly deal with any three real coefficients, also in case some of them are 0.
  2. Sort three numbers. Given three distinct numbers, print them in increasing order.
  3. Check whether three given numbers can be the measures of the sides of a triangle. For example, 3, 2 and 8 cannot be because ...
  4. As above, but in the positive case decide whether the triangle is rectangle, obtuse or acute-angled (Hint: check whether Pitagora's theorem holds, otherwise ...)
  5. Given the coordinates of three points, thus six numbers xa, ya, xb, yb, xc, yc, compute the length of the sides of the triangle having those vertices
  6. Let x1, y1, x2, y2, x3, y3, x4, y4 be the coordinates of four points. Compute and print the slopes (angular coefficient) of the line passing through (x1, y1) and (x2, y2), and the line passing through (x3, y3) e (x4, y4). notice: lines could be vertical

back to lectures list

October 13, 2023
Topics

Activation record stack. Local variables.

Parameter passing. Absence of side effects on simple (immutable) parameters.

Cycle statement while

Exercises on cycles and conditional statements

Lab activity

Activity 3

Material

A recording of October 13, 2023 lesson is available at the following links:

These recordings are for personal use only, and are reserved to Sapienza students. Dissemination of these links to persons outside the Statistics degree and master degree programs of Sapienza is not allowed.

Sources and exercises
  • Write a program that shows how many times a sheet of 0.1 millimeter thickness must be folded in two for a thickness of 500 mm. to be reached. Print the thickness obtained after each folding.
  • Modify the previous program, printing only the number of folds needed. Only one number should be printed. Then try to reach the Earth-Moon distance, about 380000 km (380,000,000,000 mm, 3.8e+11 mm).
  • Modify the previous program by defining a function that takes as parameters the thickness of the sheet and the distance it is intended to achieve, and returns the number of folds required. Write a program that calls the function just defined with various parameter values.
  • Find out what this program does ignoto.py
  • Modify program ignoto.py so that it also works when a and/or b are negative.
  • Write a program that, given two natural numbers a and b, prints ab, without using the power operator **
  • Define a function that returns the number of years it takes to reach a given capital from an initial capital and an annual interest rate. At the end of each year, the accrued interest is added to the capital, so that in the following year the interest added to the capital also gives rise to interest.
  • Check whether a natural number is prime, defining a function that has a natural n as a parameter and returns True (in case it is prime) or False (otherwise). To check whether a number a is divided by b it suffices testing whether a % b is zero.
  • Function that given a natural number returns the number of its divisors, excluded 1 and the number itself.

back to lectures list

October 20, 2023
Topics

Sequences

for ... in ... statement

Using range(...)

Simple programs with for cycles and conditional statements

Material

A recording of October 13, 2023 lesson is available at this Zoom link Passcode: !9RMe8mD

This recording is for personal use only, and is reserved to Sapienza students. Dissemination of this link to persons outside the Statistics degree and master degree programs of Sapienza is not allowed.

Sources and exercises
  • Given a natural number n, print a full square of stars with size n. The square is made by n rows, each row containing n stars.
      **********
      **********
      **********
      **********
      **********
      **********
      **********
      **********
      **********
      **********
    
    
  • Given a natural number n, print the border of square with size n, as follows:
    
      ************************
      *                        *
      *                        *
      *                        *
      *                        *
        . . . . . . . .
        
      *                        *
      ************************
    
    
  • Given a natural number n, print a triangle (full) of stars as follows. The triangle consists in n rows, with growing length from 1 to n.
      *
      **
      ***
      ****
       . . .
      
      *********
      **********
    
    
  • Print the sum of the first n items in the harmonic series: 1/1 + 1/2 + 1/3 + ... + 1/n

  • Print the Pytagorean table n × n assuming that n is in [1,18]

Pytagoric triples

Write a program that given a natural number n prints all triples x, y, z such that x^2 + y^2 = z^2, with x, y, z ≤ n. and x ≤ y

For n = 20 the only solutions are:

3^2 + 4^2 = 5^2
5^2 + 12^2 = 13^2
6^2 + 8^2 = 10^2
8^2 + 15^2 = 17^2
9^2 + 12^2 = 15^2
12^2 + 16^2 = 20^2
	

Solution: cercaTernePitagoriche.py


Function that, given two lists a, b of numbers with the same length, returns the scalar product
a_0 * b_0 + a_1 * b_1 + a_2 * b_2 + ..... + a_(n-1) * b_(n-1)

print(scalar([1, 4, 3, 0], [3, 0, 5, 5])) should give 18

Function that, given a list, returns True if the list is in decreasing order, False otherwise


Function that, given a natural n, returns the list containing the first n elements of the Fibonacci sequence.
Start by [1, 1], add element s[i] as s[i-2] + s[i-1]

s = fibonacci(10)
print(s) gives [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Function that, given two lists a, b of numbers with the same length, returns the sum of the lists, i.e., a list c of the same length such that c_i = a_i + b_i
Builds and returns the list c


Function that, given a list x, separate elements larger that the average from elements smaller or equal to the average

def separate(seq):
    small = []
    large = []
    ......
       ....  here you append to small or to large
    return small, large

x = [..., ... , .]
s, l = separate(x)
print(s, l)

back to lectures list

October 27, 2023
Topics

List parameters can be modified by a function

Exercises on lists and cycles.

Slicing, methods available in class list

Material

A recording of October 27, 2023 lesson is available at this Zoom link Passcode: kh28cRA^

This recording is for personal use only, and is reserved to Sapienza students. Dissemination of this link to persons outside the Statistics degree and master degree programs of Sapienza is not allowed.

Sources and exercises

Activity 5

Solutions are proposed in solutionsActivity5.zip

back to lectures list

November 10, 2023
Topics

List comprehension

Lists of lists (matrices)

Exercises on lists of lists

Material

A recording of November 10, 2023 lesson is available at this Zoom link Passcode: .Q^hTL9*

This recording is for personal use only, and is reserved to Sapienza students. Dissemination of this link to persons outside the Statistics degree and master degree programs of Sapienza is not allowed.

Sources and exercises

Activity 6

Solutions are proposed in solutionsActivity6.zip

back to lectures list

November 17, 2023
Topics

Dictionaries. Accessing and adding pairs

Scanning dictionaries keys. Getting keys and values by keys(), values(), items() methods. Deleting pairs by pop(...).

Compute frequencies from a list

Material

A recording of November 17, 2023 lesson is available at this Zoom link Passcode: #Y^95F?M

This recording is for personal use only, and is reserved to Sapienza students. Dissemination of this link to persons outside the Statistics degree and master degree programs of Sapienza is not allowed.

Sources and exercises

Activity 7

Solutions are proposed in solutionsActivity7.zip

back to lectures list

December 1, 2023
Topics

Reading and writing text files

ASCII and UNICODE coding, UTF-8 representation

Opening a text file by open function

Writing by print(..., file = ...) or .write(...)

Reading lines by for loop.

The try ... except ...

statement.

Material

A recording of November 1, 2023 lesson is available at this Zoom link Passcode: 8ev1S.b+

Due to technical reasons, audio has only been recorded for the initial part of the lesson.

This recording is for personal use only, and is reserved to Sapienza students. Dissemination of this link to persons outside the Statistics degree and master degree programs of Sapienza is not allowed.

Sources and exercises
  • Function that, given two strings representing file names, copies the first file into the second.
  • Function that, given three file names and an integer threshold value, reads integer values from the first file, one per line. Values larger than the threshold are written in the second file, values smaller than or equal to the threshold are written in the third file.
  • Function that, given a string representing a file name, reads key-value pairs from the file and returns the dictionary containing those pairs. File lines are assumed to contain a string, a blank space, and an integer in each line.
  • Function that, given a string representing file name, reads the file in the same format as above and returns a dictionary. The same key may appear many times in the file: in the returned dictionary the corresponding values will be the sum of all values associated to that key in the file. For example, if the file contains:
    John 34
    Ann 25
    John 10
    Paul 18
    Frank 15
    Ann 10
    
    the returned dictionary should be: {"John": 44, "Paul": 18, "Ann": 35, "Frank": 15}

back to lectures list

December 15, 2023

Exam test will be given on December 15, 2023, in via Tiburtina 205, room 15.
To register, compile this Google form.

More exercises are available here
A solution to word count problem is word_count.py

back to lectures list