type(3) # type is a built-in function
int
Marie-Hélène Burle
Python is a hugely popular interpreted language with a simple, easily readable syntax, and a large collection of external packages.
It was created by Dutch programmer Guido van Rossum in the 80s, with a launch in 1989.
Since the start of the PYPL PopularitY of Programming Language index (based on the number of tutorial searches in Google) in 2004, its popularity has grown steadily, reaching the number one position in 2018. As of June 2024, its advantage over other programming languages keeps increasing.
Python comes with a standard library. As soon as you launch the program, you can access part of the standard library such as the built-in functions and built-in constants:
Example:
Most of the standard library however is held in several thematic modules. Each module contains additional functions, constants, and facilities. Before you can use them, you need to load them into your session.
The os module contains the function getcwd
returning the path of the current working directory as a string.
This function cannot be used directly:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[2], line 1 ----> 1 getcwd() NameError: name 'getcwd' is not defined
In order to access it, you have several options:
You can create an alias for the module:
While it is a little silly for a module with such a short name, it is very convenient with modules of longer names.
You can get help on a module thanks to the help
function, but only after you have loaded that module into your session:
Help on module os:
NAME
os - OS routines for NT or Posix depending on what system we're on.
MODULE REFERENCE
https://docs.python.org/3.10/library/os.html
The following documentation is automatically generated from the Python
source files. It may be incomplete, incorrect or include features that
are considered implementation detail and may vary between Python
implementations. When in doubt, consult the module reference at the
location listed above.
...
You can also access the internal Python documentation on a function with help
:
Help on built-in function max in module builtins:
max(...)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
In Jupyter, you can also use ?max
or max?
.
Alternatively, you can print the __doc__
method of the function:
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
Some methods belong to specific objects types (e.g. lists have a method called append
).
In those cases, help(<method>)
won’t work.
Example:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[8], line 1 ----> 1 help(append) NameError: name 'append' is not defined
What you need to run instead is help(<object>.<method>)
.
Example:
Commands are usually written one per line, but you can write multiple commands on the same line with the separator ;
:
Tabs or 4 spaces (the number of spaces can be customized in many IDEs) have a syntactic meaning in Python and are not just for human readability:
Cell In[11], line 3 print(i) ^ IndentationError: expected an indented block after 'for' statement on line 2
IDEs and good text editors can indent the code automatically.
Comments (snippets of text for human consumption and ignored by Python) are marked by #
:
PEP 8—the style guide for Python code—suggests a maximum of 72 characters per line for comments. Try to keep comments to the point and spread them over multiple lines if they are too long.
The assignment statement =
binds a name (a reference) and a value to create an object (variable, data structure, function, or method).
For instance, we can bind the name a
and the value 1
to create the variable a
:
You can define multiple objects at once (here variables), assigning them the same value:
… or different values:
While I am using a
and b
a lot in this workshop (since the code has no other purpose than to demo the language itself), in your scripts you should use meaningful names (e.g. survival
, age
, year
, species
, temperature
). It will make reading the code this much easier.
Make sure not to use the names of built-in functions or built-in constants.
Deletion of the names can be done with the del
statement:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[19], line 2 1 del var ----> 2 var NameError: name 'var' is not defined
The Python garbage collector automatically removes values with no names bound to them from memory.
Python comes with multiple built-in types.
Examples (non exhaustive):
int
= integer
float
= floating point number
complex
= complex number
str
= string
bool
= Boolean
Python is dynamically-typed: names do not have types, but they are bound to typed values and they can be bound over time to values of different types.
You can also convert the type of some values:
Of course, not all conversions are possible:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[29], line 1 ----> 1 int('red') ValueError: invalid literal for int() with base 10: 'red'
You might be surprised by some of the conversions:
Pairs of single and double quotes are used to create strings. PEP 8 does not recommend one style over the other. It does suggest however that once you have chosen a style, you stick to it to make scripts consistent.
Apostrophes and textual quotes interfere with Python quotes. In these cases, use the opposite style to avoid any problem:
Cell In[36], line 2 'This string isn't easy' ^ SyntaxError: unterminated string literal (detected at line 2)
Cell In[38], line 2 "He said: "this is a problem."" ^ SyntaxError: invalid syntax
Sometimes, neither option works and you have to escape some of the quotes with \
:
Cell In[40], line 2 "He said: "this string isn't easy"" ^ SyntaxError: unterminated string literal (detected at line 2)
Cell In[41], line 2 'He said: "this string isn't easy"' ^ SyntaxError: unterminated string literal (detected at line 2)
Notice how the result can be of a different type
Variables can be used in operations:
a = a + 10
can be replaced by the more elegant: