«Everybody should learn to program a computer,
because it teaches you how to think»
– Steve Jobs
Alberto Clerici, Maurizio De Pra, M. Chiara Debernardi, Davide Tosi
Contents
1 Introduction to computer programming
1.1 Same logics, different languages
1.2 Computer and data recording
1.3 How a program works
1.4 Evolution of languages
1.5 The program development cycle
1.6 Algorithms and flow charts
1.7 Why Python?
2 Introduction to Python
2.1 What is Python?
2.2 How to install Python
2.3 IDLE: editor and shell
2.4 Writing, saving and running a program
2.5 First notions of Python syntax
2.6 Comments and docstring
2.7 Escape codes
2.8 Built-in functions
2.9 Help and other kinds of support
3 Calculations and data output
3.1 Math operators and calculations
3.2 Parentheses and operator precedence
3.3 Mathematical formulas
3.4 Data output
3.5 Functions for calculations
4 Variables and data types
4.1 Variables and assignment statements
4.2 Variables naming conventions
4.3 Reassignment
4.4 Data types
4.5 Mixed data types expressions
4.6 Type conversion functions
4.7 The input function
5 Decision-making structures and Boolean expressions
5.1 The if statement
5.2 Boolean expressions and relational operators
5.3 The if-else statements
5.4 The if-elif-else statements
5.5 The logical operators and, or, not
5.6 The pass statement
6 Iterative constructs
6.1 Introduction to loops
6.2 The while loop
6.3 The for loop
6.4 The range function
6.5 Nested loops
6.6 The break and continue statements
7 Functions
7.1 Introduction to functions
7.2 Defining and calling a function
7.3 Function arguments
7.4 Productive functions
7.5 Local variables and global variables
7.6 The docstring
7.7 Functions with loops and conditional statements
8 Strings and lists
8.1 Sequences
8.2 Strings
8.3 Operations on strings
8.4 Lists
8.5 Operations on lists
8.6 Indexing
8.7 Slicing
8.8 Functions and methods of strings
8.9 List functions
8.10 List methods
8.11 Traversing of strings and lists
8.12 Two-dimensional lists (lists of lists)
9 tuples and dictionaries
9.1 Tuples
9.2 Tuple operators, functions and methods
9.3 Traversing tuples
9.4 Dictionaries
9.5 Dictionary operators, functions and methods
10 File access and error handling
10.1 File access
10.2 Reading data from a file
10.3 Writing data in a file
10.4 Error handling in Python
10.5 The statements try… except
10.6 Debugging
11 Python libraries
11.1 Definition of module
11.2 The Python standard library
11.3 Building our own modules
11.4 Third-party libraries
11.5 Expanding the potential of Python
12 Classes, attributes and methods
12.1 Object-oriented programming
12.2 The objects: classes and instances
12.3 Working with custom classes
12.4 Inheritance
Acknowledgements
Selected bibliography