ADEL AHMED ALHAJJ
Reference
ADEL AHMED ALHAJJ
Python The elif Conditions
Let’s create an application in Python to calculate percentage and
grade according to percentage of marks of three subjects. Use
Python if-elif-else ladder statement.
3
Python The elif Conditions
4
Python The elif Conditions
5
Python The elif Conditions
6
Python The elif Conditions
Write a program that reads the sides of a triangle (L1, L2, L3)
and then:
a. Prints the word "Equilateral" in the case of equal sides
(L1 = L2 and L1 = L3 and L2 = L3)
b. Prints the word "Isosceles" in the case of two equal sides
(L1 = L2 or L1 = L3 or L2 = L3)
c. Prints the word "Scalene" in the case of all sides being
different
(L1 ≠ L2 and L1 ≠ L3 and L2 ≠ L3)
7
Python The elif Conditions
Write a program that checks the input from the user :
a. Prints the phrase "You entered a digit" if the input is a digit
(from 0 to 9)
b. Prints the phrase "You entered an uppercase letter" if the input
is an uppercase letter (from A to Z)
c. Prints the phrase "You entered a lowercase letter" if the input is
a lowercase letter (from a to z)
d. Prints the phrase "That is not a digit or a letter" if the input is
none of the above
8
Python match .. Case
In Python, the match...case statement allows us to execute a block of
code among many alternatives.
The match...case evaluates a given expression and based on the evaluated
value, it executes the certain associated statement(s).
It was first introduced in Python 3.10 which is similar to
the switch...case statement in other programming languages.
9
Python match .. Case
match expression :
case 1:
block of case 1
case 2:
block of case 2
case 3:
block of case 3
case _:
block of default case
10
Python match .. Case
11
Python match .. Case
12
Python match .. Case
13
Python match .. Case with OR
14
Python match .. Case with IF
15
Python match .. Case with OR and IF
16
Python match .. Case with OR and IF
17
Python match .. Case with OR and IF
1- Write a program to display the days of the week. When entering 1, it
prints "Sat"; when entering 2, it prints "Sun", and so on ?
2- Write a program to display the name of the month when entering the
month number (e.g., 1 for January) ?
18
Python match .. Case with OR and IF
(3x − 7)if ( x = −5)
2
y= (5 x )if ( x = 2)or ( x = 5)
( x − 4 x 3 )if ( x = −4)or ( x = 4)
19
Python Data Type
Python List Data Type
List is an ordered collection of similar or different types of items separated by
commas and enclosed within brackets [ ].
القائمة عبارة عن مجموعة مرتبة من أنواع متشابهة أو مختلفة من العناصر مفصولة بفواصل ومحاطة
.][ بأقواس
.وهي عبارة عن مصفوفة حجمها غير ثابت و يمكنها تخزين قيم من مختلف األنواع في وقت واحد
A = [ ]
B = [10, 20, 30, 40, 50]
C = ['Mhamad', 'Samer', 'Abdullah']
D = [1, 'two', 'three', 4]
20
Python Data Type
Create a Python List
We create a list by placing elements inside square brackets [], separated by
commas. For example,
# a list of three elements
ages = [19, 26, 29]
print(ages)
# Output: [19, 26, 29]
# a list containing strings and numbers
student =['Jack', 32, 'Computer’,12]
print(student) 21
Python Data Type
Accessing Elements of a List
To access any element in a list, whether to retrieve its value or change it,
we use the index number of that element.
In Python, you can access elements in a list in two ways:
From left to right (forward indexing):
If you want to access the elements of the list from left to right, i.e.,
starting from the first element entered, then the index of the first element
will be 0, the second element will be 1, and so on.
22
Python Data Type
Accessing Elements of a List
From right to left (reverse/negative indexing):
If you want to access the elements of the list from right to left, i.e.,
starting from the last element entered, then the index of the last element
will be -1, the second last will be -2, and so on.
23
Python Data Type
Access List Elements
each element in a list is associated with a number, known as an index.
The index of first item is 0, the index of second item is 1, and so on.
24
Python Data Type
Access List Elements
languages = ['Python', 'Swift', 'C++’]
# Access the first element
print(languages [ 0 ] ) # Python
# Access the third element
print(languages [ 2 ] ) # C++
25
Python Data Type
Access List Elements
Python also supports languages = ['Python', 'Swift', 'C++’]
negative indexing. The index # Access the first element
of the last element is -1,
print(languages [ -1 ] ) # C++
the second-last element is -
2, and so on. # Access the third element
print(languages [ -3 ] ) # Python
26
Python Data Type
27
Python Data Type
list الوصول لعناصر الـ
names = ['Rami', 'Sara', 'Nada', 'Mhamad', 'Salem']
print(names [ 0 ] )
print(names [ 1 ] )
names = ['Rami', 'Sara', 'Nada', 'Mhamad', 'Salem']
print(names [ -1 ] )
print(names [ -2 ] )
28
Python Data Type
Deleting Elements from a List Using
arr = [10, 20, 30, 40, 50]
the del Statement del arr[0]
The del statement is used to delete the del arr[1]
print(arr)
entire list from memory or to delete
specific elements from it.
When you delete an element from a list
using the del statement, the Python
interpreter rearranges its elements and
updates the index number of each
element. 29
Python Data Type
30
Python Data Type
arr = [10, 20, 30, 40, 50]
del arr[0 : 3]
print(arr)
31
Python Data Type
arr1 = [1, 2, 3]
arr2 = [4, 5, 6 arr = ['python'] * 3
arr3 = arr1 + arr2 print(arr)
print(arr3)
arr = ['Mhamad', ‘ali', ‘adel', 'Sara']
x = ‘adel’
print(‘Is adel in the?')
print(x in arr)
32
Python Data Type
33
Python Data Type append( ) ,insert( ) ,extend( )
34
Python Data Type
Change List Items
We can change the items of a list by assigning new values using
the = operator. For example,
35
Python Data Type
Python Tuple Data Type
A tuple is similar to the list in many ways. Like lists, tuples also contain the
collection of the items of different data types. The items of the tuple are
separated with a comma (,) and enclosed in parentheses ().
A tuple is a read-only data structure as we can't modify the size and value of
the items of a tuple.
languages = ('Arabic', 'French', 'English', 'Spanish')
36
Python Data Type
Python Tuple Data Type
)A = (10,
)print(A
وضع فاصلة بعد القيمة 10هنا يعتبر أمر إجباري حتى يفهم مترجم بايثون أنك تنوي
تعريف tupleو ليس متغير عادي قيمته .10
طريقة الوصول لعناصر ال tupleنفس ما تم شرحه في List
37
Python Data Type
38
Python Data Type
Python Set Data Type
A Set is an array with no fixed size, whose values are immutable (unchangeable),
and it can store values of different data types at the same time.
You cannot directly modify or delete its values. Also, it cannot contain duplicate
values. If you add the same value twice, only one value will be stored, not two.
In Python, we use curly braces { } to define a one-dimensional array with no fixed
size and whose values are immutable (unchangeable).
In Sets, data is stored randomly and not in the order in which it was entered. The
reason is that in this type of array, no index number is assigned to each element.
For this reason, you also cannot directly access a specific element in a Set because
it simply does not have an index number.
39
Python Data Type
40
Python Data Type
Python Dictionary Data Type
Python Dictionary Data Type
The idea of a Dictionary is to assign a key to each value. This way, you can
access the value of each element through its specific key.
So, a Dictionary is a table in which you store data in the form
of keys and values.
As for the data types stored inside a Dictionary, you can store keys and values
of any type you want.
In Python, we use curly braces {} to define a Dictionary.
d = { 1: 'One', 2: 'Tow', 3: 'Three', 4: 'Four', 5: 'Five‘ }
41
Python Data Type
Python Dictionary Data Type
# creating a dictionary
country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
"England": "London"
}
# printing the dictionary
print(country_capitals)
42
Python Data Type
43
Python Data Type
44
Python Operators >> Membership Test Operators
The in Operator
The in operator is used to check whether a collection (such as a list, tuple,
set, or dictionary) contains a specific value or not.
If there is an element in the collection that has the same value being
searched for, it returns True.
If there is no element in the collection that has the same value being
searched for, it returns False.
Function Example in Python
in [Link](a,b) nums = [1,2,3,4,5] 1 in nums True 10 in nums False
import operator [Link](nums, 2) True
not in not [Link](a,b) nums = [1,2,3,4,5] 1 not in nums False 10 not in nums True
import operator not [Link](nums, 2) False
45
Python Operators >> Membership Test Operators
The not in Operator
The not in operator is used to check whether a collection does not contain a
specific value.
If there is no element in the collection that has the same value being
searched for, it returns True.
If there is an element in the collection that has the same value being
searched for, it returns False.
Function Example in Python
in [Link](a,b) nums = [1,2,3,4,5] 1 in nums True 10 in nums False
import operator [Link](nums, 2) True
not in not [Link](a,b) nums = [1,2,3,4,5] 1 not in nums False 10 not in nums True
import operator not [Link](nums, 2) False
46
Python Operators >> Membership Test Operators
47
Function Range( )
start is the index of the list where slicing starts.
stop is the index of the list where slicing ends.
step allows you to select nth item within the range start to stop.
48
Function Range( )
Python range() function generates the immutable sequence of
numbers starting from the given start integer to the stop integer.
The range() is a built-in function that returns a range object that
consists series of integer numbers,
49
Function Range( )
Note: As you can see in the output, We got six integers starting from 0
to 5. If you notice, range() didn’t include 6 in its result because it
generates numbers up to the stop number but never includes the stop
number in its result.
50
Function Range (Start , Stop)
51
Function Range (Start , Stop , Step )
52