1 #逻辑运算
2 # and or not
3 #优先级, () > not > and > or 如果同一优先级 从左到右依次计算
4 # print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
5 #先看 and 三个and 依次是 T or T or F
6 #然后再看 or T 和 T 是 真 T or F 最后是真
7
8 # print(3 > 4 or 4 < 3 and 2 == 2)
9 # print(1 < 2 and 3 < 4 or 1 > 2)
10 # print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
11 # print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
12 #F or F and T or F
13 #F or F or F
14
15 """
16 x or y x为非零 则返回x 若x为0 则返回y
17
18 int 转换成 bool 非零转换成bool 为 True 0 转换成bool 是False
19 """
20 # print(1 or 2) 1
21 # print(3 or 2) 3
22 # print(0 or 2) 2
23 # print(100 or 3) 100
24 # print(0 or 23) 23
25 # print(bool(2)) # True
26 # print(bool(-2)) # True
27 # print(bool(0)) # False
28
29 #bool ---> int
30 # print(int(True)) # 1
31 # print(int(False)) # 0
32
33 """x or y x 为 True 则返回x"""
34 # print(2 or 100 or 3 or 4) # 2
35
36 """x and y x True , 则返回 y """
37 # print(1 and 2 )
38 # print(0 or 4 and 3 or 2) # 3
39 # print(1 > 2 and 3 or 4 and 3 < 2) # False
40 # print(2 or 1 < 3 and 2) # 2
41
42 """ x or y x 为 True 则返回 x """
43 """ x and y x 为 True 则返回 y """
44 # print(6 or 2 > 1) # 6 先看 > <
45 # print(3 or 2 > 1) # 3
46 # print(0 or 5 < 4) # False
47 # print(5 < 4 or 3) # 3
48 # print(2 > 1 or 6) # True
49 # print(3 and 2 > 1) # True
50 # print(0 and 3 > 1) # 0
51 # print(2 > 1 and 3) # 3
52
53 1 # 小练习 用户登陆 有三次输入机会
54 2 username = "蜘蛛"
55 3 password = "123"
56 4 i = 3
57 5 while i > 0:
58 6 user = input("请输入您的账号:")
59 7 i -= 1
60 8 if user == username:
61 9 passwd = input("请输入您的密码:")
62 10 if passwd == password:
63 11 print("验证成功,正在登陆...")
64 12 print("""恭喜你登陆成功!
65 13 用户名:%s
66 14 密码:%s
67 15 """%(user,passwd))
68 16 break
69 17 else:
70 18 if i == 0:
71 19 print("您的机会已经没有了!gameover 下次见。")
72 20 answer = input("是否想再试试?Yes")
73 21 if answer == "Yes":
74 22 i = 3
75 23 print("密码错误,请重新输入:")
76 24 print("你还有"+str(i)+"次机会")
77 25 else:
78 26 print("请输入正确的用户名!")
79 27 if i == 0:
80 28 print("您的机会已经没有了!")
81 29 answer = input("是否还想再试试?请输入:Yes")
82 30 if answer == "Yes":
83 31 i = 3
84 32 print("您还有"+str(i)+"次机会")
85 33 else:
86 34 print("您该歇歇了...")
87
88 """
89 数据类型:
90 int
91 方法:
92 bit_length
93 """
94 # i = 4
95 # print(i.bit_length()) # 3
96
97 """
98 bit_length
99 1 0000 0001 1
100 2 0000 0010 2
101 3 0000 0011 2
102 4 0000 0100 3
103 """
104
105
106 """
107 bool True False
108
109 int ---> str
110 i = 1
111 s = str(i)
112 print(s)
113
114 str ---> int (只能是数字 不能是字母)
115 s = "234"
116 a1 = int(s)
117 print(a1)
118
119 int ---> bool
120 i = 3
121 b = bool(i)
122 print(b) # True
123
124 --->(转化)
125 bool ---> int
126 True 1
127 False 0
128
129
130 str ---> bool
131 s = ""(空字符串)---->(False)
132 非空字符串都是True
133
134 s = " "
135 d = bool(s)
136 print(d) # True
137 a = ""
138 c = bool(a)
139 print(d) # False
140 """
141
142 #字符串的索引与切片
143 s = "QAZWSXEDCrfvedc"
144 # s1 = s[1]
145 # print(s1) # A
146 # s2 = s[0]
147 # print(s2) # Q
148 # s3 = s[8]
149 # print(s3)
150 # s4 = s[0:]
151 # print(s4)
152 # s5 = s[0:4] # 顾头不顾尾
153 # print(s5)
154 # s6 = s[0:5]
155 # print(s6)
156 # s7 = s[-1]
157 # print(s7)
158 # s8 = s[-2]
159 # print(s8)
160 # s9 = s[0:-1] # 也是顾头不顾尾
161 # print(s9)
162 #
163 # q = s[:]
164 # print(q)
165
166 # s = "QAZWSXEDCrfvedc"
167 # a1 = s[0:7:3]
168 # print(a1)
169
170 # a2 = s[5:0:-1]
171 # print(a2) # 倒着来 XSWZA
172 # a3 = s[5::-1]
173 # print(a3) # XSWZAQ
174 # a4 = s[-1::-1]
175 # print(a4) # 字符串所有元素 倒着排列
"""
字符串的操作
"""
1 # s = "qazwsx"
2 # s1 = s.capitalize() # 首字母大写
3 # print(s1)
4 #
5 # s2 = s.upper() # 都大写
6 # print(s2)
7 # s3 = s2.lower() # 都小写
8 # print(s3)
1 #输入验证码,不区分大小写
2
3 s_str = "asdQAZ"
4 you_input = input("请输入’asdQAZ‘,不区分大小写")
5 if s_str.upper() == you_input.upper():
6 print("输入成功")
7 else:
8 print("请重新输入")
1 # 大小写翻转
2 # s = "qazAZ"
3 # s3 = s.swapcase()
4 # print(s3)
5
6 #每个隔开(特殊字符或者数字)的单词首字母大写
7 # s = 'lizy waws'
8 # a1 = s.title()
9 # print(a1)
10
11 # w = "fade hello,edc/rew8tre_bvc"
12 # a2 = w.title()
13 # print(a2)
14
15
16
17 # center 居中 20 是总长度
18 # s = "hellopython" # ' hellopython '
19 # a1 = s.center(20, "#") # 总长度 20 用#号来填充
20 # print(a1)
21
22
23
24 # s = "lizyPYTHON"
25 # a1 = s.startswith("lizy") #以...开头
26 # print(a1) # True
27
28
29
30 #find 通过元素找索引,找不到返回-1
31
32 #index 通过元素找索引,找不到报错
33 # s = "lizyPYTHon"
34 # a1 = s.find("z") # 2
35 # a2 = s.index("S") # 报错
36 # print(a1)
37 # print(a2)
38
39 # strip左右一起删除 rstrip :从右开始删除 lstrip:从左开始删除
40
41 # s = " lizy "
42 # a1 = s.strip() # 去空格
43 # print(a1)
44
45 # s = " lizy "
46 # a2 = s.rstrip() # 去右边空格
47 # print(a2)
48
49 # s = "lizy ll l"
50 # a3 = s.count("l") # count 查看元素的个数
51 # print(a3)
52
53 # s = ":lizy:wufa:chenggong"
54 # a1 = s.split(":") # split 分隔符 ** str ---> list 字符串转换成列表
55 # print(a1) # 返回 ['', 'lizy', 'wufa', 'chenggong']
56
57
58 # format的三种玩儿法 格式化输出
59 # 第一种
60 # s = "我叫{}, 今年{}, 爱好{}, 再说一下我叫{}".format("蜘蛛", 23, "旅行", "蜘蛛")
61 # print(s)
62
63 #第二种 大括号里写索引 一定得按顺序写
64 # s = "我叫{0}, 今年{1}, 爱好{2}, 再说一下我叫{0}".format("蜘蛛", 23, "旅行")
65 # print(s)
66
67 #第三种 按照键值对的形式来写 顺序无所谓
68 # s = "我叫{name}, 今年{age}, 爱好{hobby}, 再说一下我叫{name}".format(name="蜘蛛", hobby="旅行", age=23)
69 # print(s)
70
71 #如果要替换一个的话 replace("原内容","替换后的内容",1) 加数字
72 #replace 替换 默认全部替换 replace("原内容","替换后的内容")
73 # s = "伍佰谓和句孙女士的举动看到你我孙女士"
74 # a1 = s.replace("孙女士", "王女士")
75 # print(a1)
注:本文是根据老男孩课程内容整理而成的,本文仅供个人笔记使用,如果有侵犯,请联系我,我立即撤销。