变量
a=1
b=2
c=a+b
print(c)
判断
a=100
if a>=0:
print(100)
else:
print(1000)
循环
names=['yang','li','zhang']
for name in names:
print(name)
sum=0
for x in range(100):
sum+=x
print(sum)
函数
def my_function(x):
if x>100:
return 100
else:
return -100
a=my_function(99)
b=my_function(1000)
print(a)
print(b)
类
class Student(object):
"""docstring for Student"""
def __init__(self, name,age,sorce):
super(Student, self).__init__()
self.name = name
self.age = int(age) 可以被外部访问
self.__age=int(age)不可以被外部访问 私有的 __相当于private
self.sorce =sorce
def get_grade(self):
if self.sorce>=90:
return 'A'
elif self.sorce>=60:
return "B"
else:
return "C"
def get_age_stage(self):
if self.age<14:
return 'juvenile'
elif self.age<30:
return 'youth'
elif self.age<60:
return 'middle'
else:
return 'oldman'
a=Student("ccc",19,99)
print(a.name)
print(a.get_grade())
print(a.get_age_stage())
网友评论