BUILT IN FUNCTIONS - Python

introschool

Introschool

Posted on May 31, 2022

BUILT IN FUNCTIONS - Python

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
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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']

Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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']

Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
introschool
Introschool

Posted on May 31, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related