1.开发工具
python开发IDE: PyCharm、eclipse
# 专业版
# 不要汉化
2.运算符
结果是值
算数运算
a = 10 * 10
赋值运算
a = a + 1 a+=1
结果是布尔值
比较运算
a = 1 > 5
逻辑运算
a = 1>6 or 1==1
成员运算
a = "蚊" in "郑建文"
3.基本数据类型
a.数字 int ,所有的功能,都放在int里
a1: int(object) 将字符串转换为数字
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #int
4 a = "123"
5 print(type(a), a)
6
7 b = int(a)
8 print(type(b), b)
9
10 num = "0011"
11 v = int(num, base=16)
12 print(v)
a2:bit_lenght(self) 当前数字的二进制,至少用n位表示
1 #!/usr/bin/env Python
2 # -*- coding:utf8 -*-
3 #int
4 age = 5
5 r = age.bit_length()
6 print(r)
b.字符串 str
b1 capitalize() 首字母大写
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 # 首字母大写 capitalize()
5 test = "aLex"
6 v = test.capitalize()
7 print(v)
b2 casefold() 所有变小写 包括特殊字符 lower() 所有英文字母变小写
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 # 所有变小写 casefold() lower()
5 test = "ShiQIanYu"
6 v1 = test.casefold()
7 print(v1)
8 v2 = test.lower()
9 print(v2)
b3 center(width, fillchar=None) width 代表总长度 fillchar 空白未知填充,一个字符,可有可无 两边填充
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 test = 'sqy'
5 v = test.center(20,"*")
6 print(v)
b31 ljust(width, fillchar=None) width 代表总长度 fillchar 空白未知填充,一个字符,可有可无 右边填充
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 test = "alex"
5 v = test.ljust(20,"*")
6 print(v)
b32 rjust(width, fillchar=None) width 代表总长度 fillchar 空白未知填充,一个字符,可有可无 左边填充
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 test = "alex"
5 v = test.rjust(20,"*")
6 print(v)
b33 zfill(width) idth 代表总长度 默认左边填充0
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 test = "alex"
5 v = test.zfill(20)
6 print(v)
b4 count(sub, start=None, end=None) 去字符串中寻找,寻找子序列的出现次数 左闭右开
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 test = "aLexalexr"
5 v = test.count('ex')
6 print(v)
7
8 test = "aLexalexr"
9 v = test.count('ex',6,8)
10 print(v)
b5 以什么什么结尾 endswith(suffix, start=None, end=None)
以什么什么开始startswith(suffix, start=None, end=None)
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 # 以什么什么结尾
5 # 以什么什么开始
6 test = "alex"
7 v = test.endswith('ex')
8 print(v)
9 v = test.startswith('alex')
10 print(v)
b6 find(sub, start=None, end=None) 从开始往后找,找到第一个之后,获取其位置 找不到返回-1
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 # 从开始往后找,找到第一个之后,获取其未知
5 # > 或 >=
6 test = "alexalex"
7 # 未找到 -1
8 v = test.find('ex')
9 print(v)
b7 index(sub, start=None, end=None) 找不到,报错 忽略
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 test = "alexalex"
5 v = test.index('8')
6 print(v)
b8 fORMat(*args, **kwargs) 格式化,将一个字符串中的占位符替换为指定的值
1 # -*- coding:utf8 -*-
2 #str
3 test = 'i am {name}, age {a}'
4 print(test)
5 v = test.format(name='alex',a=19)
6 print(v)
7
8 test = 'i am {0}, age {1}'
9 print(test)
10 v = test.format('alex',19)
11 print(v)
b9 format_map(mapping) 格式化,传入的值 {"name": 'alex', "a": 19} 字典的形式
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 # 格式化,传入的值 {"name": 'alex', "a": 19}
5 test = 'i am {name}, age {a}'
6 v1 = test.format(name='df',a=10)
7 print(v1)
8 v2 = test.format_map({"name": 'alex', "a": 19})
9 print(v2)
b10 isalnum() 字符串中是否只包含 字母和数字 汉字
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 # 字符串中是否只包含 字母和数字 汉字
5 test = "as123对的?"
6 v = test.isalnum()
7 print(v)
b10a isalpha() 是否是字母,汉字
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 test = "as对的df"
5 v = test.isalpha()
6 print(v)
b10b isdecimal()<isdigit()<isnumeric() 当前输入是否是数字 判断的数字种类依次为
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 test = "②" # 1,② ,二
5 v1 = test.isdecimal()
6 v2 = test.isdigit()
7 v3 = test.isnumeric()
8 print(v1,v2,v3)
b11 expandtabs(width) 断句 找到制表符/t 制表符两边的内容间隔的距离为width
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 test = "username\temail\tpassWord\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
5 v = test.expandtabs(20)
6 print(v)
b12 isprintable() 是否存在不可显示的字符 \t 制表符 \n换行符
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 test = "oiuas\tdfkj"
5 v = test.isprintable()
6 print(v)
b13 isspace() 判断是否全部是空格
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 #str
4 test = " 1"
5 v = test.isspace()
6 print(v)
b14 istitle() 判断是否是标题 title() 转换为标题
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 test = "Return True if all cased characters in S are uppercase and there is"
4 v1 = test.istitle()
5 print(v1)
6 v2 = test.title()
7 print(v2)
8 v3 = v2.istitle()
9 print(v3)
b15 join(iterable) 字符串中的每一个元素按照指定分隔符进行拼接
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 test = "你是风儿我是沙"
4 print(test)
5 # t = ' '
6 v = "_".join(test)
7 print(v)
b16 islower() isupper() 判断是否全部是小大写 lower() upper() 转换为小大写
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 test = "Alex"
4 v1 = test.islower()
5 v2 = test.lower()
6 print(v1, v2)
7
8 v1 = test.isupper()
9 v2 = test.upper()
10 print(v1,v2)
b17 strip(str) 去除两边str lstrip(str) 去除左边str rstrip(str) 去除右边str
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 # 移除指定字符串
4 # 有限最多匹配
5 test = " \nxa "
6 #v = test.lstrip('xa')
7 #v = test.rstrip('9lexxexa')
8 #v = test.strip('xa')
9 #print(v)
10
11 #test.lstrip()
12 #test.rstrip()
13 #test.strip()
14 #print(test)
15 # 去除左右空白
16 # v = test.lstrip()
17 # v = test.rstrip()
18 # v = test.strip()
19 # print(v)
20 # print(test)
21 # 去除\t \n
22 #v = test.lstrip()
23 #v = test.rstrip()
24 v = test.strip()
25 print(v)
b18 maketrans(src,dest) src源内容 dest 目标内容 对应关系替换
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 test = "aeiou"
4 test1 = "12345"
5
6 v = "asidufkasd;fiuadkf;adfkjalsdjf"
7 m = str.maketrans("aeiou", "12345")
8 new_v = v.translate(m)
9 print(new_v)
b19 分割为三部分 分割内容包含str partition(str) 从左边开始分割 rpartition(str) 从右边开始分割 分割为指定个数不包含str split(str,num) str 分割内容 num 分割 splitlines(boo) 只能根据,true,false:是否保留换行
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 test = "testasdsddfg"
4 # v = test.partition('s')
5 # print(v)
6 # v = test.rpartition('s')
7 # print(v)
8
9 v = test.split('s',1)
10 print(v)
11 test.rsplit()
12
13 test = "asdfadfasdf\nasdfasdf\nadfasdf"
14 v = test.splitlines(True)
15 print(v)
b20 swapcase() 大小写转换 小写转大写
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 test = "aLex"
4 v = test.swapcase()
5 print(v)
b21 字母,数字,下划线 : 标识符 def class
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 a = "d2哈好的f"
4 v = a.isidentifier()
5 print(v)
b22 将指定字符串替换为指定字符串 replace(self, old, new, count=None)
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 test = "alexalexalex"
4 v = test.replace("ex",'bbb')
5 print(v)
6 v = test.replace("ex",'bbb',3)
7 print(v)
b23 ###################### 7个基本魔法 ######################
# join # '_'.join("asdfasdf") # split # find # strip # upper # lower # replace
b24 ###################### 4个灰魔法 ######################
B241 一、for循环
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 # test = "郑建文妹子有种冲我来"
4 # for 变量名 in 字符串:
5 # 变量名
6 # break
7 # continue
8
9 # index = 0
10 # while index < len(test):
11 # v = test[index]
12 # print(v)
13 #
14 # index += 1
15 # print('=======')
16
17 # for zjw in test:
18 # print(zjw)
19
20 test = "郑建文妹子有种冲我来"
21 # for item in test:
22 # print(item)
23 # break
24
25 for item in test:
26 continue
27 print(item)
B242 索引,下标,获取字符串中的某一个字符
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 test = "妹子有种冲我来"
4 v = test[3]
5 print(v)
B243 三、切片
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 test = "妹子有种冲我来"
4 v = test[0:1] # 0=< <1
5 print(v)
B244 四、获取长度
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 test = "妹子有种冲我来"
4 # python3: len获取当前字符串中由几个字符组成
5 v = len(test)
6 print(v)
# 注意: # len("asdf") # for循环 # 索引 # 切片
B245 五、获取连续或不连续的数字
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 # Python2中直接创建在内容中
4 # python3中只有for循环时,才一个一个创建
5 r1 = range(10)
6 r2 = range(1,10)
7 r3 = range(1,10,2)
8 # 帮助创建连续的数字,通过设置步长来指定不连续
9 v = range(0, 100, 5)
10
11 for item in r3:
12 print(item)
b25 ###################### 1个深灰魔法 ######################
# 字符串一旦创建,不可修改 # 一旦修改或者拼接,都会造成重新生成字符串
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 name = "shiqianyu"
4 age = "18"
5 info = name + age
6 print(info)
b26 练习题 根据用户输入的值,输出每一个字符以及当前字符所在的索引位置 #####
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 # 根据用户输入的值,输出每一个字符以及当前字符所在的索引位置
4 usr = input("请输入值:\n")
5 index = 0
6 while index < len(usr):
7 print(index,usr[index])
8 index += 1
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 # 根据用户输入的值,输出每一个字符以及当前字符所在的索引位置
4 # test = input(">>>:\n")
5 # print(test) # test = qwe test[0] test[1]
6 # l = len(test) # l = 3
7 # print(l)
8 #
9 # r = range(0,l) # 0,3
10 # for item in r:
11 # print(item, test[item]) # 0 q,1 w,2 e
12
13 test = input(">>>\n")
14 for item in range(0, len(test)):
15 print(item, test[item])
# 制作表格
# 循环提示用户输入:用户名、密码、邮箱 (要求用户输入的长度不超过 20 个字符,如果超过则只有前 20 个字符有效)
# 如果用户输入 q 或 Q 表示不再继续输入,将用户输入的内容以表格形式大隐
1 s = ""
2 i = 1
3 while i<3:
4 name = input("请输入用户名:\t\n")
5 pwd = input("请输入密码:\t\n")
6 email = input("请输入邮箱:\t\n")
7
8 template = "{0}\t{1}\t{2}\n"
9 v = template.format(name,pwd,email)
10
11 s = s + v
12 i += 1
13 print(s.expandtabs(20))
c 列表 list
d 元组 tuple
e 字典 dict
f 布尔值 bool