Python Built-in Functions, Types of Functions, User Defined Functions, Strings Functions, Math Functions, and Input & Output Functions.
Python Built-in Functions
The Python interpreter has a number of functions and types built into it that are always available.
1. abs() Function
It returns the absolute value of a number.
x = abs(10.45)
print(x)
x = abs(10.95)
print(x)
x = abs(-10.45)
print(x)
x = abs(- 10.95)
print(x)
x = abs(- 100)
print(x)
Output:
10.45
10.95
10.45
10.95
100
2. round() Function
It rounds a number to the nearest integer.
x = round(5.76543)
print(x)
x = round(5.76543, 1)
print(x)
x = round(5.76543, 2)
print(x)
x = round(5.76543, 3)
print(x)
Output:
6
5.8
5.77
5.765
3. ord() Function
returns the number representing the unicode code of a specified character.
x = ord(‘A’)
print(x)
x = ord(‘Z’)
print(x)
x = ord(‘a’)
print(x)
x = ord(‘z’)
print(x)
x = ord(‘0’)
print(x)
x = ord(‘9’)
print(x)
Output:
65
90
97
122
48
57
4. chr() Function
It returns the character that represents the specified unicode.
var = chr(70)
print(var)
var = chr(120)
print(var)
var = chr(50)
print(var)
Output:
F
x
2
5. input() Function
It allows user input.
Example:
print(“Enter Your Name: “)
x = input()
print(“Your Name is: ” + x)
6. int() Function
It converts the specified value into an integer number.
Example:
x = int(“12”)
print(x)
x = int(1.45)
print(x)
x = int(1.95)
print(x)
Output:
12
1
1
7. float() Function
It converts the specified value into a floating point number.
x = float(3)
print(x)
x = float(“100”)
print(x)
Output:
3.0
100.0
8. str() Function
It converts the specified value into a string.
x = str(3.543)
y= str(10)
print(x+y)
Output:
3.54310
9. len() Function
Example:
It returns the number of items in an object.
mylist = [“apple”, “banana”, “cherry”]
x = len(mylist)
print (x)
mytuple = (10, 20, 30, 40, 5, 7.8, “ABCD”)
x = len(mytuple)
print (x)
myset = {10, “Orange”, “Banana”, 10.34, True}
x = len(myset)
print (x)
mydict = {“Age”: 40, “Name”: “Rama”}
x = len(mydict)
print (x)
mysrting =”India is My Country”
x = len(mysrting)
print (x)
Output:
3
7
5
2
19
10. type() Function
It returns the type of the specified object.
Example:
a = [“apple”, “banana”, “cherry”]
b= (10, 20, 30, 40, 5, 7.8, “ABCD”)
c= {10, “Orange”, “Banana”, 10.34, True}
d= {“Age”: 20, “Name”: “Venkat”}
e= “India”
f=123
g=12.34
h=False
print (type(a))
print (type(b))
print (type(c))
print (type(d))
print (type(e))
print (type(f))
print (type(g))
print (type(h))
Output:
<class ‘list’>
<class ‘tuple’>
<class ‘set’>
<class ‘dict’>
<class ‘str’>
<class ‘int’>
<class ‘float’>
<class ‘bool’>
11. max() Function
It returns the largest number.
Example:
x = max(1234, 523, 499, 2345)
print(x) # 2345
x = max(123.45, 123.455, 122.999)
print(x) # 123.455
12. min() Function
It returns the lowest number.
Example:
x = min(523, 499, 722, 123)
print(x) # 123
x = min(123.45, 123.455)
print(x) # 123.45
13. range() Function
It returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
Example:
n = range(5)
for i in n:
print(i)
Output:
0
1
2
3
4
# Create a sequence of numbers from 10 to 15, and print each item in the sequence:
n = range(10, 16)
for i in n:
print(i)
Output:
10
11
12
13
14
15
# Create a sequence of numbers from 10 to 101, but increment by 10 instead of 1.
n = range(10, 101, 10)
for i in n:
print(i)
Output:
10
20
30
40
50
60
70
80
90
100
for i in range (10, 0, -1):
print(i)
Output:
10
9
8
7
6
5
4
3
2
14. pow() Function
It returns the value of x to the power of y (x^y).
Example:
x = pow(10, 3)
print(x) #1000
x = (10 ** 3)
print(x) #1000
15. sum() Function
It returns a number, the sum of all items in an iterable.
Example:
val = (10, 20, 30, 40, 50)
x = sum(val)
print(x) #150
val = (10.5, 20.6, 30.7, 40, 50)
x = sum(val)
print(x) #151.8
16. print() Function
It prints the specified message to the screen or other standard output device.
Example:
a=10
b=20
print(“Hello Python”)
print(123)
print(12.21)
print(True)
print(a)
print(a+b)
print(“Addition of a, b is: “, (a+b))
print(“A value is: “, a, ” B value is: “, b)
mylist = [10, 20, 10.345, “India”]
print (mylist)
mytuple = (10, 20, 10.345, “India”)
print (mytuple)
myset = {10, 20, 10.345, “India”}
print (myset)
mydict = {“Name”: “Raja”, “Age”: 40, “City”:”Hyderabad”}
print(mydict)