0% found this document useful (0 votes)
8 views9 pages

Task 2

good
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Task 2

good
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Task 4: WAP to demonstrate all functions of String.

Syntax:
a="hello"
b="WORLD"
b1="1234"
c="This is a python program"
d="I am a student of BBSBEC, BBSBEC is an Engineering college"
print(a.capitalize()) # Converts the first character to upper case
print(a.center(20)) # Returns a centered string
print(d.count("BBSBEC")) # Returns the number of times a specified value occurs in a string
print(d.find("student")) # Searches the string for a specified value and returns the position
print("In",b,"L is present at",b.index("L")) # Searches the string for a specified value and returns the position

if a.isalnum(): # Returns True if all characters in the string are alphanumeric


print(a,"is Alphanumeric")

if a.isalpha(): # Returns True if all characters in the string are in the alphabet
print(a,"is Alphabet")

if b1.isdecimal(): # Returns True if all characters in the string are decimals


print(b1,"is decimal")

if a.islower(): # Returns True if all characters in the string are lower case
print(a,"is Lowercase")

print(b.lower()) # Converts a string into lower case


print(c.replace("a","an")) # Returns a string where a specified value is replaced with a specified value
print(c.split()) # Splits the string at the specified separator, and returns a list
print(a.upper()) # Converts a string into upper case

2200485 Page 5
Output:

2200485 Page 6
Task 5: WAP to demonstrate all functions of List.

Syntax:
l1 = ['apple', 'banana', 'cherry']
l2=['SUV','BMW','Enova',47,23,12,47]
l3=['hello!','hello!']
l1.append('orange') # Adds an element at the end of the list
print(l1)

l1.clear() # Removes all the elements from the list

l1=l2.copy() # Returns a copy of the list


print(l1)

print("47 is present",l2.count(47),"times in list") # Returns the number of elements with the specified value

point=(1,2,3,4)
l3.extend(point) #Add elements of list to the current list
print(l3)

l3.remove(4) # Removes the first item with the specified value


print(l3)

print(l2.index('SUV')) # Returns the index of the first element with the specified value

l3.insert(4,6) # Adds an element at the specified position


print(l3)

l=[7,3,4,9,1,2]
l.pop() # Removes the element at the specified position
print(l)

l.sort() # Sorts the list


print(l)

2200485 Page 7
Output:

2200485 Page 8
Task 6: WAP to demonstrate all functions of Tuple.

Syntax:
tupl=('hello','bye',1,1,1,2,3,4,4,0)

c=tupl.count(1) #Returns the number of times a specified value occurs in a tuple


print("1 occurs",c,"times in the tuple")

n=tupl.index(0) #Searches the tuple for a specified value and returns the position of where it was found
print("0 is present at index",n)

Output:

2200485 Page 9
Task 7: WAP to demonstrate all functions of Dictionary.

Syntax:
stud={
'name':'Pankaj',
'roll_no':45,
'branch':'CSE'
}
new=stud.copy() #Returns a copy of the dictionary
print(new)

D=('key1','key2','key3')
d=1
Dict=dict.fromkeys(D,d) #Returns a dictionary with the specified keys and value
print(Dict)

val=stud.get('branch') #Returns the value of the specified key


print(val)

item=stud.items() #Returns a list containing a tuple for each key value pair
print(item)

key=stud.keys() #Returns a list containing the dictionary's keys


print(key)

value=stud.values() #Returns a list of all the values in the dictionary


print(value)

Dict.pop('key3') #Removes the element with the specified key


print(Dict)

x=Dict.setdefault('key3','2') #Returns the value of the specified key. If the key does not exist: insert the key,
with the specified value
print(x)

stud.popitem() #Removes the last inserted key-value pair


print(stud)

stud.update({'cors':'CSE'}) #Updates the dictionary with the specified key-value pairs


print(stud)

Dict.clear() #Removes all the elements from the dictionary


print(Dict)

2200485 Page 10
Output:

2200485 Page 11
Task 8: WAP to demonstrate all functions of Set.

Syntax:
fruits = {"apple", "banana", "cherry"}

fruits.add("orange") #Adds an element to the set


print(fruits)

fruits.clear() #Removes all the elements from the set


print(fruits)

fruits = {"apple", "banana", "cherry"}

x = fruits.copy() #Returns a copy of the set


print(x)

y = {"google", "microsoft", "apple"}


z = x.difference(y) #Returns a set containing the difference between two or more sets
print(z)

x.difference_update(y) #Removes the items in this set that are also included in another, specified set
print(x)

fruits.discard("cherry") #Remove the specified item


print(fruits)

x1 = {"apple", "banana", "cherry"}


y1 = {"google", "microsoft", "apple"}
z = x1.intersection(y1) #Returns a set, that is the intersection of two other sets
print(z)

x1.intersection_update(y1) #Removes the items in this set that are not present in other, specified set(s)
print(x1)

print(x1.isdisjoint(y1)) #Returns whether two sets have a intersection or not

X = {"a", "b", "c"}


Y = {"f", "e", "d", "c", "b", "a"}
Z = X.issubset(Y) #Returns whether another set contains this set or not
print(Z)

print(Y.issuperset(X)) #Returns whether this set contains another set or not

2200485 Page 12
x2 = {"apple", "banana", "cherry"}
y2 = {"google", "microsoft", "apple"}
z2 = x2.symmetric_difference(y2) #Returns a set with the symmetric differences of two sets
print(z2)

x2.symmetric_difference_update(y2) #Inserts the symmetric differences from this set and another
print(x2)

n1={1,2,3,1}
n2={3,4,5}
n3=n1.union(n2) #Return a set containing the union of sets
print(n3)

n1.update(n2) #Update the set with the union of this set and others
print(n1)

Output:

2200485 Page 13

You might also like