python 简谈2

#1.if用法
if 1<100>3:
    print("right")
else:
    print("wrong")
elif #等价于else if
pass #不执行任何操作
if a>b and a==d: #且
if a>b or a==d: #或者
break和continue功能不变
colors=('red','balck')
if 'red' in colors: #判断列表中是否存在'red'
    print('red')
null=[]
if null: #判断列表是否是空,有值返回True,空的话返回False
    print(1)
else:
    print(0)
#2.字典用法
d={'pen':7,'apple':3,'applepen':10} #键值对
print(d['apple'])
d['apple']=10 #修改值
d2={'a':[1,2,3,4],'c':{'a':1,'b':2}}
print(d2['c']['a']) #输出1
del d['apple']
for key,value in d.items: #遍历
    print(key,value)
for key in d.keys():
    print(key)
for value in d.values():
    print(value)
for key in sorted(d.keys()):#键排序
#3.函数
def function(a=20,b=20): #设置默认值
    c=a+b
    print(c)
function(1,2)
a=1000
def function(b=20):
    global a#全局变量
    c=a+b
    print(c)
function(1,2)
def add(a,b):
    c=a+b
    return c
#4.类
class human:
    name='someone'
    age=100
    def my_name(self):
        print(self.name)
    def my_age(self):
        print(self.age)
    def eat(self):
        print('eat')
   def think(self,a,b):
     print(a+b)
person1=human()
person1.name
person1.eat()
person1.think(2,3)
class human:
    def __init__(self,name,age):#双下划线,要注意
        self.name=name
        self.age=age
    def my_name(self):
        print(self.name)
    def my_age(self):
        print(self.age)
    def eat(self):
        print('eat')
    def think(self,a,b):
        print(a+b)
person2=human('lili',29)#定义参数
class human:
    def __init__(self,name='li',age='20'):#预定义
        self.name=name
        self.age=age
    def my_name(self):
        print(self.name)
    def my_age(self):
        print(self.age)
    def eat(self):
        print('eat')
    def think(self,a,b):
        print(a+b)
person2=human()
class human():
    def __init__(self,name='li',age=25):
        self.name=name
        self.age=age
        print("human init")
    def my_name(self):
        print(self.name)
    def my_age(self):
        print(self.age)
    def eat(self):
        print('eat')
    def think(self,a,b):
        print(a+b)
class student(human):#子类继承父类
    def __init__(self,grade=1,school='mit'):
        super().__init__()#父类初始化
        self.grade=grade
        self.school=school
        self.score=100
        print('student init')
    def learn(self):
        print('learning')
    def my_school(self):
        print("school:",self.school)
    def think(self,a,b):
    print(a*b)#子类重写父类的函数,优先
stu2=student(4)























































































































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值