If you went through the cursus of preparatory classes (CPGE), you most certainly have been confronted to the Python programming language already. If not, we recall here some elements of the language to help you write a small program quickly.

Important! This page is intended as a recap of some elements that are assumed to be previously studied. If you are in a different situation (e.g., you followed a cursus without courses on Python), you should carefully read some tutorials online. There are numerous well-made resources, such as Open Classrooms (in French), Learn Python Fundamentals or Learn Python.

Why programming?

Before discussing about Python specifically, you should ask yourself: what is the purpose of programming?

There is no absolute answer to this question, as it obviously depends on your needs. Here is what Wikipedia says:

Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a specific task. Programming involves tasks such as: analysis, generating algorithms, profiling algorithms’ accuracy and resource consumption, and the implementation of algorithms in a chosen programming language (commonly referred to as coding). The source code of a program is written in one or more languages that are intelligible to programmers, rather than machine code, which is directly executed by the central processing unit. The purpose of programming is to find a sequence of instructions that will automate the performance of a task (which can be as complex as an operating system) on a computer, often for solving a given problem. Proficient programming thus often requires expertise in several different subjects, including knowledge of the application domain, specialized algorithms, and formal logic.

This may seem trivial when accustomed to coding, but many people from various domains still have never practiced any programming language, and do not even know what it is useful for. As an engineer, you will have to interact with these persons, who may be experts in medicine, history, biology, etc., and as such you should be able to identify where/when programming can be used to automate tasks.

As (real) examples, programming can be used to automatically find all cells in a cancerous tissue, and automatically classify them as sane or not. It can also be used to automatically generate an index of all nouns encountered in a 10,000 pages archive box.

Therefore, you should mainly think of programming as a way of automating tasks (often for the purpose of solving problems), leading to gains both in time and in quality.

The specificities of Python

Presentation of the language

Python is a programming language with a simple syntax, i.e., writing programs in that language is generally easy and intuitive. This aspect of the language (among other reasons) makes it an ideal choice for prototyping (quickly testing an idea) or learning how to program.

In addition to that, Python is a complete programming language, widely supported by a large community of programmers (which means you will find people to answer your questions), and used a lot both in academy and industry. Due to this large support, it features numerous functions that can provide solutions to the problems you want to solve.

An interpreted language

There are two main categories of programming languages: those that are compiled, and those that are interpreted:

  • Programs written in languages that are made to be compiled (e.g., C++) are not evaluated directly by the computer. They will first go through a compilation step, in which a compiler will transform the source code you write into a binary code, directly understandable by the machine.
  • Those written in an interpreted language (e.g., Python, although in practice it is slightly more complex than that) are not transformed before their execution, and are not directly executed on the machine. Instead, they are fed to an interpreter, which is a program running on the machine that will read the instructions written in your code, and apply them.
  • Finally, there are numerous languages that are hybrids between compiled and interpreted ones. We will not cover that here.

A few data structures in Python

We present here a few data structures that will be useful all along the course. Data structures are means to organize information. There are numerous sorts of such structures, and we will not cover them all. However, here are the ones we will often manipulate in this course:

Lists

A list, as its name suggests, is an ordered set of elements, just like a shopping list. Each element is given an index, and indices start at 0. Therefore, the first element of your shopping list (say pizza), will be associated with index 0, then beer will be at index 1, the second at index 1, etc., until the last element of the list, at index len(l) - 1, where l is the list.

In Python, lists can be manipulated as follows:

# CREATION
l = [] # Creates an empty list
l2 = ["pizza", "beer", "dvd"] # Creates a list initialized with strings

# INSERTION
l2.append("peanuts") # Adds an entry "peanuts" at the end of l2
l3 = l2 + ["carrots"] # Creates a new list l3 made of elements of l2, and ending with "carrots" (does not modify l2)

# DELETION
del l3[-1] # Removes the last element from l3
l4 = l3[1:] # Creates a new list l4 consisting of the elements in l3, except the first one (does not modify l3)

# OTHERS
print(l4[0]) # Prints the first element from l4
print(len(l4)) # Prints the number of elements in l4
print("beer" in l4) # Prints True if element "beer" is in l4, False otherwise

