Preparing Yourself for the Tests
It is recommended that you prepare yourself for the tests. The following should help you asses whether you need to study and practice more. It is not a sufficient preparation for the test.
Topics covered
Before the midterm
primitive data types (int, float, bool, str, NoneType), operators and their order, data type conversion
loops - the for loop (iterating n times, sequence traversal), the while loop (infinite loop), flow control
conditionals (if, elif, else)
function definition (fruitfulness, pure functions, side-effects), variable scope (namespaces), memory allocation (stack vs. heap)
exceptions (raising them, understanding tracebacks)
sequence data types - str, list, tuple (indexing, slicing, sequence operators)
dictionary and set data types - dict, set (traversals)
data type mutability (aliasing, shallow vs. deep copy), hashability, iterators
Additionally before the end-of-term
modules, packages, namespaces (code organization, scripts vs. libraries, import statement)
file reading and writing (with statement)
paths (current working directory, relative vs. absolute paths, os.path), directories (root, current and parent directory)
testing (doctest, unittest)
exceptions - handling (try and except statements, specifying exception types), instantiating vs. raising exception objects
instances - class definition, instance variables, method definition (__init__, __str__)
objects - state, mutability, equality and copy
class variables and methods, static methods, decorators (@property)
naming conventions, polymorphism (inheritance, operator overloading, duck typing)
Elementary micro-tasks
The following is a set of elementary tasks in Python. It is necessary to practice these tasks to get fast and comfortable programming them. It is the prerequisite of the midterm, end-of-term and exam test. It is recommended to repeat each task until you can consistently program each under 1 minute without making any mistake:
Build a list with powers of two of size 40 (i.e. [2**0, 2**1, 2**2, ..])
Build a list of numbers that are divisible by 2 and 3 simultaneously and are less than 100 (i.e. [6, 12, 18, ..])
Build a dictionary where the key is an integer between 1 and 10 and the value is its square root (i.e. 1 -> 1, 2-> 1.41, ..)
Build a string with consecutive integers up to 100, separated by a space (i.e. “1 2 3 4 5 ..”)
Write a function that returns a sum of its two parameters
Divide 224 by 2 repeatedly, until it cannot be divided without a reminder
Additionally before the end-of-term
Create a doctest for any function
Open a non-existing file and handle the exception by printing “path doesn't exist”
Create a class where the initializer assigns all its parameters to instance variables
Create your own exception subtype (inherit from existing type) and raise it
Basic tasks
These tasks already pose some challenge, so it is expected that you spend a bit more time on them, by testing and debugging your solution. You are expected to complete the task within 3-5 minutes on your first try, if you want to pass the tests.
Write a function that will return a list of alternating True and False values of specified size, which is a parameter of the function (i.e. [True, False, True, False, ..])
Replace each non-space character in a string with 'a' (“Hello world” → “aaaaa aaaaa”)
Additionally before the end-of-term
Create a module with one constant, print the constant from a script in a different file
Read all lines from a file, one-by-one
Add support for deep comparison for a class
Test-level tasks
Exam-level tasks
You are expected create readable (naming conventions), documented (docstrings), and tested (unittest) code. You should be able to complete each task in about 20 minutes, including documentation and unittest writing.
It may be helpful to get additional feedback about your code quality from VS Code directly. You can enable that by unchecking “Python Linting: Pylint Use Minimal Checkers” in File → Preferences → Settings.
Implement a function that converts CamelCase identifiers to snake_case
Create two packages. The first one will contain two modules: library
and main
. Module library
will contain one simple function definition that will be called in module main
. The second package will contain corresponding unittest modules (test_library
and test_main
).
Write a class Book
which have attributes author
and name
. Further implement two functions: save_book
which will save class attributes to a text file (you can choose the data format), and load_book
which will return initialized Book
instance.
Create a class Password
which will contain one string. Create a @property password_strength
, which will return the password strength - one of the values “strong”, “ok”, and “weak” (you can pick any reasonable criteria). Enable password concatenation with the plus operator. When converting the instance to string, the result should be "Password(****)" for an instance containing a 4-letter password (each character is converted to an asterisk).
Create a class NumericPassword
which will inherit from Password
. Ensure that the stored string contains numeric characters only. Make sure that all the functionality of Password
works for NumericPassowrd
as well. Hint: Use super()
to access Password methods, e.g. in __init__
.
Complex assignment for the revision lecture
Create a folder for this assignment which will contain a single package for the modules described below. Choose all names appropriately. At the end, every function and method should have a unittest. Place the unittest modules in the top-level folder, not in the package. Every module, class, function and method should have a docstring.
E-mail module
Create a module for working with emails.
Create a class that will hold a single e-mail. Its initializer should take a single argument.
If the class is initialized with an invalid e-mail, a custom exception (inherited from appropriate buil-in exception) is raised.
When this class is converted to a string, in the local part of the address (everything before the “@” character), the first three characters should be kept while the remeaining characters should be replaced with asterisks for privacy purposes. Example: "jenicto2@fel.cvut.cz" → "jen*****@fel.cvut.cz".
Implement a method that will return the top-level domain (e.g. “cz” or “com”) and make it a property of the class.
Enable equality test with both another e-mail instance and string
Implement a static method that gives a suggestion when an e-mail is taken. It takes an e-mail instance as an argument and returns another e-mail instance containing a modified e-mail (e.g. add “2” after the username, so that “jenicto@fel.cvut.cz” becomes “jenicto2@fel.cvut.cz”)
Create a second class for GMail whose initializer will take only the username (without the “@gmail.com”). Create a class variable to store the domain (i.e. “gmail.com”).
E-mail list module
Create another module that will work with e-mail lists.
Create a function that will store a list of e-mails into a folder, one e-mail per file. The function takes two arguments – a list of e-mail instances and a folder path.
Create a function that will load a list of e-mails from a folder. The only argument of this function is a folder path. If the folder does not exist, it returns an empty list.
Example module
Create a module (script) named “example”.
This module will execute a simple demo that will demonstrate how to use the package.