Python - L1. Introduction
Python - L1. Introduction
Introduction
2 1.1. A brief history of the development of the Python language
Python was created in the 90s of the last century by Guido van Rossum.
Python is a scripting language that was conceived to solve scientific tasks.
There exists two active version of Python: 2.x and 3.x. from them the third version is being
actively evolving.
With the development of technology, the Python language has evolved from a scripting
language to a programming language.
Why to use Python:
Ø Interpretable
Ø Interactive
Ø Object oriented
Ø Easy to start programming for beginners
3 1.2. Areas of using Python
Due to its simplicity, the use of Python language has gone beyond scientific activities. It is widely
used in many fields of information technology:
Ø Web-development (server side);
Ø Program development;
Ø Scientific tasks;
Ø System scripting;
Ø Big Data end etc.
4 1.3. Pros and Cons of Python
Advantages of using Python:
Ø Simplicity
Ø Cross-platform
Ø code development
Ø A lot of modules
Some of the advantages of language may be its disadvantages.
5 1.4. Execution of code
There are two ways execution of code in Python :
Ø Using code interpreter
Ø IDE
C++ Python
int x;
x=5
x = 5;
Note that the constraints related to the naming of variables and the use of different
constructions are managed by so-called the PEP8 standard.
For naming of variable in Python there exists the same limitation such in C++ :
Ø The variable name can contain upper and lower case characters, numbers, and lowercase
characters of the Latin alphabet.
Ø The variable name should not start with a digit
Ø The variable name must not match the reserved word used in the Python
8 Manage of the character space
The use of the space symbol must be with some caution. Namely, every instruction used in the
code must start from the extreme left edge of the construct used.
C++ Python
correct correct wrong correct
int x = 5; int x = 5; x=5 x=5
Unlike the C ++ language, the construction in Python is based on the following principle: the
desired construction on the current line should be ended with a symbol : (colon), statements for
construction should be start from next line using indent (2 or 4 space) :
C++ Python
if (x == 5) { if x == 5:
x=x+1 x=x+1
}
9 Comment
In Python can be used only one line comment. Comment starts with character '#'.
Unlike the C++ in Python we have no multi-line comments, However, in the role of a multi-line
comment, you can use a documentation line that is enclosed three times in a single or double
apostrophe :
The method print() can be used for console output in Python. The method print() before
outputting the object converts it to string representation using method str().
C++ Python
cout << "OOP Python"; print("OOP Python")
12 1.6. Operators
For working with different type of data in Python is supported appropriate operations :
Ø Arithmetic
Ø Assignment
Ø Comparison
Ø Logical
Ø Bitwise
13 Arithmetic
Python C++ Example Result
+ + 2 + 3 5
- - 2 - 3 -1
* * 2 * 3 6
/ / 2 / 3 0.6666666
// --- 2 // 3 0
% % 2 % 3 2
** --- 2 ** 3 8
14 Assignment
The assignment operator is used to store a value under a variable name. For example,
x=5
Python is a dynamic typing language, so it allows to store a different value :
x = "this is a string"
Positional assignment can be used in Python
15 Combined operators
Python C++
+= +=
-= -=
*= *=
/= /=
//= ---
%= %=
**= ---
16 Comparison
Python C++
== ==
!= !=
< <
<= <=
> >
>= >=
is ==
is not !=
17 Logical
Python C++
and &&
or ||
Unlike C ++, if two or more logical images can be mathematically combined, then Python can
do this without the use of a logical operator.
Python C++
x > 1 and x < 10 1 < x < 10 x > 1 && x < 10
18 BitWise
Python C++
& &
| |
< <
^ ^
~ ~
>> >>
<< <<
19 1.7. Conditional operator
As a conditional operator in Python can be used the following construction: if – elif –
else:
Python C++
if condition_1: if (condition_1)
set_of_instructions_1 set_of_instructions_1;
elif condition_2: else if (condition_2)
set_of_instructions_2 set_of_instructions_2;
… …
else else
set_of_instructions_N set_of_instructions_N;
20 1.7. Conditional operator
Conditional operator can be used in different form:
Ø Simple form: is used only operator if :
Ø combined form:
(1) are used operators if – elif
21 1.7. Conditional operator
Ø Combined form: (2) are used operators if – else:
Ø Full form:
22 1.7. Conditional operator
Python 3.10 was released in mid-2021 and comes with structural pattern matching, also known
as a match case statement. The new functionality allows you to more easily control the flow of
your programs by executing certain parts of code if conditions (or cases) are met.
match subject:
case <pattern_1>:
<action_1>
case <pattern_2>:
<action_2>
case <pattern_3>:
<action_3>
case _:
<action_wildcard>
23 1.8. Loop operator
In Python we have two loop operators: for and while.
Operator for looks like the same operator in C++ which was introduced by standard C++11.
Operator while has the same meaning.
Both operators have the following syntax :
for while
for object in set_of_objects: counter = 0
set_of_instructions_1 while condition:
else: set_of_instructions_1
set_of_instructions_2 counter += 1
else:
set_of_instructions_2
24 1.8. Loop operator
Python C++
where start parameter indicates start point of the interval, end – indicates end point of the
interval, which does not belong to the interval, step – indicates difference between two
neighbor elements in the interval.
26 1.9. Functions range() and enumerate()
The enumerate() method can be used to get the object and its position in the set. The method
looks like:
Example,
27 1.10. Operators continue and break
Operators continue and break has the same meaning as in C++:
Ø if during iteration we need omit some instructions inside loop, then we should use
operator continue;
Ø if we want to stop iteration inside loop, then we should use operator break.