Functions and a lot more!

Aditi Pateriya
3 min readJun 23, 2021

--

Functions in Python

When a programmer has to perform a task that is often repeated, they club the set of instructions together in a function, then call the function whenever it's needed for a variety of outputs according to the situation. Functions can be built-in or user-defined.

some of the built-in functions in python are: iter(), len(),max(),compile() etc

syntax of a function in python

def function_name(parameters):
"""docstring"""
statement(s)

A function in python is declared using the def keyword, followed by a function name of your choice, arguments, and a semicolon. The set of statements to be executed in the function is written under the function definition. The first statement to be written in a function is called a docstring.

Here’s an example of the function :

#function to print an odd or even no.
def
evenOdd(x):
if (x % 2 == 0):print "even"else:print "odd"

To call a function in your code type: function name(parameter)

#to call the above function 
evenOdd(4)
output: even

Modules

You can group similar code into a module, it makes it easier to logically understand. A module can define functions, classes, and variables.

you can include a module into your code by using the import module name statement. for example:

#importing  module calc.pyimport calc
print(add(10, 2))

here the calc.py module enables the use of add() function directly into the code, resulting in the sum of 10 and 2.

The from import Statement

Python’s from statement lets you import specific attributes from a module into the current namespace.

from fib import fibonacci

here the Fibonacci file is specifically imported from the fib module.

List comprehension

If you want to create a new list based on the values of an old list, you'd have to write a set of instructions, with list comprehension you can do that in one statement.

this is the syntax:

newlist = [expression for item in iterable if condition == True]

here is an example :

colors = ["pink", "blue", "crimson", "green", "magenta"]

newlist = [x for x in colors if "m" in x]

print(newlist)

prints a list of all the elements which contain the ‘m’ letter in them.

Iterators

Iterator in python is an object that is used to iterate over iterable objects like lists, tuples, dicts, and sets. The iterator object is initialized using the iter() method. It uses the next() method for iteration.

mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple)

print(next(myit))
print(next(myit))
print(next(myit))

Looping through an iterator

mytuple = (“apple”, “banana”, “cherry”)for x in mytuple:
print(x)

Python provides a generator to create your own iterator function. A generator is a special type of function which does not return a single value, instead, it returns an iterator object with a sequence of values. In a generator function, a yield statement is used rather than a return statement.

def mygenerator():
print('First item')
yield 10

print('Second item')
yield 20

print('Last item')
yield 30

In the above example, the mygenerator() function is a generator function. It uses yield instead of the return keyword. So, this will return the value against the yield keyword each time it is called. However, you need to create an iterator for this function.

The generator function cannot include the return keyword. If you include it, then it will terminate the function. The difference between yield and return is that yield returns a value and pauses the execution while maintaining the internal states, whereas the return statement returns a value and terminates the execution of the function.

--

--

Aditi Pateriya

A curious learner , an engineer by profession , exploring the world through writings