写在前面
本系列教程针对有一定编程经验的伙伴快速入门python基础,一些涉及开发的常识问题,本文并不涉及。
方法 function
def greet_user(name):
print(f'Hi {name}!')
print('Welcome aboard')
print('Start')
greet_user('jay')
print("Finish")
=>
Start
Hi jay!
Welcome aboard
Finish
传递参数
def greet_user(first_name,last_name):
print(f'Hi {first_name} {last_name}!')
print('Welcome aboard')
print('Start')
greet_user(last_name='chou',first_name='jay')
print("Finish")
=>
Start
Hi jay chou!
Welcome aboard
Finish
异常处理
try:
age= int(input('Age: '))
print(age)
print(1/age)
except ZeroDivisionError:
print('Age cannot be 0')
except ValueError:
print("Invalid value")
注释
#这是注释
类
class Point:
def __init__(self,x,y):
self.x= x
self.y = y
def move(self):
print("move")
def draw(self):
print("draw")
point1=Point(10,30)
point1.x=10
point1.y=20
print(point1.y)
point1.draw()
20
draw
#2
class Person:
def __init__(self,name):
self.name=name
def talk(self):
print(f"Im {self.name}")
p1=Person("JayChou")
p1.talk()
=>
Im JayChou
#继承
class Mammal:
def walk(self):
print("walk")
class Dog(Mammal):
pass
class Cat(Mammal):
pass
dog1 = Dog()
dog1.walk()