الجمهورية العربية السورية
المعهد العالي للعلوم التطبيقية والتكنولوجيا
دورة لغة البايثون في مجال تطبيقات الذكاء الصنعي /التعلم اآللي /التعلم العميق /الحقيقة المعززة
Python Programming Language for Applications of Artificial
intelligence, Machine learning, Deep learning, and Augmented reality
)Session 1 (Introduction / Data types / Control Statements / Functions
م .علي خضور ما .وسام شريفة د .وسيم صافي
26/09/2023
1
الجمهورية العربية السورية
المعهد العالي للعلوم التطبيقية والتكنولوجيا
1) Introduction:
(Procedural programming, Object oriented programming,
Functional programming),
History of Python
IDLE / PyCharm
Example: Hello World
Example: Hello User
Dynamic Typing
2
الجمهورية العربية السورية
المعهد العالي للعلوم التطبيقية والتكنولوجيا
2) Data Types:
int, float, Complex
Operations on numbers (+ / * - // % abs() round() ...)
Bitwise Operations (And OR - XOR - invert - right shift - left shift )
Strings
String Methods (count / startswith / endswith / strip / split / index / remove /replace /
...)
List (definition / Append / Remove / sort ...)
Tuple (Count / index / sum)
Multidimensional Tuple and lists
Operations on sequences:(x in s / x not in s / len / min / max)
indexing in python
Boolean Values (True / False) ( None )
References in Python (is / == )
3
الجمهورية العربية السورية
المعهد العالي للعلوم التطبيقية والتكنولوجيا
3) Control Statements:
if / else / elif / Comparison ( == , < , > , <= , >= , !=)
while loops
For loops
4) Functions:
Definition / parameters / return values /
Multiple Return Values
Optional Parameters – Default Values
Positional Parameters
Functions & Modules ( math)
4
الجمهورية العربية السورية
المعهد العالي للعلوم التطبيقية والتكنولوجيا
1) Introduction:
(Procedural programming, Object oriented programming,
Functional programming),
History of Python
IDLE / PyCharm
Example: Hello World
Example: Hello User
Dynamic Typing
5
1) Introduction:
• Python: Dynamic programming language which supports several
different programing paradigms:
• Procedural programming
• Object oriented programming
• Functional programming (map)
6
1) Introduction:
• Why Python?
• Extremely versatile language
• Website development, data analysis, numerical analysis, ...
• Syntax is clear, easy to read and learn (almost pseudo code)
• Support object oriented programming
• Full modularity, hierarchical packages
• Comprehensive standard library for many tasks
• Big community
7
1) Introduction:
• Start implementation in December 1989 by Guido van Rossum (CWI)
• 16.10.2000: Python 2.0
• Unicode support
• Garbage collector
• 3.12.2008: Python 3.0
• Not 100% backwards compatible
• 2007 & 2010 most popular programming language (TIOBE Index)
• Recommendation for scientific programming (Nature News, NPG, 2015)
• Current version: Python 3.11 (26/9/2023)
• Python2 is out of support!
8
1) Introduction:
• IDEs:
- IDLE (Integrated Development and Learning Environment)
https://docs.python.org/3/library/idle.html
- PyCharm
https://www.jetbrains.com/pycharm/
- Vscode (jupyter Notebook (interactive computational environment)
https://code.visualstudio.com/
9
1) Introduction:
• Example: Hello World
print (" Hello world !")
• Example: Hello User
name = input (" What ’s your name ? ")
print (" Hello ", name )
print (" Hello " + name )
10
1) Introduction:
• Dynamic Typing
Strong Typing:
Object is of exactly one type! A string is always a string, an integer
always an integer
Dynamic Typing:
• No variable declaration
• Variable names can be assigned to different data types in the course
of a program
• An object’s attributes are checked only at run time
11
1) Introduction:
• Dynamic Typing
number = 3
print (number , type ( number ))
print ( number + 42)
=> <class 'int'>
number = "3"
print (number , type ( number ))
print ( number + 42)
=> <class 'str'> Error: can only concatenate str (not "int") to str
number = "3"
print (number , type ( number ))
print ( number + "42")
=> <class 'str'>
12
الجمهورية العربية السورية
المعهد العالي للعلوم التطبيقية والتكنولوجيا
2) Data Types:
int, float, Complex
Operations on numbers (+ / * - // % abs() round() ...)
Bitwise Operations (And OR - XOR - invert - right shift - left shift )
Strings
String Methods (count / startswith / endswith / strip / split / index / remove /replace /
...)
List (definition / Append / Remove / sort ...)
Tuple (Count / index / sum)
Operations on sequences:(x in s / x not in s / len / min / max)
indexing in python
Boolean Values (True / False) ( None )
13
2) Data Types:
• Numerical Data Types: • Numerical Data Types:
• int : integer numbers
• float : corresponds to double in C a=1
• complex : complex numbers ( j is
the imaginary unit) b = 1.0
a = 1 c = 1e2
b = 1.0 d = 1 + 3j
c = 1e0
d = 1 + 0j print(a+a)
print(a) print(b+b)
print(b) print(c+c)
print(c)
print(d)
print(d+d)
14
2) Data Types:
• Operators on Numbers: • Operators on Numbers:
Basic arithmetics: + , - , * , / Conversion: int(x) , Not
casting
hint: Python 2 1/2 = 0
b = 2.0003e2
Python 3 1/2 = 0.5
print(b)
Div and modulo operator: // , % , divmod(x, y) print(int(b)) 200
print(1/2) real div 0.5
print(1//2) int div 0 b = 2.0003e2
print(1%2) mod 1 print(b)
print(divmod(1, 2)) div mod (0,1) c=int(b)
print(divmod(2, 3)) div mod (0,2) print(c)
Absolute value: abs(x) print(float(c)) 200.0
Power: x ** y , pow(x, y)
a= 2.5
b= 3.6
print(pow(a,b))
print(a**b)
15
2) Data Types:
• Bitwise Operations:
• AND: x & y
• OR: x | y
• exclusive OR (XOR) : xˆy
• shift right n bits: x >> n
• shift left n bits: x << n
• Use bin(x) to get binary representation string of x .
a = 5 # 0101
b = 6 # 0110
print(bin(a))
print(bin(b))
print(a & b)
print(a | b)
print(a ^ b)
print(a>>2)
print(9<<a)
16
2) Data Types:
• Strings:
• Data type: str
• s = ‘spam’ , s = "spam"
• Multiline strings:
s = """ hello
... world """
print(s)
. Generate strings from other data types: str(1.0)
print("Hello"+str(1.0))
. interpretation of special characters : s = "sp\nam“
• print ("sp\ nam") sp
am
. No interpretation of of special characters : s = r"sp\nam" or print (" sp \\
nam ") sp\ nam 17
2) Data Types:
• String Methods (startswith /
endswith / strip / split / index /
remove /replace / ...):
s=" hello world "
s="hello"
print(s.index("l"))
print(s.startswith("h"))
First Occurrence
print(s.startswith("H")) # case
sensitive non-occurrence
ValueError: substring
not found
s=" hello world "
print("--"+s.strip()+"--") s=" hello world "
print(s.split()) either one space print(s.replace("l","F"))
or more ['hello', 'world'] 18
2) Data Types:
• List (definition / Append / Remove / ...) :
• Data type: list • Extend with a second list: s.extend(s2)
• s = [1, "spam", 9.0, 42] s = []
print(s) [1, 'spam', 9.0, 42] s.append("a")
s = [] s.append([1])
print(s) [] s.append(2.6)
• Append an element: s.append(x) print(s) ['a', [1], 2.6]
s = [] s.extend(["F"])
s.append("a")
s.extend(["M"])
s.append(1)
s.append(2.6) s.extend([2,3,4])
print(s) print(s) ['a', [1], 2.6, 'F', 'M', 2, 3,
19 4]
2) Data Types:
• Count appearance of an element:
s.count(x)
• s = []
• s.append(["F"]) • Insert element at position: s.insert(i, x)
• s.append(["M"])
• s.append([2,3,4])
• s = []
• print(s.count("F")) 0 • s.append(["F"])
• print(s.count(["F"])) 1
• s.append(["M"])
• print(s.count([2,3,4])) 1
• Position of an element: s.index
• s.append(5)
• s = []
• s.append(6)
• s.append(["F"]) • print(s)
• s.append(["M"]) • s.insert(2,10) # index / object
• s.append([2,3,4]) • print(s)
• s.append(5)
• s.insert(100,"R") # index / object if outbounding
• s.append(6)
• print(s.index(["F"])) 0
• print(s)
• #print(s.index("F")) Exception
• print(s.index([2,3,4])) 2
20
• print(s.index(5)) 3
2) Data Types:
• Remove and return element at position: s.pop([i]): Remove and return item at
index (default last). Raises IndexError if list is empty or index is out of range.
s = []
s.append(["F"])
s.append(["M"])
s.append(5)
s.append(6)
print(s) [['F'], ['M'], 5, 6]
print(s.pop()) 6
print(s) [['F'], ['M'], 5]
s.pop(1)
print(s) [['F'], 5]
#s.pop(2) Exception
print(s) [['F'], 5]
#s.pop(100) Exception
print(s)
s=[]
21
#s.pop() Exception
2) Data Types: • Reverse list: s.reverse()
• s = []
• Delete element: s.remove(x): • s.append(["F"])
Remove first occurrence of • s.append(["M"])
value. Raises ValueError if • s.append(5)
the value is not present. • s.append(6)
s = [] • print(s) [['F'], ['M'], 5, 6]
s.append(["F"]) • s.reverse()
s.append(["M"]) • print(s) [6, 5, ['M'], ['F']]
s.append(5) • Sum of the elements: sum(s)
s.append(6) s = []
s.append(1)
print(s)
s.append(2)
print(s.remove(5))
s.append(5)
print(s) s.append(6)
#print(s.remove(9)) print(sum(s))
s.append("6")
#print(sum(s)) --> Exception
22