0% found this document useful (0 votes)
15 views10 pages

Python Built-in Functions Explained

Uploaded by

gillesdahan8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

Python Built-in Functions Explained

Uploaded by

gillesdahan8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Function Description

===================================================================================
===================================================================================
=====

type():- Returns the type of an object


a = ('apple', 'banana', 'cherry')
b = "Hello World"
c = 33

x = type(a)
y = type(b)
z = type(c)

print(x)
print(y)
print(z)

# output:-
<class 'tuple'>
<class 'str'>
<class 'int'>

===================================================================================
=========================================================

abs():- The Python abs() function return the absolute value. The absolute value
of any number is always positive it removes the negative sign of a number in
Python.

Python abs() Function Syntax


The abs() function in Python has the following syntax:

Syntax: abs(number)

number: Integer, floating-point number, complex number.


Return: Returns the absolute value.

# An integer
var = -94
print('Absolute value of integer is:', abs(var))

Absolute value of integer is: 94

# Function to calculate speed


def cal_speed(dist, time):
print(" Distance(km) :", dist)
print(" Time(hr) :", time)
return dist / time

# Function to calculate distance traveled


def cal_dis(speed, time):
print(" Time(hr) :", time)
print(" Speed(km / hr) :", speed)
return speed * time

# Function to calculate time taken


def cal_time(dist, speed):
print(" Distance(km) :", dist)
print(" Speed(km / hr) :", speed)
return dist / speed

# Driver Code
# Calling function cal_speed()
print(" The calculated Speed(km / hr) is :",
cal_speed(abs(45.9), abs(-2)))
print("")

# Calling function cal_dis()


print(" The calculated Distance(km) :",
cal_dis(abs(-62.9), abs(2.5)))
print("")

# Calling function cal_time()


print(" The calculated Time(hr) :",
cal_time(abs(48.0), abs(4.5)))

===================================================================================
===================================================================================
=====
all():- Returns True if all items in an iterable object are true

===================================================================================
===================================================================================
=====

any():- Returns True if any item in an iterable object is true


===================================================================================
===================================================================================
=====

ascii():- Returns a readable version of an object. Replaces none-ascii characters


with escape character
===================================================================================
===================================================================================
=====

bin():- Returns the binary version of a number


===================================================================================
===================================================================================
=====

bool():- Returns the boolean value of the specified object


===================================================================================
===================================================================================
=====

bytearray():- Returns an array of bytes


===================================================================================
===================================================================================
=====

bytes():- Returns a bytes object


===================================================================================
===================================================================================
=====

callable():- Returns True if the specified object is callable, otherwise False


===================================================================================
===================================================================================
=====

chr():- Returns a character from the specified Unicode code.


===================================================================================
===================================================================================
=====

classmethod():- Converts a method into a class method


===================================================================================
===================================================================================
=====

compile():- Returns the specified source as an object, ready to be executed


===================================================================================
===================================================================================
=====

complex():- Returns a complex number


===================================================================================
===================================================================================
=====

delattr():- Deletes the specified attribute (property or method) from the specified
object
===================================================================================
===================================================================================
=====

dict():- Returns a dictionary (Array)


===================================================================================
===================================================================================
=====

dir():- Returns a list of the specified object's properties and methods


===================================================================================
===================================================================================
=====

divmod():- Returns the quotient and the remainder when argument1 is divided by
argument2
===================================================================================
===================================================================================
=====
enumerate():- Takes a collection (e.g. a tuple) and returns it as an enumerate
object
===================================================================================
===================================================================================
=====

eval():- Evaluates and executes an expression


===================================================================================
===================================================================================
=====

exec():- Executes the specified code (or object)


===================================================================================
===================================================================================
=====

filter():- Use a filter function to exclude items in an iterable object

===================================================================================
===================================================================================
=====
float():- Returns a floating point number

===================================================================================
===================================================================================
=====

format():- Formats a specified value


===================================================================================
===================================================================================
=====

frozenset():- Returns a frozenset object


===================================================================================
===================================================================================
=====

getattr():- Returns the value of the specified attribute (property or method)


===================================================================================
===================================================================================
=====

globals():- Returns the current global symbol table as a dictionary


===================================================================================
===================================================================================
=====

hasattr():- Returns True if the specified object has the specified attribute
(property/method)
===================================================================================
===================================================================================
=====

hash():- Returns the hash value of a specified object


===================================================================================
===================================================================================
=====
help():- Executes the built-in help system
===================================================================================
===================================================================================
=====

hex():- Converts a number into a hexadecimal value


===================================================================================
===================================================================================
=====

id():- Returns the id of an object


===================================================================================
===================================================================================
=====

input():- Allowing user input


===================================================================================
===================================================================================
=====

int():- Returns an integer number


===================================================================================
===================================================================================
=====

