You are on page 1of 21

Computer Organization

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

insert() Adds an element at the specified position

pop() Removes the element at the specified position

remove() Removes the item with the specified value

reverse() Reverses the order of the list


7
sort() Sorts the list
Basics: Lists (Cont’d)
› List methods provide a handy way to manipulate a list
– my_list.append(4) #Adds 4 to the last of the list
– my_list.extend([1,2,3,4]) #Adds the list passed to this method to the
first list
– my_list.insert(2,4) #Inserts 4 at index 2 (Elements after index 2 are
pushed back one index)
– my_list.remove(4) #Removes the first occurrence of 4 in the list
– my_list.index(4) #Returns the index of the first occurrence of 4 in the
list
– my_list.sort() #Sorts a list in ascending order
– my_list.reverse() #Reverses the order of the elements of the list

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

› Packages have the extension .whl (wheel files)

› Another (easier) way to install Python packages:


– pip

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

› Some packages are used


extensively, and even other
packages rely on them

This is where “Add Python to PATH” comes in handy


15
Python Packages (Cont’d)
› There is a package for everything!

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

› Allow us to use classes and functions defined in another file

› Three formats of the command:


import somefile #Imports everything from the file
from somefile import * #Imports all the functions. They can be used without
the dot notation
from somefile import className #Imports a specific class and its methods
from a file
› We are going to stick with importing everything (import numpy,
import random etc.)

19
Example
› Mathematical functions using math
› Generating random numbers using math

20
Next week: MIPS and
Assembly!
Advice:
Download MARS simulator

21

You might also like