python基础

运算符

1.m // n 整除   m / n 浮点数除法

2.a,b=divmod(m,n) a得到商,b得到余数

3.m ** n 求乘方

4.abs(m) 绝对值

字符串常见操作

1.str.isalpha() #是否是字母

2.str.isdigit() #是否是数字

3.str.isspace() #是否是空格

4."hello world".split(" ") 通过空格隔开得到 ["hello","world"]

5.'-'.join("hello world") 得到通过-连接的字符串 "hello-world"

6."abc".upper() -->"ABC"    "ABC".lower() -->"abc"

7."aBc".swapcase() -->"AbC"       

8.字符串连接 str1+str2

字符串复制str*2

求长度len(str)	

取出字符str[i]	

判断字符是否存在if c in str 

得到ASC||ord("a")  

由ASC||码转化为字符 chr(65) 

赋值:

1.连续赋值: a=b=c=1 

2.顺序赋值 a,b,c=1,2,3

3.递增不要用n++,而是n+=1 

类型转化

str() bool() int() float()      

列表和元组

区别:列表是可变类型,元组不可变

相同点:元素的类型没有限制

创建:

列表:alist[]  或者 list()

元组: atuple() 或者 tuple()

列表操作:

alist.append(item) 末尾添加元素

alist.insert(i,item) 在第i个位置插入元素

alist.pop() 删除并返回最后一个元素

alist.pop(i) 删除并返回第i个元素

alist.sort() 从小到大排序 alist.sorted() 不影响原来的列表

alist.sort(reverse=True) 从大到小排序 

alist.reverse() 逆序列表 alist.reversed() 得到逆序但是不影响原来的列表

del alist[i] 删除第i个元素

alist.index(item) 找到item的首次出现下标

alist.count(item) 统计item的出现次数

索引:
alist[i] atuple[i]
得到对应下标的元素,list可以更改,但是tuple不能更改

# 创建一个列表
alist=[1,True,"hello"] 	
# 访问
alist[0] # 1
# 增加
alist+["world"] # [1,True,"hello","world"]
# 求长度
len(alist)
# 判断元素是否存在于列表
1 in alist
# 切片
alist[1:3] # 切片得到区间[1:3],右边界可以越界默认最后一个元素
# 步长切片
alist[0:3:2] # 在区间[0:3]以2为步长切片
# 逆序输出
alist[::-1] # 逆序输出列表
#得到元素的下标
idx=alist.index(1)
# 元素出现次数
cnt=alist.count(1)
# 求和
s=sum(alist)
# 求最值
ma=max(alist)
mi=min(alist)

atuple=(1,True,"hello")
# 元组操作跟列表类似

字典

键值必须是不可变类型: 数值 / 字符串 / 元组

创建:
花括号{} 或者dict()
{name:“jack”,age:19,gender:“boy”}

更新:
增长字典:update()

student={name1:"jack"}
# 1.
newstudent={name2:"tom"}
student.update(newstudent) 
# 2.
student.update(newstudent={name2:"tom"}

# student={name1:"jack", name2:"tom"}

删除:

student={name1:"jack",name2:"tom"}

del student[name1] # 按照key删除

student.pop(name1) # 按照key删除并返回

student.clear() # 清空

取值:

student={name1:"jack",name2:"tom"}

student[name1] # jack

student.get(name1) # jack

student.keys() # 所有key值 name1 name2

student.values() # 所有values jack tom

存在:

student={name1:"jack",name2:"tom"}

name1 in student # 是否存在键值

jack in student.values() #是否存在值

集合:
不重复元素的集合,不能加入可变数据类型

集合中必须是不可变类型:

整数,浮点数,复数,字符串,逻辑值,元组

可变类型:

列表,字典,集合

创建:

aset={} , set()

集合的操作:

aset={'a','b','c'}

aset.add(1)

aset.remove('b') 

aset.pop() # 删除任意元素并返回

aset.clear() # 清除集合

for i in aset: # 遍历集合 

if i in aset: # 判断是否属于集合

输入输出

格式化输出:

print(1,2,3,end='')
# 1 2 3

print(1,2,3,sep=',')
# 1,2,3

print("%d %s"%(1,"hello world")
# 1 hello world

print("{}".format(1))
# 1
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值