isinstance():- Returns True if a specified object is an instance of a specified


object
===================================================================================
===================================================================================
=====

issubclass():- Returns True if a specified class is a subclass of a specified


object
===================================================================================
===================================================================================
=====

iter():- Returns an iterator object


===================================================================================
===================================================================================
=====

len():- Returns the length of an object


===================================================================================
===================================================================================
=====

list():- Returns a list


===================================================================================
===================================================================================
=====

locals():- Returns an updated dictionary of the current local symbol table


===================================================================================
===================================================================================
=====

map():- Returns the specified iterator with the specified function applied to
each item
===================================================================================
===================================================================================
=====

max():- Returns the largest item in an iterable


===================================================================================
===================================================================================
=====

memoryview():- Returns a memory view object


===================================================================================
===================================================================================
=====

min():- Returns the smallest item in an iterable


===================================================================================
===================================================================================
=====

next():- Returns the next item in an iterable


===================================================================================
===================================================================================
=====

object():- Returns a new object


===================================================================================
===================================================================================
=====

oct():- Converts a number into an octal


===================================================================================
===================================================================================
=====

open():- Opens a file and returns a file object


===================================================================================
===================================================================================
=====

ord():- Convert an integer representing the Unicode of the specified character


===================================================================================
===================================================================================
=====

pow():- Returns the value of x to the power of y


===================================================================================
===================================================================================
=====

print():- Prints to the standard output device


===================================================================================
===================================================================================
=====

property():- Gets, sets, deletes a property


===================================================================================
===================================================================================
=====
range():- Returns a sequence of numbers, starting from 0 and increments by 1 (by
default)
===================================================================================
===================================================================================
=====

repr():- Returns a readable version of an object


===================================================================================
===================================================================================
=====

reversed():- Returns a reversed iterator


===================================================================================
===================================================================================
=====

round():- Rounds a numbers


===================================================================================
===================================================================================
=====

set():- Returns a new set object

===================================================================================
===================================================================================
=====

setattr():- Sets an attribute (property/method) of an object


The setattr() function sets the value of the specified attribute of the specified
object.
Syntax:- setattr(object, attribute, value)

class Person:
name = "John"
age = 36
country = "Norway"

setattr(Person, 'age', 40)

# The age property will now have the value: 40


x = getattr(Person, 'age')
print(x)
print([Link])
output :- 40
output :- 40

===================================================================================
===================================================================================
=====

slice():-
A slice object is used to specify how to slice a sequence. You can specify where to
start the slicing, and where to end. You can also specify the step, which allows
you to e.g. slice only every other item.
syntax:- slice(start, end, step)
a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = slice(2)
print(a[x])

===================================================================================
===================================================================================
=====

sorted():-
The sorted() function returns a sorted list of the specified iterable object.
You can specify ascending or descending order. Strings are sorted alphabetically,
and numbers are sorted numerically.

a = ("b", "g", "a", "d", "f", "c", "h", "e")


x = sorted(a)
print(x)

===================================================================================
===================================================================================
=====

str():- Returns a string object


a = 10 #int value
print(type(a)) #output <class 'int'>
str_a = str(a) #it will change it in string
print(type(str_a)) #output <class 'str'>

===================================================================================
===================================================================================
=====

sum():- Sums the items of an iterator


a = (1, 2, 3, 4, 5)
x = sum(a, 7)
print(x)

===================================================================================
===================================================================================
=====

super():- The super() function is used to give access to methods and properties
of a parent or sibling class.
The super() function returns an object that represents the parent class.

class Parent:
def __init__(self, txt):
[Link] = txt

def printmessage(self):
print([Link])

class Child(Parent):
def __init__(self, txt):
super().__init__(txt)
x = Child("Hello, and welcome!")

[Link]()

# output:- Hello, and welcome!

===================================================================================
===================================================================================
=====

tuple():- The tuple() function creates a tuple object.

===================================================================================
===================================================================================
=====

vars():- The vars() function returns the __dict__ attribute of an object.


The __dict__ attribute is a dictionary containing the object's changeable
attributes.

class Person:
name = "John"
age = 36
country = "norway"

x = vars(Person)

print(x["name"])

===================================================================================
===================================================================================
=====

zip():-
The zip() function returns a zip object, which is an iterator of tuples where the
first item in each passed iterator is paired together, and then the second item in
each passed iterator are paired together etc.
If the passed iterables have different lengths, the iterable with the least items
decides the length of the new iterator.

a = ("John", "Charles", "Mike")


b = ("Jenny", "Christy", "Monica", "Vicky")
x = zip(a, b)

# use the tuple() function to display a readable version of the result:


print(tuple(x))

#output:- (('John', 'Jenny'), ('Charles', 'Christy'), ('Mike', 'Monica'))

===================================================================================
===================================================================================
=====

You might also like