0% found this document useful (0 votes)
95 views13 pages

Ready Reckoner of Functions

Uploaded by

SR
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)
95 views13 pages

Ready Reckoner of Functions

Uploaded by

SR
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/ 13

READY RECKONER

OF FUNCTIONS
Some Basic Functions
2

S. NO FUNCTION NAME SYNTAX DEFINITION EXAMPLE OUTPUT

1) print() print(<object to be To print or display print(“Hello Hello World!


printed>…) output, python provides World!”)
print() function
2) input() variable_to_hold_the_ To get the input from x=input(“Enter Enter the string :
value_=input(<prompt the user, python the string :”)
to be displayed) provides the input()
function
3) len() len(<length of object To find the length of the len(“Hello 11
to be determined>) object, python provides World”)
the len() function
4) index() variable[<position of Index is the numbered X=“Hello World” o
element to be found>] position of an object in X[4]
a string, list, tuple,
dictionary, etc.
Some Mathematical Functions in math module 3

S. NO FUNCTION PROTOTYPE DESCRIPTION EXAMPLE


(GENERAL FORM)
1) ceil math.ceil(num) Returns the smallest integer not math.ceil(1.03)
less than the num >>> 2.0
2) sqrt math.sqrt(num) Returns the square root of the math.sqrt(81.0)
num. If num< 0,error occurs >>> 9.0
3) exp math.exp(arg) Returns the natural logarithm e math.exp(2.0)
raised to the arg power >>> e²
4) fabs math.fabs(num) Returns the absolute value of math.fabs(1.0)
num >>> 1.0
5) floor math.floor(num) Returns the largest integer not math.floor(1.03)
greater than the num >>> 1.0
6) log math.log(num,[base]) Returns the natural logarithm math.log(1024,2)
for num >>> log of 1024 base 2
7) log10 math.log10(num) Returns the base 10 logarithm math.log10(1.0)
for num >>> 1.0
8) pow math.pow(base, exp) Returns base raised to exp math.pow(4.0,2.0)
power i.e., base exp >>> 4²
4

S. NO FUNCTION PROTOTYPE DESCRIPTION EXAMPLE


(GENERAL FORM)
9) sin math.sin(arg) Returns the sine value of arg. math.sin(0.5236)
arg must be in radians >>> 0.5
10) Cos math.cos(arg) Returns the cosine value of math.cos(1.0472)
arg. arg must be in radians >>> 0.5
11) tan math.tan(arg) Returns the tangent value of math.tan(0.7853)
arg. arg must be in radians >>> 1
12) degrees math.degrees(x) Converts angle x from math.degrees(0.5236)
radians to degrees >>> 30
13) Radians math.radians(x) Converts angle x from math.radians(30)
degrees to radians >>> 0.5236

Random Modules :
▪ random() → Returns a random number in the range 0 to 1 (0 inclusive,1 exclusive)
▪ randint(a,b) → Returns an integer as output in the given range
▪ randrange(<start>,<stop>,<step>) → Returns random numbers in the given range
String Manipulation Functions and Methods 5

S. NO FUNCTION SYNTAX DESCRIPTION EXAMPLE


1) len len(<string>) Returns the length of the len(“Hello”)
string >>> 6
2) capitalize <string>.capitalize() Returns a copy of the string “true”.capitalize()
with first character capitalized >>> True
3) count <string>.count(sub[, start[, Returns number of substrings “abcba”.count(“ab”)
end]]) in original string >>> 1
4) find <string>.find(sub[, start[, Returns lowest index of first string,sub=“ring ring”, “ring”
end]]) occurrence of the substring. If string.find(sub)
not found -1 is the output >>> 0
5) index <string>.index(sub[, start[, Returns lowest index of first “abcba”.index(“ab”)
end]]) occurrence of the substring. >>> 0
6) isalnum <string>.isalnum() Returns True if all characters string=“abc123”
are alphanumeric. Else, False string.isalnum()
>>> True
7) isalpha <string>.isalpha() Returns True if all characters string.isalpha()
are alphabets. Else, False >>> False
8) isdigit <string>.isdigit() Returns True if all characters string.isdigit()
are digits. Else, False >>> False
6

S. NO FUNCTION SYNTAX DESCRIPTION EXAMPLE


