python 基础第二篇 2018-07-29
1、运算符
1. 结果是值
算数运算
a = 10 * 10
赋值运算
a = a + 1 a+=1
2. 结果是布尔值
空字符的布尔值是假,非空字符串的布尔值为真
数字0为假False ,非0为真Ture
test="" t1=bool(test) print(t1) 结果:False age=0 v1=bool(age)
print(v1) 结果:False
3. 比较运算
a = 1 > 5
4.逻辑运算
a = 1>6 or 1==1
5.成员运算
a = "蚊" in "郑建文"
2、基本数据类型
数字 int
——所有的功能,都放在int里
a1 = 123
a1 = 456
- int
将字符串转换为数字
a = "123" print(type(a),a) 结果:
<class 'str'> 123
b = int(a) print(type(b),b)
结果:
<class 'str'> 123
num = "0011" v = int(num, base=16)
print(v)
结果:17
- bit_length
# 当前数字的二进制,至少用n位表示
age=8 r = age.bit_length() print(r) 结果:4
字符串 str
1 首字母大写
test = "aLex" v = test.capitalize() print(v)
结果:Alex
2 所有变小写,casefold更牛逼,很多未知的对相应变小写
test = "aLex" v1 = test.casefold() print(v1) v2 = test.lower() print(v2) 结果:alex alex
3 设置宽度,并将内容居中
test = "alex" v = test.center(20,"中") print(v) 结果:中中中中中中中中aLex中中中中中中中中 test = "alex" v = test.ljust(20,"*") print(v) 结果:alex**************** test = "alex" v = test.rjust(20,"*") print(v) 结果:****************alex test = "alex" v = test.zfill(20) print(v) 结果:0000000000000000alex
4 去字符串中寻找,寻找子序列的出现次数
test = "aLexalexr" v = test.count('ex') print(v) 结果:2 test = "aLexalexr" v = test.count('ex',6,69) print(v) 结果:1 #从第7个字符l开始到最后,虽然69已经超界,但是不会报错 test = "aLexalexr" v = test.count('ex',6,-1) print(v) 结果:1 #这里的—1是指代倒数第一个字符r test = "aLexalexr" v = test.count('ex',6,-2) print(v) 结果:0 #这里-2是指倒数第二个字符
5. 以什么什么结尾, 以什么什么开始
test = "alex" v1 = test.endswith('ex') v = test.startswith('ex') print(v1) print(v)
结果:
True
False
6 expandtabs,断句20,可用作制表格
test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123" v = test.expandtabs(20) print(v) username email password laiying ying@q.com 123 laiying ying@q.com 123 laiying ying@q.com 123
7 从开始往后找,找到第一个之后,获取其位置
test = "alexalexfgjex" v = test.find('ex')
print(v)
v1 = test.find('xg') # 未找到返回 -1
print(v1)
结果:2
-1
8 index找不到,报错 忽略
test = "alexalex" #v = test.index('8') v1 =test.index("xa") #print(v) print(v1) 结果:3 print(v)的结果是 Traceback (most recent call last): File "C:/Users/Administrator.WIN7U-20180705J/PycharmProjects/untitled/py20.py", line 3, in <module> v = test.index('8') ValueError: substring not found
9 格式化,将一个字符串中的占位符替换为指定的值
test = 'i am {name}, age {a}' print(test) v = test.format(name='alex',a=19) print(v) test = 'i am {0}, age {1}' print(test) v = test.format('alex',19) print(v)
结果:
i am {name}, age {a}
i am alex, age 19
i am {0}, age {1}
i am alex, age 19
10 格式化,传入的值 {"name": 'alex', "a": 19}
test = 'i am {name}, age {a}' v1 = test.format(name='df',a=10) v2 = test.format_map({"name": 'alex', "a": 19}) print(v1) print(v2) 结果: i am df, age 10 i am alex, age 19
# 11 字符串中是否只包含 字母和数字
test1 = "123" test2 ="wan晚21" test3 ="asd@we43" v1 = test1.isalnum() v2 = test2.isalnum() v3 = test3.isalnum() print(v1) print(v2) print(v3) 结果:
True
True
False
12 是否是字母,汉子
test = "as往df" test1="sa2f" v = test.isalpha() v2=test1.isalpha() print(v) print(v2) 结果: True False
13 .当前输入是否是数字
其中.isnumeric()这个最好,支持最多。 .isnumeric()》.isdigit()》.isdecimal()
test = "二" # 1,② v1 = test.isdecimal() v2 = test.isdigit() v3 = test.isnumeric() print(v1,v2,v3) 结果:False False True
# 14 是否存在不可显示的字符
# \t 制表符
# \n 换行
# test = "oiuas\tdfkj"
# v = test.isprintable()
# print(v)
# 15 判断是否全部是空格
# test = ""
# v = test.isspace()
# print(v)
# 16 判断是否是标题
# test = "Return True if all cased characters in S are uppercase and there is"
# v1 = test.istitle()
# print(v1)
# v2 = test.title()
# print(v2)
# v3 = v2.istitle()
# print(v3)
# 17 ***** 将字符串中的每一个元素按照指定分隔符进行拼接
# test = "你是风儿我是沙"
# print(test)
# # t = ' '
# v = "_".join(test)
# print(v)
# 18 判断是否全部是大小写 和 转换为大小写
# test = "Alex"
# v1 = test.islower()
# v2 = test.lower()
# print(v1, v2)
# v1 = test.isupper()
# v2 = test.upper()
# print(v1,v2)
# 19
# 移除指定字符串
# 有限最多匹配
test = "snasdfsafh"
v1 = test.lstrip('skjnlayd')结果:fsafh 从左往右在” skjnlayd”依次匹配s n a s d f s a f h这些字母直到找不到为止,首字母要必须匹配。
v2 = test.rstrip('hsfaak')结果:snasd 从右往左在” skjnlayd”依次匹配s n a s d f s a f h这些字母直到找不到为止,未字母要必须匹配。
v3= test.strip('kfaak')
print(v1)
print(v2)
print(v3)
test = "odnasdfsafhm"
v1 = test.lstrip('kojnlayd') 结果: sdfsafhm
v2 = test.rstrip('odshfaako') 结果: odnasdfsafhm
v3= test.strip('kfaah') 结果: odnasdfsafhm
print(v1)
print(v2)
print(v3)
# test.lstrip()
# test.rstrip()
# test.strip()
# 去除左右空白
# v = test.lstrip()
# v = test.rstrip()
# v = test.strip()
# print(v)
# print(test)
# 去除\t \n
# v = test.lstrip()
# v = test.rstrip()
# v = test.strip()
# print(v)
# 20 对应关系替换
# test = "aeiou"
# test1 = "12345"
# v = "asidufkasd;fiuadkf;adfkjalsdjf"
# m = str.maketrans("aeiou", "12345")
# new_v = v.translate(m)
# print(new_v)
# 21 分割为三部分
# test = "testasdsddfg"
# v = test.partition('s')
# print(v)
# v = test.rpartition('s')
# print(v)
# 22 分割为指定个数
# v = test.split('s',2)
# print(v)
# test.rsplit()
# 23 分割,只能根据,true,false:是否保留换行
# test = "asdfadfasdf\nasdfasdf\nadfasdf"
# v = test.splitlines(False)
# print(v)
# 24 以xxx开头,以xx结尾
# test = "backend 1.1.1.1"
# v = test.startswith('a')
# print(v)
# test.endswith('a)
# 25 大小写转换
# test = "aLex"
# v = test.swapcase()
# print(v)
# 26 字母,数字,下划线 : 标识符 def class
# a = "def"
# v = a.isidentifier()
# print(v)
# 27 将指定字符串替换为指定字符串
# test = "alexalexalex"
# v = test.replace("ex",'bbb')
# print(v)
# v = test.replace("ex",'bbb',2)
# print(v)
###################### 7个基本魔法 ######################
# join # '_'.join("asdfasdf")
# split
# find
# strip
# upper
# lower
# replace
###################### 4个灰魔法 ######################
# test = "郑建文妹子有种冲我来"
# 一、for循环
# for 变量名 in 字符串:
# 变量名
# break
# continue
# index = 0
# while index < len(test):
# v = test[index]
# print(v)
#
# index += 1
# print('=======')
# for zjw in test:
# print(zjw)
# test = "郑建文妹子有种冲我来"
# for item in test:
# print(item)
# break
# for item in test:
# continue
# print(item)
# 二、索引,下标,获取字符串中的某一个字符
# v = test[3]
# print(v)
# 三、切片
# v = test[0:-1] # 0=< <1
# print(v)
# 四、获取长度
# Python3: len获取当前字符串中由几个字符组成
# v = len(test)
# print(v)
# 注意:
# len("asdf")
# for循环
# 索引
# 切片
# 五、获取连续或不连续的数字,
# Python2中直接创建在内容中
# python3中只有for循环时,才一个一个创建
# r2 = range(1,10)
# r3 = range(1,10,2)
# 帮助创建连续的数字,通过设置步长来指定不连续
# v = range(0, 100, 5)
#
# for item in v:
# print(item)
##### 练习题:根据用户输入的值,输出每一个字符以及当前字符所在的索引位置 #####
# test = input(">>>")
# for item in test:
# print(item)
# 将文字 对应的索引打印出来:
# test = input(">>>")
# print(test) # test = qwe test[0] test[1]
# l = len(test) # l = 3
# print(l)
#
# r = range(0,l) # 0,3
# for item in r:
# print(item, test[item]) # 0 q,1 w,2 e
# test = input(">>>")
# for item in range(0, len(test)):
# print(item, test[item])
###################### 1个深灰魔法 ######################
# 字符串一旦创建,不可修改
# 一旦修改或者拼接,都会造成重新生成字符串
# name = "zhengjianwen"
# age = "18"
#
# info = name + age
# print(info)
列表 list
...
元祖 tuple
...
字典 dict
...
布尔值 bool
...
----> 列表,元祖,字典,布尔值 详细操作见课上代码
Print ()函数原型
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
以空格为分割,打印后换行
1、运算符
结果是值
算数运算
a = 10 * 10
赋值运算
a = a + 1 a+=1
结果是布尔值
空字符的布尔值是假,非空字符串的布尔值为真
数字0为假False ,非0为真Ture
test=""
t1=bool(test)
print(t1)
结果:False
age=0
v1=bool(age)
print(v1)
结果:False
比较运算
a = 1 > 5
逻辑运算
a = 1>6 or 1==1
成员运算
a = "蚊" in "郑建文"
2、基本数据类型
数字 int ,
——所有的功能,都放在int里
a1 = 123
a1 = 456
- int
将字符串转换为数字
a = "123"
print(type(a),a)
b = int(a)
print(type(b),b)
num = "0011"
v = int(num, base=16)
print(v)
- bit_length
# 当前数字的二进制,至少用n位表示
age=8
r = age.bit_length()
print(r)
结果:4
字符串 str
###########################################
# 1 首字母大写
# test = "aLex"
# v = test.capitalize()
# print(v)
# 2 所有变小写,casefold更牛逼,很多未知的对相应变小写
# v1 = test.casefold()
# print(v1)
# v2 = test.lower()
# print(v2)
# 3 设置宽度,并将内容居中
# 20 代指总长度
# * 空白未知填充,一个字符,可有可无
# v = test.center(20,"中")
# print(v)
# test = "alex"
# v = test.ljust(20,"*")
# print(v)
# test = "alex"
# v = test.rjust(20,"*")
# print(v)
# test = "alex"
# v = test.zfill(20)
# print(v)
# 4 去字符串中寻找,寻找子序列的出现次数
# test = "aLexalexr"
# v = test.count('ex')
# print(v)
# test = "aLexalexr"
# v = test.count('ex',5,6)
# print(v)
# 5
# 以什么什么结尾
# 以什么什么开始
# test = "alex"
# v = test.endswith('ex')
# v = test.startswith('ex')
# print(v)
# 6 expandtabs,断句20,可用作制表格
# test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
# v = test.expandtabs(20)
# print(v)
username email password
laiying ying@q.com 123
laiying ying@q.com 123
laiying ying@q.com 123
# 7 从开始往后找,找到第一个之后,获取其未知
# > 或 >=
# test = "alexalex"
# v = test.find('ex') # 未找到 -1,找到返回0
# print(v)
# 8 index找不到,报错 忽略
# test = "alexalex"
# v = test.index('8')
# print(v)
# 9 格式化,将一个字符串中的占位符替换为指定的值
# test = 'i am {name}, age {a}'
# print(test)
# v = test.format(name='alex',a=19)
# print(v)
结果i am {name}, age {a}
i am alex, age 19
# test = 'i am {0}, age {1}'
# print(test)
# v = test.format('alex',19)
# print(v)
# 10 格式化,传入的值 {"name": 'alex', "a": 19}
# test = 'i am {name}, age {a}'
# v1 = test.format(name='df',a=10)
# v2 = test.format_map({"name": 'alex', "a": 19})
# 11 字符串中是否只包含 字母和数字
# test = "123"
# v = test.isalnum()
# print(v)
# str
# 12 是否是字母,汉子
# test = "as2df"
# v = test.isalpha()
# print(v)
# 13 当前输入是否是数字
# test = "二" # 1,②
# v1 = test.isdecimal()
# v2 = test.isdigit()
# v3 = test.isnumeric()
# print(v1,v2,v3)
# 14 是否存在不可显示的字符
# \t 制表符
# \n 换行
# test = "oiuas\tdfkj"
# v = test.isprintable()
# print(v)
# 15 判断是否全部是空格
# test = ""
# v = test.isspace()
# print(v)
# 16 判断是否是标题
# test = "Return True if all cased characters in S are uppercase and there is"
# v1 = test.istitle()
# print(v1)
# v2 = test.title()
# print(v2)
# v3 = v2.istitle()
# print(v3)
# 17 ***** 将字符串中的每一个元素按照指定分隔符进行拼接
# test = "你是风儿我是沙"
# print(test)
# # t = ' '
# v = "_".join(test)
# print(v)
# 18 判断是否全部是大小写 和 转换为大小写
# test = "Alex"
# v1 = test.islower()
# v2 = test.lower()
# print(v1, v2)
# v1 = test.isupper()
# v2 = test.upper()
# print(v1,v2)
# 19
# 移除指定字符串
# 有限最多匹配
test = "snasdfsafh"
v1 = test.lstrip('skjnlayd')结果:fsafh 从左往右在” skjnlayd”依次匹配s n a s d f s a f h这些字母直到找不到为止,首字母要必须匹配。
v2 = test.rstrip('hsfaak')结果:snasd 从右往左在” skjnlayd”依次匹配s n a s d f s a f h这些字母直到找不到为止,未字母要必须匹配。
v3= test.strip('kfaak')
print(v1)
print(v2)
print(v3)
test = "odnasdfsafhm"
v1 = test.lstrip('kojnlayd') 结果: sdfsafhm
v2 = test.rstrip('odshfaako') 结果: odnasdfsafhm
v3= test.strip('kfaah') 结果: odnasdfsafhm
print(v1)
print(v2)
print(v3)
# test.lstrip()
# test.rstrip()
# test.strip()
# 去除左右空白
# v = test.lstrip()
# v = test.rstrip()
# v = test.strip()
# print(v)
# print(test)
# 去除\t \n
# v = test.lstrip()
# v = test.rstrip()
# v = test.strip()
# print(v)
# 20 对应关系替换
# test = "aeiou"
# test1 = "12345"
# v = "asidufkasd;fiuadkf;adfkjalsdjf"
# m = str.maketrans("aeiou", "12345")
# new_v = v.translate(m)
# print(new_v)
# 21 分割为三部分
# test = "testasdsddfg"
# v = test.partition('s')
# print(v)
# v = test.rpartition('s')
# print(v)
# 22 分割为指定个数
# v = test.split('s',2)
# print(v)
# test.rsplit()
# 23 分割,只能根据,true,false:是否保留换行
# test = "asdfadfasdf\nasdfasdf\nadfasdf"
# v = test.splitlines(False)
# print(v)
# 24 以xxx开头,以xx结尾
# test = "backend 1.1.1.1"
# v = test.startswith('a')
# print(v)
# test.endswith('a)
# 25 大小写转换
# test = "aLex"
# v = test.swapcase()
# print(v)
# 26 字母,数字,下划线 : 标识符 def class
# a = "def"
# v = a.isidentifier()
# print(v)
# 27 将指定字符串替换为指定字符串
# test = "alexalexalex"
# v = test.replace("ex",'bbb')
# print(v)
# v = test.replace("ex",'bbb',2)
# print(v)
###################### 7个基本魔法 ######################
# join # '_'.join("asdfasdf")
# split
# find
# strip
# upper
# lower
# replace
###################### 4个灰魔法 ######################
# test = "郑建文妹子有种冲我来"
# 一、for循环
# for 变量名 in 字符串:
# 变量名
# break
# continue
# index = 0
# while index < len(test):
# v = test[index]
# print(v)
#
# index += 1
# print('=======')
# for zjw in test:
# print(zjw)
# test = "郑建文妹子有种冲我来"
# for item in test:
# print(item)
# break
# for item in test:
# continue
# print(item)
# 二、索引,下标,获取字符串中的某一个字符
# v = test[3]
# print(v)
# 三、切片
# v = test[0:-1] # 0=< <1
# print(v)
# 四、获取长度
# Python3: len获取当前字符串中由几个字符组成
# v = len(test)
# print(v)
# 注意:
# len("asdf")
# for循环
# 索引
# 切片
# 五、获取连续或不连续的数字,
# Python2中直接创建在内容中
# python3中只有for循环时,才一个一个创建
# r2 = range(1,10)
# r3 = range(1,10,2)
# 帮助创建连续的数字,通过设置步长来指定不连续
# v = range(0, 100, 5)
#
# for item in v:
# print(item)
##### 练习题:根据用户输入的值,输出每一个字符以及当前字符所在的索引位置 #####
# test = input(">>>")
# for item in test:
# print(item)
# 将文字 对应的索引打印出来:
# test = input(">>>")
# print(test) # test = qwe test[0] test[1]
# l = len(test) # l = 3
# print(l)
#
# r = range(0,l) # 0,3
# for item in r:
# print(item, test[item]) # 0 q,1 w,2 e
# test = input(">>>")
# for item in range(0, len(test)):
# print(item, test[item])
###################### 1个深灰魔法 ######################
# 字符串一旦创建,不可修改
# 一旦修改或者拼接,都会造成重新生成字符串
# name = "zhengjianwen"
# age = "18"
#
# info = name + age
# print(info)
列表 list
...
元祖 tuple
...
字典 dict
...
布尔值 bool
...
----> 列表,元祖,字典,布尔值 详细操作见课上代码
Print ()函数原型
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
以空格为分割,打印后换行
浙公网安备 33010602011771号