Python introduction

Author

Ludovic Deneuville

History

  • Created by Guido van Rossum (Netherlands)
  • First version in 1991
  • Managed by the Python Software Foundation since 2001
  • Python 3.13: python --version
  • Created at the end of 1989 during the Christmas holidays
  • Python Software Foundation (non-profit organization): promote and protect the language in order to expand the community

Why Learn Python?

  • Versatile and popular language
  • Easy to learn and read
  • Large community and many resources
  • Many packages
  • Important in the world of data science and engineering.

Python Fundamentals

  • Interpreted and interactive language
  • Dynamic typing
  • Clear and concise syntax
  • Supports several paradigms (procedural, object-oriented, functional)
  • Explain the difference between an interpreted and a compiled language.
  • Interactive: an environment where you can enter Python commands one by one and immediately see the results (Notebook)
  • Dynamic typing (type checking at runtime)

a = 5
a = "toto"

Python at ENSAI

Distribution: a pre-packaged version of the Python interpreter including many modules, libraries, and tools

  • Anaconda (data science and machine learning)
  • Miniconda (lighter)

Other IDEs: PyCharm, Atom

Package manager pip, to be run in a terminal

Packages

In a terminal (for example: Git Bash)


pip list                            # Installed packages
pip install <package>               # Install a package
pip install <package>==<version>    # Specific version
pip uninstall <package>

# Distribution used

python -c 'import platform; print(platform.python_implementation())'

For projects:

  • list of packages in a text file at the root
  • pip install - r requirements.txt

Demo: run Python in the terminal

Writing Your First Python Code

  • Variables and data types (int, float, str, bool, list, dict)
  • Basic operations
  • Sensitive to indentation
  • Control structures (if, for, while)
  • = : assignment
  • == : equality test / != difference

Indentation: use a formatter in VSCode

Control Structures

Collections - the 2 main ones

  • List: ordered collection of modifiable values

list = ["apple", "pear", 2, True]
  • Dictionary: unordered collection of key-value pairs

recipe = {"strawberry": 5, "Mustard": "5g"}

Other Useful Collections

  • Set: unordered collection of unique values

primes = {2, 3, 5, 7, 11, 13}
  • Tuple: ordered collection of immutable values

coord = (-1, 5, 4)
Note

Many other collection types exist (Tree, Linked List…).

Main Collections - Summary

Data Type Ordered Mutable Duplicate
list
dict
tuple
set

Development Assistance

To learn the language, avoid rushing to LLMs.

Prefer:

Useful Resources

LLMs are not suitable for memorization but are useful for:

  • debugging
  • documenting

At Work

5 notebooks are available to help you learn the basics of the language:

How to use notebooks