امثلة على استخدامات التوابع في python
python functions examples
function code in python
function example1:
#write a python program, to define a function and call it back
def say_hello():
# block belonging to the function
print('hello world')
# End of function
say_hello() # call the function
say_hello() # call the function again
Function default:
#this program is to explain the idea of default argument(parameter)
def say(message, times=1):
print(message * times)
say('Hello')
say('World', 5)
Function global:
x = 50
def func():
global x
print('x is', x)
x = 2
print('Changed global x to', x)
func()
print('Value of x is', x)
Function local:
#this code is to explain the principle of local variables
x = 50
def func(x):
print('x is', x)
x = 2
print('Changed local x to', x)
print(x)
func(f)
print('x is still', x)
Function keyword:
def func(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)
func(3, 7)
func(25, c=24)
func(c=50, a=100)
Function param:
#this program do the following: define a function with parameters then call it
def print_max(a, b):
if a > b:
print(a, 'is maximum')
elif a == b:
print(a, 'is equal to', b)
else:
print(b, 'is maximum')
# directly pass literal values
print_max(3, 4)
x = 5
y = 7
# pass variables as arguments
print_max(x, y)
Function return:
def maximum(x, y):
if x > y:
return x
elif x == y:
return 'The numbers are equal'
else:
return y
print(maximum(2, 3))
Example on functions: Traffic light program
#write python program that prompts user to enter the color of traffic light, and then acts upon it.
def guessLight(l):
if l == 'red':
print('stop')
elif l == 'yellow':
print('Slow down')
elif l == 'green':
print('go')
else:
print('invalid entry, Choose between red, green, and yellow.')
light = input('please enter the color of traffic light')
guessLight(light.lower())
'''Exercise 1: Write a function f that computes f(x) where f(x) = x^3 - x + 1. (2 solutions
are expected: with and without calling pow() from cmath library)'''
import cmath
def f(x):
#i= x ** 3 - x + 1 #without using pow()
i=pow(x,3)-x+1 #using pow()
print('the result is ',i)
#Exercise : Write a function g that computes g(x, y) where g(x, y) = x^2 + √2
import math
def g(x , y):
return x**2 + math.sqrt(y)
print("example: result of g(2,3) is : ",g(2,3))
مثال عملي كبير: برنامج الة حاسبة باستخدام التوابع functions:
#create simple calculater و using functions:
def sum(num1,num2):
return num1+num2
def subtract(num1,num2):
return num1-num2
def multiply(num1,num2):
return num1*num2
def divide(num1,num2):
return num1/num2
def remainder(num1,num2):
return num1%num2
def power(number,power):
counter = 1
result = 1
while counter <= power:
result *= number # same as result=result*n
counter += 1 # same counter=counter+1
return result
x=int(input('please enter first number:'))
y=int(input('please enter second number:'))
choice=input("1.summation\n2.subtract\n3.multiply\n4.divide\n5.remainder\n6.power")
if choice=="1":
print('the summation result is :' , sum(x,y))
elif choice=="2":
print('the subtraction result is :',subtract(x,y))
elif choice=="3":
print('the multiplication result is :',multiply(x,y))
elif choice=="4":
print('the division result is :',divide(x,y))
elif choice == "5":
print('the remainder result is :', remainder(x, y))
elif choice == "6":
print('the power result is :',power(x, y))
else:
print("invalid entry")
الفيديو التالي يشرح الامثلة السابقة بالكامل: