In this post, we will list most of common Built-In Python functions that you should to know as a beginner
Python Built-In Functions
Definition and Usage: It returns the absolute value of the specified number.
number = -999
print (abs (number))
Definition and Usage: It returns list of the attributes and methods of any object.
class Student:
def __init__(self, name, age, id, grades):
self.name = name
self.age = age
self.id = id
self.grades = grades
def talk(self):
print ('My name is:' , self.name)
std1 = Student('Alaa' , 21, 'xx00', [95, 98, 99])
std2 = Student('Reemaz', 19, 'xx01', 86)
print (dir(std2))
Definition and Usage: It returns a floating point number that is a rounded version of the specified number, with the specified number of decimals.
number = 3.673
print (round(number))
#Or
number = 3.673
print (round(number , 2))
Definition and Usage: It returns the value of x to the power of y.
number = 3
print (pow (number , 2))
#Or
print (pow(3,2))
Definition and Usage: It returns the largest of the input values.
numbers = 300, 600, 786, 1000, 267
print (max)(numbers)
#Or
print (max(300, 600, 786, 1000, 276))
Definition and Usage: It returns the smallest of the input values.
numbers = 300, 600, 786, 1000, 267
print (min)(numbers)
#Or
print (min(300, 600, 786, 1000, 276, 1000, 267))
Definition and Usage: It calculates the total of all numerical values.
numbers = 300, 600, 786, 1000, 267
print (sum(numbers))
Definition and Usage: The number of elements stored in the object is never calculated, so len helps provide the number of elements.
the_String = 'abcdefghijklmnopgrstuvwxyz'
the_list = [1, 2, 2, 3]
the_tuple = (1,2, 3)
print (len(the_String))
print (len(the_list))
print (len(the_tuple))
Definition and Usage:
It formats the specified value(s) and insert them inside the string's placeholder
Note: The placeholder is defined using curly brackets: {}
first_name = 'Alaa'
last_name = 'Salman'
age = 19
print ('my name is {} {}, and iam {} years old'.format (first_name,last_name, age))
#Or by the index
first_name = 'Alaa'
last_name = 'Salman'
age = 19
print ('my name is {1}, and my last name is {0}, and i am {2} years old'.format (last_name, first_name,age))
Definition and Usage: It filters the given sequence with the help of a function that tests each element in the sequence to be true or not.
ages = [30, 9, 15, 22, 17, 44, 26, 5]
def filtered_ages (ages):
return ages >= 18
print(list(filter(filtered_ages, ages)))
Definition and Usage: It applies a given function to each item of an iterable (list, tuple etc.) and returns an iterator.
numbers = [ 5, 10, 20, 25, 50 ]
def square (num):
return num ** 2
print (list(map(square, numbers)))
Definition and Usage: It reverses the elements of the list.
names = [ 'alaa', 'Reemaz', 'Taif' ]
names.reverse()
print (names)
Definition and Usage: It returns an object that represents the parent class
class Person:
def __init__(self, first_name, surname, tel ):
self.first_name = first_name
self.surname = surname
self.tel = tel
def full_name(self):
return self.first_name + " " + self.surname
class Employee(Person):
def __init__(self,first_name, surname, tel, salary):
super().__init__(first_name, surname, tel)
self.salary = salary
def give_raise (self, amount):
self.salary = self.salary + amount
emp1 = Employee(1700, 'Ali', 'Ahmed', '+96656xxxxxxx')
Functional programming In Python