Computer Programming I (Python) : Dr. Sami Al-Maqtari

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 170

Computer Programming I

(Python)
Dr. Sami AL-MAQTARI
Basic computer components
• Input devices:
• Keyboard, mouse, touch-screen, …etc.
• Output devices:
• Screen, printer, loud speakers, ...etc.
• Central Processing Unit (CPU)
• Processing digital data
Central Processing Unit
• Memory (CPU)
• Data storage
Input Output
devices devices

Memory

2
Hardware vs. Software
• Hardware: Physical components of computer
• Main board
• CPU
• Memory (RAM)
• Storage (HDD/SSD, CD, DVD, …)
• Input/Output devices
• Software: A generic term used to describe computer programs
• System software: System software serves as a base for application software.
It is also responsible for managing hardware components (device drivers,
operating systems, compilers, disk formatters, file explorer, …etc).
• Programming software: a set of tools to aid developers in writing programs
(compilers, linkers, debuggers, interpreters and text editors).
• Application software: to perform certain tasks. Examples of application
software include office suites, gaming applications, database systems and
educational software. Application software can be a single program or a
collection of small programs. This type of software is what consumers most
typically think of as “Software.”

3
The different types of programming languages
• Machine Code
• Almost impossible for humans to use because it consists entirely of
numbers.
• Programs are hard to maintain and debug.
• Has no mathematical functions available.
• Memory locations are manipulated directly, requiring the programmer to
keep track of every memory location
• Assembly language
• There are no symbolic names for memory locations.
• Difficult to read.
• Machine-dependent making it difficult for portability.
• High-Level Languages (HLL)
• Uses English like statements.
• Easier to modify, faster to write code and debug.
• Portable code, as it is designed to run on multiple machines.

4
High-Level Languages types
• Compiled languages
• Source code is translated into machine code (executable file) using
compilers.
• Pros: The final code can be executed directly on the target machine.
• Cons: Low portability, need to be recompiled to be executed on a
different machine.
• Interpreted (script) languages
• Source code is translated on the fly into machine code using
interpreters.
• Pros: High portability, the program can be run on a new machine using
a proper interpreter.
• Cons: Slow to execute compared to compiled code.

5
programming paradigms
• Imperative
• The program describes a sequence of steps that change the state of the computer as
each one is executed in turn. Imperative programming explicitly tells the computer
“how” to accomplish a certain goal.
• Structured programming: introducing looping structures).
• Procedural programming: the ability to combine a sequence of instructions into a
procedure so that these instructions can be invoked.
• An assembly language is an imperative language which is NOT structured or procedural.
• C is imperative and structured in nature.
• Logical
• Applied to problem domains that deal with the extraction of knowledge from basic
facts and rules.
• Rules are written as logical clauses with a head and a body:
e.g. “Y is true if X1, X2, and X3 are true.”
• Facts are expressed similar to rules, but without a body:
e.g. “Y is true.”
• Instead of telling the computer how to calculate things, you tell it what things are.
• Language example: Prolog

6
programming paradigms (cont.)
• Functional
• functions are treated as first-class objects.
• you can pass a function as an argument to another function, or a
function may return another function.
• Example languages: F# and Haskel.

• Object-Oriented
• A programming approach based on objects and classes.
• Allows to organize software as a collection of objects that consist of
both data and behavior.
• Main concepts: encapsulation, inheritance, and polymorphism.
• Provides key benefits of reusable code and code extensibility.
• Example languages: Python, C++, Java and C#.

7
Software development

• Planning of Project
• Analysis and Requirement Gathering
• Design
• Development
• Testing
• Deployment
• Maintenance

8
History of the Python
• Conceived in the late 1980s and its implementation was started in
December 1989 by Guido van Rossum.
• Van Rossum is Python’s principal author, entitled by the Python
community as the “Benevolent Dictator For Life” (BDFL), which
means he continues to oversee Python development and retains the
final say in disputes or arguments arising within the community.
• Provides constructs intended to enable clear programs on both a
small and large scale.
• A multi-paradigm programming language having full support for
Object-oriented programming and Structured programming.
• There are a number of language features which support Functional
programming.

9
History of the Python (cont)
• Python 1.0 was introduced in 1991.
• Python 2.0 was released on 16 October 2000.
• Cycle-detecting garbage collector and support for Unicode.
• Python 3.0 was released on 3 December 2008.
• Not completely backward-compatible with previous versions

Python 3.10 is the last version

10
The Zen of Python (language’s core philosophy)
• Beautiful is better than ugly. • Unless explicitly silenced.
• Explicit is better than implicit. • In the face of ambiguity, refuse the
temptation to guess.
• Simple is better than complex. • There should be one-- and preferably
• Complex is better than only one --obvious way to do it.
complicated. • Although that way may not be
obvious at first unless you're Dutch.
• Flat is better than nested.
• Now is better than never.
• Sparse is better than dense.
• Although never is often better than
• Readability counts. *right* now.
• Special cases aren't special enough • If the implementation is hard to
to break the rules. explain, it's a bad idea.
• If the implementation is easy to
• Although practicality beats purity. explain, it may be a good idea.
• Errors should never pass silently. • Namespaces are one honking great
idea -- let's do more of those!

11
Say hello everyone
• sayhello.py
print("Hello, everyone!")

• output
Hello, everyone!

12
Basic components of Python programming language
• Identifiers, keywords.
• Statements and expressions.
• Variables and data types.
• Operators, precedence and associativity
• Indentation and comments.
• Input and output statements.
• Type conversions.
• The type() function and is operator.
• Dynamic and strongly typed language.

13
Identifiers
• An identifier is a name given to a variable, function, class or
module.
• Identifiers may be one or more characters according to the
following:
• A combination of letters in lowercase (a to z) or uppercase (A to Z) or
digits (0 to 9) or an underscore (_). Names like myCountry, other_1 and
good_morning, all are valid examples.
• Can begin with an alphabet (A – Z and a – z and _).
• Cannot start with a digit but is allowed everywhere else. 1plus is
invalid, but plus1 is perfectly fine.
• Keywords cannot be used as identifiers.
• Cannot use spaces and special symbols like !, @, #, $, % etc. as
identifiers.
• Can be of any length.

14
Keywords
• Keywords are a list of
reserved words that have and as not
predefined meaning. assert finally or
break for pass
• Special vocabulary and
class from nonlocal
cannot be used by
continue global raise
programmers as identifiers
def if return
for variables, functions,
del import try
constants or with any
elif in while
identifier name.
else is with
• Attempting to use a keyword except lambda yield
as an identifier name will False True None
cause an error.

15
Statements and Expressions
• A statement is an instruction that the Python interpreter can
execute.
• Python program consists of a sequence of statements. Statements are
everything that can make up a line (or several lines) of Python code.
• For example, z = 1 is an assignment statement.
• Expression is an arrangement of values and operators which are
evaluated to make a new value.
• Expressions are statements as well. A value is the representation of
some entity like a letter or a number that can be manipulated by a
program.
• An expression, when used in interactive mode is evaluated by the
interpreter and result is displayed instantly. But the same expression
when used in Python program does not show any output altogether.
You need to explicitly print the result.

16
Variables
• Variable is a named placeholder to hold any type of data which
the program can use to assign and modify during the course of
execution.
• In Python, there is no need to declare a variable explicitly by
specifying whether the variable is an integer or a float or any
other type.
• To define a new variable in Python, we simply assign a value to
a name.

17
Legal variable names
• Variable names can consist of any number of letters, underscores and digits.
• Variable should not start with a number.
• Python Keywords are not allowed as variable names.
• Variable names are case-sensitive.
• For example, computer and Computer are different variables
• Guidelines:
• Python variables use lowercase letters with words separated by
underscores as necessary to improve readability, like this whats_up,
how_are_you. Although this is not strictly enforced, it is considered a best
practice to adhere to this convention.
• Avoid naming a variable where the first character is an underscore. While
this is legal in Python, it can limit the interoperability of your code with
applications built by using other programming languages.
• Ensure variable names are descriptive and clear enough. This allows other
programmers to have an idea about what the variable is representing

18
Assigning values to variables
• Format:
variable_name = expression

• Examples:
integer
number = 100
miles = 1000.0 float
name = "Python"
string

a = b = c = 1

a, b, c = 1, 2, 3

19
Operators
• Operators are symbols, such as +, –, =, >, and <, that perform
certain mathematical or logical operation to manipulate data
values and produce a result based on some rules.
• An operator manipulates the data values called operands.

1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators

20
Arithmetic Operators
Operator Operator Name Description Example

+ Addition Adds two operands, producing their sum 2+3=5

- Subtraction Subtracts the two operands, producing their difference 2 – 3 = −1

* Multiplication Produces the product of the operands 2*3=6

/ Division Produces the quotient of its operands where the left 2 / 3 = 1.5
operand is the dividend and the right operand is the
divisor
% Modulus Divides left hand operand by right hand operand and 2%3=2
returns a remainder
** Exponent Divides left hand operand by right hand operand and 2 ** 3 = 8
returns a remainder
// Floor division Returns the integral part of the quotient 9//2 = 4 and
9.0//2.0 = 4.0

21
Assignment Operators
• Assignment operators are used for assigning the values
generated after evaluating the right operand to the left
operand.
• Assignment operation always works from right to left.
• Assignment operators are either simple assignment operator
or compound assignment operators.
x = 5
x = x + 1
x += 1

• If you try to update a variable which doesn’t contain any value,


you get an error
z = z + 1 # error

22
Assignment Operators (cont)
Operator Operator Name Description Example

= Assignment Assigns values from right side operands to left z=p+q


side operand
+= Addition Adds the value of right operand to the left operand and z += p
Assignment assigns the result to left operand ≡z=z+p
-= Subtraction Subtracts the value of right operand from the left z −= p
Assignment operand and assigns the result to left operand ≡z=z–p
*= Multiplication Multiplies the value of right operand with the left z *= p
Assignment operand and assigns the result to left operand ≡z=z*p
/= Division Divides the value of right operand with the left operand z /= p
Assignment and assigns the result to left operand ≡z=z/p
**= Exponentiation Evaluates to the result of raising the first operand to the z**= p
Assignment power of the second operand ≡ z = z ** p
//= Floor Division Produces the integral part of the quotient of its operands z //= p
Assignment where the left operand is the dividend and the right ≡ z = z // p
operand is the divisor
%= Remainder Computes the remainder after division and assigns the z %= p
Assignment value to the left operand ≡z=z%p

23
Comparison Operators
Operator Operator Name Description Example

== Equal to If the values of two operands are equal, then the (10 == 20) is not
condition becomes True True
!= Not Equal to If values of two operands are not equal, then the (10 != 20) is True
condition becomes True
> Greater than If the value of left operand is greater than the value of (10 > 20) is not True
right operand, then condition becomes True
< Lesser than If the value of left operand is less than the value of right (10 < 20) is True
operand, then condition becomes True
>= Greater than or If the value of left operand is greater than or equal to the (10 >= 20) is not
equal to value of right operand, then condition becomes True True
<= Lesser than or If the value of left operand is less than or equal to the (10 <= 20) is True
equal to value of right operand, then condition becomes True

24
Logical Operators
Operator Operator Name Description Example

and Logical AND Performs AND operation and the result is True when True and False results
both operands are True in False
or Logical OR Performs OR operation and the result is True when any True or False results in
one of both operand is True True
not Logical NOT Reverses the operand state not True results in
False

25
Bitwise Operators
Operator Operator Name Description Example

& Binary AND Result is one in each bit position for which the 60 & 13 = 12
corresponding bits of both operands are 1s (means 0000 1100)
| Binary OR Result is one in each bit position for which the 60 | 13 = 61
corresponding bits of either or both operands are 1s (means 0011 1101)
^ Binary XOR Result is one in each bit position for which the (60 ^ 13) = 49
corresponding bits of either but not both operands are (means 0011 0001)
1s
~ Binary Ones Inverts the bits of its operand (~60) = −61
Complement (means 1100 0011 in
2’s complement form
due to a signed binary
number
<< Binary Left Shift The left operands value is moved left by the number of 60 << 2 = 240
bits specified by the right operand (means 1111 0000)
>> Binary Right The left operands value is moved right by the number of 60 >> 2 = 15
Shift bits specified by the right operand (means 0000 1111)

26
Precedence and Associativity
Operators Meaning Operators Meaning

() Parentheses & Bitwise AND

** Exponent ^ Bitwise XOR

Unary plus, Unary | Bitwise OR


+x, -x, ~x
minus, Bitwise NOT
==, !=, >, >=, <, <=, is, is Comparisons, Identity,
Multiplication, not, in, not in Membership operators
*, /, //, % Division, Floor division,
Modulus not Logical NOT

+, - Addition, Subtraction and Logical AND

<<, >> Bitwise shift operators or Logical OR

27
Data Types
• Basic data types:
• Numbers
• Integers (int): can be of any length; it is only limited by the memory available.
• Real (float): accurate up to 15 decimal places.
• Complex (complex): written in the form, 𝑥 + 𝑦𝑗, where 𝑥 is the real part and 𝑦
is the imaginary part.
• Boolean (bool)
• True or False values (reserved words).
• Strings (str)
• A sequence of one or more characters, which can include letters, numbers, and
other types of characters.
• A string can also contain spaces.
• Use single quotes ( ' ) or double quotes ( " ) to represent strings and it is also
called a string literal.
• Multiline strings can be denoted using triple quotes, ''' or """.
• None (NoneType)
• A special data type in Python, it is frequently used to represent the absence of a
value.

28
Indentation
• Requirement , not for style
• Any statements written under another statement with the
same indentation is interpreted to belong to the same code
block.
• If there is a next statement with less indentation to the left,
then it just means the end of the previous code block.
• If a code block has to be deeply nested, then the nested
statements need to be indented further to the right.
• Usually, four whitespaces are used for indentation and are
preferred over tabs. Incorrect indentation will result in
IndentationError

29
Indentation (cont.)
• Block 2 and Block 3 are nested under Block 1.

Block 1

Block 2

Block 3

Block 2, continuation

Block 1, continuation

30
Comments
• A text that describes what the program or a particular part of
the program is trying to do.
• Ignored by the Python interpreter.
• Used to help you and other programmers understand,
maintain, and debug the program.
• Single line comments
# This is single line Python comment
• Multiline comments
# This is '''This is
# multiline comments multiline comment
# in Python in Python using triple quotes'''

31
Interacting with input/output
• Input:
variable_name = input([prompt])
prompt (optional): is a string written inside the parenthesis that is
printed on the screen.
Return value: string.
person = input("What is your name? ")

• Output:
print([message])
message (optional): a variable/value to be printed.
Return value: none
print(person)

32
Input/output example
# greeting.py
print("Greeting program")
name = input("What is your name? ")

greeting = "Hello, " + name


print(greeting)

Greeting program
What is your name? Youssof
Hello, Youssof

print("Hello, " + name)

33
String formatting
• format() function from str class
str.format()

name = "Youssof"

print("Name: " + name)


print("Name: {}".format(name))

Name: Youssof

• f-string
Strings with the letter f before the starting quotes.
print(f"{name}")

34
str.format()
name = "Youssof"
age = 16

print("Name: " + name + ", Age: " + str(age) + " years old")
print("Name: {}, age: {} years old".format(name, age))
print("Name: {0}, age: {1} years old".format(name, age))
print("Age: {1} old, name: {0}".format(name, age))
print("A boy aged {1} years old named {0} (born {1} years ago))".format(name, age))
print("Name: {n}, age: {a} years old".format(n=name, a=age))
print("Name: {0}, age: {a} years old".format(name, a=age))

Name: Youssof, Age: 16 years old


Name: Youssof, age: 16 years old
Name: Youssof, age: 16 years old
Age: 16 years old, name: Youssof
A boy aged 16 years old named Youssof (born 16 years ago))
Name: Youssof, age: 16 years old
Name: Youssof, age: 16 years old

35
f-string (~Python 3.6)
name = "Youssof"
age = 16

print("Name: " + name + ", Age: " + str(age) + " years old")
print(f"Name: {name}, age: {age} years old")
print(f"Age: {age} years old, name: {name}")
print(f"A boy aged {age} years old named {name} (born {age} years ago))")
print(f"{name = }, {age = } years old")

Name: Youssof, Age: 16 years old


Name: Youssof, age: 16 years old
Age: 16 years old, name: Youssof
A boy aged 16 years old named Youssof (born 16 years ago))
name = 'Youssof', age = 16 years old

36
String format modifiers
:< Left aligns the result (within the available space)
:> Right aligns the result (within the available space)
:^ Center aligns the result (within the available space)
:= Places the sign to the left most position
:+ Use a plus sign to indicate if the result is positive or negative
:- Use a minus sign for negative values only
: Use a space to insert an extra space before positive numbers (and a minus sign before negative numbers)
:, Use a comma as a thousand separator
:_ Use a underscore as a thousand separator
:e Scientific format, with a lower case e
:E Scientific format, with an upper case E
:f Fix point number format
:F Fix point number format, in uppercase format (show inf and nan as INF and NAN)
:b Binary format
:d Decimal format
:o Octal format
:x Hex format, lower case
:X Hex format, upper case
:n Number format
:% Percentage format

37
String format modifiers (cont.)
txt = "We have {:<8} chickens." We have 49 chickens.
print(txt.format(49)) We have 49 chickens.
We have 49 chickens.
txt = "We have {:>8} chickens." The temperature is - 5 degrees celsius.
print(txt.format(49)) The temperature is between -3 and +7 degrees celsius.
The temperature is between -3 and 7 degrees celsius.
txt = "We have {:^8} chickens." The temperature is between -3 and 7 degrees celsius.
print(txt.format(49))

txt = "The temperature is {:=8} degrees celsius."


print(txt.format(-5))

txt = "The temperature is between {:+} and {:+} degrees celsius."


print(txt.format(-3, 7))

txt = "The temperature is between {:-} and {:-} degrees celsius."


print(txt.format(-3, 7))

txt = "The temperature is between {: } and {: } degrees celsius."


print(txt.format(-3, 7))

38
String format modifiers (cont.)
txt = "The universe is {:,} years old." The universe is 13,800,000,000 years old.
print(txt.format(13800000000)) The universe is 13_800_000_000 years old.
We have 5.000000e+00 chickens.
txt = "The universe is {:_} years old." We have 5.000000E+00 chickens.
print(txt.format(13800000000)) The price is 45.00 dollars.
The price is 45.000000 dollars.
txt = "We have {:e} chickens." The price is INF dollars.
print(txt.format(5)) The price is inf dollars.

txt = "We have {:E} chickens."


print(txt.format(5))

txt = "The price is {:.2f} dollars."


print(txt.format(45))

txt = "The price is {:f} dollars."


print(txt.format(45))

x = float('inf')

txt = "The price is {:F} dollars."


print(txt.format(x))

txt = "The price is {:f} dollars."


print(txt.format(x))

39
String format modifiers (cont.)
txt = "The binary version of {0} is {0:b}" The binary version of 5 is 101
print(txt.format(5)) We have 5 chickens.
The octal version of 10 is 12
txt = "We have {:d} chickens." The Hexadecimal version of 255 is ff
print(txt.format(0b101)) The Hexadecimal version of 255 is FF
You scored 25.000000%
txt = "The octal version of {0} is {0:o}" You scored 25%
print(txt.format(10))

txt = "The Hexadecimal version of {0} is {0:x}"


print(txt.format(255))

