Basics of Programming

Programming – Session 1

  • What is a program?
  • Good programming practices.
  • Programming errors.
  • What is a program?
  • Good programming practices
  • Programming errors

What is a program?

From algorithm to program

Computer Program: a definition

  • Is 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.

What is a program?

Programming language

Programming language

A programming language is a system of notation for writing computer programs. It is described in terms of syntax and semantics. (Wikipedia)

What is a program?

Some examples

Python

  • A high-level interpreted language.
  • Easy to learn and use.
  • Very popular and widely used for AI data science.
  • Main language for this semester.

What is a program?

Some examples

Java

  • A high-level compiled language.
  • class-based and object-oriented: more complex to learn
  • Very present in the industry.
  • Shown as an example this semester, studied in the next one.

What is a program?

How to select the right language?

Choosing the relevant language depends on various factors:

Adaptadness, performance, ease of use, community, etc.

What is a program?

From human to computer

What is a program?

Translation process

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.
  • What is a program?
  • Good programming practices
  • Programming errors

Good programming practices

Readability

Many very simple rules make reading source code easier:

  • Indentation: to show the structure of the code.
  • Naming conventions: to make the code self-explanatory.
  • Avoid constants: use variables instead.
  • Avoid global variables: use local variables instead.

Good programming practices

Maintainability

Software evolves over time, and it is essential to make it understandable and easy to maintain.
  • Comments: to explain the logic of the code, not the syntax.
  • Documentation: to explain the purpose of the code.
  • Modularity: to split the code into small, reusable parts.

Good programming practices

Reliability

Related to the confidence you can have in the functionality provided.
  • Structured programming: to avoid spaghetti code.
  • Testing: to ensure the code works as expected.
  • Built-in functions and librairies: to avoid reinventing the wheel.

Good programming practices

Python good practices

Coding norms may vary from one language to another.

  • What is a program?
  • Good programming practices
  • Programming errors

Programming errors

Syntax errors

The code does not respect the syntax of the language.

# 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

Error line 6:

SyntaxError: invalid syntax

Programming errors

Runtime errors

The code is syntactically correct but produces an error during execution.

# 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 program

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

However, it is possible to set a seed to ensure reproducibility.

# Needed imports
import random

# Set the seed
random.seed(42)

Recap of the session

What’s next?

Practical activity (~2h30)

Implementing algorithmic solutions with programs

  • Understanding the provenance of errors
  • Write and execute programs

After the session

  • Complete the practical activity
  • Assess your understanding of the basic notions
  • Check next session’s “Before the class” section