wangmiao 发表于 2022-5-16 22:09:29

python基础

#比较运算符
#一般运用在判断语句或者进行测试的断言
#a = 5
#b = 3
#c = 3
#print(a>b)   #ture
#print(a<c)   #false
#print(b>=c)#ture
#print(a!=c)#ture
#print(b!=c)#false
#print(b==c)#ture
#print(b<=a)#ture

#逻辑运算符
# a = 8
# b = 5
# c = 5
# # print(a>b and a>c)   #ture
# # print(a>b and a<c)   #false
# # print(a>b or a<b)    #ture
# # print(not b == c)    #false
# print(not b != c)      #ture

#成员运算符
# list1 = ["xian02","dcs","yrr","wm",10086]
# print("10086" not in list1)#ture
# print(10086 not in list1)    #false
# print("xian02" not in list1) #false
# print("dcs" in list1)      #ture
# if "xian02" in list1:
#   print("测试成功")
# else
#   print("测试失败")


#python中的位运算符#
# a = 5
# b = 11

#print(a & b)#1   两个相应值都为1,则该位结果为1,否则为0
'''
存储单位
1TB = 1024GB
1GB = 1024MB
1MB = 1024KB
1KB = 1024BIYE
1BIYE = 8BIT比特位是计算机中最小的存储单位,由8位0或者1组成
0   0000 0 0 0 ----二进制
128 64 32 16 8 4 2 1 ----十进制中的数值

a = 5 =====>转换成二进制
00000101 =====>二进制中的5

b = 1
00001011 =====>二进制中的11

计算:
00000101
00001011
---------
00000001 =====>结果是十进制中的1
'''

#print(a|b)   #15    只要对应的二进位有一个为1时,结果就为1
'''
00000101
00001011
---------
00001111 =====>十进制中的15
'''

#print(a^b)   #14   当对应的二进位相异时,结果为1
'''
00000101
00001011
---------
00001110 =====> 十进制中的14
'''

#a = 5
#print(~a)#-6公式:-(x+1)按位取反的公式
'''
-(5+1) =====>-6
'''

#b = 11
#print(b << 2)#44    左移动运算符,高位丢弃,低位补零
'''
00001011 =====> 00101100==44
'''

#print(b >> 2)   #2   右移动运算符,低位丢弃,高位补零
'''
00001011 =====> 00000010==2
'''
#python中的索引
#索引分为:使用格式str
#1.正向索引:从0开始,从左到右取值
#2.负向索引:从-1开始,从右到左取值

#str1 = "yrrzuimei"
#print(str1)#i
#print(str1)#y
#print(str1[-5]) #u
#print(str)   #TypeError: 'type' object is not subscriptable 索引越界错误


#python中的字符串函数#

capitalize():字符串首字符大写
# str1 = "hello world"
# print(str1.capitalize())# Hello world

title():函数返回标题化的字符串,单词的首字母大写
# str1 = "hello world"
# print(str1.title())   # Hello World

count()统计字符串中字符出现的次数
# str1 = "hello world"
# print(str1.count("l"))# 3

join()
# str1 = "hello world"
# print("~".join(str1))   # h~e~l~l~o~ ~w~o~r~l~d

# list1 =["hello","world"]
# print("".join(list1))   #helloworld

split()函数
# str1 = "hello world"
# print(str1.split(" "))# ['hello', 'world']
# list1 = str1.split(" ")
# print(list1)   #hello
# print(list1)   #world
strip()函数,删除字符串开头与结尾的字符
# str1 = "duoceshioud"
# print(str1.strip("du"))   # oceshio
# print(str1.strip("ud"))   # oceshio
# print(str1.strip("oud"))# ceshi
# print(str1.strip("uod"))# ceshi
# print(str1.strip("d"))    #uoceshiou
# print(str1.strip("u"))    #duoceshioud

lstrip()函数,删除字符串开头的字符
# str1 = "duoceshioud"
# print(str1.rstrip("udo"))   #duoceshi
# print(str1.rstrip("ud"))    #duoceshio

rstrip()函数,删除字符串结尾的字符
# str1 = "duoceshioud"
# print(str1.rstrip("odu"))    #duoceshi
# print(str1.rstrip("u"))      #duoceshioud

startswith():判断字符串是否以什么开始 ====>返回布尔值
endswith():判断字符串是否以什么结束
# str1 = "duoceshioud"
# print(str1.startswith("duo"))   #True
# print(str1.endswith("oud"))   #True
# print(str1.endswith("duo"))   #False

