0% found this document useful (0 votes)
175 views31 pages

Chapter 3 String, List, Tuple & Dictionary Functions

The document provides information about string, list, tuple and dictionary functions in Python. It discusses basic operations like concatenation and replication for strings. For lists, it covers functions like append, insert, remove and sorting. For tuples, it discusses operations like joining and slicing. For dictionaries, it discusses functions like length, clear and keys.

Uploaded by

PRASHANT KUMAR
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
175 views31 pages

Chapter 3 String, List, Tuple & Dictionary Functions

The document provides information about string, list, tuple and dictionary functions in Python. It discusses basic operations like concatenation and replication for strings. For lists, it covers functions like append, insert, remove and sorting. For tuples, it discusses operations like joining and slicing. For dictionaries, it discusses functions like length, clear and keys.

Uploaded by

PRASHANT KUMAR
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 31

Chapter 3

String, List, Tuple


& Dictionary
Functions
STRING
Basic String Operations:
a) String Concatenation Operator (+) b) String Replication Operator (*)
The + operator creates a new The '*' operator when used with String,
string by joining the operand then it creates a new string that is a
strings. number of replications of the input
Example: string.
"Hello" + "Students" e.g.
will result into : "HelloStudents" "XY" * 3 will give: "XYXYXY"
'11' + ‘22’ will give: ‘1122' "3" * 4 will give: "3333“
"a" + "5" will give : "a5" "5" * "7" is invalid
'123' + 'abc' will give: '123abc‘
"a" + 5 is invalid Note: operands must be one string &
other Number
Note: both operands must be strings.
ord( ) and chr( ) Functions:
ord() function: chr() function:
It gives ASCII value/ It is opposite of ord()
number of a single function.
character.

