Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
STRING FUNCTIONS:
Do it at your own:
Number of alphabets:
Number of digits:
LISTS IN PYTHON
L=list()
L=list(“hello”)
>>>L
>>>L
>>>L
[2,3,4,5,6,7]
1. Lists are stored in memory exactly like strings, only difference is that list
store a reference at each index instead of a single character as in strings.
2. Lists are mutable but strings are immutable.
LIST OPERATIONS:
>>>L1*3
[1,3,9,1,3,9,1,3,9]
Only using assignment operator does not make a copy of the list
e.g. colors=[‘red’,’blue’,’green’]
b=colors #does not copy the list
LIST FUNCTIONS
1. index method: returns the index of first matched item from the list
>>>L=[10,12,14,16]
>>>L.index(12)
1
2. append method: adds an item to the end of the list
>>>L=[10,12,14,16]
>>>L.append(18)
>>>L
[10,12,14,16,18]
3. extend method: used to add multiple elements(in form of list) to the list.
>>>L=[10,12,14,16]
>>>L1=[20,21,22]
>>>L.extend(L1) # argument is a list
>>>L
[10,12,14,16,20,21,22]
4. insert method: inserts an item at a given position
>>>L=[10,12,14,16]
>>>L.insert(3,15) #insert(pos, item)
>>>L
[10,12,14,15,16]
5. pop method: used to remove an item from the list using index
List.pop(index)
>>>L=[10,12,14,16]
>>>e=L.pop(0)
>>>e
10
6. remove method: used to remove first occurrence of given item from list.
List.remove(value)
>>>L=[10,12,14,16]
>>>L.remove(10) #first occurrence of passed value will be removed.
>>>L
[12,14,,16]
7. clear method: remove all items from the list and make it empty
>>>L=[10,12,14,16]
>>>L.clear()
>>>L
[]
For deleting the list del <list> statement to be used
8. count method: returns the count of item passed as argument. if item is not
in the list, returns 0
>>>L=[18,20,15,20,24,20]
>>>L.count(20)
>>>3
9. reverse method: it reverse the items in the list
>>>L=[10,12,14,16]
>>>L.reverse()
>>>L
[16,14,12,10]
10.sort method: sorts the items in the list
>>>L=[10,9,14,16]
>>>L.sort()
>>>L
[9, 10,14,16]
TUPLES IN PYTHON
T=tuple(“hello”)
>>>T
>>>T
>>>T
(2,3,4,5,6,7)
TUPLE OPERATIONS
>>>T1*3
(1,3,9,1,3,9,1,3,9)
>>>T=(1,2,3,4,5)
>>>len(T)
5
2. max() method:returns the maximum element from the tuple
>>>T=(1,2,3,4,15)
>>>max(T)
15
Dictionaries in PYTHON
It is an unordered collection of elements in the form of key-value pairs
that associate keys to values. Dictionaries are mutable.
Creating a dictionary
Emp= {“john”: 25000, “blake”:30000, “smith”:40000}
Key are : john, blake and smith
Corresponding values are: 25000,30000 and 40000
Updating existing elements in a dictionary
>>>Emp[“john”]=50000
>>>Emp
{“john”: 50000, “blake”:30000, “smith”:40000}
>>>Emp1.update(Emp2)
>>>Emp1
Emp1= {“john”: 25000, “blake”:30000, “smith”:25000, “kevin”: 50000,
“lalit”:30000}
WAP that checks for presence of a value inside a dictionary and print its
key.
Sol. Info={“Riya”:”Csc.”, “Mark”:”Eco”,”Ishpreet”:”Eng”,”Kamaal”:”EVS”}
inp=input(“enter the value to be searched for”)
if inp in info.values():
for a in info:
if info[a]==inp:
print(“The key of given value is”,a)
break
else:
print(“Given value does not exist in the dictionary”)
SORTING TECHNIQUES
SORTING: Arranging elements in a specific order ascending or descending.
1. Bubble Sort:The basic idea of bubble sort is to compare two adjoining
values and exchange them if they are not in proper order.
Program to sort a list using Bubble Sort
aList=[15,6,13,22,3,52,2]
print(“orginal list is:”, aList)
n=len(aList)
for i in range(n):
for j in range(0,n-i-1):
if aList[j]>aList[j+1]:
aList[j],aList[j+1]=aList[j+1],aList[j]
print(“List after sorting”, aList)
2. Insertion Sort: It is a sorting algorithm that builds a sorted list one
element at a time from the unsorted list by inserting the element at its
correct position in the sorted list.
Program to sort a sequence using insertion sort.
Sol. aList=[15,6,13,22,3,52,2]
print(“orginal list is:”, aList)
for in range(1,len(aList)):
key=aList[i]
j=i-1
while j>=0 and key< aList[j]:
aList[j+1]= aList[j]
else:
aList[j+1]=key
print(“List aftersorting”,aList)
********************