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

Python - L1. Introduction

The document provides an introduction to the Python programming language. It discusses Python's history and development, common uses, advantages and disadvantages, code execution methods, code structure and elements like variables and data types. It also covers operators like arithmetic, assignment, comparison, logical and bitwise operators. Finally, it discusses conditional logic like if/elif/else statements and loops using for and while in Python.

Uploaded by

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

Python - L1. Introduction

The document provides an introduction to the Python programming language. It discusses Python's history and development, common uses, advantages and disadvantages, code execution methods, code structure and elements like variables and data types. It also covers operators like arithmetic, assignment, comparison, logical and bitwise operators. Finally, it discusses conditional logic like if/elif/else statements and loops using for and while in Python.

Uploaded by

matebal1488
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Lecture 1.

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

There exists different text editors


(VScode, Sublime text, atom and etc.)
and IDEs (Anaconda Navigator, PyCharm
and etc.) for coding in Python.
There exists online tools such is jupyter
notebook
6 1.5. Structure of Python code and its elements
The process of developing code in Python is simpler than developing code in C ++, there are
some limitations that need to be met.
Namely, if the use of an extra "space" character in the code in the C++ language is not a
problem, incorrect use of the "space" symbol in the Python can lead to an error.
In addition to the "space" character, a pair of characters { and } (also, pairs () and [] ) used to
indicate a block of code in C ++, Python has a different connotation and etc.
Also, the syntax for writing code in Python is different from the code for writing code in C ++.
7 Variables
As we know, C++ is strictly typed language. Unlike the C ++, Python is dynamically typed
language. which means that we can use variable without specifying the type .

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 :

"""This can be used as a comment and as a


source code documentation string."""
10 Data types
The following types are supported in the Python:

Python has a built-in object-oriented approach.


11 Data Input/Output
The method input() can be used for console input in Python. The method treats any type of
data entered from the keyboard as string data.
C++ Python
int x;
x = input()
cin >> x;

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++

st = 'String' string st = 'String'


for s in st: for (auto s : st)
print(s, end=' ') cout << s << " ";

st = 'String' string st = 'String';


i = 0 int i = 0;
while i < len(st): while (i < st.length()){
print(st[i], end=' ') cout << st[i] << " ";
i += 1 i ++;
}
25 1.9. Functions range() and enumerate()
The Python language uses the range() method to get a predefined numeric interval. The
range () method looks like:

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.

You might also like