Skip to content
Snippets Groups Projects
Commit f02bc745 authored by Nicola Spallanzani's avatar Nicola Spallanzani
Browse files

update

parent 80915475
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
![](images/title_intro_python.png)
%% Cell type:markdown id: tags:
## Outlook
* Philosophy
* Environment
* Interpreter
* Built-in types and operations
* Program file
* Built-in containers
* Tuples
* Lists
* Sets
* Dicts
%% Cell type:markdown id: tags:
## Outlook
* Flow control contructs
* if-elif-else
* For loops
* While loops
* Comparison and logical operators
* Object reference
* File I/O
* Functions
* Argument Passing
* Doc string
* Lambda functions
%% Cell type:markdown id: tags:
## Outlook
* Introspection
* Functional programming
* sort
* lambda functions
* filter, map
* String formatting
* Old and new style
* Modules
* Packages
* Program arguments
%% Cell type:markdown id: tags:
## Outlook
* Standard Library
* re
* datetime
* math, random
* os.path, glob
* sqlite3, csv, json
* sys, os, time
* argparse
* logging
* subprocess
%% Cell type:markdown id: tags:
## Outlook
* Scientific Modules (very quick introduction)
* Numpy
* Matplotlib
* Scipy
* Pandas
%% Cell type:markdown id: tags:
## Outlook (extra)
* Classes
* Instances
* Methods
* Attributes
* Inheritance
* Iterables and Iterators
* Error handling
%% Cell type:markdown id: tags:
# introduction
%% Cell type:markdown id: tags:
## philosophy
Python is a programming language with many good features:
* Very easy to use
* Easy to learn (looks like pseudo-coding)
* Excellent readability (there is only one way to do anything)
* Excellent portability
%% Cell type:markdown id: tags:
## philosophy
Do not call it a “scripting language”! Although it can be used as such, it is much more.
It is an high level language, modern, complete, with which it is possible to realize highly complex software.
It is an interpreted language, but it would be more appropriate to call it "dynamic language".
%% Cell type:markdown id: tags:
## philosophy
A dynamic language is a high­level language in which many of the controls are executed run-time, while in other languages they are done at compile time.
Actually python "compile" the source into bytecode that runs on a virtual machine (like Java).
%% Cell type:markdown id: tags:
## philosophy
It is a multiparadigm language:
* Imperative
* Object-oriented
* Functional
* Structural
* Aspect-oriented
* Design by contract (with an extension)
* ...
%% Cell type:markdown id: tags:
## The Zen of python
%% Cell type:code id: tags:
``` python
import this
```
%% Output
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
%% Cell type:markdown id: tags:
## disavtanges?
Python is often considered a slow language. To a large extent this is true: it is slower than Java, for
example.
But the speed is not always the bottleneck. Often, the management of the complexity is a problem more important than speed.
However, there are several ways to make faster the "critical" parts of a python program.
%% Cell type:markdown id: tags:
## performance
“People are able to code complex algorithms in much less time by using a high-level language like
Python (e.g., also C++). There can be a performance penalty in the most pure sense of the
term.”
%% Cell type:markdown id: tags:
## optimization
"The best performance improvement is the transition from the nonworking to the working state."
--John Ousterhout
"Premature optimization is the root of all evil."
--Donald Knuth
"You can always optimize it later."
-- Unknown
%% Cell type:markdown id: tags:
## python2 / python3
There are two main branches of development of python:
* python2: it is a “dead” branch, that is maintained, but that will not undergo updates / evolution
* python3: it is the new python, actually still little used compared to python2.
* python2: it is a “dead” branch, that is maintained, but that will not undergo updates / evolution;
* python3: it is the new python.
The differences are not huge, also there is the utility 2to3 to convert code from the old to the new python.
%% Cell type:markdown id: tags:
## python3
* We will take care mainly of python3.
* I will try to point out the main differences.
%% Cell type:markdown id: tags:
# environment
%% Cell type:markdown id: tags:
## environment
With the installation of the Python interpreter it comes also the so called standard library.
In order to use it you need to import the modules using the keyword **import**.
%% Cell type:code id: tags:
``` python
import math
print(math.pi)
```
%% Output
3.141592653589793
%% Cell type:markdown id: tags:
## virtualenv
Virtualenv is a tool to create isolated Python environments.
It allow a user to install python modules in its privare area without need **root** permissions.
Virtualenv has one basic command:
```
$ virtualenv ENV
```
Where ```ENV``` is a directory to place the new virtual environment.
%% Cell type:markdown id: tags:
## virtualenv
A virtual environment needs to be activated before using it:
```
$ source ENV/bin/activate
```
on Windows:
```
> env\Scripts\activate
```
Simply type ```deactivate``` to exit from the virtual environment.
%% Cell type:markdown id: tags:
## virtualenv
At the prompt will be added a prefix to emphasize that the environment is active.
```
(ENV) $
```
An interesting option allow you to choose the interpreter executable to use to create the virtual environment.
```
$ virtualenv --python=python2.7 ENV
```
%% Cell type:markdown id: tags:
## pip
`pip` is the Python Packaging Authority ([PyPA](https://www.pypa.io)) recommended tool for installing Python packages form the Python Packege Index ([PyPI](https://pypi.org/)).
```
(ENV) $ pip search peppercorn
peppercorn (0.5) - A library for converting a token stream into a data structure for use in web form posts
pepperedform (0.6.1) - Helpers for using peppercorn with formprocess.
```
%% Cell type:markdown id: tags:
## pip
`pip` take care of installing dependencies before their dependents, i.e. in "topological order".
```
(ENV) $ pip install peppercorn
```
or, if you prefer not the latest version
```
(ENV) $ pip install peppercorn==0.4
```
%% Cell type:markdown id: tags:
## pip
It is possible to use pip to install packages downloaded by the user.
```
(ENV) $ wget https://some_url/SomeProject.zip
(ENV) $ unzip SomeProject.zip
(ENV) $ pip install -e SomeProject
```
The option `-e` allow to install local projects in "editable" mode.
%% Cell type:markdown id: tags:
## test (virtualenv)
- Create a virtual environment
- Activate it
- Install the latest version of jupyter on it
%% Cell type:markdown id: tags:
## conda
If you are using the Anaconda (or Miniconda) distribution you can create virtual environments with the command `conda`:
```
$ conda create -n ENV
```
A directory named ENV, containing the virtual environment, will be created in the sub-directory envs inside the Anaconda installation directory.
```
$ conda info --envs
# conda environments:
#
base * /home/nicola/Utils/miniconda3
ENV /home/nicola/Utils/miniconda3/envs/ENV
```
%% Cell type:markdown id: tags:
## conda
It is possible to choose the path of the virtual environment directory
```
$ conda create -p /home/nicola/Projects/intro-python/ENV
```
```
$ conda info --envs
# conda environments:
#
/home/nicola/Projects/intro-python/ENV
base * /home/nicola/Utils/miniconda3
ENV /home/nicola/Utils/miniconda3/envs/ENV
```
%% Cell type:markdown id: tags:
## conda
Activate, deactivate and remove the virtual environment:
```
$ conda activate ENV
(ENV) $ conda deactivate
$ conda remove -n ENV --all
$
```
or
```
$ conda activate /home/nicola/Projects/intro-python/ENV
(/home/nicola/Projects/intro-python/ENV) $ conda deactivate
$ conda remove -p /home/nicola/Projects/intro-python/ENV --all
$
```
%% Cell type:markdown id: tags:
## conda
Install new modules:
```
(ENV) $ conda search django
Loading channels: done
# Name Version Build Channel
django 1.6.5 py26_0 pkgs/free
django 1.6.5 py27_0 pkgs/free
[...]
django 2.1.1 py35_0 pkgs/main
django 2.1.1 py36_0 pkgs/main
django 2.1.1 py37_0 pkgs/main
django 2.1.2 py36_0 pkgs/main
django 2.1.2 py37_0 pkgs/main
(ENV) $ conda install django=2.1.1
```
%% Cell type:markdown id: tags:
## test (virtualenv)
- Create a virtual environment
- Activate it
- Install the latest versions of the modules jupyter, numpy, matplotlib, pandas and scipy
%% Cell type:markdown id: tags:
# interpreter
%% Cell type:markdown id: tags:
## interpreter
- Python is an interpreted language
- The interpreter performs a compilation from source to bytecode, which is then executed on a virtual machine, as in Java
- The interpreter is also an excellent "calculator", to be used in interactive!
%% Cell type:code id: tags:
``` python
2**1024
```
%% Output
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
%% Cell type:markdown id: tags:
## interactive mode
To launch the interpreter in interactive mode:
- from a terminal or a command prompt simply type `python`
- launch the idle program
%% Cell type:markdown id: tags:
## interactive mode
* When used in interactive mode, the interpreter acts a little differently.
* This is the prompt: “>>>”
* If an expression has a value, it is printed automatically:
<pre>
>>> 34 + 55 - 2
87
</pre>
%% Cell type:markdown id: tags:
## interactive mode
* Any error can occur during the execution of the interpreter in interactive mode, the interpreter survives, even in case of SyntaxError:
%% Cell type:code id: tags:
``` python
5/0
```
%% Output
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-3-67a69f72677d> in <module>()
----> 1 5/0
ZeroDivisionError: division by zero
%% Cell type:markdown id: tags:
## interactive mode
%% Cell type:code id: tags:
``` python
fact(100)
```
%% Output
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-4-dd06d576a896> in <module>()
----> 1 fact(100)
NameError: name 'fact' is not defined
%% Cell type:code id: tags:
``` python
@@@ausfd?=
```
%% Output
File "<ipython-input-5-0dc3d51d84fc>", line 1
@@@ausfd?=
^
SyntaxError: invalid syntax
%% Cell type:markdown id: tags:
## print()
To print on standard output you may use the print() function.
It can take any number of arguments:
%% Cell type:code id: tags:
``` python
print(0, 2, 4)
print(6)
```
%% Output
0 2 4
6
%% Cell type:code id: tags:
``` python
print(0, 2, 4, end='')
print(6)
```
%% Output
0 2 46
%% Cell type:markdown id: tags:
## python2
Please note, in python2 print is an operator:
<pre>
>>> print 1, 2, 3
</pre>
%% Cell type:markdown id: tags:
## ipython interpreter
Born in 2001 as a work of a student (Fernando Perez).
Based on features he liked in Mathematica and trying to create a system for everyday scientific computing.
* Command history, which can be browsed with the up and down arrows on the keyboard.
* Tab auto-completion.
* In-line editing of code.
* Object introspection, and automatic extract of documentation strings from python objects like classes and functions.
* Good interaction with operating system shell.
* Support for multiple parallel back-end processes, that can run on computing clusters or cloud services like Amazon EE2.
%% Cell type:markdown id: tags:
## ipython notebook
[IPython notebook](http://ipython.org/notebook.html) is an HTML-based notebook environment for Python
* Based on the IPython shell
* Provides a web cell-based interactive environment powered with Javascript
* System profiles to access unix-terminal-like capability
* Comments and notes with HTML and markdown formats
* Integrates embedded plots
%% Cell type:markdown id: tags:
## jupyter project
In 2014, Fernado Perez announced a spin-off project from IPython called Project Jupyter (**JU**lia **PYT**hon and **R**).
IPython will continue to exist as a Python shell and a kernel for Jupyter, while **the notebook** and *other language-agnostic parts of IPython* will move under the Jupyter name.
Jupyter added support for Julia, R, Haskell and Ruby.
source: https://en.wikipedia.org/wiki/IPython#Project_Jupyter
%% Cell type:markdown id: tags:
## jupyter notebook
Jupyter notebooks are based on **jupyter kernels**.
- A ‘kernel’ is a program that runs and introspects the user’s code.
A notebook is crazy simple and fun:
- markdown and notes (consider to [learn markdown language](http://markdowntutorial.com/))
- download ipynb, python, html
- install a library and use
- slideshow it
%% Cell type:markdown id: tags:
## jupyter notebook
You can start it from a terminal by running `jupyter notebook`
First, we need to explain how to run cells. Try to run the cell below!
%% Cell type:code id: tags:
``` python
print("Hi! This is a cell. Press the ▶ button above to run it")
```
%% Cell type:markdown id: tags:
You can also run a cell with Ctrl+Enter, Shift+Enter or Alt+Enter. Experiment a bit with that.
%% Cell type:markdown id: tags:
## jupyter notebook
One of the most useful things about IPython notebook is its tab completion.
Try this: click just after read_csv( in the cell below and press Shift+Tab 4 times, slowly
%% Cell type:code id: tags:
``` python
import pandas as pd
```
%% Cell type:code id: tags:
``` python
pd.read_csv()
```
%% Cell type:markdown id: tags:
Okay, let's try tab completion for function names!
%% Cell type:code id: tags:
``` python
pd.r
```
%% Cell type:markdown id: tags:
## magic functions
IPython has all kinds of magic functions. Here's an example of comparing sum() with a list comprehension to a generator comprehension using the %time magic.
%% Cell type:code id: tags:
``` python
%timeit sum([x for x in range(1000000)])
```
%% Output
60 ms ± 1.59 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%% Cell type:code id: tags:
``` python
import numpy as np
%timeit np.arange(1000000).sum()
```
%% Output
2.34 ms ± 116 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%% Cell type:markdown id: tags:
For a list of magic functions type **%quickref**
%% Cell type:markdown id: tags:
## launch a python program
Form a terminal or command prompt you can simply type `python` and the name of a file containing python code
```
$ python codes/hello.py (for Linux and MacOS)
> python codes\hello.py (for Wondows)
```
From a jupyter notebook use the **%run** magic
%% Cell type:code id: tags:
``` python
%run codes/hello.py
```
%% Output
Hello World!
%% Cell type:markdown id: tags:
# python program file
%% Cell type:markdown id: tags:
## python program file
<pre>
#!/usr/bin/env python3
# this is a useless comment
a = 10 # this is an inline comment
print(a)
</pre>
%% Cell type:markdown id: tags:
## python program file
Let us analyze the content:
* The first line
```
#!/usr/bin/env python3
```
It is nothing more than a comment but instructs the Unix shell that will be used the interpreter python3 to run the program. The trick /usr/bin/env is to avoid worrying about the exact path of the python installation, indeed the env command is able to find it.
* It must be the first line of the file.
* The file name is arbitrary (not necessarily have to end in .py).
%% Cell type:markdown id: tags:
## python program file
Let us analyze the content:
* The line
```
# this is a useless comment
```
is a comment, and it is ignored.
* Also the next line contains an inline comment. All that is after a # until the end of the line is not interpreted.
%% Cell type:markdown id: tags:
## source encoding
* The python code can be written in any system of characters. By default, if you do not declare anything, it is utf-8.
* But it can be anything, even Chinese.
%% Cell type:markdown id: tags:
## python2
* In python2 the default encoding is latin1, and it is nothing but ASCII characters. Accented letters, for example, are not allowed, not even in the comments.
* To change the encoding is used a line like this at the beginning of the file:
```
# -*- coding: utf-8 -*-
```
(PEP 0263)
%% Cell type:markdown id: tags:
## test (name.py)
* Define a string name that contains your name, a string surname containing your last name;
* Print the length of the two strings;
* Concatenate the two strings forming name_surname;
* Print the length of name_surname.
......
%% Cell type:markdown id: tags:
![](images/title_intro_python.png)
%% Cell type:markdown id: tags:
# built-in types and operations
%% Cell type:markdown id: tags:
## integers
* In python the integers have arbitrary precision; This allowed us to calculate `2**1024` without problems.
* Any integer whose definition can be contained in the RAM of your computer is permitted.
%% Cell type:markdown id: tags:
## python2
In python2 there are two types of integers:
* int for integers up to `2**63-1` (see sys.maxint)
* long for integers of any size!
The int values are automatically converted into long as
needed:
<pre>
>>> print a, type(a)
9223372036854775807 &lt;type 'int'&gt;
>>> a += 1
>>> print a, type(a)
9223372036854775808 &lt;type 'long'&gt;
</pre>
%% Cell type:markdown id: tags:
## floating point
The floating point numbers are represented by the type _**float**_:
%% Cell type:code id: tags:
``` python
2.0**-1024
```
%% Output
5.562684646268003e-309
%% Cell type:code id: tags:
``` python
type(2.0)
```
%% Output
float
%% Cell type:markdown id: tags:
## floating point
The floating point numbers have finite precision:
%% Cell type:code id: tags:
``` python
2.0**+2048
```
%% Output
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
<ipython-input-10-7897dac30894> in <module>()
----> 1 2.0**+2048
OverflowError: (34, 'Numerical result out of range')
%% Cell type:markdown id: tags:
## integer division
In python, dividing two integers produces a float:
%% Cell type:code id: tags:
``` python
10 / 4
```
%% Output
2.5
%% Cell type:markdown id: tags:
When you want the division with truncation, you can use the operator //:
%% Cell type:code id: tags:
``` python
10 // 4
```
%% Output
2
%% Cell type:markdown id: tags:
Despite being highly atypical compared to other languages, this is very convenient.
%% Cell type:markdown id: tags:
## division with truncation
The operator // division with truncation is also
available for the float:
%% Cell type:code id: tags:
``` python
10.0 / 4.0
```
%% Output
2.5
%% Cell type:code id: tags:
``` python
10.0 // 4.0
```
%% Output
2.0
%% Cell type:markdown id: tags:
## python2
In python2, integer division produces an integer,
and it matches with the division with truncation.
%% Cell type:markdown id: tags:
## divmod
Sometimes it is helpful to have both the quotient and the remainder of a integer division; you can use the divmod() function
This function returns two values, the quotient and the remainder (or modulo).
%% Cell type:code id: tags:
``` python
divmod(10,4)
```
%% Output
(2, 2)
%% Cell type:markdown id: tags:
## test (interactive mode)
<pre>
Do the following:
>>> 2**1024
>>> 100/3
>>> 100//3
>>> 100.0/3
>>> 100.0//3
>>> 100%3
>>> divmod(100, 3)
</pre>
%% Cell type:code id: tags:
``` python
divmod(100,3)
```
%% Output
(33, 1)
%% Cell type:markdown id: tags:
## complex
There is a `complex` type
%% Cell type:code id: tags:
``` python
z = 3.0 + 4.0j
w = 3.0 - 4.0j
z+w
```
%% Output
(6+0j)
%% Cell type:code id: tags:
``` python
z*w
```
%% Output
(25+0j)
%% Cell type:code id: tags:
``` python
type(z)
```
%% Output
complex
%% Cell type:code id: tags:
``` python
print(z.real, z.imag, abs(z))
```
%% Output
3.0 4.0 5.0
%% Cell type:markdown id: tags:
## variables
You can assign to any object a symbolic name, which does not need to be declared.
%% Cell type:code id: tags:
``` python
a = 5
b = 3.2
c = a
C = b # it's not "c", it's another variable
```
%% Cell type:markdown id: tags:
## variables
Although it is a bit premature to explain it now, actually __symbolic names a, b, c and C are not
variables.__
Moreover, the symbol "=" is not at all what it seems!
For now anyway we can continue "pretending" that these symbols are variables and that "=" execute an assignment or copy (as in C, C++ or Fortran).
%% Cell type:markdown id: tags:
## operators
Common operators are available:
* \+ (sum, concatenation)
* \- (subtraction)
* \* (product)
* / (division)
* // (integer division, this is new)
* % (modulo)
* ...
%% Cell type:markdown id: tags:
## binary operators
Like in C, binary operators are associated to assignment operators:
* += (increase)
* -= (decrease)
* ...
therefore,
%% Cell type:code id: tags:
``` python
a = 10
a += 4
a
```
%% Output
14
%% Cell type:markdown id: tags:
## strings
* The type str is commonly used for the strings. It can handle unicode strings.
* They can be created either with single quotes ('alpha') or double quotes (“alpha”)
%% Cell type:code id: tags:
``` python
"alpha" + 'beta'
```
%% Output
'alphabeta'
%% Cell type:markdown id: tags:
## strings
Strings can be printed in 2 ways:
* If function print() is used, the string content is shown
```python
>>> print("Hello, " + "world!")
Hello, world!
```
* In the interactive intepreter, if we write an expression that produces a string, the content will be shown with quotes:
```python
>>> "Hello, " + "world!"
'Hello, world!'
```
%% Cell type:markdown id: tags:
## strings
* Triple quotes: `“””` or `'''`, can be used for strings that span over more than one line, or that contain single or double quotes (or the other triple quotes):
%% Cell type:code id: tags:
``` python
a = """This string spans over two lines,
and contains 'single quotes', "double quotes" and
'''triple quotes'''"""
print(a)
```
%% Output
This string spans over two lines,
and contains 'single quotes', "double quotes" and
'''triple quotes'''
%% Cell type:markdown id: tags:
## strings
* Escape characters are more or less like in C code:
%% Cell type:code id: tags:
``` python
print("alpha\nbeta\tgamma")
```
%% Output
alpha
beta gamma
%% Cell type:markdown id: tags:
## strings
* It's possible to create raw strings (escape characters will not be interpreted):
%% Cell type:code id: tags:
``` python
print(r"alfa\nbeta\tgamma")
```
%% Output
alfa\nbeta\tgamma
%% Cell type:markdown id: tags:
* This is particular helpful in defining regular expression
%% Cell type:markdown id: tags:
## strings
It is possible to operate on strings in different ways:
Try yourself on a python interpreter
%% Cell type:code id: tags:
``` python
s = "Hello, world!"
```
%% Cell type:code id: tags:
``` python
s.lower()
```
%% Output
'hello, world!'
%% Cell type:code id: tags:
``` python
s.upper()
```
%% Output
'HELLO, WORLD!'
%% Cell type:code id: tags:
``` python
s.title()
```
%% Output
'Hello, World!'
%% Cell type:code id: tags:
``` python
s.replace('o', 'x')
```
%% Output
'Hellx, wxrld!'
%% Cell type:markdown id: tags:
## strings
%% Cell type:code id: tags:
``` python
s.find('or')
```
%% Output
8
%% Cell type:code id: tags:
``` python
len(s)
```
%% Output
13
%% Cell type:code id: tags:
``` python
"Hello, world!".upper()
```
%% Output
'HELLO, WORLD!'
%% Cell type:markdown id: tags:
## strings
We can have access to single characters or substrings:
%% Cell type:code id: tags:
``` python
hi_folk = "Hi, folk!"
# 012345678
```
%% Cell type:code id: tags:
``` python
hi_folk[0]
```
%% Output
'H'
%% Cell type:code id: tags:
``` python
hi_folk[4]
```
%% Output
'f'
%% Cell type:code id: tags:
``` python
hi_folk[-1]
```
%% Output
'!'
%% Cell type:markdown id: tags:
## strings
%% Cell type:code id: tags:
``` python
hi_folk = "Hi, folk!"
# 012345678
```
%% Cell type:code id: tags:
``` python
hi_folk[2:]
```
%% Output
', folk!'
%% Cell type:code id: tags:
``` python
hi_folk[:3]
```
%% Output
'Hi,'
%% Cell type:code id: tags:
``` python
hi_folk[2:5]
```
%% Output
', f'
%% Cell type:markdown id: tags:
## strings
Interestingly, strings cannot be modified:
%% Cell type:code id: tags:
``` python
hi_folk[1] = 'X'
```
%% Output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-41-4010d49c126f> in <module>()
----> 1 hi_folk[1] = 'X'
TypeError: 'str' object does not support item assignment
%% Cell type:markdown id: tags:
## strings
Often we are faced with the need of splitting strings in tokens:
%% Cell type:code id: tags:
``` python
s = "alpha:beta:gamma"
s.split(":")
```
%% Output
['alpha', 'beta', 'gamma']
%% Cell type:code id: tags:
``` python
':'.join(['alpha', 'beta', 'gamma'])
```
%% Output
'alpha:beta:gamma'
%% Cell type:markdown id: tags:
## strings
Some times we need to remove leading or trailing characters:
%% Cell type:code id: tags:
``` python
s = " \t\n alpha beta\tgamma "
s.strip()
```
%% Output
'alpha beta\tgamma'
%% Cell type:code id: tags:
``` python
s.lstrip()
```
%% Output
'alpha beta\tgamma '
%% Cell type:code id: tags:
``` python
s.rstrip()
```
%% Output
' \t\n alpha beta\tgamma'
%% Cell type:markdown id: tags:
## strings
By default str.strip() remove all the characters of space type (' ', '\t', '\n'); it can also
receive the characters to remove:
%% Cell type:code id: tags:
``` python
s = "aaabbbccc"
s.strip("a") # try stripping also "b", "c", and "ab"
```
%% Output
'bbbccc'
%% Cell type:markdown id: tags:
## bool
The bool type is used for logical values True/False.
Thus, a bool object can assume either one of:
valori:
* `True`
* `False`
%% Cell type:markdown id: tags:
## None
In python exists a special object called None. It is used as an indefinite value (or no value). It is not a type, but an object!
%% Cell type:code id: tags:
``` python
a = None
print(a)
```
%% Output
None
%% Cell type:markdown id: tags:
## test (name.py)
* Define a string name that contains your name, a string surname containing your last name;
* Print the length of the two strings;
* Concatenate the two strings forming name_surname;
* Print the length of name_surname.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment