-
Web 和 Internet开发
-
科学计算和统计
-
教育
-
桌面界面开发
-
软件开发
-
后端开发
一、python 语法规则
1、空格很重要(一般四个空格)
2、python支持中文(一般使用英文)
3、变量不需要提前声明,可以直接使用
二、python数据类型(我只写了两个特别的)
数字:整型、浮点型、布尔、复数
列表: all_in_list[]
特点-->可以放任意类型且有序
从左往右 从0开始 反之从-1开始
列表的增删改查:(从上往下一直运行下去)
例如:all_in_list=[0.3,'hello',True]
res=all_in_list[-3]#查找元素(从右往左即从-1开始) 0.3
res=all_in_list[0:1]#列表的切片 左闭右开[0,1) [0.3]
all_in_list.append('hello world')#追加元素 [0.3, 'hello', True, 'hello world']
all_in_list.insert(0,'pre-hello')#插入元素 ['pre-hello', 0.3, 'hello', True, 'hello world']
all_in_list.remove('hello world')#删除 ['pre-hello', 0.3, 'hello', True]
del all_in_list[:2]# ['hello', True]
all_in_list[0]=100#元素的修改 [100, True]
列表的推导式:
b=[i for i in range(1,11)]#列表推导式 i为返回值 for后面表示对i的说明
c=[i**2 for i in range(1,11) if i%2==0 ]
三、for 循环
for 元素 in 集合:
缩进4个空格 do sth
例如:x=[]
for i in range(10):
x.append(i)
print(x)
四、操作符(与其他编程语言类型):python特点--->逻辑操作符可以连续比较
五、条件判断语句
if :
print()
elif :
print()
else:
print()
六、字符串:不可变数据类型
字符串的方法:
string='my name'
res=string[0]#字符串的索引
res=string[:2]#截取
res=string*2#重复
res=string+'is jj'#拼接
res=string.split(sep=',')#以,为分隔符
res=string.lower()#将元素全部变为小写
七、字典 (大括号) {} :字典中元素无顺序 通过键来访问值
特点:元素成对出现
键不能重复 不可修改(不能为列表)
键来索引值
增删改查与列表类似
dic={'h':'hello',0.5:[0.3,0.2],'w':'wporld'}
dic['h']=100#修改 即重新进行赋值
dic['hw']='helloworld'#新增键值对
#{'h': 100, 0.5: [0.3, 0.2], 'w': 'wporld', 'hw': 'helloworld'}
dic.update({1:2,3:4})#新增键值对
del dic['h']#删除
字典的推导式:
a={i:i**2 for i in range(10)}
八、文件操作
open(文件名,访问模式)
write()
read()