Basics of Programming

Programming – Session 1

  • What is a program?
  • Good programming practices
  • Assessing quality of a Python code
  • Programming errors
  • Recap of the session

What is a program?

From algorithms to programs

Computer program

  • A translation of an algorithm into a programming language
  • Has a valid syntax and semantics
  • Is a set of instructions that a computer follows to perform a task

Programming language

  • A programming language is a system of notations for writig computer programs

  • It is described in terms of syntax and semantics (Wikipedia)

What is a program?

Examples of programming languages

Python

  • High-level interpreted language
  • Easy to learn and use
  • Very popular and widely used for AI
  • Strong community

$\rightarrow$ Main language for this semester

Java

  • High-level compiled language
  • More complex to learn (class-based, object-oriented)
  • Very present in the industry
  • Strong community

$\rightarrow$ Shown as an example this semester, studied in the next one

Choosing the relevant language depends on various factors, e.g., adaptadness, performance, ease of use, community, etc.

What is a program?

A list of programming languages

Wikipedia - List of programming languages

What is a program?

From human to computer

Main steps

  1. Writing – The programmer writes the code
  2. Lexing – The code is transformed into tokens
  3. Parsing – The tokens are transformed into an abstract syntax tree
  4. Compilation – The abstract syntax tree is transformed into machine code
  5. Execution – The machine code is executed by the computer

Good programming practices

Things you should keep in mind when writing code

Readability

  • Indentation – Show the structure of the code
  • Naming conventions – Make the code self-explanatory
  • Avoid values – Use variables instead
  • Avoid global variables – Use local variables

Reliability

  • Structured programming – Avoid spaghetti code
  • Testing – Ensure the code works as expected
  • Built-in functions and librairies – Avoid reinventing the wheel

Maintainability

  • Comments – Explain the logic of the code, not the syntax
  • Documentation – Explain the purpose of the code
  • Modularity – Split the code into small, reusable parts

Assessing quality of a Python code

Some tools to help you keep it clean

Norms help ensuring uniform code

Tools exist to assess the code

  • Pylint – A tool to check if a module satisfies a coding standard
  • Type hinting – An optional Python mechanism to specify the type of variables $\rightarrow$ (advice: use for functions and not for all variables)
  • Mypy – A static type checker

Programming errors

Syntax errors

What do you think of this code?

# The string to manipulate
s = "IMT Atlantique"

# Loop to append to a new string
result = ""
for i j in range(len(s)):
    result = s[i] + result
File "session_1.py", line 7
  for i j in range(len(s)):
          ^
SyntaxError: invalid syntax

Programming errors

Runtime errors

What do you think of this code?

# Ask user at runtime for a value
den = input("Please enter the value of the denominator")

# Perform a computation
result = 42 / den
print(result)

Will generate a division by zero runtime error if den == 0

Programming errors

Non-deterministic programs

Non-determinism

  • When using random values, the program may not always produce the same result
  • Thus, it may be difficult to debug

Controlling randomness

  • It is possible to set a seed to ensure reproducibility
  • Random seeds are not meant to optimize your programs!!!
# Needed imports
import random

# Set the seed
random.seed(42)

Recap of the session

Main elements to remember

A program is a translation of an algorithm into a programming language


  • Programming languages have their specificities and determine the valid syntax of programs

  • A programming language can be interpreted or compiled

  • It is important to follow good practices for keeping codes clean, maintainable and reliable

  • Some tools can help you check quality of your code

  • Syntax errors happen in the text, while runtime errors occur when executing the program

  • Beware of random numbers!

Recap of the session

What’s next?

Practical activity (~2h30)

Implementing algorithmic solutions with programs

  • Understanding the source of errors
  • Write and execute programs
  • Ask other students to review the quality of your code!

After the session

  • Review the articles of the session
  • Check your understanding with the quiz
  • Complete the practical activity
  • Check next session’s “Before the class” section