Python Programming — Function

def hello():
print('Hello world !')
hello()
hello()
hello()
Hello world
Hello world
Hello world
def hello(nama):
print('Hello %s!' % nama)
def print_max(a, b):
if a > b:
print('%s is the maximum value' % a)
elif a == b:
print('%s equals %s' % (a, b))
else:
print('%s is the maximum value' % b)
hello('World')
hello('Indonesian People')
print_max(100, 100)x = 5
y = 3
print_max(x, y)
Hello World!
Hello Indonesian People!
100 equals 100
5 is the maximum value
def areaTriangle(a,t):
area = a*t/2
print('Area of triangle with base', a,
'and height', t,
'is', area)
base = 10
height = 20
areaTriangle(base,height)
Area of triangle with base 10 and height 20 is 100.0
def areaTriangle(a,t):
area = a*t/2
return area
base = 9
height = 21
print('Area of triangle with base', base,' and high', height,'is', areaTriangle(base,height))
Area of triangle with base 9  and high 21 is 94.5
x = 50
def function(x):
print('x = ', x)
x = 2
print('change local variable x = ', x)
function(100)
print('x value is still %s' % x)
x =  100
change local variable x = 2
x value is still 50
x = 50def function():
print('x = ', x)
def function2():
x = 100 # write to local variable
print('x = ', x)
def function3():
global x
x = 100
print('x = ', x)
function()
print('value x = ', x)
function2()
print('value x = ', x)
function3()
print('value x = ', x)
x =  50
value x = 50
x = 100
value x = 50
x = 100
value x = 100

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store