day03 notes
1. Integer and Boolean values
2. Detailed string (common methods
3.for cycle
1. Integer and Boolean values
python3: in
python2: int, long (long integer)
num = 15
print(num.bit_length())
结果为4
十进制 -- 二进制
# 15 1
# 7 1
# 3 1
# 1 1
# 0
# 34(十进制)
# 32 16 8 4 2 1
# 1 0 0 0 1 0
二进制 -- 十进制
# 1*2**0 + 0*2**1 + 1*2**2 + 0*2**3 + 1*2**4 + 1*2**5
# 1 + 0 + 4 + 0 + 16 + 32
# 45 十进制转二进制
# 32 16 8 4 2 1
# 1 0 1 1 0 1
bin -- 将十进制转换成二进制
print(bin(45))
int -- 将二进制转换成十进制
print(int("101101",2))
print(int(0b101101))
Boolean value:
Type Conversion
Only numbers 0 is False, the rest are True
String as long as the content is True, no content is False
print(bool(""))
print(type(str(False)))
print(int(True))
print(int(False))
2. Detailed strings
s = "meet"
String element (alpha)
String: store some data
Strings are ordered
Strings are immutable
Index: accurately and quickly find value
s = "meet"
0123 # # right to left, left to right -4-3-2-1
s [0] # find elements by index accurately
slice:
Care regardless of
print (s [0:10]) # [starting index: Index termination]
print (s [-6:]) # [starting index: default content acquisition end of the string]
print (s [:]) # [default content acquisition start of the string: the default content acquisition end of the string]
Compared:
s = "alex_wusir|oldboy"
index:
print (s [20]) # error: string index out of range out of range index being given
slice:
print (s [1: 100]) # slice without error index exceeds
Steps
s = "alex_wusir|oldboy"
print(s[0:5:2])
Step 1 by default starting index: End Index: step
Step step may decide the search direction and the pace, the n may take, may be taken backwards
Palindrome
Shanghai water from the sea
while True:
msg = input("请输入内容:") # 12321#
if msg == msg[::-1]: # 将字符串进行反转#
print("这是回文")#
else:
print("请重新输入!")
3. The method of operation of the string
s1 = s.strip()
Default remove head and tail ends of the spaces and line breaks, tabs (Tab) *****
s1 = s.strip("a")
s2 = s1.split("+")
The default split by spaces, line breaks, tabs, returns a list of
s1 = s.split ( "-", maxsplit = 1) is not specified by default all divided maxsplit
s1 = s.replace("n","s",1)
Replace (old, new, Replace Occurrence)
Filter grateful word
s = "TMD"
msg = input(">>>")
if s in msg:
msg = msg.replace(s,len(s) * "*") # len求长度#
print(msg)
1. To achieve a calculator, the calculator supports only addition and subtraction, only three numbers, for example, supports (11 + 22--12)
s = input("请输入要计算的公式:")
s1 = s.replace("-","+-")
s2 = s1.split("+") # s2是一个列表,列表支持索引
print(int(s2[0]) + int(s2[1]) + int(s2[2]))
s1 = s.upper()
ALL CAPS
s2 = s.lower()
All lowercase
验证码不区分大小写
s = "XbGj"
msg = input("请输入验证码(XbGj):")
if msg.upper() == s.upper():
print("成功!")
else:
print("失败")
import random # 随机数
ascii
48 - 57 数字
65 - 90 大写
97 - 122 小写
print(chr(97)) # 通过十进制查找编码上对应的字符
自动生成验证码:
s = f"{chr(random.randint(48,57))}{chr(random.randint(65,90){chr(random.randint(97,122))}{chr(random.randint(48,57))}" # 4位
msg = input(f"请输入验证码{s}:")
if msg.lower() == s.lower():
print("验证成功!")
else:
print("验证失败")
s1 = s.count("e")
Counting statistics
print(s.startswith("x",3))
Judgment begins with what - returns a Boolean value
print(s.endswith("r"))
Judgment as to what the end - returns a Boolean value
series is
s = "123 Hello aaaa"
print (s.isalpha ()) # judgment is not letters, Chinese composition - returns a Boolean value
print (s.isdigit ()) # determine whether it is digital - bug
print (s.isdecimal ()) # determines whether the number of decimal
print (s.isalnum ()) # determines whether the letters, numbers, characters
4.for cycle
for循环
固定结构:
for i in 可迭代对象:
循环体
s = '6314'
for i in s:
print(i)
print(i)
面试题
s = '123'
for i in s:
pass # 占位
print(i)
思维 : 借助了你的循环次数
s = "12"
for i in s: # 1 2
print(s) # 12 12
Iterables: removing (int - Integer; bool - Boolean)
s = '321'
for i in s:
print('倒计时'+i)
count = 0
while count < len(s):
print('倒计时%s'%s[count])
count += 1