0% found this document useful (0 votes)
5 views23 pages

Pyhton Functions Cs

Uploaded by

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

Pyhton Functions Cs

Uploaded by

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

PYTHON

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))

2. Functions that we define ourselves and


then use

• We treat the of the built-in function names as "new"


reserved words (i.e. we avoid them as variable names)
FUNCTION DEFINITION

• In Python a function is some reusable code that takes


arguments(s) as input does some computation and then
returns a result or results
• We define a function using the def reserved word
• We call/invoke the function by using the function name,
parenthesis and arguments in an expression
DEFINING A FUNCTION
STORED (AND REUSED) STEPS

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")

sample_func() # function call


print("i am outside the func def")
sample_func() #function call
def product(num1,num2): def sum(a,b):
return num1*num2 c=a+b
print(c)

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)

2. Create a function showEmployee() in such a way that it should accept employee


name, and it’s salary and display both, and if the salary is missing in function call it
should show it as 9000
(hint:In Python, we can specify default values for arguments when defining a function.)
(function2.py)
FUNCTIO EXERCISE
• Write a Python function to find the Max of three numbers.

• Write a Python function to multiply all the numbers in a list


(list_func.py)

• Write a Python function that accepts a string and calculate the


number of upper case letters and lower case letters.
(string_test.py)
PYTHON EXAM: TESTING
YOUR KNOWLEDGE
https://www.w3schools.com/quiztest/quiztest.asp?qtest=PYTHON
MORE ABOUT STRINGS
String Length
To get the length of a string, use the len() function.

Example

The len() function returns the length of a string:

a = "Hello, World!"
print(len(a))
The strip() method removes any whitespace from
the beginning or the end:

a = " Hello, World! "


print(a.strip()) # returns "Hello, World!"
The lower() method returns the
string in lower case:

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.

• Check if the phrase "ain" is present in the following text:

• txt = "The rain in Spain stays mainly in the


plain"
x = "ain" in txt
print(x)
Check if the phrase "ain" is NOT present in the following text:

txt = "The rain in Spain stays mainly in the plain"


x = "ain" not in txt
print(x)
Use the format() method to insert numbers into strings:

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

myorder = "I want {} pieces of item {} for {} dollars."


print(myorder.format(quantity, itemno, price))
• quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item
{1}."
print(myorder.format(quantity,itemno, price))

You might also like