BUILT-IN FUNCTIONS
STRING
LIST
TUPLE
Empty string:
S=""
S1=str()
Empty list:
L=[]
L1=list()
Creating a string:
S1=str(5)
O/p: '5'
Accessing a string:
>>> var = “hello”
>>> var[0] #h
>>> var[-2] #l
Creating a list:
L1=list('Come')
O/p: ['C’, 'o', 'm', 'e']
Accessing a list:
>>> L1 = [10,20,30]
>>> L1[1] #20
>>> L1[-1] #30
Empty tuple:
T=()
T1=tuple()
Note: Inside the tuple built-in
function we can assign only one
argument.
Creating a tuple:
T1=tuple('Come')
O/p: ('C’, 'o', 'm', 'e')
Accessing a tuple:
>>> T1 = [10,20,30]
>>> T1[1] #20
>>> T1[-1] #30
Traversing a string:
Using for loop and while loop
Concatenation:
str1 + str2
Traversing a list:
Using for loop and while loop
Concatenation:
List1 + List2
Traversing a tuple:
Using for loop and while loop
Concatenation:
Tuple1 + Tuple2
Repetition:
str1 * 2 (Integer only)
Membership Testing:
in and not in operator
Indexing:
str_name[index]
>>> var = “hello”
>>> var[0] #h
>>> var[-2] #l
Repetition:
List1 * 2 (Integer only)
Membership Testing:
in and not in operator
Indexing:
List_name[index]
>>> L1 = [10,20,30]
>>> L1[1] #20
>>> L1[-1] #30
Repetition:
Tuple1 * 2 (Integer only)
Membership Testing:
in and not in operator
Indexing:
tuple_name[index]
>>> T1 = (10,20,30)
>>> T1[1] #20
>>> T1[-1] #30
DICTIONARY
Empty dictionary:
D={}
D1=dict()
Creating a dictionary:
D1=dict()
O/p: { }
Accessing a dictionary:
Dictionary_name[key]
Note: This syntax returns only value.
>>> d={1:10,2:20}
>>> d[2] #20
Traversing a dictionary:
Using for loop only
Appending values:
Dictionary_name[key]=value
>>> d={1:10,2:20}
>>> d[3]=30
>>> print(d) #{1:10,2:20,3:30}
__
Membership Testing:
in and not in operator
Indexing:
Dictionary_name[key]
>>> d={1:10,2:20}
>>> d[2] #20
Page | 1
Slicing:
string[start : stop : step] (or)
string[range]
>>> a=’save money’
>>> a[1:6] # ’ave m’
>>> a[10:15] # ’ ‘
>>> a[-10:2] # ‘sa’
(No error )
Note: Output always display in list
only.
Aliasing:
newstr = oldstr
>>> s1=’hello’
>>> s2 = s1
>>> print(s1) #’hello’
>>> print(s2) #’hello’
Slicing:
list[start : stop : step]
>>> L1 = [1,2,3,4,5]
>>> L1[1:4] #[2,3,4]
>>> L1[-10:3] #[1,2,3]
>>> L1[3:10] #[4,5]
>>> L1[7:10] #[]
(No error )
Note: Output always display in list
only.
Aliasing:
newlist = oldlist
>>> L1=[1,2,3]
>>> L2 = L1
>>> L2[2] = 200
>>> print(L2) #[1,2,200]
>>> print(L1) #[1,2,200]
Copying tuple:
Newstr = str(oldstr)
Copying lists:
newlist = oldlist[ : ]
newlist = list(oldlist)
newlist = oldlist.copy()
Note: String has no attribute
‘copy()’
Join(sequence):
Str.join(sequence)
>>> s=’1234’
>>> s1=’-‘
>>> s.join(s1) #’1-2-3-4
split():
str.split([separator,maxsplit])
>>> s=’hello’
>>> s.split(‘l’) # ['he', '', 'o']
>>> s.split() #['hello']
Note: It always return output as
Slicing:
Tuple_name[start : stop : step]
>>> T1 = (1,2,3,4,5)
>>> T1[1:4] #(2,3,4)
>>> T1[-10:3] #(1,2,3)
>>> T1[3:10] #(4,5)
>>> T1[7:10] #()
(No error )
Note: Output always display in tuple
only.
Aliasing:
newtuple = oldtuple
>>> T1 = (1,2,3)
>>> T2 = T1
>>> print(T1) #(1,2,3)
>>> print(T2) #(1,2,3)
Here, we can’t change the value
because it is immutable.
Copying tuple:
Newlist = tuple(oldlist)
Slicing:
Slicing cannot be done in dictionary.
Aliasing:
newdict = olddict
>>> d1 = {1:10,2:20,3:30}
>>> d2 = d1
>>> print(d1) #{1:10,2:20,3:30}
>>> print(d2) #{1:10,2:20,3:30}
Copying dictionary:
D1 = d.copy()
Note: Tuple has no attribute ‘copy()’
append():
list.append(item)
append():
Tuple has no attribute ‘append’
because it is immutable.
append():
Dictionary has no attribute ‘append’.
extend():
list1.extend(list2)
extend():
Tuple has no attribute ‘extend’
because it is immutable.
extend():
Dictionary has no attribute ‘extend’.
Page | 2
list.
partition():
str.partition(separator)
>>> s=’hello’
>>> s.partition(‘l’) #('he', 'l', 'lo')
>>> s.partition() # TypeError:
str.partition() takes exactly one
argument (0 given)
Note: Partition into 3 parts. It
always return output as tuple.
reverse():
String has no attribute ‘reverse’
find():
str.find(substring,start,end)
>>> s = ‘hello’
>>> s.find(‘l’) #2
>>> s.rfind(‘l’) #3
Note: rfind returns the last
(rightmost) occurrence & returns its
index.
find() returns the first (leftmost)
occurrence & returns its index.
If the substring is not found, it
returns -1 instead of error.
index():
str.index(substring,start,end)
>>> s = ‘hello’
>>> s.index(‘l’) #2
>>> s.index(‘w’) #Error
Note: index() returns the first
(leftmost) occurrence & returns its
index.
If the substring is not found, it
insert():
listname.insert(indexnumber,
value)
>>> a=[1,2,3]
>>> a.insert(-2,4) # [1,4,2,3]
>>> a.insert(7,5) #[1,4,2,3,5]
insert():
Tuple has no attribute ‘insert’
because it is immutable.
insert():
Dictionary has no attribute ‘insert’.
reverse():
listname.reverse()
reverse():
Dictionary has no attribute ‘reverse’.
find():
‘list’ object has no attribute ‘find’
reverse():
‘tuple’ object has no attribute
‘reverse’
find():
‘tuple’ object has no attribute ‘find’
index():
list.index(item)
(or)
list.index(item,start,stop)
index():
tuple.index(item)
(or)
tuple.index(item,start,stop)
index():
Dictionary has no attribute ‘index’.
>>> x = [1,2,3]
>>> x.index(2) #1
>>> x.index(4) #IndexError
>>> x = (1,2,3)
>>> x.index(2) #1
>>> x.index(4) #IndexError
find():
Dictionary has no attribute ‘find’.
Page | 3
returns error.
len():
len(str)
>>> s=’This is Meera\’s pen’
>>> len(s) #19
Note: ‘\’ is counted as one
character.
count():
str.count(element)
>>> a=’hello’
>>> a.count(‘l’) #2
any():
any(str)
>>> a=’hello’
>>> print(any(a)) #True
>>> b=''
>>> print(any(b)) #False
sorted():
sorted(str) #ascending order
sorted(str, reverse=True)
#descending order
>>> s=’123’
>>> sorted(s) # ['1', '2', '3']
>>> sorted(s, reverse=True)
# ['3', '2', '1']
s='hello'
s1=(sorted(s))
print(s)
hello
print(s1)
#['e', 'h', 'l', 'l', 'o']
Note: string has no attribute ‘sort’
sorted() – It creates & returns a
len():
len(listname)
len():
len(tuplename)
len():
len(dict_name)
count():
listname.count(element)
>>> L1 = [1,2,3,3]
>>> L1.count(3) #2
any():
any(listname)
>>> a=[1,2,3]
>>> print(any(a)) #True
>>> b=[]
>>> print(any(b)) #False
sort() and sorted():
list.sort() #ascending order
list.sort(reverse=True) #descending
order
count():
tuplename.count(element)
>>> T1 = (1,2,3,3)
>>> T1.count(3) #2
any():
any(tuplename)
>>> a=(1,)
>>> print(any(a)) #True
>>> b=()
>>> print(any(b)) #False
sorted():
sorted(tuple) #ascending order
sorted(tuple, reverse = True)
#descending order
count():
Dictionary has no attribute ‘count()’
sorted(list) #ascending order
sorted(list, reverse = True)
#descending order
Note: tuple has no attribute ‘sort’
sorted() – It creates & returns a new
sorted tuple without changing the
original one.
Note: sort() – It sorts the original
list in place.
sorted() – It creates & returns a
new sorted list without changing
the original one.
any():
any(dict_name)
>>> d={1:10,2:20,3:30}
>>> print(any(d)) #True
>>> d1= {}
>>> print(any(d1)) #False
sorted():
sorted(dict_name) #ascending order
sorted(dict_name, reverse = True)
#descending order
>>> d={1:10,2:20,3:30}
>>> sorted(d)
>>> sorted(d.items())
>>> sorted(d.keys())
These three are sorted by keys.
>>> sorted(d.values())
This is sorted by values.
Note: dictionary has no attribute ‘sort’
sorted() – It creates & returns a new
sorted dictionary without changing the
original one. Also, it sorts by keys only.
If you want to sort by values, then we
have to mention sorted(d.values())
Page | 4
new sorted string without changing
the original one. It return as list.
clear():
String has no attribute ‘clear’
because it is immutable.
max(), min(), sum():
max(str)
min(str)
>>> s=’hello’
>>> max(s) # ‘o’
>>> min(s) #'e'
sum(str) – TypeError: unsupported
operand type(s) for +: 'int' and 'str'
Updating a string:
In string, we can’t change the value
because it is immutable.
Searching a string:
str.index(element)
Deleting a string:
del string_name
clear():
list_name.clear()
>>> L=[12,23,45]
>>> L.clear() #[ ]
max(), min(), sum():
max(list_name)
min(list_name)
sum(list_name)
Note: All data values must be in
same data type.
clear():
Tuple has no attribute ‘clear’
because it is immutable.
Updating a list:
list[index]=newvalue
Updating a tuple:
In tuple, we can’t change the value
because it is immutable.
Searching a List:
listname.index(element)
Deleting a List:
list.pop(index)
– It deletes the specific position.
list.pop()
– It deletes the last element.
max(), min(), sum():
max(tuple_name)
min(tuple_name)
sum(tuple_name)
Note: All data values must be in
same data type.
Searching a Tuple:
tuplename.index(element)
Deleting a Tuple:
del tuple_name
clear():
dict_name.clear()
>>> d={1:10,2:20,3:30}
>>> d.clear() #{ }
max(), min(), sum():
max(dict_name)
min(dict_name)
sum(dict_name)
Note: Dictionary considers only the
keys. All Keys must be in same data
type.
Updating elements:
Dictionary_name[key]=value
>>> d={1:10,2:20}
>>> d[2]=200
>>> print(d) #{1:10,2:200}
Merging two dictionaries:
Dict_name1.update(Dict_name2)
Searching a Dictionary:
Dictionary has no attribute ‘index’.
Deleting a dictionary:
del dict_name[key]
It deletes the specific key and
value.
del dict_name
It deletes the entire dictionary
del list[index]
Page | 5
dictname.pop(key)
It will return the deleted value.
del list[start_index : end_index]
list.remove(element)
Dict_name.popitem()
It removes the last item and
returns this deleted item.
Note: pop return the deleted
element but del does not return the
deleted element.
capitalize():
str.capitalize()
>>> s=’welcome Home’
>>> s.capitalize() #Welcome home
title():
str.title()
>>> s=’welcome home’
>>> s.title() #Welcome Home
replace():
str.replace(old_ele,new_ele)
>>> s=’welcome home’
>>> s.replace(e,a) #walcoma homa
strip():
str.strip()
- This returns the string after
removing the spaces both
on the left and the right on
__
__
__
__
__
__
__
__
get() method:
dict_name.get(key, default = None)
- This is used to display the value but if
the specified key is not present it
doesn’t show error instead the
specified message has been displayed.
>>> d={1:10,2:20,3:30}
>>> d.get(3) #30
>>> d.get(4) #None
>>> d.get(4,40) #40
items():
dict_name.items()
-It returns as a list of tuples.
>>> d={1:10,2:20,3:30}
>>> d.items()
#dict_items([(1,10),(2,20),(3,30)})
keys():
dict_name.keys()
-It returns as a list of keys.
>>> d={1:10,2:20,3:30}
>>> d.keys() #dict_keys([1,2,3])
values():
dict_name.values()
-It returns as a list of keys.
>>> d={1:10,2:20,3:30}
>>> d.values() #dict_values([10,20,30])
Page | 6
the string.
str.rstrip()
- This returns the string after
removing the spaces on the
right side on the string.
str.lstrip()
- This returns the string after
removing the spaces on the
left side on the string
startswith () & endswith():
str.endswith(substr)
str.startswith(substr)
Note: Output return as True / False
isupper():str.isupper()
islower():str.islower()
istitle(): str.istitle()
isspace(): str.isspace()
isalnum(): str.isalnum()
>>> s=’hello’
>>> s1 = ‘123’
>>> s.isalnum() #True
>>> s1.isalnum() #True
>>> s1 = 123
>>> s.isalnum() #AttributeError:
'int' object has no attribute
'isalnum'
isalpha(): str.alpha()
isdigit(): str.isdigit()
Note: Output return as True / False
swapcase():
str.swapcase()
>>> s=’hElLo’
__
__
__
__
__
__
fromkeys():
dict_name.fromkeys(collection of keys,
value)
>>> d.fromkeys([1,2,3],100)
#{1:100,2:100,3:100}
zip() method:
d=dict(zip([1,2,3],[10,20,30]))
print(d)
#{1:10,2:20,3:30}
setdefault():
value = dict_name.setdefault(key,
default value)
Page | 7
>>> s.swapcase() #HeLlO
ord():
- This function returns the
ASCII value of the character.
>>> ord(‘A’) #65
chr():
- This function returns the
character represented by
the inputted ASCII number.
>>> chr(66) # ’B’
It adds a new key-value pair
only if the key does not already
exist in the dictionary. If the key
already exists, it returns the
current value of the key without
modifying it.
>>> d={1:10,2:20,3:30}
>>> d1=d.setdefault(1,40)
>>> print(d1) # 10
>>> d2=d.setdefault(4,50)
>>> print(d2) # 50
>>> d3=d.setdefault(5)
>>> print(d3) #None
Page | 8
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )