Lab SSE1
Lab SSE1
1
INTRODUCTION TO PYTHON
4
BASIC PACKAGES MANAGING
•Install package
pip install package
•Uninstall package
pip uninstall package
•Snapshot installed packages in requirements.txt
pip freeze > requirements.txt
•Install all packages snapshotted in requirements.txt
pip install -r requirements.txt
•Now you can import and use this packages in your project
5
ANACONDA
Anaconda is a distribution of Python that aims to simplify package management
and deployment, suitable for Windows, Linux, and macOS.
Package versions in Anaconda are managed by conda, an open source, cross-
platform, language-agnostic package manager and environment management
system
Conda analyses the current environment including everything currently installed,
works out how to install/run/update a compatible set of dependencies
6
PYTHON IDE + MINICONDA = PYCHARM
Pycharm offers configurable python interpreter
and virtual environment support.
8
INSTALLATION 2/2
•Accept the JetBrains Privacy Policy
9
NEW PROJECT
Create a new project. Name it “first_project”.
10
GUI
Menu Toolbar Run/Debug buttons
Project view
Editor
11
INTERPRETER: DEFAULT CONFIGURATIONS
Is there a red X here?
12
PYTHON BASICS
13
INDENTATION
In python code blocks are not surrounded by curly brackets. Just use
the correct indentation!
14
BUILT-IN TYPES
Python is dynamically typed, i.e. types are determined at runtime.
Variables can freely change type during the execution.
In python there are a lot of built-in types, the most notables are:
•Boolean (bool)
•Strings (str)
•Numbers (int, float)
•Sequences (list, tuple)
•Mapping (dict)
15
VARIABLES
Variables can be assigned to given values…
pi = 3
name = “Pippo"
…Even multiple variables at once via iterable unpacking. Click for more
on iterable data structures (e.g. lists, tuples…)!
pi, name = 3, “Pippo"
first, second, third = SomeSequence
In Python everything is stored and passed as reference with the only
exception of Numbers.
a = [1, 2, 3]
b=a
b[0] = 5 # now a = [5, 2, 3]
16
CONDITIONAL INSTRUCTION 1/2
Simple conditional instruction with the if keyword.
17
CONDITIONAL INSTRUCTION 2/2
The if-else statement is used as it follows
18
TRY IT YOURSELF!
19
LOOPS 1/2
There is no do-while loop, just while and for loops
20
LOOPS 2/2
zip() combines one-by-one the elements of two or more iterables
For efficient loops an iterable object can leverage the itertools packages.
The simplest iterable can be a list created via list comprehension
21
FUNCTIONS: DEFINITION
A new function can be defined by using the keyword def
22
FUNCTIONS: POSITIONAL ARGUMENTS
A variable number of arguments can be defined via the * symbol
23
FUNCTIONS: KEYWORD ARGUMENTS
A function using keyword arguments needs to have the ** symbol as
last argument.
You can also pass a dict of keyword arguments using the symbol **
while calling the method
24
TRY IT YOURSELF!
Modify the print_hi function to accept a dict as an argument. Print:
• the first number squares, if number is greater than zero
• "Just a zero?!" if number is zero
• «Hi, my name is name», otherwise
def print_hi(**kwargs):
if kwargs["number"] > 0:
squares = [i**2 for i in range(0, kwargs["number"])]
for elem in squares:
print(elem)
elif kwargs["number"] == 0:
print("Just a zero?!")
else:
print("Hi, my name is " + kwargs["name"])
if __name__ == '__main__':
argv = dict(name="Luca", number=0)
print_hi(**argv)
25
CLASSES: METHODS
In python classes are defined with the class keyword. Class methods
are defined with the def keyword. Class method have the first
argument equal to self, in contrast with static method.
However, unless you want to use a decorator, Python does not know
about static/non-static methods, it is all about notation!
26
CLASSES: ATTRIBUTES
In python you can create, modify and retrieve instance attributes
using the dot (.) selector on the instance reference. You can create and
assign an instance attribute everywhere in a class method.
You can create class attributes specifying them after the class
declaration. You can modify and retrieve class attributes using the dot
(.) selector on the class reference
27
CLASSES: VISIBILITY
In python there is no such thing as a private method or attribute.
Everything is public. The naming convention for “private” methods
and attributes is to precede their name with the _ symbol.
28
CLASSES: CONSTRUCTOR
In python the construct function is named __init__ and it is called at
object instantiation.
You can specify one or more arguments. The first argument is the
object instance reference.
29
CLASSES: INHERITANCE
You can extend a base class with another specifying the base class
between the parenthesis at class definition. In order to get the base
class reference you need to use the super() function.
30
TRY IT YOURSELF!
Include the modified version of print_hi in the class Person, use the class attributes,
and derive the new class Student!
31
MORE ON CLASSES: DATA MODEL
The are a lot of built-in functions provided by the base class of all the
classes object. Each of which provide a specific behavior, a few are:
•__len__ (self)
Returns the “length” of the object (called by len())
•__str__ (self)
Returns the object as a string (called by str())
32
WORKING WITH EXISTING PROJECTS
Pycharm provides different functionalities for project and code
navigation.
Navigate the project with specialized project views, use shortcuts, jump
between files, classes, methods and usages.
33
OPEN A PROJECT
1. Unzip “simpleClassifier.rar”
34
ALTERNATIVE INTERPRETER CONFIGURATION
File > Settings > Project > Python interpreter > Show all
4
2
8
35
PROJECT REQUIREMENTS 1/2
The requirements are all the packages that our software needs to run properly. Those
can be installed with a PyCharm plugin.
Double click on “requirements.txt” in the Project View > Install Plugin
Once the plugin is installed click on “Install requirements”, select all and click install.
36
PROJECT REQUIREMENTS 2/2
• If everything goes smoothly you might see the package installation status progress
at the bottom of the GUI.
• Once the package installation is done a notification appears, but it is still NOT
possible to go to next step, at least until PyCharm has finished "Updating
skeletons".
• Each package can also be installed via GUI or with the Terminal by using
• “pip install <name_lib>” to install a single package
• “pip install –r requirements.txt” to install the packages on the requirements.txt
37
CHECK THE PACKAGES AVAILABLE
It can take some minutes to complete the requirements installation. A restart may be
required before moving to the next step!
Once done you can see (add, eliminate and upgrade*) the packages and libraries
available in your virtual environment by checking :
File > Settings > Project > Python Interpreter
38
CONFIGURE THE FIRST RUN
1. Click “Add configuration” 3. Select “main.py” in your project folder.
2. Select “Python”
4. Click OK
39
RUN IT! Click RUN
41