0% found this document useful (0 votes)
2 views

basic python

This document provides an introduction to Python programming, covering its simplicity, data types, and operators. It explains the Python interpreter modes, variable naming conventions, and the concept of constants and data affectation. Additionally, it outlines various operators and their functions, including arithmetic, alphanumeric, comparison, and logical operators.

Uploaded by

belhadi.ttl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

basic python

This document provides an introduction to Python programming, covering its simplicity, data types, and operators. It explains the Python interpreter modes, variable naming conventions, and the concept of constants and data affectation. Additionally, it outlines various operators and their functions, including arithmetic, alphanumeric, comparison, and logical operators.

Uploaded by

belhadi.ttl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Course3:

Basics in Python programming:


data types and operators
Introduction
Python is Developed under Free Open-Source license (Available for free).it is considered a high-level programming
language and known for its simplicity and readability, making it easier for beginners to learn and write code. This
language has a large standard library and a thriving community of developers, which makes it a popular choice
among programmers.
it is widely used for numerous purposes such as web development, data analysis, artificial intelligence, text
manipulation, numerical calculation, management of files, databases, compression, cryptography, multimedia, and
graphical interfaces.

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

a real number have to contain either a decimal point or a symbol e (it


means 10 to the power ...).
float (floating point There may be nothing before or after the point.
numbers) Ex: 7.2, 4e6, 8.4e9
.34 is equal to 0.34
34. is equal to 34.0

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.

 The variable’s name can be viewed as a label that is linked to an object.


For example, when a=4, the object (in this case, an integer) 4 has the label that is known by "a".

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.

Incrementing a variable means adding a numerical value to it


(usually the value 1).

Decrementing a variable means removing a numerical value from


it (generally, the value 1).

Casting or type conversion


You can change the data type by indicating it in parentheses, preceded by the new type name.

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.

The * operator is used to repeat the same string multiple times.


Comparison operators

Program (script mode)


• A Python script file, with a .py extension, is also called a module.
• To obtain the final result display, it is necessary to use the print() function in this mode.
Exercise:
Try to manually evaluate the instructions below:

You might also like