Expt8.ipynb - Colaboratory
Expt8.ipynb - Colaboratory
ipynb - Colaboratory
EXPERIMENT 8
ROLL NO.: 36
SECTION: A(A2)
FUNCTIONS
DEFINING A FUNCTION
def my_functin():
print("Hello from my
function") CALLING A
FUNCTION
def my_function():
print("Hello from my
function") my_function()
def my_function(fname):
print(fname + " Khan")
my_function("Salman")
my_function("Aamir")
my_function("Shahrukh")
Salman Khan
Aamir Khan
Shahrukh Khan
Salman Khan
def my_function(*fruits):
print("The sweetest fruit is " +
fruits[2]) my_function("Banana","Apple",
"Papaya")
VALUES
def my_function(country =
"Norway"): print("I am from "
+ country) my_function("Sweden")
my_function()
my_function("India")
I am from Sweden
I am from Norway
I am from India
https://colab.research.google.com/drive/1esD-vl1U7t6SghZJhZ_XZyCTp29ZnCkk#scrollTo=bIV8OmkHlZ-H&printMode=true
12/12/23, 8:43 PM expt8.ipynb - Colaboratory
["apple", "banana", "mango"]
my_function(fruits)
apple
banana mango
RETURN
VALUES
def my_function(x):
return 5*x
print(my_function(1))
print(my_function(2))
print(my_function(3))
5
10
15
def sum(numbers):
total = 0 for x in
numbers: total += x
return total
print(sum((8, 2, 5, 6,
5)))
26
def add_numbers(x,
y): sum = x + y
return sum
num1 = 5
num2 = 6
print("The sum is ", add_numbers(num1,
num2))
The sum is 11
def even_odd(x):
if x % 2 == 0:
print("even")
else:
print("odd")
even_odd(2)
even_odd(3)
even
odd
def swap(x,
y): temp =
x x = y y
= temp
x = 2 y =
3
swap(x,
y)
print(x)
print(y)
2
3
https://colab.research.google.com/drive/1esD-vl1U7t6SghZJhZ_XZyCTp29ZnCkk#scrollTo=bIV8OmkHlZ-H&printMode=true