##1
def my_function():
print("Hello from a function")
my_function()
##2
def my_function(fname):
print(fname + " Gates")
my_function("Bill")
my_function("Steve")
my_function("Elon")
##3
def my_function(country = "China"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
##4
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
##5
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
##6
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Phoebe", child2 = "Jennifer", child3 = "Rory")
##7
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Phoebe", "Jennifer", "Rory")
##8
def myfunction():
pass
##9
def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result
print("\n\nRecursion Example Results")
tri_recursion(6)
# thisdict = dict(brand="Porsche", model="911", year=1963)
# print(thisdict)
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11))
def myfunc(n):
return lambda a : a * n
mytripler = myfunc(3)
print(mytripler(11))
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
###10
print('*'*10)
class MyClass:
x = 5
p1 = MyClass()
print(p1.x)
###11
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Bill", 63)
print(p1.name)
print(p1.age)
##12
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("Bill", 63)
p1.myfunc()
python练习写几个简单函数和类_20220706
最新推荐文章于 2024-06-17 17:35:06 发布