This repository is dedicated to the laboratory sessions of the course "Computer Sciences" of Politecnico di Torino.
As an assistance towards the students, we provide to you some indicative solutions to the laboratory sessions.
Also, these solutions represent the author's way to approach the problem, which may differ from the way you would approach and solve the problem. This does not render your solution wrong or inferior. Programming is a school of thought, and most of the times there are more than 1 correct solutions to a problem.
With that said, each set of solutions will be uploaded 2 weeks after the lab delivery. For example, the solutions for lab 1 will be uploaded during the delivery of lab3, the solutions for lab2 during the delivery of lab4 and so on.
-
Don't be afraid to use representative variable names! Remember that the readability of your code plays a key role to its understanding by other readers or even by you when you try to debug it. Use
nouns
for variables and for functions usefull sentences
. For example:minimum = find_minimum(integer_list)
. For functions specifically, if they return aboolean
value it is a good practice to have thehas_
oris_
as a prefix to their name (e.g.,if is_prime(number):
) -
Use capital letters for "global" variables and constants. For example:
PI = 3.14
-
Avoid using python3 keywords as variable names. In the case you still want to do it, end the variable name with a single underscore e.g.,
list_ = [1,2,3]
-
Use the boilerplate:
def main(): """ Your Code Here """ if __name__ == "__main__": main()
-
In case of handling exceptions, be specific and as acurate as posible with your
try except
blocks. Do not engulf huge code segments but only the point of failure. For example:- ✔️ do ✔️ :
try: with open("numbers.txt") as infile: """ read the file contents """ except FileNotFoundError: """ handle it """ """ do stuff with the just read file contents """ exit
- ❌ don't ❌ :
try: with open("numbers.txt") as infile: """ read the file contents """ """ do stuff with the just read file contents """ exit except FileNotFoundError: """ handle it """
- Always advise the pydocs for the existing exception classes and their hierarchy.
-
In the case that you are opening files using the plain
open()
function instead of thewith open()
statement, DO NOT FORGET to useclose()
as well when you are done with the file‼️ -
- Q: What is the very first line I see in some python3 scripts
#!/usr/bin/python3
? - A: Its called shebang
- Q: What is the very first line I see in some python3 scripts
-
It is a good practice to have a single point of exit in functions i.e., use a single
return
statement when this is possible and avoid having multiple cases. For example:- ✔️ do ✔️ :
def my_function(arg1,arg2): if (condition1): # handle this case elif (condition2): # handle this case as well else: # handle that one too and then... return # once
- ❌ don't ❌ :
def my_function(arg1,arg2): if (condition1): return this elif (condition2): return that else: return other
-
When coding functions do use
typing
support for arguments and return statements. For exampledef is_empty(a_list : list) -> bool: return len(a_list) == 0
- For arguments use the following syntax
arg : type
- For the return type use the following syntax
-> : type
- For arguments use the following syntax
-
Advise prof. Squillero's repository for extra information.
-
Bookmark https://docs.python.org/3/ as your main
goto
place for finding useful information about the standard library with convenient examples and use cases. -
Check PEP 8 -- Style Guide for coding conventions for the Python code comprising the standard library in the main Python distribution.
-
Check out Advent of Code by Eric Wastl.