You are on page 1of 12

Introduction

• Functions are self contained programs that perform some particular tasks.
• Once the function is created by the programmer for a specific task, this function can
be called anytime to perform that task.
• There are mainly two types of function, Built in function and User defined function

Advantages.
• They reduce duplication of code in a program
• They break the large complex problems into small parts
• They help in improving the clarity of code, (made the code easy to understand)
• A piece of code can be reused as many times as we want with the help of functions
Built in functions
• These are the functions already defined in the Python programming
language.
• We can directly call them to perform a specific task.
• Every built in functions in python performs some particular task.
Mathematical Functions
• Python provide a math module that contains most of the familiar and
important mathematical functions.
• A module is a file that contains some predefined python codes.
• Before using a module in a python we have to import it.

• >>>import math
Date time functions
• Python provides the built in modules time and calendar through
which we can handle date and time in several ways.
• In order to use the time module we need to import it.
• Similarly for working with the dates, we have to import the calendar
module first.
Date and time
• Getting current date and time
• >>>localtime=time.localtime(time.time())
print(localtime)
Getting formatted date and time

>>> localtime=time.asctime(time.localtime(time.time()))
print(localtime)
Random numbers
• Python provides a function random that generates a random number
in the range [0,1].

• The random module provides another function randint(a,n) randomly


generate a number from a to n. Eg( 1,n) means 1 to n.

You might also like