python 简谈2
来源:互联网 发布:ps修淘宝图片 编辑:程序博客网 时间:2024/05/24 04:39
#1.if用法if 1<100>3: print("right")else: print("wrong")elif #等价于else ifpass #不执行任何操作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']) #输出1del 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=1000def 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.nameperson1.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
- python 简谈2
- python 2 & python 3
- Python--python基础知识(2)
- 【python】python基础知识2
- Python简谈
- Python-(2)
- Python(2)
- python(2)
- Python - 2
- Python(2)
- Python-2
- python(2)
- Python 2
- python-2
- python(2)
- python 2 和 python 3
- Python-2 Python小程序
- Python 语言介绍--Python(2)
- make命令工具与Makefile
- Python学习笔记之类和对象
- Linux 文件描述符与文件系统
- bzoj4590 [Shoi2015] 自动刷题机 二分答案
- 《spring cloud微服务实战》读书笔记——Spring Cloud Eureka(五)Eureka详解
- python 简谈2
- Redis初窥:常用命令
- Centos 6.5(64位) vim 8.0 安装
- python和类相关的几个函数
- 树莓派真的适合学习Linux系统吗?
- Docker单机安装
- Spring配置详解
- 简要理解CommonJS规范
- 极限存在准则及两个重要极限