1.运算符
+ - * / ** % //
in not in
== > < => <= != <>不等于
and or
补充:
先计算括号内
执行顺序:
从前到后
结果:
True or ==》True
True and ==》继续执行下一步
False or ==》继续执行下一步
False and ==》False
布尔值:
True:真
Flase:假
赋值运算符
i += 1
比较运算符
逻辑运算符
2.基本数据类型
数字:int
将字符串转换成数字:a="123";b=int(a);type()查看类型
r = age.bit_length()
数字:int(整形)---python3
# a = '123'
# print(type(a),a)
# b = int(a)
# print(type(b),b)
#
# num = "b"
# v = int(num,base=16)
# print(v)
# 字符串:str
test = "ALEX"
v = test.capitalize() #首字母大写
v1 = test.lower() #全部小写
v2 = test.center(10,'*') #10:总长度;*:填充
print(v2)
A = "alengthex"
x = A.endswith('h')
w = A.count('e',4) # 计算‘e’出现的次数;4:表示从第4位开始查找“e”
r = A.find('ex',5,9)
print(r)
#format:将一个字符串中的占位符替换为指定的值
# B = 'I am {name},age {a}'
# T = B.format(name='alex',a=19)
# print(T)
B = 'I am {0},age {1}'
T = B.format('alex',19)
print(T)
#B.format_map() 格式化,传入的值:0: ,1: 。
#判断字符串中是否只包含字母和数字
test_01 = 'a'
v3 = test_01.isalnum()
print(v3)
root = "username\tpassword\nlifei\t123456\nwangwu\t123456"
k = root.expandtabs(20)
print(k)
# 判断是否是数字
root1 = '②'or '二'
k1 = root1.isdecimal()
'''特殊数字有的支持'''
k2 = root1.isdigit()
k3 = root1.isnumeric()
print(k1,k2,k3)
# 判断是否是字符:isalpha()
#判断是否全部是空格:isspace()
#判断是否是标题:istitle() title可转换成标题
#将字符串中的每一个元素按照指定分隔符进行拼接
kk = "你是风儿我是沙"
print(kk)
kk1 = ' '
vv = kk1.join(kk)
print(vv)
#ljust(10,'*')把*放左边;rjust(10,'*')把*放右边
#移除指定字符:lstrip()/rstrip()/strip()
#分割:partition() 找到第一个开始分割;split()全部分割,可指定分割几部分
#大小写转换
kk2 = "alex"
vv1 = kk2.swapcase()
print(vv1)
# 字符串一旦创建,不可修改
# 一旦修改或者拼接,都会重新生成字符串
# niu = "有种冲我来!"
# for i in niu:
# print(i)
# num = range(0,10)
# for ii in num:
# print(ii)
tue = input(">>>")
print(tue)
l = len(tue)
pp = range(0,l)
for item in pp:
print(item,tue[item])
tue1 = input(">>>")
for item1 in range(0,len(tue1)):
print(item1,tue1[item1])
字符串:str
以下基本必须要会:join/split/find/strip/upper/lower/replace
次要:索引、切片、len()、'_'.join(" ")、for循环、range
字符串一旦创建,不可修改
一旦修改或者拼接,都会重新生成字符串
python2中range:立即创建
python3中range:for循环时候才一个个创建
# 输入字符为某值是用***替换
#法一
# s = input(">>>")
# if s == "苍老师" :
# print("***")
# elif s == 京东热":"
# print("***")
# else:
# print(s)
#法二
# n = 0
# while n<7:
# s = input(">>>")
# if s == "苍老师" and "京东热":
# print("****")
# break
# else:
# print(s)
# n += 1
# 计算数值,字符串与数值的转换
# content = input("请输入内容: ")
# print(content)
# v1,v2 = content.split("+")
# v1 = int(v1)
# v2 = int(v2)
# v = v1 +v2
# print(v)
# test_01 = 'a'
# v3 = test_01.isalnum()
# print(v3)
# root = "username\tpassword\nlifei\t123456\nwangwu\t123456"
# k = root.expandtabs(20)
# print(k)
列表:list
中括号括起来;‘,’分割每个元素;列表中的元素可以是数字、字符串、列表、布尔值
append、index、insert、reverse、pop、extend、remove、count、copy、clear
# 类,列表
# li = [1,2,5,15,"age",["wangzhe","王五"],"alex",True]
# # 索引
# print(li[-1])
# # 切片
# print(li[1:5])
# # for循环
# for item in li:
# print(item)
# # 修改
# li[0] = [5,'nihao',666]
# print(li)
# # 删除
# del li[1:3]
# print(li)
# # 支持in 操作
# # li = [1,2,5,15,"age",["wangzhe","王五"],"alex",True]
# s = 5 in li
# print(s)
# 转换
# 既有数字又有字符串
# li1 = [5842,899,38,"wugang","@163.com"]
# s = ""
# for i in li1:
# s = s + str(i)
# print(s)
# # 只是字符串,可以用下列方法转换
# li2 = ["wu","gang"]
# v = "".join(li2)
# print(v)
####################列表的关键字使用讲解#######################
li = []
# 1.原列表末尾添加:append
# li.append(66)
# li.append("niubi")
# print(li)
# 2.清空列表
# li.clear()
# print(li)
# 3.拷贝:copy
# v = li.copy()
# print(v)
# 4.计算元素出现个数
li = [66,55,"wugang","niubi",66,"wang","wu"]
v = li.count(66)
print(v)
# 5.扩展原列表:extend
li.extend(["wangba",886])
print(li)
li.extend("不得了")
print(li)
# 6.根据值找索引位置:index
v1 = li.index("wu")
print(v1)
# 7.在指定位置插入元素:insert()
li.insert(1,"王者荣耀")
print(li)
# 8.删除某个值(指定索引,不指定默认最后一个值),并获取删除的值:pop
# 9.删除列表中的指定值,左边优先:remove
li.pop(-1)
print(li)
# 10.反转:reverse()
li.reverse()
print(li)
###11.排序:sort、compare、key、sorted
元组:tuple
元组的一级元素不可以删除、修改、增加
####元组:tuple
# 元祖不可修改,不能增加或者删除
tu = (111,"linux",(33,44),True)
#可被for循环,可迭代对象
for item in tu:
print(item)
# 字符串、列表的转换
s = "abcdefghjklm"
li = [222,666,"alex",888]
tu = (456,123,"wangzhe")
v1 = tuple(s)
print(v1)
v2 = tuple(li)
print(v2)
v3 = list(tu)
print(v3)
# 字符串之间可以用 .join
tu1 = ("wznghe","rongyao")
v = '_'.join(tu1)
print(v)
# 元组,有序;元组的一级元素不可修改,删除,增加
tu = (111,"awe","6gx",[555,686],666,"dng",666)
tu[3][0]=886
print(tu)
# 元素出现的个数
v=tu.count(666)
print(v)
字典:dict
info = {"k1":"v1","k2":"v2"} 键值对
列表、字典不能作为字典的key;布尔值可以作为字典的key
# 字典
info = {
"key1":521,
"value":886,
(123,321):"元组",
"key4":False,
True:666,
#{"k1":11,"k2":22 }:998
}
print(info)
# for循环
# for item in info:
# print(item,info[item])
#
# for k,v in info.items():
# print(k,v)
####fromkeys:根据序列,创建字典,并指定统一的值
dic = {
"k1":"v1",
"k2":"v2"
}
# v = dict.fromkeys(["k1",123,"999"],123)
# v1 = dict.fromkeys(["xingm"],"wugang")
# print(v)
# print(v1)
#####get:根据key获取值,key不存在时可以指定默认值(None)
s = dic.get("k1",11111)
print(s)
#####pop:删除字典内指定的键值对
# dic.pop("k1")
# print(dic)
#####setdefault:设置值,已存在,不设置,获取当前key对应的值;不存在,设置,获取当前key对应的值
vv = dic.setdefault("k22222","666")
print(dic,vv)
#####update更新
dic.update(k1=567,k3=789)
print(dic)
布尔值:bool