CERN - Introduction to Python for Programmers

Aim:

The aim of this course is to allow scientists, engineers and others who have programming experience become confident in reading, writing and working with code in Python.

We'll start with syntax, data types and flow control and finish with working with files, threads and processes.

Each section will be accompanied with slides, sample code, a lab and solutions.

As a live course, we can go deeper on topics that are of interest, whether or not they are on the syllabus or not.

Timings:

  • 09.00-10.30 - Session 1
  • 10.30-11.00 - Break
  • 11.00-12.30 - Session 2
  • 12.30-13.30 - Lunch
  • 13.30-15.15 - Session 3

Outline

01 - Intro

02 - Python environments

  • What is Python and where is it used?
  • What are common Python libraries?
  • Exploring the Python execution model
  • Running programs
  • Isolating Python environments

Lab 1: Get setup with your environment. Use Pycharm, VSCode or any other editor that you feel comfortable with.

Variables and data types

  • Variables and types
  • Converting types
  • Strings, Numbers and None
  • Arithmetic and assignment

Lab 2: Make things interactive. Capture user input to variables and use it.

Flow control

  • If, else, eli
  • Comparison and logical
  • If expressions (tenaries)
  • While
  • For
  • Break, Continue, Else

Lab 3: Iteration and control

Data containers

  • Python Collection types
  • Tuples
  • Lists
  • Collection constructor
  • Check for membership and iterations

Lab 4: Use containers to capture your values

Advanced data containers

  • Sets
  • FrozenSets
  • Dictionaries
  • List comprehensions

Lab 5: Use dictionaries

Functions

  • Defining functions
  • Default parameters
  • Named parameters
  • Returning values
  • Returning multiple values
  • The sum function

Lab 6: Make your code more functional

Further function

  • Docstrings and reStructuredText
  • Arbitrary parameter lists
  • Positional and Keyword parameters
  • Local and Global variables
  • Lambda functions
  • Higher order functions - filter and map

Lab 7: Higher order functions

Classes

  • Everything is an object
  • Class terminology
  • Defining and instantiating a class
  • Printing objects
  • Defining behaviour

Lab 8: Classes and object oriented programming

Error handling

  • Errors and exceptions
  • Exception handling
  • try-except blcoks
  • Raising an Exception
  • Defining new exceptions

Lab 9: Handling failure

Modules

  • Python modules
  • Importing modules
  • Module properties
  • The main modules
  • Importing non-local modules

Lab 10: Modularise your code to make it more reusable

Intro to testing

  • Testing frameworks
  • Pytest
  • Writing tests
  • Assertions
  • Fixtures
  • Parameterising tests
  • Testing for exceptions

Lab 11: Leveraging automated testing to trust your code more

Protocols and iterators

  • Duck typing
  • Impliciat contracts
  • Protocols
  • Iterables and Iterator protocol

Lab 12: Know how to leverage built-in Python features

Generators

  • Data generators
  • Generators and Generator functions
  • Generator with a for loop
  • yield

Lab 13: Experiment with generators and decide if you like them.

Additional features

  • String and regular expressiongs
  • Copying
  • Operators as methods

Lab 14: Compare, contrast and regular expressions

Files

  • Obtaining a file reference
  • File access modes
  • Reading and writing
  • Context handlers
  • Renaming, deleting and managing files

Lab 15: Work with files

Introduction to threads

  • What is a thread?
  • What are the thread states in Python?
  • The Thread Class
  • Creating a Thread
  • Passing arguments to a thread
  • Multiple threads
  • Timers

Lab 16: Optimise your code with threads

Introduction to processes

  • The multiprocessing library
  • The process class
  • Working with the Process class
  • Alternative ways to run a process
  • Using a pool

Lab 17: Optimise your code with processes

Google Drive (CERN Python programming) https://drive.google.com/drive/folders/1Ivr3lT3lIq-1RXcvpDbh91NXcVBYhFuJ?usp=share_link