Python Course
Python Course
GCyB-I
26/11/2023 2
1- Installation of Python Environement
The pre-installed version may not be the most recent one (2.6.2 and 3.1.1 as of Sept 09)
Anaconda - https://www.anaconda.com/download.
Spyder - https://www.spyder-ide.org.
Jupyter - https://jupyter.org/install.
26/11/2023 3
1- Installation of Python Environement
https://www.python.org/downloads/release/python-
3110/
26/11/2023 4
2- Installation of Pycharm IDE
https://www.jetbrains.com/pycharm/download/?section=wind
ows
26/11/2023 5
2- Installation of Pycharm IDE
26/11/2023 6
3- Installation of Anaconda
26/11/2023 7
3- Installation of Anaconda
26/11/2023 8
3-1- First test with Spyder
26/11/2023 9
3-2 First test with Jupyter Notebook
26/11/2023 10
26/11/2023 11
4- Operators in Python
Operators Description ^ Bitwise XOR
+, - Addition and Subtraction & Bitwise AND
*, /, % Multiplication, Division and Remainder <<, >> Shifts
** Exponentiation | Bitwise OR
<, <=, >, >=, !=, == Comparisons
~x Bitwise NOT
or Boolean OR
26/11/2023 12
4- Operators in Python
Operators Description
x[index] Subscription
x[index:index] Slicing
26/11/2023 13
5- Python Syntaxe
• if...else statement
• if...elif...else statement
26/11/2023 14
5-2 Loops
Loops makes an execution of a program chunk iterative.
Two types of loops in Python:
for loop
while loop
When our iterations are countable , we often use for loop.
When our iterations are uncountable, we often use while loop.
26/11/2023 15
5-2 Loops
26/11/2023 16
5-2 Loops
26/11/2023 17
5-2 Loops
The break statement is used to break out of a loop statement (stop the execution of a
looping statement).
if you break out of a for or while loop, any loop else block is not executed.
Example:
26/11/2023 18
5-2 Loops
26/11/2023 19
6- Data structures
Data structures are used to store a collection of related data.
Built-In Data
Structure
26/11/2023 20
6-1 Lists
An ordered group of items
Does not need to be the same type
Could put numbers, strings, Boolean or any other type of data in the same list.
The list of items should be enclosed in square brackets separated by commas.
Lists are mutable; We can add, remove items in a list.
Example: fruit_list = ['apple', 'mango', 'banana’]
We can access members of the list by using their position (fruit_list[0]).
Two-dimensional list: a list that contains other lists as its elements
Example:
Matrix=[[1,2,3],[8,7,9],[4,6,5]]
26/11/2023 21
6-1 Lists
List Methods and Useful Built-in Functions
Method Description
Append(item) used to add items to a list – item is appended to the end of the existing list
Index(item) used to determine where an item is located in a list, Returns the index of the first
element in the list containing item
Min/max(list) built-in functions that returns the item that has the lowest /highest value in a list
26/11/2023 22
6-1 Lists
A- Uni-dimension list
26/11/2023 23
6-1 Lists
26/11/2023 24
6-1 Lists
A- Lists indexing
B- Lists Slicing
26/11/2023 25
6-1 Lists
A- Multi-dimension list
26/11/2023 26
6-2 Tuples
Tuples are defined by specifying items separated by commas within a pair of parentheses.
Tuples are similar to lists except that they are immutable (Once it is created it cannot be
changed).
Tuples support operations as lists
• Subscript indexing for retrieving elements
• Methods such as index
• Built in functions such as len, min, max
• Slicing expressions
Tuples do not support the methods: Append,Remove, Insert, Reverse, sort.
26/11/2023 27
6-2 Tuples
26/11/2023 28
6-4 Sets
A set is an unordered collection of elements where each element must be unique ((no
duplicates allowed)
).
Attempts to add duplicate elements are ignored.
Creating a set:
mySet = set(['a', 'b', 'c', 'd’])
Or:
myList = [1, 2, 3, 1, 2, 3]
mySet2 = set(myList)
Note that in the second example, the set would consist of the elements {1, 2, 3}
26/11/2023 29
6-4 Sets
List of set methods
s=set([7,9,10,'A',4]) ; t=set([1,2,3]) ; c=set([9,10])
s.union(t) new set with elements from both s and t s.union(c) {'A', 4, 7, 9, 10}
s.intersection(t) new set with elements common to s and t s. difference(c) {9, 10}
26/11/2023 30
6-4 Dictionaries
26/11/2023 31
6-4 Dictionaries
26/11/2023 32
6-4 Dictionaries
26/11/2023 33
7- String
26/11/2023 34
7-1 Some features of strings
Backward Indexing
Python String indexing
Forward Indexing
26/11/2023 35
7-1 Some features of strings
A- String Slicing
To slice a string we use indexes
String[i:j] is the substring that begins with index i and ends with index j ( but j not include)
greeting = ‘hello, world’
greeting[1:3] 'el’
greeting[-3:-1] ‘rl’
Omit begin or end to mean 'as far as you can go’
greeting[:4] ‘hell’
greeting[7:] ‘world’
26/11/2023 36
7-1 Some features of strings
* Repetition - Creates new strings, concatenating multiple copies of the a*2 will give -HelloHello
same string
[] Slice - Gives the character from the given index a[1] will give e
[:] Range Slice - Gives the characters from the given range a[1:4] will give ell
in Membership - Returns true if a character exists in the given string H in a will give 1
not in Membership - Returns true if a character does not exist in the given M not in a will give 1
string
26/11/2023 37
7-2 Strings Methods
Method Description Example
capitalize Capitalizes first letter of string "hello".capitalize() Hello
strip/lstrip/ rstrip used to remove left and right (left/right) padded spaces in a given string “ hello”.lstrip() “hello”
find find a perticular character or string in a given string. "hello".find("l") 2; "hello".find(« e") 1
Stratswith/endswith check string start/end with particular string or not "hello".startswith("h") True; "hello".startswith("h") ==> False
Isdigit/isnumeric/isd check string is digit (number) or not "1A234".isdigit() False ; "1234".isdigit() True
ecimal
len get a length of string. len("hello") 5
split Split a string "hel,l,o".split(‘,’) ['hel', 'l', 'o’] ; "hel l o".split()) ['hel', 'l', 'o’]
26/11/2023 39
8-1 Functions parameters
Parameters are values we supply to the function to perform any task.
Specified within the pair of parentheses in the function definition, separated by commas.
When we call the function, we keep the values in the same way and order.
The names given in the function definition are called parameters.
The given values in the function call are called arguments.
26/11/2023 40
8-1 Functions parameters
# Demonstrating Function Parameters
def printMax(a, b):
if a > b:
print(a, ‘is maximum’ )
else:
print(b, ‘is maximum’)
26/11/2023 41
8-2 Local and global variables
Variable declared inside a function are not related in any way to other variables with the same
names used outside the function
Variable declarations are local to the function.
Example:
26/11/2023 42
8-2 Local and global variables
Global variables are used for assigning to a variable defined outside the function.
The keyword “global” is used to declare that the variable is global.
It is impossible to assign to a variable defined outside a function without the “global” statement.
We can specify more than one global variables using the same global statement (global x, y, z).
Example:
26/11/2023 43
8-2 The return statement
The return statement is used to return from a function, i.e. break out of the function.
Return a value from a function is optional.
Return statement without a value is equivalent to return None.
Every function implicitly contains a return None statement (if return doesn’t exist).
Example:
26/11/2023 44