9) islower <string>.islower() Returns True if all characters string.islower()
in string are lowercase. >>> False
Else, False
10) isspace <string>.isspace() Returns True if all characters string.isspace()
in string are whitespaces. >>> False
Else, False
11) isupper <string>.isupper() Returns True if all characters string.isupper()
in string are uppercase. >>> False
Else, False
12) lower <string>.lower() Returns a copy of the string str=“HELLO”
converted to lowercase str.lower()
>>> hello
13) upper <string>.upper() Returns a copy of the string str=“hello”
converted to uppercase str.upper()
>>> HELLO
14) lstrip <string>.lstrip() Leading Whitespaces removed str=“ Hello ”
rstrip <string>.rstrip() Trailing Whitespaces removed str.lstrip() >>> “Hello ”
strip <string>.strip() All Whitespaces removed str.rstrip() >>> “ Hello”
str.strip() >>> “Hello”
7
S. NO FUNCTION SYNTAX DESCRIPTION EXAMPLE
15) startswith <string>.startswith() Returns True if string starts with “abcd”.startswith(“ab”)
endswith the substring. Else, False >>> True
<string>.endswith() Returns True if string ends with
the substring. Else, False
16) title <string>.title() Returns a title-cased version of “abcd”.endswith(“cd”)
the string where all words starts >>> True
with uppercase
17) istitle <string>.istitle() Returns True if the string has “hello world”.title()
title case >>> ‘Hello World
18) replace <string>.replace(old,new) Returns a copy of the string with “abcdcba”.replace(“ab”,”hi”)
old occurrences replaced by new >>> “hicdcba”
substring
19) join <string>.join(<string Returns lowest index of first “*”.join(“Hello”)
iterable>) occurrence of the substring >>> “H*e*l*l*o”
20) split <string>.split(<string/char>) Returns a list containing split “Hello world”,split()
strings as members >>> [“Hello”, “world”]
21) partition <string>.partition(<sep/strin Returns a tuple containing three str=“Working in python”
g>) items as string till separation, x=str.partition(“king”)
sep is seperator separator and string after print(x)
separator >>>(“Wor”,“king” ,“in python”)
List Manipulation Functions and Methods 8

S. NO FUNCTION SYNTAX DESCRIPTION EXAMPLE


1) len len(<list>) Returns the length of its len([1,2,3])
argument list >>> 3
2) list list([sequence]) Returns a list created from the list(“hello”)
passed argument >>> [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
3) index list.index(<item>) Returns the index of first L1=[13,14,64,18]
matched item from the list L1.index(18)
>>> 3
4) append list.append(<item>) Adds an item to the end of the colors=[‘red’, ’green’, ’blue’]
list colors.append(‘yellow’)
>>> [‘red’, green’,‘blue’,’yellow’]
5) extend list.extend(<list>) Used for adding multiple T1,T2=[‘a’, ‘b’, ‘c’],[‘d’, ‘e’]
elements to a list T1.extend(T2)
>>>[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
6) insert list.insert(<pos>,<item>) This method is also an insertion [‘a’, ‘b’, ‘c’].insert(2, ‘i’)
method for lists >>> [‘a’, ‘b’, ‘i’, ‘c’]
7) pop list.pop(<index>) Used to remove the item from print([‘a’, ‘b’, ‘c’].pop(0))
the list. >>> ‘a’
8) remove list.remove(<value>) Removes the first occurrence of X=[‘a’, ‘b’, ‘c’, ‘d’].remove(“b”)
the given item from the list print(X)
>>> [‘a’, ‘c’, ‘d’]
9

S. NO FUNCTION SYNTAX DESCRIPTION EXAMPLE


9) clear list.clear() Removes all the items from the L1=[2,3,4,5,6]
list and the list becomes empty L1.clear()
>>> []
10) count list.count(<item>) Returns the count of the item L1.count(2)
that you passed as argument >>> 1
11) reverse list.reverse() Reverses the items of the the T1=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
list. This is done “in place” T1.reverse()
>>> [‘e’, ‘d’, ‘c’, ‘b’, ‘a’]
12) sort list.sort() Sorts the items of the list, by T1=[12,65,23,0]
default in increasing order T1.sort()
>>> [0,12,23,65]
13) sorted sorted(<iterable_sequence>, Takes the name of the list as an Y=[17,24,36,40,2,0,1]
[reverse=False]) argument and returns a new SY=sorted(Y)
sorted list with sorted elements print(SY)
in it >>> [0,1,2,17,24,36,40]
14) min min(<list>) Returns the minimum element Val=[17,24,15,30]
max max(<list>) and sum of the elements of the min(Val) >>> 15
sum sum(<list>) list respectively max(Val) >>> 30
Sum(Val) >>> 86
Tuple Manipulation Functions and Methods 10