txt = "The Hexadecimal version of {0} is {0:X}"


print(txt.format(255))

txt = "You scored {:%}"


print(txt.format(0.25))

txt = "You scored {:.0%}"


print(txt.format(0.25))

40
Type conversion
• Explicitly cast, or convert, a variable from one type to another.
age = input("How old are you? ") How old are you? 21
age *= 2 Your double age is 2121
print("Your double age is " + age)
• The int() function:
Explicitly convert a float number or a string to an integer.
age = int(input("How old are you? ")) How old are you? 21
age *= 2 TypeError: can only
print("Your double age is " + age) concatenate str (not "int")
to str
• The str() function:
age = int(input("How old are you? ")) How old are you? 21
age *= 2 Your double age is 42
print("Your double age is " + str(age))

41
Type conversion (cont.)
int_to_float = float(4)
string_to_float = float("1") #number treated as string
print(f"After Integer to Float Casting the result is {int_to_float}")
print(f"After String to Float Casting the result is {string_to_float}")

int_to_string = str(8)
float_to_string = str(3.5)
print(f"After Integer to String Casting the result is {int_to_string}")
print(f"After Float to String Casting the result is {float_to_string}")

complex_with_string = complex("1")
complex_with_number = complex(5, 8)
print(f"Result after using string in real part {complex_with_string}")
print(f"Result after using numbers in real and imaginary part {complex_with_number}")

After Integer to Float Casting the result is 4.0


