Python: the basics

Author

Marie-Hélène Burle

About Python

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 on Google) in 2004, its popularity has grown steadily, reaching the number one position in 2018 where it still is (as of January 2023).

The standard library

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:

type(3)    # type is a built-in function
int

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.

Example: the os module

The os module contains the function getcwd returning the path of the current working directory as a string.

This function cannot be used directly:

getcwd()
NameError: name 'getcwd' is not defined

In order to access it, you have several options:

  • Load the module, then access the function as a method of the module:
import os
os.getcwd()
'/home/marie/parvus/prog/mint/python'

You can create an alias for the module:

import os as o
o.getcwd()
'/home/marie/parvus/prog/mint/python'

While it is a little silly for a module with such a short name, it is very convenient with modules of longer names.

  • Import the function directly:
from os import getcwd
getcwd()
'/home/marie/parvus/prog/mint/python'

Help and documentation

Module

You can get help on a module thanks to the help function, but only after you have loaded that module into your session:

import os
help(os)
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.
    
... 

Functions

You can also access the internal Python documentation on a function with help:

help(max)
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:

print(max.__doc__)
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.

Methods of object types

Some methods belong to specific objects types (e.g. lists have a method called append).

In those cases, help(<method>) won’t work.

Example:

help(append)
NameError: name 'append' is not defined

What you need to run instead is help(<object>.<method>).

Example:

help(list.append)
Help on method_descriptor:

append(self, object, /)
    Append object to the end of the list.

Syntax

Commands are usually written one per line, but you can write multiple commands on the same line with the separator ;:

a = 2.0; a
2.0

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:

# Incorrect code
for i in [1, 2]:
print(i)
IndentationError: expected an indented block after 'for' statement on line 2 (1993980772.py, line 3)
# Correct code
for i in [1, 2]:
    print(i)
1
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 #:

# This is a full-line comment

a         # This is an inline comment
2.0

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.

Creating and deleting objects

Assignment

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:

a = 1

You can define multiple objects at once (here variables), assigning them the same value:

a = b = 10
print(a, b)
10 10

… or different values:

a, b = 1, 2
print(a, b)
1 2

Your turn:

a = 1
b = a
a = 2

What do you think the value of b is now?

Choosing names

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.

Deleting objects

Deletion of the names can be done with the del statement:

var = 3
var
3
del var
var
NameError: name 'var' is not defined

The Python garbage collector automatically removes values with no names bound to them from memory.

Data types

Python comes with multiple built-in types.

Examples (non exhaustive):

type(1), type(1.0), type('1'), type(3+2j), type(True)
(int, float, str, complex, bool)

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.

var = 2.3
type1 = type(var)
var = "A string."
type2 = type(var)

type1, type2
(float, str)

You can also convert the type of some values:

'4', type('4'), int('4'), type(int('4'))
('4', str, 4, int)
float(3)
3.0
str(3.4)
'3.4'
bool(0)
False
bool(1)
True
int(True)
1
float(False)
0.0

Of course, not all conversions are possible:

int('red')
ValueError: invalid literal for int() with base 10: 'red'

You might be surprised by some of the conversions:

int(3.9)
3
bool(3.4)
True

Quotes

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.

"This is a string."
'This is a string.'
type("This is a string.")
str
'This is also a string.'
'This is also a string.'
type('This is also a string.')
str

Apostrophes and textual quotes interfere with Python quotes. In these cases, use the opposite style to avoid any problem:

# This doesn't work
'This string isn't easy'
SyntaxError: unterminated string literal (detected at line 2) (368933316.py, line 2)
# This is good
"This string isn't easy"
"This string isn't easy"
# This doesn't work
"He said: "this is a problem.""
SyntaxError: invalid syntax (466663664.py, line 2)
# This is good
'He said: "this is a problem."'
'He said: "this is a problem."'

Sometimes, neither option works and you have to escape some of the quotes with \:

# This doesn't work
"He said: "this string isn't easy""
SyntaxError: unterminated string literal (detected at line 2) (392662328.py, line 2)
# This doesn't work either
'He said: "this string isn't easy"'
SyntaxError: unterminated string literal (detected at line 2) (521375870.py, line 2)
# You can use double quotes and escape double quotes in the string
"He said: \"this string isn't easy\""
'He said: "this string isn\'t easy"'
# Or you can use single quotes and escape single quotes in the string
'He said: "this string isn\'t easy"'
'He said: "this string isn\'t easy"'

Basic operations

3 + 2
5
3.0 - 2.0
1.0
10 / 2
5.0

Notice how the result can be of a different type

Variables can be used in operations:

a = 3
a + 2
5

a = a + 10 can be replaced by the more elegant:

a += 10
a
13