python基础篇 第二节课
布尔值
true 真
false 假
比较运算符号
== 等于
< 小于
> 大于
<= 小于等于
>= 大于等于
!= 不等于
<> 不等于
not
and
or
补充:执行顺序
user == ‘alex’ and (pwd == “123” or == 1) 这样的 先算括号里的
user = "alex" pwd = "123" v = user == "alex" and (pwd == "124" or 1 == 1) print(v) #输出 true
其他的 从前往后
true or —————— true
true and —————— 继续走
false or —————— 继续走
false and —————— false
user = "alex" pwd = "123" v = user == "alex" and pwd == "123"or 1==1 and pwd = "4654" print(v) #输出true
运算结果是值
赋值运算 count = count + 1 (count + = 1)
算数运算 a = 1 b = 3 print(a + b )
运算结果是布尔值
成员运算 a = “李” in “我是李怕怕”
比较运算 a = 1 > 5
逻辑运算 a = 1 > 6 or 1 == 1
补充: 成员运算 判断某个东西是否在定义的内容中
name = "我是李怕怕"
if "李" in name:
print("ok")
else:
print("error")
基本数据类型
数字 int #python3中无范围限定 , 在python2 中有范围限定,超过了长度范围要用 long
字符串 str
列表 list
字典 dict
元祖 tuple
布尔值 bool
数字 int 功能
将字符串转换为数字(默认十进制)
a = "123" b = int(a) print (b)
#输出123
将十六进制数转换为十进制数
a = "bcd" v = int(a,base=16) print(v)
#输出3021
.bit_length() 当前数字的二进制至少用n位表示的个数
a = 195 v = a.bit_length() print(v) #输出8
字符串 str 功能
.capitalize() 首字母大写
test="alex" v = test.capitalize() print(v) #输出Alex
.lower() 将定义的内容中所有的大写变为小写
test="alEx" v = test.lower() print(v) #输出alex
.islower() 判断字符串中每个元素是否都为小写
test = "alEx" v =test.islower() print(v) #输出 false
.upper() 将字符串中每个元素都变成大写
test = "alEx" v =test.upper() print(v) #输出ALEX
.isupper() 判断字符串中每个元素是否都为大写
test = "alEx" v =test.isupper() print(v) #输出False
.casefold() 将所有大写变为小写 ,很多未知的 也做相应变小写
test="alEx" v = test.casefold() print(v) #输出alex
.cent(10,“*”) 设置宽度,并将内容居中,10代表总长度,*代表未知填充(一个字符)
test="alex" v = test.center(10,"*") print(v) #输出***alex***
.ljust(10,"*") 设置宽度,将内容放在最左边
test = "alex" v =test.ljust(10,"*") print(v) #输出alex******
.rjust(10,"*") 设置宽度,将内容放在最右边
test = "alex" v =test.rjust(10,"*") print(v) #输出******alex
.zfill(10) 把内容放最右边前面填0
test = "alex" v =test.zfill(10) print(v) #输出000000alex
.count('al',3,5)去字符串中寻找,寻找子序列的出现次数,3 第三位开始寻找,5 第五位停止寻找
test="alexalex" v = test.count('al',3,6) print(v) #输出1
.endwith(‘ex’) 判断 以...结尾 是否正确
test="alexalex" v = test.endswith("ex") print(v) #输出true
.startwith("al") 判断 以...开始 是否正确
test="alexalex" v = test.startswith("al") print(v) #输出true
** .find("ex",5,8) 从开始往后找,找到第一个之后,获取其位置
test="alexalex" v = test.find("ex",5,8) print(v) #输出6
.format(LPP = ‘KLY’,a = 19) 格式化,将一个字符串中的占位符替换为指定的值
test="i am {LPP},age {a}" v = test.format(LPP = "KLY",a = 19) print(v) #输出 i am KLY,age 19
test="i am {0},age {1}" v = test.format("KLY",19) print(v) #输出i am KLY,age 19
.expandtables(20) 断句,从前往后20个为一组,有\t的那组不足20个 空出直到这组满20个,\n空行 制表
test="12345\t789" v=test.expandtabs(6) print(v) #输出12345 789
test="username\temail\tpasswd\nkly\tkly@qq.com\t123\nlpp\tlpp@qq.com\t123\nzjl\tzjl@qq.com\t123" v = test.expandtabs(20) print(v) #输出 username email passwd kly kly@qq.com 123 lpp lpp@qq.com 123 zjl zjl@qq.com 123
.isapha() 判断字符串是否为汉字,字母
test = "sdha怕怕hsadhasd"
v =test.isalpha()
print(v)
#输出true
test = "sdhahs12315dhasd"
v =test.isalpha()
print(v)
#输出false
.isdecimal() .isdigit() .isnumeric() 判断字符串是否为数字
数字123
test = "123" v =test.isdecimal() print(v) #输出true
test = "123" v =test.isdigit() print(v) #输出true
test = "123" v =test.isnumeric() print(v) #输出true
汉字 二
test = "二" v =test.isnumeric() print(v) #输出true
test = "二" v =test.isdigit() print(v) #输出false
test = "二" v =test.isnumeric() print(v) #输出true
带圆圈 ②
test = "②" v =test.isdecimal() print(v) #输出false
test = "②" v =test.isdigit() print(v) #输出true
test = "②" v =test.isnumeric() print(v) #输出true
.swapcase() 大小写转换
test = "AlEx"
v =test.swapcase()
print(v)
#输出aLeX
.isidentifier() 字母,数字,下划线 :标识符 def class
test = "_123" v = test.isidentifier() print(v) #输出true
.isprrintable() 判断是否存在不可显示的字符 \t 制表符 \n 换行
test = "dasddsda" v = test.isprintable() print(v) #输出true
test = "dasdd\nsda" v = test.isprintable() print(v) #输出false
test = "dasdd\tsda" v = test.isprintable() print(v) #输出false
.isspace()判断是否全部是空格
test = " " v = test.isspace() print(v) #输出true
.istitle() 判断是否是标题(每个单词的首字母是否大写)
test = "i am A good boy" v = test.istitle() print(v) #输出false
.title() 将每个单词的首字母变为大写
test = "i am A good boy" v = test.title() print(v) #输出 I Am A Good Boy
** .join() 将字符串中的每一个元素按照指定分隔符进行拼接
test = "我是李怕怕" t = " " v = t.join(test) print(v) #输出 我 是 李 怕 怕
test = "我是李怕怕" v = " ".join(test) print(v) #输出 我 是 李 怕 怕
** .strip() 去除左右两边的空格 也可去除 \t \n
test = " alex " v =test.strip() print(v) #输出alex
.lstrip 去除左边的空格
test = " alex " v =test.lstrip() print(v) #输出alex
.rstrip 去除右边的空格
test = " alex " v =test.rstrip() print(v) #输出 alex
.partition(“s”) 分割 前面第一个 s
test = "dasdasdsadas" v =test.partition("s") print(v) #输出('da', 's', 'dasdsadas')
.rpartition(“s”) 分割 最后面一个 s
test = "dasdasdsadas" v =test.rpartition("s") print(v) #输出('dasdasdsada', 's', '')
.split("s",2) 从前往后 分割并且去除两个s
test = "dasdasdsadas" v =test.split("s",2) print(v) #输出['da', 'da', 'dsadas']
.splitlines() 只能根据 True 和 False 是否保留换行
test = "dasda\nsdsa\ndas" v =test.splitlines(True) print(v) #输出['dasda\n', 'sdsa\n', 'das']
test = "dasda\nsdsa\ndas" v =test.splitlines(False) print(v) #输出['dasda', 'sdsa', 'das']
索引,下标 获得字符中的某一个字符
test = "dasdasdsadas" v =test[3] print(v) #输出 d
test = "dasdasdsadas" v =test[0:3] print(v) #输出das
test = "dasdasdsadas" v =test[0:-1] print(v) #输出dasdasdsada
len() 获取当前字符串由几个字符组成
test = "dasdasdsadas" v =len(test) print(v) #输出12
test = "李怕怕" v =len(test) print(v) #输出3 (python3中是3,python2中是9)
test = [11,22,33,44,55,66,"sdsad"] v =len(test) print(v) #输出7 (用逗号分隔符分开的算一个)
for 分割 for 变量词 in 字符串
test = "我是李怕怕是怕谁" for lpp in test: print(lpp) #输出
我 是 李 怕 怕 是 怕 谁
test = "我是李怕怕" n = 0 while n < len(test): v = test[n] print(v) n += 1 #输出 #我 #是 #李 #怕 #怕
.replace(被替换的,替换的,替换前几个)
test = "klyshishuaigeklyshishuaigeklyshishuaigeklyshishuaige" v = test.replace("shi","haowuyiwenshi",2) print(v) #输出klyhaowuyiwenshishuaigeklyhaowuyiwenshishuaigeklyshishuaigeklyshishuaige
range
v = range(100) print(v) #输出range(0, 100) (python3中是range(0, 100),python2中是0~99)
帮助创建连续的数字
v = range(10) for l in v: print(l) #输出 0 1 2 3 4 5 6 7 8 9
创建有步长的数字
v = range(0,10,2) for l in v: print(l) #输出 #0 #2 #4 #6 #8
将文字对应的索引打印出来
test = input("<<<") l = len(test) r = range(0,l,) for v in r: print(test[v],v) #输入 我是李怕怕
#输出
#我 0
#是 1
#李 2
#怕 3
#怕 4
test = input("<<<") for v in range(0,len(test)): print(test[v],v) #输入 我是李怕怕 #输出 #我 0 #是 1 #李 2 #怕 3 #怕 4

浙公网安备 33010602011771号