BUILT IN FUNCTIONS - Python
Introschool
Posted on May 31, 2022
Subscribe to our Youtube Channel To Learn Free Python Course and More
Built-in Functions
In this section, we will discuss common built-in functions of all the data types and data structures.
Numbers
abs()
The abs() function return the absolute value of a given number. An input number could be an integer, float or complex.
s = -20
abs(s)
# output: 20
float()
The float() function return a floating point number from a number or string.
s = 2
float(s)
# output: 2.0
x = '32.44'
float(x)
# output: 32.44
# String should contain only digits.
int()
The int() function return an integer number from a number or string.
s = -3.43
int(s)
# output: -3
x = '45'
int(x)
# Output: 45
# String should only contain digits.
round()
The round() function rounds off to the given number of digits and returns the floating point number, if no number of digits is provided for round off, it rounds off the number to the nearest integer.
# round(nmber, number of digits)
s = 30.654
round(s)
# output: 31
round(s, 1)
# Output: 30.7
round(s, 2)
# Output: 30.65
String
capitalize()
The capatilize() function returns a string with first letter capitalized and all other characters lowercased. It doesn't modify the original string.
s = 'we Are leArnIng PythON'
new_s = s.capitalize()
print(new_s)
# output: We are learning python
lower()
The lower() ftnction return an lowercase string.
s = 'We are learning Python'
new_s = s.lower()
print(new_s)
# output: we are learning python
upper()
The upper() function return an uppercased string.
s = ‘We are learning Python’
new_s = s.upper()
print(new_s)
# output: WE ARE LEARNING PYTHON
title()
The title() function returns a string with the capitalized first letter of each word.
s = ‘We are learning Python’
new_s = s.title()
print(new_s)
# output: We Are Learning Python
List
append()
The append() function adds a single item to the existing list. It doesn't return a new list; rather it modifies the original list.
s = [1, 2, 3.45, 7, 'Python', False]
s.append('Hello World')
print(s)
# output: [1, 2, 3.45, 7, 'Python', False, 'Hello World']
insert()
The insert() function inserts an element at a given index. It doesn’t return anything. It modifies the existing list.
# insert(index, element)
s = [1, 2, 3.45, 7, 'Python', False]
s.insert(2, 'Hello World')
print(s)
# output: [1, 2, 'Hello World', 3.45, 7, 'Python', False]
remove()
The remove() function searches for the given element in the list and removes the first matching element.
s = [1, 2, 3.45, 7, 'Python', False, 'Python']
s.remove('Python')
print(s)
# Output: [1, 2, 3.45, 7, False, 'Python']
Tuple
tuple()
The tuple() function is used to create a tuple in Python. It takes any iterable like list, string, or dictionary as a parameter.
tup1 = tuple('String')
print(tup2)
# Output: ('S', 't', 'r', 'i', 'n', 'g')
tup2 = tuple([1,2,3,4,5])
print(tup3)
# Output: (1, 2, 3, 4, 5)
Dictionary
get()
The get() function returns the value for the specified key if key is in dictionary.
vehicle = {'type': 'Car', 'color': 'Blue', 'price': 500000}
print('vehicle_type:', vehicle.get('type'))
print('vehicle_color:', vehicle.get('color'))
# output
# vehicle_type: Car
# vehicle_color: Blue
keys()
The keys() function returns a view object that displays a list of all the keys in the dictionary. You can use for loop to get all the keys using this function.
vehicle = {'type': 'Car', 'color': 'Blue', 'price': 500000}
print(vehicle.keys())
# Output: dict_keys(['type', 'color', 'price'])
for key in vehicle.keys():
print(key, ':', vehicle[key])
# output
# type : Car
# color : Blue
# price : 500000
values()
The values() function returns the view object that displays the list of all the values in the dictionary.
vehicle = {'type': 'Car', 'color': 'Blue', 'price': 500000}
print(vehicle.values())
# Output: dict_values(['Car', 'Blue', 500000])
for value in vehicle.values():
print(value)
# output
# Car
# Blue
# 500000
items()
The items() function returns a view object that displays a list of dictionary's (key, value) tuple pairs.
vehicle = {'type': 'Car', 'color': 'Blue', 'price': 500000}
print(vehicle.items())
# Output:dict_items([('type', 'Car'), ('color', 'Blue'), ('price', 500000)])
for key, value in vehicle.items():
print(key, ':', value)
# output
# type : Car
# color : Blue
# price : 500000
Posted on May 31, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.