快捷键
注释:ctrl+?
函数说明:shift+tab
运算符
"""
运算
+ 加
- 减
* 乘
/ 除-带小数
% 取余
// 取整-无小数
"""
变量
"""
变量
a=1
b=2.3
s1="abc"
"""
循环
condition=1
while condition<=5:
print(condition)
condition+=1
"""
0-4,5个数
"""
for i in range(5):
print(i)
"""
指定2开始,到10结束,不输出10
"""
for i in range(2,10):
print(i)
"""
从1开始,10结束(不包含10),间隔2
"""
for i in range(1,10,2):
print(i)
"""遍历列表"""
for i in [1,3,5,7]:
print(i)
"""
遍历元组
"""
for i in (1,2,3):
print(i)
列表
"""列表基础:从0开始计数"""
a_list=[1,2,3,4.5,"abc"]
print(a_list)#打印整个列表
print(a_list[1])#打印下标为1的元素
print(a_list[4])#打印下标为4的元素
print(a_list[-1])#从右往左数第一个元素
print(a_list[-2])#从右网左数第二个元素
print(a_list[1:3])#下标为1到下标为2,不到下标3
print(a_list[:-2])#从0开始到从右往左数第二个(不包含)
print(a_list[-3:])#从右往左数第三个,打印完
"""遍历列表"""
for i in a_list:
print(i)
#列表长度
len(a_list)
#元素第一次出现的位置
print(a_list.index(4.5))
#元素出现次数
print(a_list.count(1))
#排序
b_list=[3,2,4]
b_list.sort()#正序
print(b_list)
b_list.sort(reverse=True)#倒序
print(b_list)
"""列表操作"""
#修改值
a_list[0]=100
print(a_list)
#末尾增加值
a_list.append("dfg")
print(a_list)
#下标为2增加值-插入
a_list.insert(2,300)
print(a_list)
#删除关键字,用位置
del a_list[-1]
print(a_list)
#删除,删除值_第一个出现的删除掉
a_list.remove("dfg")
print(a_list)
#弹出最后一个元素到变量中
a=a_list.pop()
print(a)
print(a_list)
"""多维列表"""
c_list=[[1,2,3],
[4,5,6],
[7,8,9]]
print(c_list[1])
print(c_list[2][1])
元组
"""数据类型-元组-不允许修改数据(除非重新赋值)"""
a_tuple=(1,20,30,70,50)
b_tuple=2,50,60
print(a_tuple)
print(b_tuple)
print(a_tuple[2])
print(a_tuple[2:4])
for i in a_tuple:
print(i)
a_tuple=(1,2)
print(a_tuple)
条件语句
"""条件语句
> 大于
>= 大于等于
< 小于
<= 小于等于
== 等于
!= 不等于
"""
x=1
y=2
z=3
m=1
if x>y:
print("1-right")
else:
print("1-wrong")
if x<y<z:
print("2-right")
if x==y:
print("x==y")
elif x==z:
print("x==z")
elif y==z:
pass #不做处理
else:
print("none")
"""
and 并且
or 或者
"""
x=1
y=2
z=3
m=1
if y>z and x==m:
print("1-right")
else:
print("1-wrong")
if y>z or x==m:
print("2-right")
else:
print("2-wrong")
colors=['red','black','blue','green']
for color in colors:
if color=='blue':
print('blue')
break #调出循环
else:
print('not blue')
colors=['red','black','blue','green']
for color in colors:
if color=='blue':
continue #跳出单次循环
print('blue')
else:
print('not blue')
if 'red' in colors:
print('true')
null=[]
if null: #判断是否为空
print('true')
else:print('false')
#比较字符串大小
if 'avc'>'hbg':
print('true')
else:print('false')
字典
"""数据类型-字典-无序"""
d1={'pen':7,'apple':4,'pear':2}#key:value
d2={1:'a',5:'b',3:'c'}
d3={1.2:3,'a':3.5,1:'aaa'}
print(d1)
d1['pen']=10#修改value
print(d1)
print(d2)
print(d3)
print(d1['pen'])
print(d2[5])
print(d3[1])
d4={'a':[1,2,3],'b':(1,2,3,4),'c':{'aa':1,'bb':2}}
print(d4)
print(d4['c']['aa'])
d4['d']='d'#添加键值对
print(d4)
del d4['d']# 删除键值对
print(d4)
#遍历字典键值对
for key,value in d4.items():
print('key:',key,'\t','value:',value)
#只遍历键
for key in d4.keys():
print('key',key)
#遍历值
for value in d4.values():
print('value',value)
#对键进行排序
for key in sorted(d2.keys()):
print('key',key)
函数
"""函数"""
def function1():
a=1
b=2
print(a+b)
function1()
def function2(a,b):#带参数函数,ab为形参
print(a)
print(b)
print(a+b)
function2(10,20)
def function3(a=2,b=4):#设置默认值,不传参数,使用默认值
print('a=',a)
print('b=',b)
print('sum=',a+b)
function3()
function3(1,6)
def function4(a,b=2):#a无默认值,必须赋值
print('a=',a)
print('b=',b)
print('sum=',a+b)
function4(2)
function4(2,3)
a=100
#函数里面调用全局变量
def function5(b=2):
global a
print('a=',a)
print('b=',b)
print('sum=',a+b)
function5(5)
#带返回值的函数
def add1(a,b):
return a+b
c=add1(2,2)
print(c)
模块
写函数——》download as->.py文件-》文件放到jupyter的文件目录下(我的是e:jupyter)->然后就可以导入了
"""模块"""
#导入自定义max模块
import max
a=max.fun_max(2,3)
print(a)
#从max木块导入fun_max函数
from max import fun_max
b=fun_max(3,4)
print(b)
#导入模块所有的内容
from max import *
#导入max,用as指定max别名为max1
import max as max1
c=max1.fun_max(5,6)
print(c)
#导入系统模块
import os
#输出当前文件所在路径
print(os.getcwd())
类(类的继承)
"""类"""
class human:
#类的属性
name="jupyter"
age=10
#类的方法
def my_name(self):#自己这个类
print("my name is",self.name)
def my_age(self):#自己这个类
print("my age is",self.age)
def eat(self):
print('eat')
def think(self,a,b):
print(a+b)
#类的实例化
person1=human()
person1.name
person1.name='lisi'
person1.name
person1.eat()
person1.my_name()
person2=human('xiaohong',10)
person2.my_name()
person2.my_age()
class human:
#初始化方法-创建对象时执行
def __init__(self,name="lisi",age=5):#设置初始值
self.name=name
self.age=age
print("human init")
#类的属性
name="jupyter"
age=10
#类的方法
def my_name(self):#自己这个类
print("my name is",self.name)
def my_age(self):#自己这个类
print("my age is",self.age)
def eat(self):
print('eat')
def think(self,a,b):
print(a+b)
# 类的继承-子类继承父类
class student(human):
pass
stu1=student()
stu1.my_age()
# 类的继承-子类继承父类
class student(human):
def __init__(self,school="as",grade=1):
#调用父类的初始化,初始化名字和年龄
super().__init__()
self.grade=grade
self.school=school
self.score=100
print("student init")
#添加自己的方法
def learn(self):
print('learning')
def my_school(self):
print("my shool if",self.school)
stu2=student('er',4)
stu2.my_age()
stu2.my_school()
# 类的继承-子类继承父类
class student(human):
def __init__(self,school="as",grade=1):
#调用父类的初始化,初始化名字和年龄
super().__init__()
self.grade=grade
self.school=school
self.score=100
print("student init")
#添加自己的方法
def learn(self):
print('learning')
def my_school(self):
print("my shool if",self.school)
#重写父类方法
def think(self,a,b):
print(a*b)
stu3=student()
stu3.think(3,4)
input输入
"""输入-input"""
a_input=input("please input something...")
print(a_input)
name=input("please input your name:")
print("hello",name)
number=input('please input a number..')
if int(number)==1:
print('well done')
elif number=='2':
print("All right")
else:print('ok')
文件读写
text="Writing a text\nhello world"
print(text)
"""文件读写"""
my_file=open('file1.txt','w')#已写入的方式打开文件,如果文件不存在会创建文件
my_file.write(text)
my_file.close()
#with打开不用关
with open('file2.txt','w') as f2:#清空文件,然后写入
f2.write('hahha\n123')
with open('file2.txt','a') as f2:#文件后面追加内容
f2.write(text)
with open('file2.txt','r') as f2:#以读取方式打开文件
content=f2.read()#读取全部内容
print(content)
with open('file2.txt','r') as f2:
content=f2.readline()#读取一行
print(content)
with open('file2.txt','r') as f2:
content=f2.readlines()#读取所有行放到一个列表中
print(content)
filename='file2.txt'
with open(filename) as f:
for line in f:
print(line.rstrip())#rstrip删除字符串后面的换行
异常处理
"""异常处理"""
try:
#先尝试打开,出错执行except
file=open('hahha','r+')#先去读一个文件们如果能打开就写入
except Exception as e:
print(e)
response=input("do you want to creat it:")
if response=='yes':
with open('hahha','w')as f:
pass
print("the file was created successfully")
else:
pass
"""异常处理"""
try:
file=open('hahha','r+')#先去读一个文件们如果能打开就写入
except Exception as e:
print(e)
response=input("do you want to creat it:")
if response=='yes':
with open('hahha','w')as f:
pass
print("the file was created successfully")
else:
pass
else:#try没有出错
file.write('123')
file.close()
json数据存储
"""json数据存储"""
import json
a_dict={'user':'abc','age':12,100:200}
#数据存储
with open('example.json','w') as f:
json.dump(a_dict,f)
#读取内容
with open('example.json','r') as f:
content=json.load(f)
print(content)