After String to Float Casting the result is 1.0
After Integer to String Casting the result is 8
After Float to String Casting the result is 3.5
Result after using string in real part (1+0j)
Result after using numbers in real and imaginary part (5+8j)
42
Type conversion (cont.)
ascii_to_char = chr(100)
print(f'Equivalent Character for ASCII value of 100 is {ascii_to_char}’)

unicode_for_integer = ord('4')
unicode_for_alphabet = ord("Z")
unicode_for_character = ord("#")
print(f"Unicode code point for integer value of 4 is {unicode_for_integer}")
print(f"Unicode code point for alphabet 'A' is {unicode_for_alphabet}")
print(f"Unicode code point for character '$' is {unicode_for_character}")

int_to_hex = hex(255)
print(f"After Integer to Hex Casting the result is {int_to_hex}")

int_to_oct = oct(255)
print(f"After Integer to Hex Casting the result is {int_to_oct}")

Equivalent Character for ASCII value of 100 is d


Unicode code point for integer value of 4 is 52
Unicode code point for alphabet 'A' is 90
Unicode code point for character '$' is 35
After Integer to Hex Casting the result is 0xff
After Integer to Hex Casting the result is 0o377

43
Dynamic and Strongly Typed Language
• Python is a dynamic language as the type of the variable is
determined during run-time by the interpreter.
• Python is also a strongly typed language as the interpreter
keeps track of all the variables types.
• you are simply not allowed to do anything that’s incompatible with the
type of data you are working with.

44
Control Flow Statements
• Sequential Control Flow Statements:
Line by line execution, in which the statements are executed sequentially,
in the same order in which they appear in the program.
• Decision Control Flow Statements:
Depending on whether a condition is True or False, the decision structure
may skip the execution of an entire block of statements or even execute
one block of statements instead of other (if, if…else and if…elif…else).
• Loop Control Flow Statements:
Allows the execution of a block of statements multiple times until a loop
termination condition is met (for loop and while loop).
Loop Control Flow Statements are also called Repetition statements or
Iteration statements.

45
Control Flow Statements (cont.)

46
The if Statement
• Syntax:
if condition:
statement
statement
...

• Example
a = 33
b = 200
if b > a:
print("b is greater than a")

• Attention !
if b > a:
print("b is greater than a") # IndentationError

• Short hand version


if condition: one_statement

47
The if Statement (example)
number = int(input("Enter a number: "))
if number >= 0:
print("The number entered by the user is a positive number")

weight = int(input("How many kilos does your suitcase weigh? "))


if weight > 20:
print("There is a $25 surcharge for heavy luggage")
print("Thank you")

• Attention !
weight = int(input("How many kilos does your suitcase weigh? "))
if weight > 20:
print("There is a $25 surcharge for heavy luggage")
print("Thank you")

48
The if… else Statement
• Syntax:
if condition:
statements_if_true
...
else:
statements_if_false
...

• Example
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")

• Short hand version


statement_if_true if condition else statement_if_false

49
The if… else Statement (example)
number = int(input("Enter a number: "))
if number % 2 == 0:
print(f"{number} is Even number")
else:
print(f"{number} is Odd number")

number_1 = int(input("Enter the first number: "))


number_2 = int(input("Enter the second number: "))
if number_1 > number_2:
print(f"{number_1} is greater than {number_2}")
else:
print(f"{number_2} is greater than {number_1}")

50
The if… elif… else Statement
• Syntax:
if condition_1:
statements_if_condition_1_true
...
elif condition_2:
statements_if _condition_2_true
...
... # any other elif statements
elif condition_n:
statements_if _condition_n_true
...
else:
statements_if_all_false
...

51
The if… elif… else Statement (example)
score = float(input("Enter your score: "))
if score < 0 or score > 1:
print('Wrong Input')
elif score >= 0.9:
print('Your Grade is "A" ')
elif score >= 0.8:
print('Your Grade is "B" ')
elif score >= 0.7:
print('Your Grade is "C" ')
elif score >= 0.6:
print('Your Grade is "D" ')
fruit_type = input("Enter the Fruit Type:")
else:
if fruit_type == "Oranges":
print('Your Grade is "F" ')
print('Oranges are $0.59 a kilo')
elif fruit_type == "Apples":
print('Apples are $0.32 a kilo')
elif fruit_type == "Bananas":
print('Bananas are $0.48 a kilo')
elif fruit_type == "Cherries":
print('Cherries are $3.00 a kilo')
else:
print(f'Sorry, we are out of {fruit_type}')

52
Nested if Statement
• Syntax:
if condition_1: if condition_1:
if condition_2: statements_1
statements_1 ...
... else:
else: if condition_2:
statements_2 statements_2
... ...
else: else:
statements_3 statements_3
... ...

• Short hand version


statement_1 if condition_1 else statement_2 if condition_2 else statement_3

53
Nested if Statement (example)
year = int(input('Enter a year: '))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(f'{year} is a Leap Year')
else:
print(f'{year} is not a Leap Year')
else:
print(f'{year} is a Leap Year')
else:
print(f'{year} is not a Leap Year')

year = int(input('Enter a year: '))


if ((year % 400 == 0) or (year % 100 != 0) and (year % 4 == 0)):
(year
print(f'{year}
% 100 != 0)
isand
a Leap Year')
else:
(year % 4 == 0)):
print(f'{year} is a
notLeap
a Leap
Year')
Year')
else:
print(f'{year} is not a Leap Year')

54
The while loop statement
• Syntax:
while condition:
statement
...

• Example
i = 0
while i < 10:
print(f"Current value of i is {i}")
i += 1

55
The while loop statement (example 1)
number = int(input("Enter an upper number to find the average: "))
i = 0
sum = 0
while i < number:
i = i + 1
sum = sum + i
average = sum/number
print(f"The average of {number} natural numbers is {average}")

Enter an upper number to find the average: 6


The average of 6 natural numbers is 3.5

56
The while loop statement (example 2)
m = int(input("Enter first positive number: "))
n = int(input("Enter second positive number: "))
if m == 0 and n == 0:
print("Invalid Input")
elif m == 0:
print(f"GCD is {n}")
elif n == 0:
print(f"GCD is {m}")
else:
while m != n:
if m > n:
m = m - n
if n > m:
n = n - m
print(f"GCD of two numbers is {m}")
Enter first positive number: 72
Enter second positive number: 30
GCD of two numbers is 6

57
The while loop statement (example 3)
number = int(input('Enter a number: '))
result = 0
remainder = 0
while number != 0:
remainder = number % 10
result = result + remainder
number = int(number / 10)
print(f"The sum of all digits is {result}")

Enter a number: 12589


The sum of all digits is 25

58
The while loop statement (example 4)
nterms = int(input("How many Fibonacci Terms? "))
if nterms <= 0:
print("You have to enter a positive number")
else:
print("Fibonacci squence:")
previous = 1
current = 0
next_term = 0
counter = 0
while counter < nterms: How many terms? 8
print(current) Fibonacci sequence
next_term = previous + current 0
previous = current 1
current = next_term 1
counter += 1 2
3
5
8
13

59
The for loop statement
• Syntax:
for iteration_variable in sequence:
statement
...

• range() function:
range([start,] stop[, step])

• start → indicates the beginning of the sequence (zero by default).


• stop → Generates numbers up to this value but not including the
number itself.
• step → indicates the difference between every two consecutive
numbers in the sequence (1 by default). The step value can be both
negative and positive but not zero.

60
The for loop statement (example 1)
print("Only 'stop' argument value specified in range function")
for i in range(3):
print(f"{i}")

print("Both 'start' and 'stop' argument values specified in range function")


for i in range(2, 5):
print(f"{i}")

print("All three arguments 'start', 'stop' and 'step' specified in range function")
for i in range(1, 6, 3):
print(f"{i}")

Only 'stop' argument value specified in range function


0
1
2
Both 'start' and 'stop' argument values specified in range function
2
3
4
All three arguments 'start', 'stop' and 'step' specified in range function
1
4
61
The for loop statement (example 2)
for character in "Blue":
print(f"Iterate through character {character} in the string 'Blue'")
Iterate through character B in the string 'Blue'
Iterate through character l in the string 'Blue'
Iterate through character u in the string 'Blue'
Iterate through character e in the string 'Blue'

number = int(input("Enter a number: "))


even = 0
odd = 0
for i in range(number):
if i % 2 == 0:
even = even + i
else:
odd = odd + i
print(f"Sum of Even numbers are {even} and Odd numbers are {odd}")
Enter a number: 25
Sum of Even numbers are 156 and Odd numbers are 144

62
The for loop statement (example 3)
number = int(input('Enter a number for factorial: '))
factorial = 1
if number < 0:
print("Factorial doesn't exist for negative numbers")
elif number == 0:
print('The factorial of 0 is 1')
else:
for i in range(1, number + 1):
factorial = factorial * i
print(f"The factorial of number {number} is {factorial}")

Enter a number for factorial: -1


Factorial doesn't exist for negative numbers

Enter a number for factorial: 0


The factorial of 0 is 1

Enter a number for factorial: 5


The factorial of number 5 is 120

63
The pass Statement
• Syntax:
pass

• Do nothing, perform no operation


• Example:
x = 1
if x > 0:
pass
else:
print("Else statement")

64
The break statement
• Syntax: The latest value of n is 0
The latest value of n is 1
break The latest value of n is 2
The latest value of n is 3
• Use to break the whole loop The latest value of n is 4
The latest value of n is 5
• Example: The value of n is greater than 5
n = 0
while True:
print(f"The latest value of n is {n}")
n = n + 1
if n > 5:
print(f"The value of n is greater than 5")
break

65
The break statement (example)
number = int(input('Enter a number > 1: '))
prime = True
for i in range(2, number):
if number % i == 0:
prime = False
break
if prime:
print(f"{number} is a prime number ")
else:
print(f"{number} is not a prime number")

66
The continue statement
• Syntax:
continue

• Used to skip the current iteration of the loop


• Example:
n = 10 The current value of number is 9
while n > 0: The current value of number is 8
n = n - 1 The current value of number is 7
if n == 5: The current value of number is 6
print(f"Jumping at {n}") Jumping at 5
continue The current value of number is 4
print(f"The current value of number is {n}") The current value of number is 3
The current value of number is 2
The current value of number is 1
The current value of number is 0

67
The else statement in loops
• Syntax:
for variable_name in iterable: while condition:
... ...
else: else:
... ...
• The else clause will not be executed if the loop gets terminated
by a break statement.
• The else clause will be executed once after the loop has
completed all its iterations (meaning, after the loop has
completed normally).
• The statements inside the loop’s else clause will get executed
once after the loop has completed normally.

68
The else statement in loops (example)
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print( n, 'equals', x, '*', n/x)
break
else: #no break
# loop fell through without finding a factor
print(n, 'is a prime number')
2 is a prime number
3 is a prime number
4 equals 2 * 2.0
5 is a prime number
6 equals 2 * 3.0
7 is a prime number
8 equals 2 * 4.0
9 equals 3 * 3.0

69
Exception handling
• Syntax:
try:
statements...
except exception_name:
statements...
except exception_name_2:
statements...
except: #all other exceptions/errors
statements...
else: #no exceptions/errors
statements...
finally: #in all cases
statements...

70
Exception handling (example 1)
try:
print(x)
except NameError:
print("An exception occurred")

Traceback (most recent call last):


File “prog.py", line 1, in <module>
print(x)
NameError: name 'x' is not defined

An exception occurred

71
Exception handling (example 2)
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")

Variable x is not defined

72
Exception handling (example 3)
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")

Hello
Nothing went wrong

73
Exception handling (example 4)
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")

Something went wrong


The 'try except' is finished

74
Exception handling (example 5)
print("opening the file")
f = open("demofile.txt", "w")
print("writing to the file")
f.write("Lorum Ipsum")
print("closing the file")
f.close()

opening the file


writing to the file
closing the file

opening the file


Traceback (most recent call last):
File "prog.py", line 2, in <module>
f = open("demofile.txt")
FileNotFoundError: [Errno 2] No such file or directory: 'demofile.txt'

opening the file


Traceback (most recent call last):
File "prog.py", line 2, in <module>
f = open("demofile.txt", "w")
PermissionError: [Errno 13] Permission denied: 'demofile.txt'
76
Exception handling (example 5)
try:
print("opening the file")
f = open("demofile.txt")
print("writing to the file")
f.write("Lorum Ipsum")
print("closing the file")
f.close()
except:
print("Error when opening the file")

opening the file


Error when opening the file

opening the file


writing to the file
Error when opening the file

77
Exception handling (example 5)
try:
print("opening the file")
f = open("demofile.txt")
try:
print("writing to the file")
f.write("Lorum Ipsum")
print("closing the file")
f.close()
except:
print("Error when writing to the file")
except:
print("Error when opening the file")

opening the file


writing to the file
Error when writing to the file

78
Exception handling (example 5)
try:
print("opening the file")
f = open("demofile.txt")
try:
print("writing to the file")
f.write("Lorum Ipsum")
except:
print("Error when writing to the file")
finally:
print("closing the file")
f.close()
except:
print("Error when opening the file")

opening the file


writing to the file
Error when writing to the file
closing the file

79
Functions and Reusability concept
Don’t repeat the code. Write once, call multiple times
• Functions are one of the fundamental building blocks in Python
programming language.
• Used when a block of statements needs to be executed multiple
times within the program.
• This block of statements are grouped together and is given a name
which can be used to invoke it from other parts of the program.
• Reduce the size of the program by eliminating rudimentary code.
• Functions can be either Built-in Functions or User-defined functions.

80
Built-In Functions (BIF)
• A number of functions that are built into the Python interpreter and are always available.
• Examples:
• abs(x)
where x is an integer or floating-point number.
returns the absolute value of a number.
• min(arg_1, arg_2, arg_3, ..., arg_n)
where arg_1, arg_2, arg_3 are the arguments.
returns the smallest of two or more arguments.
• max(arg_1, arg_2, arg_3, ..., arg_n)
where arg_1, arg_2, arg_3 are the arguments.
returns the largest of two or more arguments.
• divmod(a, b)
where a and b are numbers representing numerator and denominator.
takes two numbers as arguments and return a pair of numbers consisting of their quotient and
remainder (a // b, a % b).
If either a or b s a floating-point number, then the result is (q, a % b), where q is the whole
number of the quotient.
• pow(x, y)
where x and y are numbers.
returns x to the power y which is equivalent to using the power operator: x**y.
• len(s)
where s may be a string, byte, list, tuple, range, dictionary or a set.
returns the length or the number of items in an object.

81
Modules
• Modules in Python are reusable libraries of code having .py
extension, which implements a group of methods and
statements.
• Python comes with many built-in modules as part of the
standard library.
• To use a module in your program, import the module using
import statement.
• All the import statements are placed at the beginning of the
program.

83
The import statement
• Syntax:
import module_name
module_name.function_name()
• Example:
import math

print(math.sqrt(5)) # 2.23606797749979
print(math.ceil(2.4)) # 3
print(math.floor(-2.4)) # -3
print(math.exp(1)) # 2.718281828459045
print(math.pow(3, 4)) # 81.0

84
The built-in function dir()
• Returns a sorted list of comma separated strings containing the
names of functions, classes and variables as defined in the
module.
print(dir(math))
['__doc__', '__loader__', '__name__', '__package__',
'__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan',
'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh',
'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1',
'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum',
'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite',
'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log',
'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm',
'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh',
'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']

85
The built-in function help()
• invokes the built-in help system. The argument to the help()
function is a string, which is looked up as the name of a
module, function, class, method, keyword, or documentation
topic, and then a related help page is printed in the console.
print(help(math.gcd))

Help on built-in function gcd in module math:

gcd(*integers)
Greatest Common Divisor.

None

86
The random module
• For random numbers generation.
• random.random()
Returns a random float in the interval [0, 1[.
• random.randint(a, b)
Returns random integer in range [a, b], including both end points.
• random.randrange(start, stop[, step])
Returns a random item from range(start, stop[, step])
import random
print(random.random())
print(random.randint(5,10))
print(random.randrange(5,10))
0.20182703325482432
8
7

87
Third-party modules or libraries
• Can be installed and managed using Python’s package manager
pip.
pip install module_name

pip install arrow


import arrow
a = arrow.utcnow()
print(a.now())

2021-11-11T17:18:30.963968+03:00

88
Function Definition and Calling the Function
• Syntax:
def function_name(param_1, param_2, ..., param_n):
statement
...

• The first statement among the block of statements within the


function definition can optionally be a documentation string or
docstring. Triple quotes are used to represent docstrings.
def sayHello(name):
""" print Hello, followed by the parameter name """
print(f"Hello, {name}")

sayHello("Python")

89
Import files in a project
# prog1.py
import math
print("inside prog1.py")
x = 72
y = 30
print(f"{x = }, {y = }, {math.gcd(x, y) = }")

inside prog1.py
x = 72, y = 30, math.gcd(x, y) = 6

# prog2.py
import prog1
print("inside prog2.py")

inside prog1.py
x = 72, y = 30, math.gcd(x, y) = 6
inside prog2.py

90
The built-in variable __name__
• If the Python interpreter is running the source program as a
stand-alone main program, it sets the special built-in __name__
variable to have a string value "__main__".
• All of the code that is at indentation level 0 gets executed. Block
of statements in the function definition is not executed unless
the function is called.
• The special variable, __name__ with "__main__", is the entry
point to your program.
if __name__ == "__main__":
statement(s)

91
The built-in variable __name__
def function_definition_with_no_argument():
print("This is a function definition with NO Argument")

def function_definition_with_one_argument(message):
print(f"This is a function definition with {message}")

def main():
function_definition_with_no_argument()
function_definition_with_one_argument("One Argument")

if __name__ == "__main__":
main()

This is a function definition with NO Argument


This is a function definition with One Argument

92
Calculating values
def area_trapezium(a, b, h):
area = 0.5 * (a + b) * h
print(f"Area of a Trapezium is {area}")

def main():
area_trapezium(10, 15, 20)

if __name__ == "__main__":
main()

Area of a Trapezium is 250.0

93
The return statement and void function
• Syntax:
return [expression_list]

• Terminates the function and returns optional value to the


caller.
• A function without return statement is called a void function
and returns None.

94
The return statement (example)
import math

degrees = int(input("Enter the degrees: "))


nterms = int(input("Enter the number of terms: "))
radians = degrees * math.pi / 180

def calculate_sin(): Enter the degrees: 45


result = 0 Enter the number of terms: 9
numerator = radians value is sin(x) calculated using the series is 0.7071067811865475
denominator = 1
for i in range(1, nterms+1):
single_term = numerator / denominator
result = result + single_term
numerator = -numerator * radians * radians
denominator = denominator * (2 * i) * (2 * i + 1)
return result

def main():
result = calculate_sin()
print(f"value is sin(x) calculated using the series is {result} ")

if __name__ == "__main__":
main()
95
The return statement (example)
import math

def calculate_sin():
result = 0
numerator = radians
denominator = 1
for i in range(1, nterms+1):
single_term = numerator / denominator
result = result + single_term
numerator = -numerator * radians * radians
denominator = denominator * (2 * i) * (2 * i + 1)
return result

if __name__ == "__main__":
degrees = int(input("Enter the degrees: "))
nterms = int(input("Enter the number of terms: "))
radians = degrees * math.pi / 180
result = calculate_sin()
print(f"value is sin(x) calculated using the series is {result} ")

96
Nested functions
• A function defined inside another function.
• Inner function contains the scope of the outer function.
• The inner function can use the arguments and variables of the outer
function. The opposite is not possible.
• The inner function is called from the definition of outer function.

def add_cubes(a, b):


def cube_surface_area(x):
return 6 * pow(x, 2)
return cube_surface_area(a) + cube_surface_area(b)

if __name__ == "__main__":
result = add_cubes(2, 3)
print(f"The surface area after adding two Cubes is {result}")

The surface area after adding two Cubes is 78

97
Scope and Lifetime of Variables
• Python programs have two scopes:
• Global
• Its value is accessible and modifiable throughout your program.
• Local
• A variable that is defined inside a function.
• Created and destroyed every time the function is executed.
• Cannot be accessed by any code outside the function definition.

• Global variables are accessible from inside a function, as long as you


have not defined a local variable with the same name.
• A local variable can have the same name as a global variable, but they
are totally different.

98
Scope and Lifetime of Variables (cont.)
• LEGB rule:
• Local → Enclosing → Global → Built-in

99
Scope and Lifetime of Variables (example)
test_variable = 5

def outer_function():
test_variable = 60

def inner_function():
test_variable = 100
print(f"Local variable value of {test_variable} \
having local scope to inner function is displayed")
inner_function()
print(f"Local variable value of {test_variable} \
having local scope to outer function is displayed ")

outer_function()
print(f"Global variable value of {test_variable} is displayed ")

Local variable value of 100 having local scope to inner function is displayed
Local variable value of 60 having local scope to outer function is displayed
Global variable value of 5 is displayed

100
The global keyword
• Use the globally defined variable instead of locally creating one.
To use the keyword, simply type 'global', followed by the
variable name.
def change_greeting(new_greeting):
global greeting
greeting = new_greeting

def greeting_world(): Hello World


world = "World" Hello World
print(greeting, world)
Hello World
Hi World
greeting = "Hello"
greeting_world()
change_greeting("Hi")
greeting_world()

101
The nonlocal keyword
• Used in nested functions and causes the variable to refer to the
previously bound variable in the closest enclosing scope.

def outer(): inner - second_num is: 1


first_num = 1 outer - first_num is: 1

def inner(): inner - second_num is: 1


nonlocal first_num outer - first_num is: 0
first_num = 0
second_num = 1
print("inner - second_num is: ", second_num)
inner()
print("outer - first_num is: ", first_num)

outer()

102
Default Parameters
• Provides a default value for a value if no value is given
def work_area(prompt, domain="Data Analytics"):
print(f"{prompt} {domain}")

def main():
work_area("Sam works in")
work_area("Alice has interest in", "Internet of Things")

if __name__ == "__main__":
main()

Sam works in Data Analytics


Alice has interest in Internet of Things

103
The print() default parameters
• Syntax:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.


Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

print("Hello,", "There")
print("Hello,", "There", sep="_") Hello, There
print("Hello,", end=" ") Hello,_There
print("There") Hello, There
print("How are you?") How are you?

104
Recursion
• A function can call itself
• Requirements:
• Recursive formula with different argument.
• Stop condition (special case).
• Check stop condition before the recursive formula.
def recurse(value):
if condition:
special_case
else:
recurse(new_value)

recurse(a_given_value)

105
Recursion (example 1)
• Factorial function
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))

num = 7
print("The factorial of", num, "is", factorial(num))

The factorial of 7 is 5040

106
Recursion (example 1)
• Factorial function
def factorial(x):
if x == 1:
return 1
return (x * factorial(x-1))

num = 7
print("The factorial of", num, "is", factorial(num))

The factorial of 7 is 5040

107
Recursion (example 2)
• Hanoi tour
def hanoi(n_disks, start, end, middle):
if n_disks == 1:
print(f"Move one disk from {start} to {end}")
else:
hanoi(n_disks - 1, start, middle, end)
hanoi(1, start, end, middle)
hanoi(n_disks - 1, middle, end, start)

n = 3
print(f"Solving Hanoi tour for {n} disks")
hanoi(n, "left", "right", "middle")

Solving Hanoi tour for 3 disks


Move one disk from left to right
Move one disk from left to middle
Move one disk from right to middle
Move one disk from left to right
Move one disk from middle to left
Move one disk from middle to right
Move one disk from left to right 108
Recursion (example 2)
• Hanoi tour
def hanoi(n_disks, start, end, middle):
if n_disks > 0:
hanoi(n_disks - 1, start, middle, end)
print(f"Move one disk from {start} to {end}")
hanoi(n_disks - 1, middle, end, start)

n = 3
print(f"Solving Hanoi tour for {n} disks")
hanoi(n, "left", "right", "middle")

Solving Hanoi tour for 3 disks


Move one disk from left to right
Move one disk from left to middle
Move one disk from right to middle
Move one disk from left to right
Move one disk from middle to left
Move one disk from middle to right
Move one disk from left to right 109
Recursion (cont.)
• Advantages of Recursion
• Recursive functions make the code look clean and elegant.
• A complex task can be broken down into simpler sub-problems using
recursion.
• Sequence generation is easier with recursion than using some nested
iteration.
• Disadvantages of Recursion
• Sometimes the logic behind recursion is hard to follow through.
• Recursive calls are expensive (inefficient) as they take up a lot of
memory and time.
• Recursive functions are hard to debug.

110
Keyword Arguments
• From a function's perspective:
• A parameter is the variable listed inside the parentheses in the function
definition.
• An argument is the value that is sent to the function when it is called.
• Arguments are normally treated in the order of passing.
• However, arguments can be sent with the key = value syntax.
def my_function(child3, child2, child1):
print("The youngest child is " + child3)

my_function("Sanad", "Firas", "Youssof")


my_function(child1="Youssof", child2="Firas", child3="Sanad")

The youngest child is Sanad

111
Non-Keyword Arguments, *args
• The special syntax *args in function definitions in python is
used to pass a variable number of arguments to a function. It is
used to pass a non-keyworded, variable-length argument list.
• The syntax is to use the symbol * to take in a variable number of
arguments; by convention, it is often used with the word args.
• What *args allows you to do is take in more arguments than the number
of formal arguments that you previously defined. With *args, any
number of extra arguments can be tacked on to your current formal
parameters (including zero extra arguments).
• Using the *, the variable that we associate with the * becomes an
iterable meaning you can do things like iterate over it, run some higher-
order functions such as map and filter, etc.

112
Non-Keyword Arguments, *args (example 1)
def add_numbers(*numbers):
sum = 0
for x in numbers:
sum += x
return sum

print(f"{add_numbers(1, 2, 4, 8) = }")
print(f"{add_numbers(15, -6) = }")
print(f"{add_numbers(7, 2, -3) = }")

add_numbers(1, 2, 4, 8) = 15
add_numbers(15, -6) = 9
add_numbers(7, 2, -3) = 6

113
Non-Keyword Arguments, *args (example 2)
def print_per_line(*args):
for arg in args:
print (arg)

print_per_line("Hello", "Welcome", "to", "Python", "class")

Hello
Welcome
to
Python
class

114
Non-Keyword Arguments, *args (example 3)
def mix_function(arg1, *args):
print ("First argument :", arg1)
for arg in args:
print("Next argument through *argv :", arg)

mix_function("Hello", "Welcome", "to", "Python", "class")

First argument : Hello


Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : Python
Next argument through *argv : class

115
Keyword Arguments, **kwargs
• The special syntax **kwargs in function definitions in python is
used to pass a keyworded, variable-length argument list.
• A keyword argument is where you provide a name to the variable as
you pass it into the function.
• One can think of the kwargs as being a dictionary that maps each
keyword to the value that we pass alongside it. That is why when we
iterate over the kwargs there doesn’t seem to be any order in which
they were printed out.

116
Keyword Arguments, **kwargs (example)
def children_names(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")

children_names(first="Youssof", second="Firas", third="Sanad")


children_names("Youssof", "Firas", "Sanad")

first: Youssof
second: Firas
third: Sanad
Traceback (most recent call last):
File "prog.py", line 6, in <module>
children_names("Youssof", "Firas", "Sanad")
TypeError: children_names() takes 0 positional arguments but 3 were given

117
*args and **kwargs (example 1)
def cheese_shop(kind, *args, **kwargs):
print(f"Do you have any {kind} ?")
print(f"I'm sorry, we're all out of {kind}")
for arg in args:
print(arg)
print("-" * 40)
for kw in kwargs:
print(kw, ":", kwargs[kw])

def main():
cheese_shop("Limburger", "It's very runny, sir.",
"It's really very, VERY runny, sir.",
shop_keeper="Michael Palin",
client="John Cleese",
sketch="Cheese Shop Sketch")
Do you have any Limburger ?
if __name__ == "__main__": I'm sorry, we're all out of Limburger
main() It's very runny, sir.
It's really very, VERY runny, sir.
----------------------------------------
shop_keeper : Michael Palin
client : John Cleese
sketch : Cheese Shop Sketch
118
*args and **kwargs (example 2)
def children_names(*args, **kwargs):
for arg in args:
print(f"{arg = }")
for key, value in kwargs.items():
print(f"{key}: {value}")

children_names(first="Youssof", second="Firas", third="Sanad")


children_names("Youssof", "Firas", "Sanad")

first: Youssof
second: Firas
third: Sanad
arg = 'Youssof'
arg = 'Firas'
arg = 'Sanad'

119
Command Line Arguments
• Command line arguments is a methodology in which user will
give inputs to the program through the console using
commands.
• A Python program can accept any number of arguments from
the command line.
• You need to import sys module to access command line
arguments.
• All the command line arguments in Python can be printed as a
list of string by executing sys.argv.

120
Command Line Arguments
import sys

def main():
print(f"sys.argv prints all command line arguments including file name")
print(f"{sys.argv}")
print(f"len(sys.argv) prints the total number of command line arguments")
print(f"{len(sys.argv)}")
print("You can use for loop to traverse through sys.argv")
for arg in sys.argv:
print(arg)

if __name__ == "__main__":
main()
python testing.py arg1 arg2 arg3

sys.argv prints all command line arguments including file name


['testing.py', 'arg1', 'arg2', 'arg3']
len(sys.argv) prints the total number of command line arguments
4
You can use for loop to traverse through sys.argv
testing.py
arg1
arg2
arg3 121
Creating and Storing strings
• One or more characters surrounded by matching quotation
marks (either ' ' or "").
single_quote = 'This is a single message'
double_quote = "Hey it is my book"
single_char_string = "A"
empty_string = ""
empty_string = ''
single_within_double_quote = "Opportunities don't happen. You create them."
double_within_single_quote = 'Why did she call the man "smart"?'
same_quotes = 'I\'ve an idea'
triple_quote_string = '''This
is
triple This is a single message
quote''' Hey it is my book
A
print(single_quote)
print(double_quote) Opportunities don't happen. You create them.
print(single_char_string) Why did she call the man 'smart'?
print(empty_string) I've an idea
print(single_within_double_quote) This 'This\nis\ntriple\nquote'
print(double_within_single_quote) is
print(same_quotes) triple
print(triple_quote_string) quote
print(type(single_quote)) <class 'str'>

122
The str() function
• Syntax:
str([object])

• returns a string which is considered an informal or nicely


printable representation of the given object.
• If the object is not provided, then it returns an empty string.

123
Basic string operations
• Concatenation
string_1 = "face"
string_2 = "book"
concatenated_string = string_1 + string_2
print(concatenated_string)

• Supported between strings only


• Repetition facebook

repetition_of_string = "wow" * 5 wowwowwowwowwow


print(repetition_of_string)
fruit_sub_string in fruit_string = True
• Testing for substring another_fruit_string not in fruit_string = True

fruit_string = "apple is a fruit"


fruit_sub_string = "apple"
print(f"{fruit_sub_string in fruit_string = }")

another_fruit_string = "orange"
print(f"{another_fruit_string not in fruit_string = }")

124
String comparison

print("january" == "jane") False


print("january" != "jane") True
print("january" < "jane") False
print("january" > "jane") True
print("january" <= "jane") False
print("january" >= "jane") True
print("filled" > "") True

125
Built-in functions for strings
• len()
• Calculates the number of characters in a string.
• White space characters are also counted.
print(len("eskimos")) 7

• max()
• returns a character having highest ASCII value.
print(max("axel")) x

• min()
• returns character having lowest ASCII value.
print(min("Brad")) B

126
Accessing Characters in String by Index Number
• Syntax:
string_name[index]

word_phrase = "be yourself"


print(word_phrase[0]) b
print(word_phrase[1]) e
print(word_phrase[2])
print(word_phrase[10]) f
print(word_phrase[11]) IndexError: string index out of range

127
Accessing Characters in String by Index Number
• Syntax:
string_name[index]

word_phrase = "be yourself"


print(word_phrase[-1]) f
print(word_phrase[-2]) l
print(word_phrase[-3]) e
print(word_phrase[-11]) b
print(word_phrase[-12]) IndexError: string index out of range

128
String slicing and joining
• Syntax:
string_name[[start]:[end][:step]]

healthy_drink = "green tea"


print(healthy_drink[0:3]) gre
print(healthy_drink[0:8:3]) get
print(healthy_drink[:5]) green
print(healthy_drink[6:]) tea
print(healthy_drink[:]) green tea
print(healthy_drink[4:4])
print(healthy_drink[6:20]) tea
print(healthy_drink[-3:-1]) te
print(healthy_drink[6:-1]) te
print(healthy_drink[::-1]) aet neerg

129
String slicing and joining (example 1)
def main():
user_string = input("Enter string: ")
if user_string == user_string[::-1]:
print(f"User entered string is palindrome")
else:
print(f"User entered string is not a palindrome")

if __name__ == "__main__":
main()
Enter string: madam
User entered string is palindrome

Enter string: cat


User entered string is not a palindrome

130
Joining strings using join() method
• Syntax:
string_name.join(sequence)

date_of_birth = ["17", "09", "1950"]


print(":".join(date_of_birth))
social_app = ["instagram", "is", "a", "photo", "sharing", "application"]
print(" ".join(social_app))
numbers = "123"
characters = "amy"
print(numbers.join(characters))

17:09:1950
instagram is a photo sharing application
a123m123y

131
Split strings using split() method
• Syntax:
string_name.split([separator [, maxsplit]])

inventors = "edison, tesla, marconi, newton"


print(inventors.split(","))
inventors = "edison, tesla, marconi, newton"
print(inventors.split(",", 2))
watches = "rolex hublot cartier omega"
print(watches.split())

['edison', ' tesla', ' marconi', ' newton']


['edison', ' tesla', ' marconi, newton']
['rolex', 'hublot', 'cartier', 'omega']

132
Strings are immutable
• The characters in a string cannot be changed once a string
value is assigned to string variable.
• However, you can assign different string values to the same
string variable.
immutable = "dollar"
immutable[0] = "c"
TypeError: 'str' object does not support item assignment
immutable = "dollar"
string_immutable = "c" + immutable[1:]
print(string_immutable)
immutable = "rollar" collar
print(immutable) rollar

133
String traversing
def main():
alphabet = "google"
index = 0
print(f"In the string '{alphabet}'")
for each_char in alphabet:
print(f"Character '{each_char}' has an index value of {index}")
index += 1

if __name__ == "__main__":
main()

In the string 'google'


Character 'g' has an index value of 0
Character 'o' has an index value of 1
Character 'o' has an index value of 2
Character 'g' has an index value of 3
Character 'l' has an index value of 4
Character 'e' has an index value of 5

134
Find the common letters
def common_characters(string_1, string_2):
for letter in string_1:
if letter in string_2:
print(f"Character '{letter}' is found in both the strings")

if __name__ == "__main__":
common_characters('rose', 'goose')

Character 'o' is found in both the strings


Character 's' is found in both the strings
Character 'e' is found in both the strings

135
Count the Total Number of Vowels, Consonants and
Blanks in a String
def main():
user_string = input("Enter a string: ")
vowels = 0
consonants = 0
blanks = 0
for each_character in user_string:
if each_character in 'aAeEiIoOuU':
vowels += 1
elif ("a" <= each_character <= "z“ or "A" <= each_character <= "Z"):
consonants += 1
elif each_character == " ":
blanks += 1
print(f"Total number of Vowels is {vowels}")
print(f"Total number of Consonants is {consonants}")
print(f"Total number of Blanks is {blanks}")

if __name__ == "__main__":
Enter a string: Some text to test the program
main()
Total number of Vowels is 8
Total number of Consonants is 16
Total number of Blanks is 5

136
Calculate the length of a string without len() function
def main():
user_string = input("Enter a string: ")
count_character = 0
for each_character in user_string:
count_character += 1
print(f"The length of '{user_string}' is {count_character} ")

if __name__ == "__main__":
main()

137
String methods
print(dir(str))

['__add__', '__class__', '__contains__', '__delattr__', '__dir__',


'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__iter__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', 'capitalize',
'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs',
'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha',
'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower',
'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join',
'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix',
'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition',
'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip',
'swapcase', 'title', 'translate', 'upper', 'zfill']

138
String methods (cont.)
• string.capitalize() • string.endswith(suffix[, start[, end]])
• Returns a copy of the string with its first • Returns True if the string ends with the
character capitalized and the rest specified suffix substring, otherwise returns
lowercased. False.
• string.casefold() • With optional start, test beginning at that
position.
• Returns a casefolded copy of the string.
• With optional end, stop comparing at that
• Casefolded strings may be used for caseless position.
matching.
• string.find(substring[, start[, end]])
• string.center(width[, fillchar])
• Checks if substring appears in string or if
• Makes string centered by taking width substring appears in string specified by
parameter into account. starting index start and ending index end.
• Padding is specified by parameter fillchar. • Return position of the first character of the
Default filler is a space. first instance of string substring in string,
• string.count(substring [, start [, otherwise return –1 if substring not found in
end]]) string.
• Returns the number of nonoverlapping • strin.isalnum()
occurrences of substring in the range [start, • Returns Boolean True if all characters in the
end]. string are alphanumeric and there is at least
• Optional arguments start and end are one character, else it returns Boolean False.
interpreted as in slice notation.
139
String methods (cont.)
• string.isalpha() • string.islower()
• Returns Boolean True if all characters in the • Returns Boolean True if all characters in the
string are alphabetic and there is at least string are lowercase, else it returns Boolean
one character, else it returns Boolean False False.
• string.isdecimal() • string.isspace()
• Returns Boolean True if all characters in the • Returns Boolean True if there are only
string are decimal characters and there is at whitespace characters in the string and
least one character, else it returns Boolean there is at least one character, else it returns
False. Boolean False.
• string.isdigit() • string.isnumeric()
• Returns Boolean True if all characters in the • Returns Boolean True if all characters in the
string are digits and there is at least one string are numeric characters, and there is
character, else it returns Boolean False. at least one character, else it returns Boolean
False.
• string.isidentifier()
• Numeric characters include digit characters
• Returns Boolean True if the string is a valid and all characters that have the Unicode
identifier, else it returns Boolean False. numeric value property.

140
String methods (cont.)
• string.istitle() • string.ljust(width[, fillchar])
• Returns Boolean True if the string is a title • Returns the string left justified.
cased string and there is at least one • Total length of string is defined in first
character, else it returns Boolean False. parameter of method width.
• string.isupper() • Padding is done as defined in second
• Returns Boolean True if all cased characters parameter fillchar. (default is space).
in the string are uppercase and there is at • string.rjust(width[, fillchar])
least one cased character, else it returns
Boolean False. • Returns the string right justified.
• Total length of string is defined in the first
• string.upper() parameter of the method, width.
• Converts lowercase letters in string to • Padding is done as defined in second
uppercase. parameter fillchar. (default is space).
• string.lower() • string.title()
• Converts uppercase letters in string to • Returns “titlecased” versions of string, that
lowercase is, all words begin with uppercase
characters and the rest are lowercase.

141
String methods (cont.)
• string.swapcase() • string.strip([chars])
• Returns a copy of the string with uppercase • Returns a copy of the string in which
characters converted to lowercase and vice specified chars have been stripped from
versa. both side of the string.
• string.splitlines([keepends]) • If char is not specified then space is taken as
default.
• Returns a list of the lines in the string,
breaking at line boundaries. • string.rstrip([chars])
• Line breaks are not included in the resulting • Removes all trailing whitespace of string.
list unless keepends is given and true.
• string.lstrip([chars])
• string.startswith(prefix[, start[, • Removes all leading whitespace in string.
end]])
• string.replace(old, new[, max])
• Returns Boolean True if the string starts
with the prefix, otherwise return False. • Replaces all occurrences of old in string with
• With optional start, test string beginning at new.
that position. • If the optional argument max is given, then
• With optional end, stop comparing string at only the first max occurrences are replaced.
that position. • string.zfill(width)
• Pads the string on the left with zeros to fill
width.

142
Lists
• A variable hold a single value normally.
• A list is a container that holds a number of values.
• Each element or value that is inside a list is called an item.
• All the items in a list are assigned to a single variable.
• Lists avoid having a separate variable to store each item which
is less efficient and more error prone when you have to
perform some operations on these items.
• Lists can be simple or nested lists with varying types of values.
• Lists are one of the most flexible data storage formats in
Python because they can have values added, removed, and
changed.

143
Creating lists
• Syntax:
list_name = [item_1, item_2, ..., item_n]
list_name = []

superstore = ["metro", "tesco", "walmart", "kmart", "carrefour"]


print(superstore)
number_list = [4, 4, 6, 7, 2, 9, 10, 15]
mixed_list = ['dog', 87.23, 65, [9, 1, 8, 1]]
print(mixed_list)
print(type(mixed_list))
empty_list = []
print(empty_list)
print(type(empty_list))

['metro', 'tesco', 'walmart', 'kmart', 'carrefour’]


['dog', 87.23, 65, [9, 1, 8, 1]]
<class 'list'>
[]
<class 'list'>
144
Basic list operations
• Concatenation list_1 = [1, 3, 5, 7]
list_2 = [2, 4, 6, 8]
• Repetition print(list_1 + list_2)
print(list_1 * 3)
• Testing for equality print(list_1 == list_2)
print(list_1 != list_2)
• Testing for membership print(5 in list_1)
print(10 in list_1)

[1, 3, 5, 7, 2, 4, 6, 8]
[1, 3, 5, 7, 1, 3, 5, 7, 1, 3, 5, 7]
False
True
True
False

145
The list() function
• Used to create a list
• Syntax:
list([sequence])

• Where the sequence (optional) can be a string, tuple or list itself.


quote = "How are you doing?"
string_to_list = list(quote)
print(string_to_list)
friends = ["j", "o", "e", "y"]
print(friends + quote)
print(friends + list(quote))
['H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u', ' ', 'd', 'o', 'i', 'n', 'g', '?’]
TypeError: can only concatenate list (not "str") to list
['j', 'o', 'e', 'y', 'H', 'o', 'w ', ' ', 'a', 'r', 'e ', ' ', 'y', 'o', 'u', ' ', 'd', 'o', 'i', 'n', 'g', '?']

146
Indexing and slicing in lists
• Syntax:
list_name[index]

superstore = ["metro", "tesco", "walmart", "kmart", "carrefour"]


print(superstore[0]) metro
print(superstore[1]) tesco
print(superstore[2]) walmart
print(superstore[3]) kmart
print(superstore[4]) carrefour
print(superstore[9]) IndexError: list index out of range

147
Indexing and slicing in lists
• Syntax:
list_name[index]

superstore = ["metro", "tesco", "walmart", "kmart", "carrefour"]


print(superstore[-1]) carrefour
print(superstore[-2]) kmart
print(superstore[-3]) walmart
print(superstore[-4]) tesco
print(superstore[-5]) metro
print(superstore[-9]) IndexError: list index out of range

148
Indexing and slicing in lists
• Syntax:
list_name[[start]:[end][:step]]

superstore = ["metro", "tesco", "walmart", "kmart", "carrefour"]


print(superstore[1:4]) ['tesco', 'walmart', 'kmart']
print(superstore[2:]) ['walmart', 'kmart', 'carrefour']
print(superstore[:2]) ['metro', 'tesco']
print(superstore[1:9:3]) ['tesco', 'carrefour']
print(superstore[1:-1]) ['tesco', 'walmart', 'kmart']
print(superstore[-1:1]) []
print(superstore[::-2]) ['carrefour', 'walmart', 'metro']

149
Modifying items in lists
animals = ["Cat", "Lion", "Snake"]
print(animals)
animals[0] = "Dog"
print(animals)
animals[2] = "Horse"
print(animals)
animals[-1] = "Bison"
print(animals)

['Cat', 'Lion', 'Snake']


['Dog', 'Lion', 'Snake']
['Dog', 'Lion', 'Horse']
['Dog', 'Lion', 'Bison']

150
Modifying items in lists
zoo = ["Lion", "Tiger", "Zebra"]
forest = zoo
print(type(zoo))
print(type(forest))
print(forest)
zoo[0] = "Fox" <class 'list’>
print(zoo) <class 'list'>
print(forest) ['Lion', 'Tiger', 'Zebra']
forest[1] = "Deer" ['Fox', 'Tiger', 'Zebra']
print(forest) ['Fox', 'Tiger', 'Zebra']
print(zoo) ['Fox', 'Deer', 'Zebra']
['Fox', 'Deer', 'Zebra']

151
Some built-in functions used on lists
• len()
• returns the numbers of items in a list.
• sum()
• returns the sum of numbers in the list.
• any()
• returns True if any of the Boolean values in the list is True.
• all()
• returns True if all the Boolean values in the list are True, else returns False.
• sorted()
• returns a modified copy of the list while leaving the original list untouched.

152
Built-in functions used on lists (examples)
lakes = ['superior', 'erie', 'huron', 'ontario', 'powell']
print(f"{lakes = }")
print(f"{len(lakes) = }")
numbers = [1, 2, 3, 4, 5]
print(f"{sum(numbers) = }")
print(f"{max(numbers) = }")
print(f"{min(numbers) = }")
print(f"{any([1, 1, 0, 0, 1, 0]) = }")
print(f"{all([1, 1, 1, 1]) = }")
lakes_sorted_new = sorted(lakes)
print(f"{lakes_sorted_new = }")
lakes = ['superior', 'erie', 'huron', 'ontario', 'powell']
len(lakes) = 5
sum(numbers) = 15
max(numbers) = 5
min(numbers) = 1
any([1, 1, 0, 0, 1, 0]) = True
all([1, 1, 1, 1]) = True
lakes_sorted_new = ['erie', 'huron', 'ontario', 'powell', 'superior']

153
List methods
print(dir(list))

['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__',


'__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__',
'__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__',
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__',
'__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__',
'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse',
'sort']

154
List methods (cont.)
• list.append(item)
• Adds a single item to the end of the list.
• This method does not return new list and it just modifies the original.
• list.clear()
• Remove all the elements from a list.
• list.copy()
• Returns a copy of the specified list.
• list.count(item)
• Counts the number of times the item has occurred in the list and returns it.
• list.extend(iterable)
• Adds the specified list elements (or any iterable) to the end of the current list.
• list.index(item)
• Searches for the given item from the start of the list and returns its index.
• If the value appears more than once, you will get the index of the first one.
• If the item is not present in the list then ValueError is thrown by this method.

155
List methods (cont.)
• list.insert(index, item)
• Inserts the item at the given index, shifting items to the right.
• list.pop([index])
• Removes and returns the item at the given index.
• This method returns the rightmost item if the index is omitted.
• list.remove(item)
• Searches for the first instance of the given item in the list and removes it.
• If the item is not present in the list then ValueError is thrown by this method.
• list.reverse()
• Reverses the items in place in the list.
• This method modifies the original list and it does not return a new list.
• list.sort()
• Sorts the items in place in the list.
• This method modifies the original list and it does not return a new list.

156
List methods (examples)
cities = ["oslo", "delhi", "washington", "london", "seattle", "paris", "washington"]
print(cities.count('seattle'))
print(cities.index('washington'))
cities.reverse()
print(cities)
cities.append('brussels')
print(cities)
cities.sort()
print(cities)
cities.pop()
print(cities)
more_cities = ["brussels", "copenhagen"]
cities.extend(more_cities)
print(cities)
cities.remove("brussels")
print(cities)

1
2
['washington', 'paris', 'seattle', 'london', 'washington', 'delhi', 'oslo']
['washington', 'paris', 'seattle', 'london', 'washington', 'delhi', 'oslo', 'brussels']
['brussels', 'delhi', 'london', 'oslo', 'paris', 'seattle', 'washington', 'washington']
['brussels', 'delhi', 'london', 'oslo', 'paris', 'seattle', 'washington']
['brussels', 'delhi', 'london', 'oslo', 'paris', 'seattle', 'washington', 'brussels',
'copenhagen']
['delhi', 'london', 'oslo', 'paris', 'seattle', 'washington', 'brussels', 'copenhagen']

157
Populating lists with items
continents = []
continents.append("Asia")
continents.append("Europe")
continents.append("Africa")
print(continents)

['Asia', 'Europe', 'Africa']

158
Populating lists with items (cont.)
list_items = input("Enter list items separated by a space: ").split()
print(f"List items are {list_items}")
items_of_list = []
total_items = int(input("Enter the number of items: "))
for i in range(total_items):
item = input("Enter list item: ")
items_of_list.append(item)
print(f"List items are {items_of_list}")

Enter list items separated by a space: Asia Europe Africa


List items are ['Asia', 'Europe', 'Africa']
Enter the number of items: 2
Enter list item: Australia
Enter list item: Americas
List items are ['Australia', 'Americas']

159
Traversing of lists
fast_food = ["waffles", "sandwich", "burger", "fries"]
for each_food_item in fast_food:
print(f"I like to eat {each_food_item}")
for each_food_item in ["waffles", "sandwich", "burger", "fries"]:
print(f"I like to eat {each_food_item}")

I like to eat waffles


I like to eat sandwich
I like to eat burger
I like to eat fries

silicon_valley = ["google", "amd", "yahoo", "cisco", "oracle"]


for index_value in range(len(silicon_valley)):
print(f"The index value of '{silicon_valley[index_value]}' is {index_value}")

The index value of 'google' is 0


The index value of 'amd' is 1
The index value of 'yahoo' is 2
The index value of 'cisco' is 3
The index value of 'oracle' is 4

160
Bubble sort
def bubble_sort(list_items):
for i in range(len(list_items)):
for j in range(len(list_items)-i-1):
if list_items[j] > list_items[j+1]:
temp = list_items[j]
list_items[j] = list_items[j+1]
list_items[j+1] = temp
print(f"The sorted list using Bubble Sort is {list_items}")

def main():
items_to_sort = [5, 4, 3, 2, 1]
bubble_sort(items_to_sort)

if __name__ == "__main__":
main()
The sorted list using Bubble Sort is [1, 2, 3, 4, 5]

161
Linear search
def read_key_item():
key_item = int(input("Enter the key item to search: "))
return key_item

def linear_search(search_key):
list_of_items = [10, 20, 30, 40, 50]
found = False
for item_index in range(len(list_of_items)):
if list_of_items[item_index] == search_key:
found = True
break
if found:
print(f"{search_key} found at position {item_index + 1}")
else:
print("Item not found in the list")

def main():
key_in_list = read_key_item()
linear_search(key_in_list) Enter the key item to search: 30
30 found at position 3
if __name__ == "__main__": Enter the key item to search: 50
main() 50 found at position 5

162
Some extra examples (1)
def find_sum(list_items):
positive_sum = 0
negative_sum = 0
for item in list_items:
if item > 0:
positive_sum = positive_sum + item
else:
negative_sum = negative_sum + item
average = (positive_sum + negative_sum) / 5
print(f"Sum of Positive numbers in list is {positive_sum}")
print(f"Sum of Negative numbers in list is {negative_sum}")
print(f"Average of item numbers in list is {average}")
print("Items above average are")
for item in list_items:
if item > average:
print(item) Sum of Positive numbers in list is 9
Sum of Negative numbers in list is -6
def main(): Average of item numbers in list is 0.6
find_sum([-1, -2, -3, 4, 5]) Items above average are
4
if __name__ == "__main__": 5
main()

163
Some extra examples (2)
def check_for_sort_order(list_items):
ascending = descending = True
for i in range(len(list_items) - 1):
if list_items[i] < list_items[i+1]:
descending = False
if list_items[i] > list_items[i+1]:
ascending = False
if ascending:
print("Items in list are in Ascending order")
elif descending:
print("Items in list are in Descending order")
else:
print("Items in list are not sorted")

def main():
check_for_sort_order([1, 4, 2, 5, 3])

if __name__ == "__main__":
main() Items in list are not sorted

164
Some extra examples (3)
import math

def statistics(list_items):
mean = sum(list_items)/len(list_items)
print(f"Mean is {mean}")
variance = 0
for item in list_items:
variance += (item-mean)**2
variance /= len(list_items)
print(f"Variance is {variance}")
standard_deviation = math.sqrt(variance)
print(f"Standard Deviation is {standard_deviation}")

def main():
statistics([1, 2, 3, 4]) Mean is 2.5
Variance is 1.25
if __name__ == "__main__": Standard Deviation is 1.118033988749895
main()

165
Some extra examples (4)
stack = [] def main():
stack_size = 3 push_item_to_stack(1)
push_item_to_stack(2)
def display_stack_items(): push_item_to_stack(3)
print("Current stack items are: ") display_stack_items()
for item in stack: push_item_to_stack(4)
print(item) pop_item_from_stack()
display_stack_items()
def push_item_to_stack(item): pop_item_from_stack()
print(f"Push an item to stack {item}") pop_item_from_stack()
if len(stack) < stack_size: pop_item_from_stack()
stack.append(item)
else: if __name__ == "__main__":
print("Stack is full!") main()

def pop_item_from_stack():
if len(stack) > 0:
print(f"Pop an item from stack {stack.pop()}")
else:
print("Stack is empty.")

166
Some extra examples (4)
def main(): Push an item to stack 1
push_item_to_stack(1) Push an item to stack 2
push_item_to_stack(2) Push an item to stack 3
push_item_to_stack(3) Current stack items are:
display_stack_items() 1
push_item_to_stack(4) 2
pop_item_from_stack() 3
display_stack_items() Push an item to stack 4
pop_item_from_stack() Stack is full!
pop_item_from_stack() Pop an item from stack 3
pop_item_from_stack() Current stack items are:
1
if __name__ == "__main__": 2
main() Pop an item from stack 2
Pop an item from stack 1
Stack is empty.

167
Some extra examples (5)
from collections import deque

def queue_operations():
queue = deque(["Eric", "John", "Michael"])
print(f"Queue items are {queue}")
print("Adding few items to Queue")
queue.append("Terry")
queue.append("Graham")
print(f"Queue items are {queue}")
print(f"Removed item from Queue is {queue.popleft()}")
print(f"Removed item from Queue is {queue.popleft()}")
print(f"Queue items are {queue}")

def main():
queue_operations()
Queue items are deque(['Eric', 'John', 'Michael'])
if __name__ == "__main__": Adding few items to Queue
main() Queue items are deque(['Eric', 'John', 'Michael',
'Terry', 'Graham'])
Removed item from Queue is Eric
Removed item from Queue is John
Queue items are deque(['Michael', 'Terry', 'Graham'])

168
Nested lists (list of lists)
• Syntax:
nested_list_name = [[item_1, item_2, item_3],
[item_4, item_5, item_6],
[item_7, item_8, item_9]]

asia = [["India", "Japan", "Korea"],


["Srilanka", "Myanmar", "Thailand"],
["Cambodia", "Vietnam", "France"]]
print(asia[0])
print(asia[0][1])
asia[1][2] = "Philippines"
print(asia)

['India', 'Japan', 'Korea']


Japan
[['India', 'Japan', 'Korea'], ['Srilanka', 'Myanmar', 'Philippines'],
['Cambodia', 'Vietnam', 'France']]

169
Nested lists (example 1)
matrix = [[10, 20],
[30, 40],
[50, 60]]
matrix_transpose = [[0, 0, 0],
[0, 0, 0]]

def main():
for rows in range(len(matrix)):
for columns in range(len(matrix[0])):
matrix_transpose[columns][rows] = matrix[rows][columns]
print("Transposed Matrix is")
for items in matrix_transpose:
print(items)

if __name__ == "__main__":
main() Transposed Matrix is
[10, 30, 50]
[20, 40, 60]

170
Nested lists (example 2)
matrix_1 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

matrix_2 = [[1, 2, 3], Addition of two matrices is


[4, 5, 6], [2, 4, 6]
[7, 8, 9]] [8, 10, 12]
[14, 16, 18]
matrix_result = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]

for rows in range(len(matrix_1)):


for columns in range(len(matrix_2[0])):
matrix_result[rows][columns] = matrix_1[rows][columns] + \
matrix_2[rows][columns]
print("Addition of two matrices is")
for items in matrix_result:
print(items)

171
The del statement
• Remove an item from a list based on its index rather than its
value.
• del statement does not return any value while the pop()
function returns a value.
a = [5, -8, 99.99, 432, 108, 213]
del a[0]
print(a)
del a[2:4]
print(a) [-8, 99.99, 432, 108, 213]
del a[:] [-8, 99.99, 213]
print(a) []

172

You might also like