# 应用场景
# 判断文件是否以.txt结尾的
# file_name = input("请输入您需要上传的文件名称")
# if file_name.endswith(".txt"):
#   print("文件有效")
# else
#   print("文件无效")

find()/rfind():字符串查询find()从左边开始查,rfind()从右边开始查
# str1 = "duoceshioud"
# print(str1.find("u"))   # 1
# print(str1.rfind("u"))#9

#如果遇到三个怎么办?
# str1 = "duoceshiduoduoduo"
# print(str1.find("o"))# 2
# print(str1.find("o",3))# 10

replace():替换某个字符串replace(substring,newstring,max)
# str1 = "xiaozhang"
# print(str1.replace("zh","w"))# xiaowang
# print(str1.replace("a","c"))   #xicozhcng
# print(str1.replace("a","c",1)) #xicozhang

lower():转换字符串中所有大写字符为小写
# str1 = "XiaoZhang"
# print(str1.lower())   # xiaozhang
upper():转换字符串中所有小写字符为大写
# str1 = "XiaoZhang"
# print(str1.upper())    # XIAOZHANG

isdigit():判断字符串中是否全为数字
# str1 = "12345e"
# print(str1.isdigit())    #False

isalpha():判断字符串中是否全为字母
# str1 = "xiaozhang"
# print(str1.isalpha())      #True

isalnum():判断字符串当中是否全为数字,全为字母,或者数字字母组合
# str1 = "asdfgg_"
# print(str1.isalnum())   #False

istitle():判断字符串中首字母是否为大写,其他是否为小写,其他不为小写则为否
# str1 = "xiaozhang"
# print(str1.istitle())    #False

isupper()/islower():判断一个字符串是否展示为都是大写或者小写
# str1 = "xiaozhang"
# print(str1.isupper())      #False
# print(str1.islower())      #True


#python中的列表#
# 列表是一个容器,可以装任何类型的数据
# 列表用list表示
# 表达式为:list = []

#定义列表
1.第一种方式
# list1 = ["dcs","xian02",10086,]
# print(list1)    # ['dcs', 'xian02', 10086, ]
# print(type(list1))   #<class 'list'>
2.使用list()函数把字符串可以转换成列表
#int(),str(),list()
# str1 = "xian01"
# list1 = list(str1)
# print(list1)# ['x', 'i', 'a', 'n', '0', '1']
# print(type(list1))   # <class 'list'>

#列表中常用的函数与方法
1.索引赋值
# list1 = ["dcs","xian01","xiaozhang",10086]
# list1 = "hello"#把hello赋值给到list1列表中索引为0的位置
# print(list1)   # ['hello', 'xian01', 'xiaozhang', 10086]
2.切片赋值
# list1 = ["dcs","xian01","xiaozhang",10086]
# list1 = "hello"
# print(list1)   # ['h', 'e', 'l', 'l', 'o', 'xiaozhang', 10086]

append()函数在列表的末尾处添加元素
#append可以添加任何数据类型的元素到列表的最末尾
# list1 = ["dcs","xian01","xiaozhang",10086]
# list1.append("xiaomiao")# 可以添加字符串
# print(list1)# ['dcs', 'xian01', 'xiaozhang', 10086, 'xiaomiao']
# list1 = ["dcs","xian01","xiaozhang",10086]
# list1.append(10010)   #可以添加整型
# print(list1)    # ['dcs', 'xian01', 'xiaozhang', 10086, 10010]
# list1 = ["dcs","xian01","xiaozhang",10086]
# list1.append()#可以添加列表
# print(list1)   # ['dcs', 'xian01', 'xiaozhang', 10086, ]

insert()函数在列表任意位置添加元素
# list1 = ["dcs","xian01","xiaozhang",10086]
# list1.insert(0,"xiaoliu")#可以添加字符串
# print(list1)# ['xiaoliu', 'dcs', 'xian01', 'xiaozhang', 10086]
# list1 = ["dcs","xian01","xiaozhang",10086]
# list1.insert(1,10010)    #可以添加整型
# print(list1)   # ['dcs', 10010, 'xian01', 'xiaozhang', 10086]
# list1 = ["dcs","xian01","xiaozhang",10086]
# list1.insert(1,)   #可以添加列表
# print(list1)   # ['dcs', , 'xian01', 'xiaozhang', 10086]

页: [1]
查看完整版本: python基础