CSCI 131 – Python Language



CSCI 131 – Python Language

Functions Practice

Example 1:

#In this example function has one numeric parameter and function has a return value

#function definition

def circleArea (radius):

area= radius *radius *3.14

return area

# main function that uses the function welcome

radius = 2.5

#function call inside print statement

print circleArea ( radius )

print circleArea ( 3.5)

radius = 4.5

#function call in assignment statement

areaResult = circleArea ( radius )

print areaResult

Example 2:

#In this example function has one numeric parameter, function doesn’t have return value

#function definition

def mantra ( n ):

counter =0

while counter < n:

print “I love Python “

counter = counter +1

#main program

num = input (“please enter the number “)

#function call

mantra (num)

Example 3:

#In this example function doesn’t have any parameters, function doesn’t has return value

#function definition

def fiveStars ( ):

print “*****”

#main program

num = input ( “Please enter the number “)

counter =0

while ( counter < num ):

fiveStars ( )

counter = counter +1

Example 4:

#function definition

def sumProd (num, letter):

if (( num >= 0 ) and ( letter == “S” )):

counter = 0

sum = 0

while ( counter = 0) and ( letter == “P” )):

if ( num==0):

prod =0

else:

counter=1

prod =1

while ( counter ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download