Python语法总结
Python数值计算
算术运算种类:+(加) -(减) *(乘) / (除) **(幂次) % (取模)
a=10/3
print(a)
b=10%3
print(b)
赋值运算:
= += -= *= /= %= **=
c**=a
c=c**a
逻辑运算:
and 与 or 或 not 非
查找运算符:
in
not in
身份运算符:
is
简单基本函数:
is not
类型运算符 type()
取绝对值 abs()
取整函数 round()
取最值函数 max() min()
地址运算符 id()
科学计数法:
8e5
8*(10**5)
三元运算符:
=
1 if
2 Result = a if a>b c
强制类型转换:
常用数据类型:DATATYPE
等
价
于
在
指
定
序
列
中
不
在
指
定
序
列
中
用
于
判
断
两
个
标
识
符
是
否
来
自
同
一
内
存
空
间
等
价
于
结
果
值
条
件
值
int
float
str
bool (true false)
类型转换方法:
DATATYPE(Num)
a='123'
type(a)
输出:str
float(a)
输出:123.0
3>3
输出:False
max(1,2,3,4)
输出:4
min([1,2,3,4])
输出:1
max,min既可以操作数组又可以操作列表
Python字符串操作(str)
python
‘ ’ , “ ”
str="Hello python" ##双引号必须用英文格式
要
求
字
符
串
必
须
使
用
引
号
括
起
来
,
使
用
单
引
号
也
行
,
使
用
双
引
号
也
行
注
意
:
字
符
串
对
象
是
不
可
改
变
的
,
也
就
是
说
在
创
建
一
个
字
符
串
后
,
你
不
能
把
这
个
字
符
中
的
某
一
部
分
改
变
。
任
何
下
面
的
函
数
改
变
了
字
符
串
后
,
都
会
返
回
一
个
新
的
字
符
串
,
原
字
串
却
并
没
有
变
。
字
符
串
创
建
:
使
用
引
号
括
起
来
字符串拼接:
+
str1="Hello"+" "+"Python"
print(str1)
输出:Hello Python
字符串乘法:
*
str="Hello"*3
print(str)
输出:HelloHelloHello
字符串长度计算:
len()
len(str)
输出:15
数值转化字符串:
repr()
a=12
b=repr(a) ##使用str函数报错
print(b)
type(b)
输出:12
字符串核心操作:
字符串切分为列表:
str.split()
str="1 2 3 4 5"
str.split()##以空格为界进行拆分
输出: ['1', '2', '3', '4', '5']
使
用
加
法
表
示
复
制
使
用
函
数
使
用
使
用
函
数
str="1,2,3,4,5"
str.split(',') ##以逗号未拆分
输出: ['1', '2', '3', '4', '5']
字符串聚合:
使用str.join(str1)函数
str1
str(
)连接生成一个新的字符串*
str='hellopython'
str_blank=' '
str_blank.join(str)
输出: 'h e l l o p y t h o n'
字符串替换:
使用str.replace(str1,str2)函数
str1
str2
str="hello python"
str.replace("python","guo")
输出: 'hello guo'
字符串去首尾空格:
str.strip() 去掉str首尾空格 str.lstrip() 去掉str首空格 str.rstrip() 去掉str尾空格
str=" hello python "
str.strip()
输出: 'hello python'
str.lstrip()
输出: 'hello python '
str.rstrip()
输出: ' hello python'
字母大小写替换:
str.upper() 字母转换为大写
str.lower() 字母转换为小写
将
字
符
串
以
指
定
的
字
符
分
隔
符
注
意
使
用
顺
序
替
换
为
替
换
只
是
浅
替
换
str="hello python"
str.upper()
输出: 'HELLO PYTHON'
str.lower()
输出: 'hello python'
查找字符串:
str.find(str2)
查找字符串str中str2的索引
str='hello'
str_1='el'
str.find(str_1)
输出: 1
Python数据结构索引
从前面是从0开始
从后面是从-1开始
str="hello python"
str[2]
输出: 'l'
str[-3]
输出: 'h'
切片操作,也叫冒号操作
切片 :
表示从哪到哪,左闭右开的区间
str[0:4] 表示 str[0]--str[3] str[1:-2] 表示从第一个到倒数第二个 str[:7] 表示从第一个到第六个 str[-2:] 表示从倒数
第二个到最后一个 str[:] 表示取所有 str[::2] 表示每隔两个取一个 str[::-1] 表示翻转
str[0:4]
str[1:-2]
str[:7]
str[-2:]
str[:]
str[::2]
str[::-1]
输出:'nohtyp olleh'
Python列表(list)
python列表类似于c中的数组 python的列表是比较强大的,它包含了很多不同类型的数据:整型数字,浮点型,字
符串以及对象等 没有长度限制
列表创建:
[] list([]) ##该方法可能报错
list_1=[1,2,3,4,5]
list_1
输出:[1, 2, 3, 4, 5]
列表核心操作
列表长度计算: len(list)
列表扩展: list_1+list_2
列表复制: list*3
列表索引: 冒号:索引
列表元素删除 del list[Num] 删除list的索引为Num的元素
list=[1,2,"python",3.4]
del list[1]
list
输出:[1, 'python', 3.4]
相同元素计数:
list.count(str) 统计list中str的个数
list=[1,2,3,5,4,2,4,2,3,5,4,3,4,5]
list.count(1)
输出:1
获取指定元素的索引:
list.index(str) 获取list中str的索引、 注意list中没有find函数操作
list.index(2)
输出:1
列表元素添加操作:
list.append(element) 在list中添加元素element element可以是字符串,列表
列表元素移除:
list.remove(element) 在list中移除元素element element可以是字符串,列表,remove移除后原列表改变
列表元素获取:
list.pop(element) pop只能弹出某个元素,原列表不会变
list=[1,2,3,4,5,6]
list.remove(2)
list.pop(3) ##pop弹出后,list中将没有该元素
list
输出:[1, 2, 3, 5, 6]
列表元素排序:
list.sort() list_1=sorted(list) 对list内元素排序 列表元素逆序:list.reverse() 注意:逆序操作目前只适用于字符串
list_1=[2,3,1,23,4,2]
list_1.sort()
list_1
输出:[1, 2, 2, 3, 4, 23]
list_2=["hellopython",'kaishi']
list_2.reverse()
list_2
输出:['kaishi', 'hellopython']
Python字典
字典(Dictionary)在Python中是一种可变的容器模型,它是通过一组键(key)值(value)对组成,这种结构
类型通常也被称为映射,或者叫关联数组,也有叫哈希表的。每个key-value之间用“:”隔开,每组用“,”分割,整个字
典用“{}”括起来
字典的创建:
括号法:dict={"key":value,....} dict_1=dict()
注意: 定义字典时,键在前,值在后,键必须唯一性,值可以不唯一,如果键有相同,值则取最后一个; 值可以是
任何的数据类型,但是键必须是不可变的数据类型(数字、字符串、元组);
字典核心操作:
访问字典中某一键的值:
dict[key] 注意字典存储并无顺序可言,而且只能用键值索引 dict.pop[key]
访问字典中的属性:
dict.keys() dict.values() dict.items() 上述数据都是要以列表形式返回
字典中增加键值:
dict[new_key]=newnumber 在字典中添加新键new_key,对应新值newnumber
字典中删除键值:
del dict[key] 删除键key
清空字典:
dict.clear()
字典的更新:
dict.update(dict_1) 用dict_1更新dict 但是dict_1不变
字典的查找:
dict.get(key_1,"Not exist") 在dict中查找key_1,不存在返回Not exist,存在即输出键的值
dict_1={"c":12,"python":11,"java":10}
dict_2={'cuda':3,'matlab':30}
list_1=dict_1.keys() ##获取python的键
print(list_1)
list_2=list(list_1) ##使用list()将字典类型的键转为列表结构就可以进行索引
print(list_2)
print(list_2[1])
输出: dict_keys(['c', 'python', 'java'])
['c', 'python', 'java']
python
dict_1['c']
输出: 12