Table of contents
Namaste Coders ๐ฉ
From this article onwards, we will start learning and discussing functions in Python. What are function ๐? Different types of functions, all will be discussed and covered.
Imagine you are starving for food ๐ฅ but you don't know how to cook ๐. Now you have two ways to go. Either you can purchase ingredients, look for recipes, follow tutorials and cook by yourself or you can give ingredients to someone who is a professional cook so that you can enjoy delicious and healthy meals every day. Which sounds easy and less time-consuming. I guess it's the second way for everyone. Functions in Python do the same for us ๐๐
What are Functions?
Functions are the block of organized, reusable code that is used to perform a single action. For example, we need to calculate the sum of two numbers 4 times. Should we write the algorithm 4 times in a single program๐ฏ? No, we should not because it will make code regressive and will violate the zen of python. It will reduce readability.
Let's start playing with functions ๐
Python Built-in Functions
Python provides us with many functions which are already defined and ready to use by the programmers. These are called built-in functions.
It is not new to you as we are dealing with built-in functions from the very first Python program. Yes, you heard ๐ it right.
To print Hello World we use none other than a print() function. There are many built-in functions like input(),type(), tuple() ,dict() ,list(),set() and many more.
Here is a list of some built-in functions provided by Python ๐
- abs(): Returns the absolute value of a number
- 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
- callable(): Returns True if the specified object is callable, otherwise False
- chr(): Returns a character from the specified Unicode code.
- complex(): Returns a complex number
- delattr(): Deletes the specified attribute (property or method) from the specified object
- dict(): Returns a dictionary
- 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
- 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
- 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
- iter(): Returns an iterator object
- len(): Returns the length of an object
- list(): Returns a list
- 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
- 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
- slice(): Returns a slice object
- sorted(): Returns a sorted list
- str(): Returns a string object
- sum(): Sums the items of an iterator
- super(): Returns an object that represents the parent class
- tuple(): Returns a tuple
- type(): Returns the type of an object
- vars(): Returns the dict property of an object
- zip(): Returns an iterator, from two or more iterators
# abs function example
a=3.5
print("abs function example: ",abs(a))
#abs function example: 3.5
# all function example
l=[0,True,True]
print("all function example: ",all(l))
#all function example: False
#any function example
l=[0,True,True]
print("any function example: ",any(l))
#any function example: True
#ascii function example
s='Pythรถn is interesting'
print("ascii function example: ",ascii(s))
#ascii function example: 'Pyth\xf6n is interesting'
#bin function example
num=10
print("bin function example: ",bin(num))
#bin function example: 0b1010
#bool function example
num=10
print("bool function example: ",bool(num))
#bool function example: True
#bytearray function example
string = "Python is interesting."
# string with encoding 'utf-8'
arr = bytearray(string, 'utf-8')
print("bytearray function example ",arr)
#bytearray function example bytearray(b'Python is interesting.')
size = 5
arr = bytearray(size)
print("bytearray function example 2 ",arr)
#bytearray function example 2 bytearray(b'\x00\x00\x00\x00\x00')
#chr function example
# It will return a line as chr(10)='/n'
num=10
print("chr function example: ",chr(num))
#chr function example:
#callable function example
l=[1,2,3,4,5]
print("callable function example: ",callable(l))
callable function example: False
#divmod function example
print("divmod function example: ",divmod(2,3))
#divmod function example: (0, 2)
#eval function example
print("eval function example: ",eval('2+3'))
#eval function example: 5
#enumerate function example
print("enumerate function example: ",enumerate('2+3'))
#enumerate function example: <enumerate object at 0x009D3580>
#frozenset function example
l=[1,2,3,1,2,4,5]
print("frozenset function example: ",frozenset(l))
#frozenset function example: frozenset({1, 2, 3, 4, 5})
P.S. : Will continue with the functions and discuss more in upcoming articles. Stay tuned๐ง
The End
I hope you enjoyed the article and had a good learning experience.
Follow for more articles and keep sharing๐ฉ
Keep coding