0% found this document useful (0 votes)
0 views4 pages

Python Programming 7

Uploaded by

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

Python Programming 7

Uploaded by

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

#Function is block of statements that perform a specific task.

# syntax

# def function_name(parameters1,parameters2)

# coding

# return value

# Creating a Function

# In Python a function is defined using the def keyword:

def hello_function():

print("Hello World")

# Calling a Function

# To call a function, use the function name followed by parenthesis:

hello_function()

hello_function()

hello_function()

hello_function()

hello_function() #output Hello World 5 times

#wap to add two numbers

def calc_sum(a,b):

sum=a+b

print(sum)

return sum

calc_sum(12,4) #output 16

calc_sum(10,7) #output 17
calc_sum(2,4) #output 6

calc_sum(15.5,10) #output 25.5

#average of 3 numbers

def avg_function(a,b,c):

avg=(a+b+c)/3

print(avg)

return avg

avg_function(3,3,3) #output 3.0

avg_function(4,5,2) #output 3.6666666666666665

avg_function(5,5,2) #output 4.0

avg_function(6,5,7) #output 6.0

# Types of function

#1) Built in function User-defined function

# print() Prints output to the screen

# input() Takes input from the user as a string

# abs() Absolute value

# pow() Power: pow(x, y) = xⁿ

# round() Rounds a number

# max() Returns largest value

# min() Returns smallest value

# sum() Sums all items in an iterable

# len() Length of an object

# type() Returns the type of an object

# range() Generates a sequence of numbers

#print() and input()

name = input("Enter your name: ")


print("Hello", name) #output Hello Ankita

#abs()

print(abs(-7)) #7

#pow()

print(pow(2, 3)) #8

#round() round to nearest integer

print(round(4.6)) # Output: 5

print(round(4.4)) # Output: 4

print(round(5.5)) # Output: 6

# round() Round to specific decimal places(number,nth digit)

print(round(3.1415, 2)) # Output:3.14

print(round(2.71828, 3)) # Output: 2.718

#max() and min()

numbers=[10,15,20,5,16]

print(max(numbers)) # Output: 20

print(min(numbers)) # Output: 5

#sum()

numbers = [10, 20, 30]

total = sum(numbers)

print(total) # Output: 60

#len()

numbers=[3,5,8,2,6,8,4]

print(len(numbers)) # output 7
#type()

print(type(50)) # output <class 'int'>

print(type("Hello")) #output <class 'str'>

#range()

for i in range(3):

print(i) # output 0, 1, 2

#2) User defined function

# 1) Function with no Parameters

def greet():

print("Hello, welcome to Python")

greet() #output Hello, welcome to Python

# 2) Function with Parameters

def add(a, b):

result = a + b

return result

print(add(5, 3)) # Output: 8

You might also like