Professional Documents
Culture Documents
Lab #6
Dr Sajid Gul Khawaja
LE Kashaf Raheem
Python and Algorithms
Aim
Exploring python syntax and problem-
solving w.r.t list and packages
2
Python Syntax
Lists, packages and more
3
Use of Else with Loops
› for i in ['T','P']:
– print(i)
› else:
– # Loop else statement
– # there is no break statement in for loop, hence else part gets
executed directly
– print("For Loop-else statement successfully executed")
› Result:
–T
–P
– For Loop-else statement successfully executed
4
Contd.
› for i in ['T','P']:
– print(i)
– if (i == ‘T’):
› break
› else:
– # Loop else statement
– # there is no break statement in for loop, hence else part gets
executed directly
– print("For Loop-else statement successfully executed")
› Result:
–T
5
Nested loops
PROBLEM SOLUTION
› Write a python code which can › N = int(input("Enter End Term: "))
print the following
› for i in range(N):
– 0
– 0 1 › for j in range(i+1):
– 0 1 2…. › print(j, end=" ")
› print(end="\n")
› print("End of Program")
6
Lists and Python
› Python has numerous built-in functions for provide ease
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
8
Example(s)
› Extending a list
– var_list = [1, 2, 3]
– var_list.append(9)
– print(var_list)
› Reversing
– var_list = [1, 2, 3]
– var_list.reverse()
9
Tasks
Make sure to comment your code.
Use appropriate variable name
Know the location where file is being saved
Create different files for different tasks
10
Task 1:
› Design an Algorithm and write a program in python
check if the user entered word is in alphabetical order
or not
– If all the letters are not in alphabetical order, then your
program should display till which letter they are in
alphabetical order.
Task 2:
› Design an Algorithm and write a program in python
that extracts all unique characters entered in a
sentence. E.g.,
› User Enters:
– Question for Lab 6
› Output is:
– There are 15 Unique Letters including:
– Q, U, E, S, T, I, O, N, , F, R, L, A, B, 6,
12
Task 3:
a) Write a program in python that gets a string from
user and checks whether the user entered data is a
palindrome or not. E.g.,
› User enters: LEVEL, It is a palindrome as Reverse of this remains
LEVEL
› User enters: 12344321, It is a palindrome as Reverse of this remains
12344321
b) Edit the code such that it re-runs until user enters No
or Quit
13
Python Packages
› Just like libraries in C++/C#/Java, Python has packages
14
Python Packages (Cont’d)
› Installing a Python package is a
matter of executing a single
command in command prompt
(cmd):
– pip install name-of-the-package-
you-want-to-install
16
Some Commonly Used Python Packages
› NumPy:
– For numerical/mathematical processing pip install numpy
› OpenCV:
– For image processing pip install opencv-python
› Matplotlib:
– For plotting graphs and visualizing data pip install matplotlib
› Pandas:
– For handling/analyzing data pip install pandas
› Scipy:
– For scientific/technical computing pip install scipy
› Scikit-learn:
– For machine learning pip install scikit-learn
17
Some Commonly Used Python Packages (Cont’d)
› sys
– Lots of handy stuff
› os
– interacting with the operating system
› math
– Mathematical code
› Random
– Generating random numbers
› Copy
– Dealing with shallow copy issues
18
Importing Python Packages
› As discussed earlier, Python packages are akin to C++ libraries
19
Example
› Mathematical functions using math
› Generating random numbers using math
20
Next week: MIPS and
Assembly!
Advice:
Download MARS simulator
21