Python Functions
Python Functions
type()
It return the type(Datatype) of parameter/value.
Example-
int = 10
Output-
2.abs()
The abs() function in Python is used to return the absolute value of a number.
Example-
a = -5
b = -3.7
print(abs(a))
print(abs(b))
Output-
3.7
3.all()
This function takes a sequence as an parameter and returns True if all values in para,eter
sequence(List,Tuple,String,etc) have a Boolean value True; otherwise False.
Example-
list1 = [1, 2, 3, 4, 5]
list3 = [True,True]
list4 = [True,False]
print(all(list1))
print(all(list2))
print(all(list3))
print(all(list4))
Output-
True
False
True
False
4.any()
This function returns True if any one values in parameter sequence(List, Tuple, String, etc) have a
Boolean value True; otherwise returns false.
Example-
list1 = ["","",""]
list3 = [True,False]
print(any(list1))
print(any(list2))
print(any(list3))
Output-
False
True
True
5.callable()
This function checks if an object appears callable (i.e., if it can be called as a function or method). It
returns True if the object can be called, and False otherwise.
Example-
print(callable(type))
print(callable([10,20,20]))
Output-
True
False
6.isinstance()
This function checks if an object is an instance of a specified class or a subclass. It returns True if the
object is an instance of the class, and False otherwise.
Example-
x = 10
print(isinstance(x, int))
print(isinstance(x, str))
Output-
True
False
7.id()
The id() function in Python returns the unique identifier (memory address) for an object.
Example-
x = 10
y = 10
Output-
id of x: 1719180526096
id of y: 1719180526096
8.hasattr()
The hasattr() function in Python checks if an object has a specified attribute. It returns True if the
object has the attribute, and False otherwise.
Example-
class MyClass:
name = "Python"
obj = MyClass()
print(hasattr(obj, 'name'))
print(hasattr(obj, 'age'))
Output-
True
False
9.bin()
This function returns binary conversion of specified integer parameter.
Example-
x = 10
print(bin(x))
Output-
0b1010
10.hex()
This function returns hexa-decimal conversion of specified integer parameter.
Example-
x = 10
print(hex(x))
Output-
0xa
11.oct()
This function returns octal conversion of specified integer parameter.
Example-
x = 10
print(oct(x))
Output-
0o12
12.bool()
The bool() function in Python converts a value to a boolean (True or False) based on its truthiness. It
returns False for falsy values like None, 0, "", and False, and True for all other values.
Example-
x=0
print(bool(x))
y = "Hello"
print(bool(y))
Output-
False
True
13.float()
The float() function in Python converts a number or a string into a floating-point number.
Example-
x = "3.14"
print(float(x))
y=5
print(float(y))
Output-
3.14
5.0
14.int()
The int() function in Python converts a number or a string into an integer.
Example-
x = "42"
print(int(x))
y = 3.99
print(int(y))
Output-
42
15.list()
The list() function converts specified data-structure into list and returns.
Example-
x = "hello"
print(list(x))
y = (1, 2, 3)
print(list(y))
Output-
[1, 2, 3]
16.tuple()
The tuple() function converts specified data-structure into tuple and returns.
Example-
x = [1, 2, 3]
print(tuple(x))
y = "hello"
print(tuple(y))
Output-
(1, 2, 3)
17.str()
This function converts and returns parameter value into string type.
Example-
a = 15+15
b = str(15) + str(15)
print(a)
print(b)
Output-
30
1515
18.ord()
This function returns ASCII value of specified character as parameter.
Example-
char = 'A'
print(ord(char))
sy = '+'
print(ord(sy))
num = '7'
print(ord(num))
Output-
65
43
55
19.chr()
This function returns a related character based on provided ASCII value as parameter.
Example-
num = 65
print(chr(num))
num = 122
print(chr(num))
Output-
20.input()
Accepts input from the user.
Example-
Output-
Hello, adt
21.print()
This function prints to the standard output device.
Example-
print("Hello")
Output-
Hello
22.open()
Opens a file and returns an object of type class_io.TextIOWrapper.
Example-
f = open(“D:\\example1.txt”)
print(type(f)
Output-
23.complex()
Returns a complex number of specified parameter.
Example-
a = complex(3)
b = complex(2,7)
print(a)
print(b)
Output-
(3+0j)
(2+7j)
24.eval()
Evaluates and executes an expression provided as parameter string.
Example-
x=5
Output-
10
25. @staticmethod()
This function can externally convert a non-static member-function into a static member function.
Example-
class MyClass:
@static method
def greet(name):
print(MyClass.greet("Alice"))
Output-
Hello, Alice!
26.issubclass()
Example-
class Animal:
pass
class Dog(Animal):
pass
print(issubclass(Dog, Animal))
print(issubclass(Dog, object))
Output-
True
True
27.sum()
This function takes a sequence as a parameter and returns sum of all elements of specified sequence.
Example-
numbers = [1, 2, 3, 4]
print(sum(numbers))
print(sum(numbers, 5))
Output-
10
15
28.max()
This function returns the largest item from specified sequence.
Example-
Output-
29.min()
This function returns the smallest item from specified sequence.
Example-
30.pow()
This function obtains and returns the value of base raised to power.
Example-
Output-
2 raised to 3 is: 8
31.range()
This function returns a sequence of numbers, based on specific start_value to end_value – 1 by
processing on specified step.
Example-
32.help()
Example-
help()
Output-
If this is your first time using Python, you should definitely check out
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
with a one-line summary of what it does; to list the modules whose name