Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
809 views
in Blog Post by 6 8 12
edited by

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

  • Syntax: abs()  

Definition and Usage: It returns the absolute value of the specified number.

number = -999
print (abs (number))
  • Syntax: dir()

Definition and UsageIt 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))
  • Syntax: round()

Definition and UsageIt 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))
  • Syntax: pow()

Definition and UsageIt returns the value of x to the power of y. 

number = 3
print (pow (number , 2))
#Or
print (pow(3,2))
  • Syntax: max() 

Definition and UsageIt returns the largest of the input values.

numbers = 300, 600, 786, 1000, 267
print (max)(numbers)
#Or
print (max(300, 600, 786, 1000, 276))
  • Syntax: min()

Definition and UsageIt 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))
  • Syntax: sum()

Definition and UsageIt calculates the total of all numerical values.

numbers = 300, 600, 786, 1000, 267
print (sum(numbers))
  • Syntax: len()

Definition and UsageThe 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))
  • Syntax: Format()

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))
  • Syntax: filter()

Definition and UsageIt 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)))
  • Syntax: map()

Definition and UsageIt 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)))
  • Syntax: reverse()

Definition and UsageIt reverses the elements of the list.

names = [ 'alaa',  'Reemaz',  'Taif' ]
names.reverse()
print (names)
  • Syntax: super()

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


If you don’t ask, the answer is always NO!
...