S. NO FUNCTION SYNTAX DESCRIPTION EXAMPLE


1) len len(<tuple>) Returns the length of the tuple X=(“Hello”,24)
len(X)
>>> 2
2) max max(<tuple>) Returns the element from the Y=(10,20,40,60,100)
tuple having maximum value max(Y)
>>> 100
3) min min(<tuple>) Returns the element from the Y=(10,20,40,60,100)
tuple having minimum value min(Y)
>>> 10
4) sum sum(<tuple>) Returns the sum of the Y=(10,20,40,60,100)
elements of the tuple sum(Y)
>>> 230
5) index <tuplename>.index(<item>) Returns the index of an T1=(10,20,30,40,50)
existing element of a tuple T1.index(40)
>>> 3
6) count <sequence_name>.count( Returns the count of a T1=(10,20,30,40,50)
<object>) member element/object in a T1.count(30)
sequence (list/tuple) >>> 1
11

S. NO FUNCTION SYNTAX DESCRIPTION EXAMPLE


7) sorted sorted(<iterable_sequence>,[ Takes the name of the list as an V=(27,34,25,50)
reverse=False]) argument and returns a new SV=sorted(V)
sorted list with sorted elements print(SV)
in it >>> [25,27,34,50]
8) tuple tuple(<sequence>) Creates tuples from different A=tuple(“Hello”)
types of iterable sequence print(A)
>>> (‘H’, ‘e’, ‘l’, ‘l’, ‘o’)
Dictionary Manipulation Functions and Methods 12

S. NO FUNCTION SYNTAX DESCRIPTION EXAMPLE


1) len len(<string>) Returns the number of items D={“a”:10, “b”:20}
in the dictionary len(D)
>>> 2
2) get <D>.get(key,[default]) Returns the corresponding D.get(‘a’)
value is the key already exists. >>> 10
Else, it returns error.
3) items <D>.items() Returns a sequence of D.items()
(key,value) pairs, the items >>> (‘a’, 10)
(‘b’, 20)
4) keys <D>.keys() Returns a list sequence of keys D.keys()
>>> [‘a’, ‘b’]
5) values <D>.values() Returns a list sequence of D.values()
values >>> [10, 20]
6) fromkeys dict.fromkeys(<key Creates a new dictionary from dict.fromkeys([2,4,6,8],100]
sequence>, [<value>]) a sequence containing all the >>> {2:100, 4:100, 6:100,
keys and a common value 8:100}
S. NO FUNCTION SYNTAX DESCRIPTION EXAMPLE
7) setdefault dict.setdefault(<key>, Inserts a new key:value pair only D={“a”:10, “b”:20}
<value>) if the key doesn’t already exist. D.setdefault(‘c’, 30)
Else returns the current value of >>> ={“a”:10, “b”:20, “c”:30}
the key
8) update <D>.update(<other – Merges key:value pairs from the A={“d”:40, “e”:50}
dictionary>) new dictionary into the original D.update(A)
dictionary >>> {“a”:10, “b”:20, d”:40, “e”:50}
9) copy <dict>.copy() Creates a shallow copy of the D1=D.copy()
dictionary >>> D={“a”:10, “b”:20}
10) pop <dict>.pop(key, value) Removes and returns the D.pop(‘a’)
elements associated with keys >>> 10
11) popitem <dict>.popitem() Removes and returns a (key, D.popitem()
value) pair from the dictionary >>> (‘b’, 20)
12) clear <D>.clear() Removes all items D.clear()
>>> {}
13) sorted Sorted(<dict>, [reverse = Returns a sorted list of sorted(D, reverse = False)
False]) dictionary keys >>> [‘a’, ‘b’]
14) max max(<dict>) Gives the minimum key, A={10: ‘N’, 20: ‘H’, 30: ‘S’}
min min(<dict>) maximum key or sum of keys max(A) >>> (10)
sum sum(<dict>) from the dictionary min(A) >>> (30)
sum(A) >>> (60)

You might also like