There are many more things you can do with lists. Here is the official documentation page for data structures, where you can find more basic operations on lists.

Tuples

Tuples are very similar to lists, as they are ordered sets of elements, which are indexed from 0 to len(t) - 1, where t is the tuple. The main difference is that tuples cannot be updated once created (i.e., one cannot add a new element to it, and existing elements cannot be changed).

Tuples are created as follows, and manipulated as lists above:

t = (1234, 5678) # Creates a tuple of 2 integers
t2 = ("a", "b", "c") # Creates a tuple of 3 strings

Dictionaries

A dictionary is exactly what it sounds like. It is a structure that associates a key with its definition:

  • A key is something that is hashable (i.e., a data that can be transformed into an address in memory so that the associated definition can be retrieved). In practice, most of Python objects are hashable and can be used as keys (e.g., strings, integers, tuples, etc.);
  • What we previously called definition is in fact called a value. It is basically anything you want to store and want to be able to retrieve using the associated key.

In Python, dictionaries can be manipulated as follows:

# CREATION
d = {} # Creates an empty dictionary
d2 = {"teachers": ["bastien", "nicolas", "patrick"], "students": [("roger", 14), ("salma", 16), ("edith", 15)]} # Creates a dictionary already initialized with some entries. Here, there are two keys ("teachers" and "students"). The value associated with key "teachers" is a list of the teachers of the course. The value associated with key "students" is a list of tuples, here probably corresponding to their grades in a test

# INSERTION
d["price"] = 5 # Associates the value 5 to key "price" in dictionary d

# DELETION
del d["price"] # Removes the key "price" and its value from d

# OTHERS
print(len(d2)) # Prints the number of elements in d2
print("teachers" in d2) # Prints True if key "teachers" is in the keys of d2, False otherwise
print(list(d2.keys())) # Prints the keys in d2
print(list(d2.values())) # Prints the values in d2

As for lists and tuples, there are many other things you can do with dictionaries. This is a fundamental structure for the course, as the maze map will be provided as a dictionary in the PyRat game, so make sure you understand how it works.

Cheat sheet

Here is a small document found online (2 pages) that recaps the most usual Python syntax. Feel free to download and print it, if that can help you during the labs:

mementopython3

Writing code

Code editors

There are many editors you can use to create/edit code, ranging from very basic text editors (e.g., Notepad, Gedit…) to more advanced ones (e.g., VSCode, Spyder, Jupyter…) or more traditional ones (e.g., Emacs, Nano, Vim…).

Obviously, advanced tools offer more advanced functionalities, and we advise you to go toward one of these options. However, there is no best editor. Just find what fits your needs and what you are comfortable working with. PyRat will work regardless of your choice anyway.

Before you choose your tools, it is therefore important that you experiment a bit and determine the functionalities you need. If you need some advice, I would currently recommend VSCode. It allows installing a lot of plugins to customize your environment. Among these, you may be interested in LiveShare, that allows you to work easily with others.

Python is not that green button

A common misconception for first year students is that Python is basically that green « play » button you find on editors like Spyder or Jupyter.

The Spyder editor interface. This green button calls Python in the background.

In practice (although this is a simplification of the truth), when you click on that button, here is what happens:

  1. The code you have written in the left part of the interface is sent to the Python interpreter. Basically, if your program is called my_program.py, then this is equivalent to writing python my_program.py in the shell (i.e., the terminal where you write system commands on your operating system);
  2. The Python interpreter, which is a program that understands Python language, goes through the instructions in the my_program.py file, and interprets them in order, to realize the task the program describes.

Therefore, if you need to add arguments to your command line, you may want to go through the parameters of your editor, or run it directly from the shell like this:

python my_program.py my_arguments

Since Python is a widely supported language, there are numerous resources online or on paper to help you learn programming/debug your codes. Here are two more useful links that you should keep somewhere, as you will most probably need them at some point of this course:

These two links are definitely the most useful ones when it comes to debugging your code. In addition, it is worth noticing that there is a large diversity in available supports for Python, including MOOCs, smartphone apps, Youtube channels, etc. Choose the one that fits you best! Here is a tutorial I like on Python: