Professional Documents
Culture Documents
Sequences
Course Instructors
Dr. Majid Iqbal Khan Dr. Ashfaq Hussain Farooqi Mr. Rizwan Rashid Ms. Sajida Kalsoom
Agenda
2
What are Sequences?
Sequences can be consider as container where different items (int, float, object) are
stored
Following are three sequences of length four
Each fruit/number/character in these sequences are called element of the sequence
Sequence s1
Sequence s2 12 3 6 32
Concatenation
Repetition
Membership
testing
Indexing Sequence s
Slicing
Other useful 4
operations
Concatenation
s=
5
Repetition
Sequence s1
Repeat
Sequence s1
3 times
Code: s1 = s1 x n
Or s1 = n x s1 # n copies of sequence s concatenated
s1 =
6
Membership testing
Sequence s
Is a member? Is a member?
False True
Code: x in s # True if element x is in sequence s. Code: x not in s # True if element x is not in sequence s.
Indexing
Index: 0 1 2 3 4 5
Element: s[0] s[1] s[2] s[3] s[4] s[5]
Traverses elements from left to right in a for loop. Following code will all I elements in Sequence s
Code: for i in range (0, 6): OR for i in s:
print (s[i]) print (s)
8
Slicing
Sequence s
Index: 0 1 2 3 4 5
Operation Description
len(s) Length of sequence s, i.e., the number of elements in s.
min(s) Smallest element in sequence s.
max(s) Largest element in sequence s.
sum(s) Sum of all elements in sequence s.
<, <=, >, >=, =, != Compares two sequences.
10
Sequences in Python
Tuples Sets
11
List
List is one of the important sequences available in Python where different items can be stored
list1 = list() # Create an empty list having name list1
OR list1 = []
Following two definitions can only be made using list1 = list() and not with list1 = []
3 4 5
import random
num1 = [2, 4, 6, 8, 10] Output
random.shuffle(num1)
shuffle print ( num1 ) - [10, 4, 6, 8, 2]
15
List methods
num1 = [2, 4, 6, 8, 10]
num2 = [1, 3] Output
num1.append(20)
append print ( num1 ) - [2, 4, 6, 8, 10, 20]
num1.insert(5, 4)
Insert(index, element) print ( num1 ) - [2, 4, 6, 8, 10, 4, 20]
num1.extend(num2)
extend print ( num1 ) - [2, 4, 6, 8, 10, 4, 20, 1, 3]
print ( num1.count(4) ) - 2
count 16
List methods
print ( num1.pop(2) ) - 6
pop print ( num1.pop() ) 3
num1.remove(4) - [2, 6, 8, 10, 4, 20, 1, 3]
remove print ( num1 )
num1.reverse()
reverse print ( num1 ) - [3, 1, 20, 4, 10, 8, 6, 4, 2]
num1.sort()
sort print ( num1 ) - [1, 2, 3, 4, 4, 6, 8, 10, 20]
17
Example – storing marks of the students
18
Exercise
Write a program that reads unspecified number of integers from user into the list and
displays them in the reverse order in which they were read.
Enter integers for the list or 0 to print the list in reverse order:
6
3
4
5
0
19
To copy the data in one list to another list,
Copying Lists you have to copy individual elements
from the source list to the target list.
list2 = list1
However, this statement does not copy the contents of the list referenced by list1 to list2;
instead, it merely copies the reference value from list1 to list2.
After this statement, list1 and list2 refer to the same list
20
Copying Lists
The list previously referenced by list2 is no longer referenced; it
becomes garbage.
The memory space occupied by list2 will be automatically collected and
reused by the Python interpreter.
Before the assignment statement, list1 and list2 point to separate memory locations. After the assignment, the
list1 reference value is passed to list2.
Copying Lists
or simply:
list2 = [ ] + list1
22
When passing a list to a function,
the contents of the list may change
Passing Lists to Functions after the function call, since a list
is a mutable object.
Since list is an object, passing a list to a function is just like passing an object to a function.
Signature
def printList(lst):
Function
Definition for element in lst:
Body
print(element)
Invoking
Function printList([3, 1, 2, 6])
23
When a function returns
Returning a List from a Function a list, the list’s reference
value is returned.
24
Strings
25
Functions for Strings
All of the basic operations discussed in the context of sequences also apply to
the Stings.
Concatenation
Indexing
26
Testing Strings
islower() Returns True if all characters in this string are lowercase s = "welcome to python"
letters and there is at least one character. s.islower() => True
isupper() Returns True if all characters in this string are uppercase
letters and there is at least one character.
isspace() Returns True if this string contains only whitespace s = "welcome to python"
characters. s.isspace() => False 27
Searching for Substrings
find(s1) Returns the lowest index where s1 starts in this string, s = "welcome to python"
or –1 if s1 is not found in this string.
s.find("become") => -1
rfind(s1) Returns the highest index where s1 starts in this string,
or –1 if s1 is not found in this string.
count(substring) Returns the number of non-overlapping occurrences s = "welcome to python"
of this substring.
s.count("o") => 3
28
Other String functions
Converting String
Methods let you control the capitalization of letters in the string’s copy, or to replace the string
entirely.
Functions: capitalize(), lower(), upper(), swapcase(), replace(old, new)
Stripping Whitespace Characters from a String
Methods to strip whitespace characters from the front, end, or both the front and end of a string.
Functions: lstrip(), rstrip(), strip()
Formatting Strings
Methods to return a formatted string
Functions: center(width), ljust(width), rjust(width), format(items)
29
Checking Palindromes
A string is a palindrome if it reads the same forward and backward. The words “mom,”
“dad,” and “noon,” for instance, are all palindromes.
30