1 of 27

Part 6

Intro to Programming

9.10.2025

2 of 27

Last Week

  • mixed types of items in lists
  • matrices
  • references
  • lists as arguments and return values
  • dictionary
  • tuple

3 of 27

Feedback from Part 5

  • When to use lists / tuples / dictionaries?
  • Why the tests fail even if my exercise works for me: note the "small" differences (print vs. return, print formatting etc.)
  • THIS: "On how I can deal with longer complicated tasks, though am getting better by every part." You ARE getting there!
  • Problem solving or creating larger programs seems difficult
  • Maybe a "cheatsheet" of list and string operations and similar would be nice → this is an excellent idea, I'm going to add it to the TODO list
  • Shallow or deep copy → maybe little bit too complicated for this course, but is an important issue e.g. with matrices

4 of 27

Reading files

Typical action in programs

Using text files is easy in Python

Other file formats may be more complicated

5 of 27

Example

Reading file contents

with-block handles automatic closing of file

6 of 27

Handling files one row at a time

Method read reads the contents of the entire file

Often, it's easier to read file one row at a time

This is easy with the for statement

7 of 27

CSV Files

Comma Separated Values

Several data points in one row, separated with suitable character (i.e. a separator)

8 of 27

Splitting strings

String can be splitted into a list with the split method

The separator character is provided as an argument

9 of 27

Reading same file several times

File can be read in with-block only once

10 of 27

Removing extra characters

Reading data from file also returns line feed characters

Easiest way to get rid of these is to use the strip method

11 of 27

Creating a file

New file can be created by providing character 'w' as the second argument for the open function

Note, that if file exists, is will be overwritten!

12 of 27

Writing to file

Lines can be written to the file with the write method

Please note, that the linefeeds must be added manually

13 of 27

Appending data to file

New data can be appended after the existing data in the 'a' file mode

14 of 27

Writing a CSV file

15 of 27

Emptying and removing files

16 of 27

Validating inputs

Typical examples of erroneous input data:

  • missing or empty input values in mandatory fields, such as empty strings when the length of the string is critical
  • negative values where only positive values are accepted, such as -15 as the amount of an ingredient in a recipe
  • missing files or typos in filenames
  • values that are too small or too large, for example when working with dates and times
  • invalid indexes, such as trying to access index 3 in the string "hey"
  • values of a wrong type, such as strings when integers are expected

17 of 27

Validating input (2)

It's often possible to prepare for erroneous data

Inputted data can be validate with conditions

18 of 27

Exceptions

Some errors are difficult to check with conditional statements only

Exceptions are errors that occur during the execution of programs

19 of 27

Preparing for exceptions

Exceptions can be caught with try-except structure

20 of 27

Typical exceptions

ValueError

TypeError

IndexError

ZeroDivisionError

Exceptions in file handling

21 of 27

More than one except block

If more than one error may be thrown in the try block, it can be followed with more than one except block

22 of 27

Undefined exception

All possible exceptions can be caught by not defining the exception in the except block

Note, that this also catches the errors made by the programmer

23 of 27

Passing exceptions

Exception thrown in a function is passed back to the caller of the function

24 of 27

Throwing exceptions

Exception can be thrown manually with keyword raise

This is a good method for handling e.g. invalid parameters in functions

25 of 27

Local variables

Variables defined in functions are only visible inside functions

26 of 27

Keyword global

Variables defined in the main program can be used inside functions with keyword global

This is, in general, a bad idea. Instead of side effects, the functions should return a value

27 of 27

Next week

Modules

Random

Handling times and dates

More file handling

Own modules

More about Python's features

Few words about the exam