basic python
basic python
Python interpreter
Interactive (command mode)
Users can provide one command at a time, also known as the REPL
loop. This loop consists of three steps: reading the input, evaluating
it, and then displaying the output.
Program (script mode)
it allows programmers to run a .py file where the program was
created.
Data types
Type definition
An integer is made up of one or more digits and must not begin with
zero, except for the value of zero.
it can be represented in binary (base 2) by adding the prefix 0b, in octal
(base 8) by adding prefix 0o, and in hexadecimal (base 16) by adding
Int (integer)
prefix 0x.
Ex: 42, 0o52, 0b101010, 0x2a have the same value,
042 is not a valid integer
it is used for writing the imagine part of complex numbers and consists
of a real or integer number followed by the letter j.
complex
Ex : 3.14j, 10.j, 10j, .001j 1e100j, 3.14e-10j
bool (Boolean) The only Boolean literal constants are True and False.
A string value is represented by enclosing it in either quotation marks
(") or apostrophes ('). It can consist of any characters, including letters,
numbers, spaces, punctuation, and so on.
Ex: "bonjour", 'Salut’, ‘tonton’, ‘ <3 !'
To represent certain characters, we often have to use one of the
Str (String) following sequences:
\' for the character '
\"for character"
\n for a newline
\t for a tab
\\ for the character \
Remark:
The instruction type (myvariable) allows you to know the
type of myvariable.
Naming variables
• start with a minuscule or capital letter without accent, numbers and the underscore _ex: speed, distance, surface,
éspace , 2spon, _high.
• be as explicit as possible: don't use names that are too short and have no obvious meaning like v1, v2, v3, v4, but
instead use x, y, a, b (if they make sense in context) or names more explicit like age, length, number, number,
sum….
• The Python language distinguishes between upper and lower case letters (case distinction)
Ex : age, AGE, aGe, Age
• Python has the reserved keywords that can't be used as variable names. these are:
Example:
Variables and data affectation
The algorithm instructions include data (numeric, text, etc.) that can be entered by the user, result from a
computer calculation, etc.
It is convenient to save this data in the computer's memory to be able to reuse it as the program progresses.
The memory spaces where this data is held are called variables.
The arrangement of data (content) in a variable (a container) is called affectation.
A constant is a type of variable that retains a fixed value during the execution of a program. it is conventionally
named in capital letters.
Example: NB_MAX_TP = 14
NOTE_MIN_UE = 8
Affectation process
Evaluation (i.e. calculation) of the expression located to the right of the affectation operator (according to the
corresponding calculation priority rules for the data type). The result of this evaluation is the data (the content) which
will be “stored” in the variable.
Certain castings are not allowed (the value that assigned to the variable cannot be converted to the desired type):
Operator
An operator is a sign that acts on one or more values (called operands) to produce a result.
Example: +, -, OR, modulo
The operands are combined values that are operated on by operators.
There are several families of operators
Arithmetic operators
Alphanumeric operators
Comparison operators
Logical operators (see in course of conditional structure)
Arithmetic operators
Alphanumeric operators
The plus operator (concatenation) is utilized to join two strings together, meaning to
place two strings next to each other to form a new string.
If you want to concatenate a string and a numeric value, you must first convert the
numeric value into a string with the str function.