Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
def
def mini(x,
mini(x, y):
y):
if x<y:
parmetros
definicin de if x<y:
formales
return
return xx
la funcin
else:
else:
return
return yy
age1
age1 == int(input('Enter
int(input('Enter age
age of
of first
first person:
person: '))
'))
weight1
weight1 = int(input('Enter weight of first person: '))
= int(input('Enter weight of first person: '))
age2
age2 == int(input('Enter
int(input('Enter age
age of
of second
second person:
person: '))
'))
weight2
weight2 == int(input('Enter
int(input('Enter weight
weight of
of second
second person:
person: '))
'))
invocacin o llamado young = mini(age1, age2)
young = mini(age1, age2) argumentos
de la funcin slim
slim == mini(weight1,
mini(weight1, weight2)
weight2)
print('The
print('The young
young one
one is',
is', young,
young, 'years
'years old')
old')
print('and
print('and the
the slim
slim one
one weights',
weights', slim,
slim, 'kilos')
'kilos')
s1
s1 == 'Hola'
'Hola'
s2
s2 == 'holas'
'holas'
print(strIsIn(s1,
print(strIsIn(s1, s2,
s2, False))
False)) ## imprime
imprime Yes
Yes
print(strIsIn(s1,
print(strIsIn(s1, s2,
s2, True))
True)) ## imprime
imprime No
No
argumentos asignados
print(strIsIn(s2,
print(strIsIn(s2, s1,
s1, False))
False)) por nombre
print(strIsIn(sen=False,
print(strIsIn(sen=False, str2=s1,
str2=s1, str1=s2))
str1=s2))
argumento omitido por
print(strIsIn(str2=s1,
print(strIsIn(str2=s1, str1=s2))
str1=s2)) tener valor por defecto
def
def f(x):
f(x):
variables yy == 11
locales a f xx == xx ++ yy
print('x
print('x =', =', x)
x)
return
return xx
variables xx == 33
globales yy == 22
zz == f(x)
f(x) ## valor
valor de
de xx usado
usado como
como argumento
argumento
print('z
print('z =',
=', z)
z)
print('x
print('x =',
=', x)
x)
print('y
print('y =',
=', y)
y)
g
h
xx == xx ++ 11
print('x
print('x =', =', x)
x) h h h h h
h()
h() g g g g g
f
g()
g()
x x x x x x
z
print('x
print('x =', =', x)
x)
return
return gg
z z z z z z z z
global
xx == 33 x x x x x x x x
zz == f(x)
f(x) f f f f f f f f
print('x
print('x =',
=', x)
x)
1 2 3 4 5 6 7 8
print('z
print('z =',
=', z)
z)
z()
z()
Funciones Algoritmos y Programacin 8
Nested functions
def
def compare():
compare():
def
def mini(x,
mini(x, y):y):
if
if x<y:
x<y:
return
return xx funcin anidada
else:
else:
return
return yy
age1
age1 == int(input('Enter
int(input('Enter age
age of
of first
first person:
person: '))
'))
weight1
weight1 == int(input('Enter
int(input('Enter weight
weight of
of first
first person:
person: '))
'))
age2 = int(input('Enter age of second person:
age2 = int(input('Enter age of second person: ')) '))
weight2
weight2 == int(input('Enter
int(input('Enter weight
weight of
of second
second person:
person: '))
'))
young
young == mini(age1,
mini(age1, age2)
age2)
slim
slim == mini(weight1,
mini(weight1, weight2)
weight2)
print('The
print('The young
young one
one is',
is', young,
young, 'years
'years old')
old')
print('and the slim one weights', slim, 'kilos')
print('and the slim one weights', slim, 'kilos')
while(True):
while(True):
#print(mini(7,8))
#print(mini(7,8)) funcin desconocida en este mbito
wish
wish == input('Do
input('Do you
you want
want to
to compare
compare people?
people? ')')
if wish == 'no':
if wish == 'no':
break
break
compare()
compare()
print(suma(1,
print(suma(1, 2,
2, 3))
3))
print(suma(1,
print(suma(1, 2,
2, 3,
3, 4,
4, 5))
5))
def
def showMessage(msg):
showMessage(msg):
print(msg)
print(msg)
showMessage("Ready.")
showMessage("Ready.")
def
def showMessage(msg):
showMessage(msg):
print(strftime("%H:%M:%S",
print(strftime("%H:%M:%S", gmtime()))
gmtime()))
print(msg)
print(msg)
showMessage("Processing.")
showMessage("Processing.")
print("Original
print("Original list:",
list:", n)
n)
def
def f(x):
f(x):
x.pop()
x.pop()
x.pop()
x.pop()
x.insert(0,
x.insert(0, 0)
0)
print("Inside
print("Inside f():",
f():", x)
x)
f(n)
f(n)
print("After
print("After function
function call:",
call:", n)
n)
yy == lambda
lambda x:
x: x*6
x*6 ++ 33
print(y(8))
print(y(8))
zz == lambda
lambda x,y:
x,y: x**2
x**2 ++ y**2
y**2
print(z(2,3))
print(z(2,3))
Vp
Vp == 100
100
ff == 60
60
ww == 2*math.pi*60
2*math.pi*60
fi
fi == 00
f1
f1 == lambda
lambda t:
t: Vp*math.sin(w*t+fi)
Vp*math.sin(w*t+fi)
print(f1(0))
print(f1(0))
Funciones Algoritmos y Programacin 14
Anonymous functions
import
import math
math
yy == lambda
lambda x:
x: x*6
x*6 ++ 33
print(y(8))
print(y(8))
zz == lambda
lambda x,y:
x,y: x**2
x**2 ++ y**2
y**2
print(z(2,3))
print(z(2,3))
Vp
Vp == 100
100
ff == 60
60
ww == 2*math.pi*60
2*math.pi*60
fi
fi == 00
f1
f1 == lambda
lambda t:
t: Vp*math.sin(w*t+fi)
Vp*math.sin(w*t+fi)
print(f1(0))
print(f1(0))
Funciones Algoritmos y Programacin 15
Modules
main.py geom.py
from
from geom
geom import
import ** pi
pi == 3.14159
3.14159
rr == input('Ingrese
input('Ingrese el
el radio:
radio: ')
') def
def area(radio):
area(radio):
return
return pi*(radio**2)
pi*(radio**2)
rr == float(r)
float(r)
def
def perimetro(radio):
perimetro(radio):
print('pi:
print('pi: ',
', pi)
pi) return
return 2*pi*radio
2*pi*radio
print('rea:
print('rea: ',
', area(r))
area(r)) def
def sfEsfera(radio):
sfEsfera(radio):
return
return 4*pi*(radio**2)
4*pi*(radio**2)
print('permetro:
print('permetro: ',
', perimetro(r))
perimetro(r))
def
def volEsfera(radio):
volEsfera(radio):
print('superficie:
print('superficie: ',
', sfEsfera(r))
sfEsfera(r)) return
return (4/3)*pi*(radio**3)
(4/3)*pi*(radio**3)
print('volumen:
print('volumen: ',
', volEsfera(r))
volEsfera(r))
mdulos