Python Programming Module 2
Python Programming Module 2
Python Functions
• A function is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a function.
• A function can return data as a result.
Example:
def my_function():
print("Hello from a function")
Calling a Function
Example:
def my_function():
print("Hello from a function")
my_function()
Output:
Hello from a function
Arguments
Example:
def my_function(name):
print(name , " World")
my_function("Hello")
my_function("Hai")
my_function("Welcome")
Output:
Hello World
Hai World
Welcome World
Parameters or Arguments?
The terms parameter and argument can be used for the same thing: information that
are passed into a function.
Number of Arguments
my_function("Hello", "World")
Output:
Hello World
If you do not know how many arguments that will be passed into your function,
add a * before the parameter name in the function definition.
Example:
def my_function(*kids):
print(kids)
Output:
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Output:
I am from Sweden
I am from India
I am from Norway
I am from Brazil
1. print():
• Prints the specified message to the screen or other standard output device.
• Example: print("Hello, World!")
2. len():
• Returns the length (number of items) of an object like a list, string, or tuple.
• Example: len("Python") # Output: 6
3. type():
• Returns the type of the object passed to it (e.g., whether it's an integer, list,
etc.).
• Example: type(42) # Output: <class 'int'>
4. sum():
5. max():
• Returns the largest item in an iterable or the largest of two or more arguments.
• Example: max(1, 5, 3) # Output: 5
6. min():
7. input():
• Allows the user to input data from the keyboard. Returns the input as a string.
• Example: name = input("Enter your name: ")
8. abs():
9. round():
10. sorted():
In Python, random numbers can be generated using functions from the random
module. Here are three commonly used random number generating functions:
1. random()
import random
print(random.random()) # Output: 0.7451 (example output)
2. randint(a, b)
import random
print(random.randint(1, 10)) # Output: 4 (example output)
3. uniform(a, b)
import random
print(random.uniform(1.5, 5.5)) # Output: 3.7345 (example output)
DateTime Module
The datetime module in Python provides several classes for working with dates and
times. Here are the primary classes in the datetime module:
1. datetime.date
2. datetime.time
3. datetime.datetime
Math Module
The Python math module provides various values of various constants like pi, and
tau. We can easily write their values with these constants. The constants provided
by the math module are:
• Euler’s Number
• Pi
• Tau
• Infinity
• Not a Number (NaN)
• import math
math.e #output: 2.718281828459045
math.pi #ouput: 3.141592653589793
• import math
x=5
math.factorial(x) #Returns the factorial of the number
lambda()
Syntax:
lambda argument(s): expression
Example:
composition of functions
• Function composition is a concept where the output of one
function is used as the input to another function.
• This allows you to combine simple functions to build more
complex operations.
• In Python, function composition can be done using regular
functions or lambda functions.
Example:
def multiply_by_two(x):
return x * 2
def add_three(y):
return y + 3
# Output: 16
In this example, the function add_three is applied to the input 5, resulting in 8, and
then multiply_by_two is applied to 8, resulting in 16.
Recursion
Example:
def factorial(n):
# Base case: factorial of 0 or 1 is 1
if n == 0 or n == 1:
return 1
# Recursive case
else:
return n * factorial(n - 1)
# Calculate the factorial of 5
result = factorial(5)
print(result)
# Output: 120