Pyhton Functions Cs
Pyhton Functions Cs
FUNCTION
FUNCTION
TYPES OF FUNCTION
• There are two kinds of functions in Python.
1. Built-in functions that are provided as part of Python - input(), type(),
float(), int() ,range(), len(),max(),min
aList = [4, 6, 8, 24, 12, 2]
print(max(aList))
def
hello(): Program:
print
Output:
'Hello' def thing():
print 'Fun' print 'Hello’
print 'Fun’ Hello
hello() Fun
thing()
print 'Zip’ Zip
print “Zip” thing()
Hello
Fun
hello()
We call these reusable pieces of code “functions”.
HOW TO CALL A FUNCTION
def sample_func():
print("we are inside the function
definition")
print("to write logic of subproblems")
pro=product(5,6) sum(3,4)
print(pro)
print(product(6,3)) l=90
m=100
sum(l,m)
FIND OUTPUT
def highest_even(li):
evens = []
for item in li:
if item % 2 == 0:
evens.append(item)
return max(evens)
print(highest_even([10,1,2,3,4,8]))
FUNCTION EXERCISE
1. Write a function calculation() such that it can accept two variables and calculate the
addition and subtraction of it. And also it must return both addition and subtraction
in a single return call (Hint:n Python, we can return multiple values from a
function. You can do this by separating return values with a comma.)
(function1.py)
Example
a = "Hello, World!"
print(len(a))
The strip() method removes any whitespace from
the beginning or the end:
a = "Hello, World!“
print(a.lower())
The upper() method returns the
string in upper case:
a = "Hello, World!"
print(a.upper())
The replace() method replaces a
string with another string:
a = "Hello, World!"
print(a.replace("H", "J"))
The split() method splits the string into
s u b s t r i n g s i f i t fi n d s i n s t a n c e s o f t h e
s e p a r a t o r:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', '
World!']
check if a certain phrase or character is present in a string,
we can use the keywords in or not in.
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
THE FORMAT() METHOD TAKES UNLIMITED
NUMBER OF ARGUMENTS, AND ARE PL ACED
INTO THE RESPECTIVE PL ACEHOLDERS:
• quantity = 3
itemno = 567
price = 49.95