Example: Example:
ord("a") : 97 chr(97) : “a"
ord("Z") : 90 chr(90) : “Z"
Strings Functions & Methods:
1. string.capitalize():
It gives the copy of string with its first character capitalized.
Example:
if t = "sumitrana” then R= t.capitalize()
will create a copy of string in R as,
R = "Sumitrana“
2. string.find(subString[,start [,end]):
It returns the lowest index in the string where the sub string is found
with in the slice range of start & end, returns -1 if sub string not
found.
Example:
t="We are learning Strings. Strings are used in Python"
k = "Strings"
t.find(k) Output: 16 (index no.)
t.find(k,20) Output : 25 (index no.)
t.find(k,20,30) Output: -1 will search from index 20 to 30
t.find(“Hello”) Output: -1 ( as string is not found)
Strings Functions & Methods:
3. string.isalnum(): It gives True, if the characters in
the string are alphanumeric (alphabet or number)
and there is at least one character, False otherwise.
Example:
t ="Hello123” then t.isalnum( ) gives True
t ="Hello 123” then t.isalnum( ) gives False
4. string.isalpha(): it gives True if the characters in
the string are alphabet only and there is at least one
character, False otherwise.
Example:
t ="Hello123” then t.isalpha( ) gives False
t = "Hello” then t.isalpha( ) gives True
Strings Functions & Methods:
5. string.isdigit(): It gives True if the characters in the
string are digits only and there should be at least one
character, False otherwise.
Example:
t ="Hello123“ then t.isalnum() gives False
t= "123” then t.isalnum() gives True
6. string.islower(): this function gives True, if all letters
in given string are lowercase, otherwise it gives False.
Example:
if t="sunday" then t.islower() gives True
if t="Sunday" then t.islower() gives False

7. string.isupper(): opposite of islower()


Strings Functions & Methods:
8. string.isspace(): It gives True if there are only
whitespace characters in the string, False otherwise.
Example:
if t=" " then t.isspace() gives True
if t=" S“ then t.isspace()gives False
9. string.upper(): This function gives a copy of the string
converted to uppercase.
Example:
if t="SUNday12“ then x=t.upper() will store "SUNDAY12“ in x.
10. string.lower(): Opposite of upper()
11. len(string): This function gives the length of given string.
Example:
Len(“Top Brand”) gives 9
LIST
eval( ) function:
It is used to evaluate and return the result of
an expression given as string.
Example:
eval(‘5+8’) => 13
y = eval(“3*10”)
print(y) => 30
len(L) function:
It returns the number of items(count)in the list L.
Example:
L = [ ‘a’,’e’,’i’,’o’,’u’ ]
len(L) => 5
Repeating or Replicating Lists:
* operator used to replicate a list specified number of
times.
Example:
L = [1, 3, 5]
L * 3 => [1,3,5,1,3,5,1,3,5]
Slicing the Lists:
List [start:stop] creates a list slice out of list L with
elements falling between indexes start and stop, not
including stop.
Example:
L = [ 10, 12, 14, 20, 22, 24, 30, 32, 34 ]
Lnew = L [3: -3]
Lnew => [20, 22, 24]
Appending Elements to a List:
The append( ) function adds a single item to the end
of the list.
Example:
L = [1, 3, 5]
L.append(9) => [1, 3, 5, 9]
Updating Elements to a List:
To update or change an element of the list L place,
just assign new value to the element’s index in List.
Example:
L = [ 10, 12, 14, 20 ]
L[ 2 ] = 16
L => [10, 12, 16, 20]
Deleting Elements from a List:
The del statement is used to remove an individual item at index or to
remove all items identified by a slice.
Example:
L= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
del L[10]
L => [1,2,3,4,5,6,7,8,9,10,12,13,14,15,16]
del L[10:13]
L => [1,2,3,4,5,6,7,8,9,10,15,16]
pop( ) function:
It is used to remove single element, not list slices. It also returns the
removed item along with deleting it from List.
Example:
L= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
L.pop( ) => 16 (means Last Item)
L.pop(10) => 11
List Functions and Methods:
(i) index( ) method:
It returns the index of first matched item from the List. If the given item is
not in the List, it raises exception value error.
Example:
L= [13, 18, 11, 16, 18, 14]
L.index(18) => 1
L.index(33) => ValueError: 33 is not in List
(ii) extend( ) function:
It is used for adding multiple elements (given in the form of a list).
Example:
L1= [ ‘a’, ‘b’, ‘c ]
L2 = [‘d’, ‘e’ ]
L1.extend(L2)
L1 => [ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
L3 = L1.extend(L2)
L3 => L3 is empty, will not return any value, but L1 is extended with
elements of L2 again.
List Functions and Methods:
(iii) insert( ) method:
append( ) and extend( ) insert the elements at the end of the
list, but insert( ) method insert the element in between or any
position of choice.
Example:
L= [‘a’, ‘e’, ‘u’]
L.insert(2, ‘i’)
L => [ ‘a’, ‘e’, ‘i’, ‘u’ ]
(iv) remove( ) function:
It removes the first occurrence of given item from the list.
Example:
L1= [ ‘a’, ‘e’, ‘i’, ‘p’, ‘q’, ‘a’, ‘q’, ‘p’ ]
L1.remove( ‘p’ )
L1 => [ ‘a’, ‘e’, ‘i’, ‘q’, ‘a’, ‘q’, ‘p’]
List Functions and Methods:
(v) clear( ) method:
It removes all the items from the list and the list becomes
empty. It removes only items of list but not the existence
of the list, but del(List) removes the items along with
existence of the list also.
Example:
L= [ 2, 3, 4, 5 ]
L.clear( )
L => [ ]
(vi) count( ) function:
It returns count of the item that is passed as argument.
Example:
L1= [ ‘a’, ‘e’, ‘i’, ‘p’, ‘q’, ‘a’, ‘q’, ‘p’ ]
L1.count( ‘q’ ) => 2
List Functions and Methods:
(vii) reverse( ) method:
It reverses the items of the list.
Example:
L= [ 2, 3, 4, 5 ]
L.reverse( )
L => [ 5, 4, 3, 2 ]
L1 = L,reverse( )
L1 => L1 stores nothing as reverse( ) does not return anything
(viii) sort( ) function:
It sorts the items of the list, by default in increasing order.
Example:
L= [ ‘e’, ‘i’, ‘q’, ‘a’, ‘q’, ‘p’ ]
L.sort( )
L => [ ‘a’, ‘e’, ‘i’, ‘p’, ‘q’, ‘q’]
L.sort(reverse = True) # for decreasing order
L => [ ‘q’, ‘q’, ‘p’, ‘i’, ‘e’, ‘a’ ]
TUPLE
Tuple Operations:
Joining Tuples:
+ operator is used to join two tuples. Both the operands
must be of tuple types.
Example:
T1 = (1, 3, 5)
T2 = (6, 7, 8)
T1 + T2 => (1, 3, 5, 6, 7, 8)
Slicing the Tuples:
List [start:stop] creates a list slice out of list L with elements
falling between indexes start and stop, not including stop.
Example:
T = ( 10, 12, 14, 20, 22, 24, 30, 32, 34 )
Tnew = L [3: -3]
Tnew => (20, 22, 24)
Unpacking Tuples:
Creating individual values from a tuple’s elements is called unpacking.
Example:
T = (1, 2, ‘A’, ‘B’)
w, x, y, z = T
print (w) => 1
print (x) => 2
print (y) => ’A’
print (z) => ’B’
Deleting Tuples:
Tuples are immutable, so that individual elements of a tuple cannot be
deleted.
Example:
T = ( 10, 12, 14, 20 )
del T[2] => # Error: Tuple doesnot support Item deletion
del T
print( T) =># Error: T is not defined
Tuple Functions and Methods:
(i) max( ) and min( ) method:
It returns the element from the tuple having maximum and minimum
value.
Example:
T= (10, 12, 14, 20, 22, 24, 30, 32, 34)
max( T ) => 34
min( T ) => 10
(ii) extend( ) method:
It is used for adding multiple elements (given in the form of a list).
Example:
L1= [ ‘a’, ‘b’, ‘c ]
L2 = [‘d’, ‘e’ ]
L1.extend(L2)
L1 => [ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
L3 = L1.extend(L2)
L3 => L3 is empty, will not return any value, but L1 is extended with
elements of L2 again.
List Functions and Methods:
(iii) index( ) method:
It returns the index of an existing element of a tuple.
Example:
T= (‘a’, ‘e’, ‘u’)
T.index( ‘e’ ) => 1
(iv) count( ) function:
It returns the count of a member element in a given
tuple.
Example:
T1= ( ‘a’, ‘e’, ‘i’, ‘p’, ‘q’, ‘a’, ‘q’, ‘p’ )
T1.count( ‘p’ ) => 2
List Functions and Methods:
(v) tuple( ) method:
It is used to create tuples from different types of
values.
Example:
tuple( ) => ( ) # emppty tuple
T = tuple([1,2,3])
T => (1,2,3) # Creating a tuple from a List
T = tuple(“abc”)
T => (‘a’,’b’,’c’) # Creating a tuple from a String
Q. Write a Program to find the second
largest element in the tuple
Ans:
T = ( 23, 45, 34, 66, 77, 67, 70)
maxvalue= max(T)
length = len(T)
secmax= 0
for a in range(length):
if secmax< T[a] < maxvalue:
secmax= T[a]
print ("second largest value is :", secmax)
DICTIONARY
Dictionary Functions and Methods:
(i) len( ) method:
It returns length of the dictionary.
Example:
T1 = {“Number”:5, ”String”:”a”, ”Tuple”:(1,2)}
len(T1)
Output:
Number : 3
(ii) clear() method:
It removes all items from the dictionary and the dictionary becomes empty. It
does not delete the dictionary, it just delete all the elements inside the
dictionary.
Example:
T1 = {“Number”:5, ”String”:”a”, ”Tuple”:(1,2)}
T1.clear( )
T1 => { }
del T1
T1 => NameError : nameT1 is not defined
Note: del statement deletes existence of dictionary.
Dictionary Functions and Methods:
(iii) get( ) method:
It is used to get the item with the given key.
Example:
T1 = {"Name":"Vijay", "Salary":10000,"Age":24}
T1.get(“Salary”)
Output:
10000
Dictionary Functions and Methods:
(iv) items( ) method:
It returns all the items in the dictionary as a sequence of
(key, value).
Example:
T1 = {"Name":"Vijay", "Salary":10000,"Age":24}
myList = T1.items( )
for x in myList:
print(x)
Output:
('Name', 'Vijay')
('Salary', 10000)
('Age', 24)
Dictionary Functions and Methods:
(v) keys( ) method:
It returns all of the keys in the dictionary as a sequence of
keys.
Example:
T1 = {"Name":"Vijay", "Salary":10000,"Age":24}
T1.keys( )
Output:
['Name', 'Salary’, 'Age’]
(vi) values( ) method:
It returns all the values from the dictionary as a sequence.
Example:
T1 = {"Name":"Vijay", "Salary":10000,"Age":24}
T1.values( )
Output:
[‘Vijay', 10000,24]
Dictionary Functions and Methods:
(vii) update( ) method:
This method merges key:value pairs from the new dictionary
into the original dictionary, adding or replacing as needed.
The items in the new dictionary are added to the old one and
override any items already there with the same keys.
Example:
T1 = {"Name":"Vijay", "Salary":10000,"Age":24}
T2 = {"Name":“Tushar", "Salary":25000,“Dept":”Sales”}
T1.update(T2)
T1
{"Name":“Tushar", "Salary":25000,”Dept”:”Sales”,"Age":24}
T2
{"Name":“Tushar", "Salary":25000,“Dept":”Sales”}
THANK YOU

You might also like