day03_python basics

I will not take notes for some content of the meeting, but make some rusty notes

1. Loop statement

1.1 continue

continue, which is used in the loop to end this loop and start the next loop.

Example 1:

print("开始")
while True:
	print("红旗飘飘,军号响。")
	continue
	print("剑已出鞘,雷鸣电闪。")
	print("从来都是狭路相逢勇者胜。")
print("结束")

# 输出
开始
红旗飘飘,军号响。
红旗飘飘,军号响。
红旗飘飘,军号响。
红旗飘飘,军号响。
...

Example 2:

print("开始")
i = 1
while i < 101:
	if i == 7:
		i = i + 1
		continue
	print(i)
	i = i + 1
print("结束")

# 输出
开始
1
2
3
4
5
6
8
9
10
...
100
结束

Example 3:

print("开始")
i = 1
while True:
	if i == 7:
		i = i + 1
		continue
	print(i)
	i = i + 1
	if i == 101:
		break
print("结束")

# 输出
开始
1
2
3
4
5
6
8
9
10
...
100
结束

Written at the end, both for breakand continueare placed in the loop statement to control the loop process. Once encountered, all loops will breakbe executed 停止, and once encountered, continuethis loop will be stopped 开始下次循环.

Of course, if there is no more break和continue, we can use the judgment of the while condition and other assistance to complete many functions. With break and continue, our code logic can be simplified to a certain extent.

1.2 while…else

When the condition after while is not true, the code in else will be executed. If a break is encountered, it will not enter the else

#  书写格式
while 条件:
  代码
  ...
else:
  代码
while False:
    print('哈哈哈')
else:
    print('else')
#  结果为 else
n = 1
while n < 3:
    print(n)
    n += 1
else:
    print(666)
# 运行结果
1
2
666

Some people may ask, why not whilejust write directly after the endprint(666)

n = 1
while n < 3:
    print(n)
    n += 1
print(666)
#  结果也是和上面的一样的

If whilethere is a loop breakto terminate the loop, it will 直接退出循环, and will not enter intoelse

n = 1
while n < 3:
    print(n)
    break
else:
    print(666)
#  结果为 1

2. String formatting

String 格式化, using a more convenient form to achieve string splicing

2.1 %

2.1.1 Basic formatting operations

name = '王不易'
#  占位符
text = '我叫%s, 今年18岁' % '王不易'
#  两种方法都是可以的
text1 = '我叫%s, 今年18岁' % name
print(text)
print(text1)

Multiple placeholders are enclosed in parentheses (according to position 一一对应)

name = '王不易'
age = 18
#  占位符
text = '我叫%s, 今年%s岁' % (name, age)
print(text)
#  结果: 我叫王不易, 今年18岁
#  但是这样写不够严谨,因为整数可以用%d来表示,但上面这样写也是可以的
text1 = '我叫%s, 今年%d岁' % (name, age)  # 结果与上面一样

The following is the content about all %. It is not required to remember. When you don’t know, just click in and have a look.

https://www.cnblogs.com/wupeiqi/articles/5484747.html

The following is based on 名称the one-to-one correspondence of the placeholders

#  %(name)s -> 给占位符加名称。后面要是字典的形式%{'name':'xxx'}
name = '王不易'
text1 = '%(name)s你快过来呀,今天桂千理不在家' % {
    
    'name':name}
print(text1)

#  多个占位符,则在中括号里加键值对就可以了
name1 = '桂千理'
text2 = '%(name)s你快过来呀,今天%(name1)s不在家' % {
    
    'name': name, 'name1': name1}
print(text2)

2.1.2 Percentage

If it exists 一段字符串in 百分号, you need to write it 两个百分号. When you format the string, you know that this is not a placeholder, but an ordinary %. If you don’t write it, you will get an error.

name = '王不易'
s = '%s,我的片以及下载了90%了,居然断网了' % s
print(s)  # 这样子输出会报错

#  正确的写法
s = '%s,我的片以及下载了90%%了,居然断网了' % s
print(s)  # 王不易,我的片以及下载了90%了,居然断网了

Summary: Once there is a percentage display in the string format, please be sure to add %% to achieve the output %.

2.2 format

name = '王不易'
age = 18

#  和上面一样,按照位置一一对应
text = '我叫{},今年18岁'.format(name)  # {} 占位符
print(text)  # 我叫王不易,今年18岁

text = '我叫{},今年{}岁'.format(name, age)  # 传多个
print(text)  # 我叫王不易,今年18岁

text = '我叫{},今年{}岁,真实的名字也叫{}'.format(name, age, name)  # 传多个
print(text)  # 我叫王不易,今年18岁,真实的名字也叫王不易
name = '王不易'
age = 18
#  带数字的一一对应(索引)
text = '我叫{0},今年18岁'.format(name)  # {0} 占位符
print(text)  # 我叫王不易,今年18岁

#  类似列表,表示将format中的第几个索引传过去
text = '我叫{0},今年{1}岁'.format(name, age)  # 传多个
print(text)  # 我叫王不易,今年18岁

#  能够复用
text = '我叫{0},今年{1}岁,真实的名字也叫{0}'.format(name, age)  # 这样穿参数就比上面方便很多
print(text)  # 我叫王不易,今年18岁,真实的名字也叫王不易
#  通过名称一一对应
name = '王不易'
age = 18
text = '我叫{n1},今年18岁'.format(n1=name)
print(text)  # 我叫王不易,今年18岁

text = '我叫{n1},今年{n2}岁'.format(n1=name, n2=age)  # 传多个  # 也是可以复用的
print(text)  # 我叫王不易,今年18岁
#  当复用模版
text = '我叫{0},今年{1}岁'  # 当成一个模版,可以多次使用

data1 = text.format('王不易', 18)
data2 = text.format('桂千理', 20)
print(data1)  # 我叫王不易,今年18岁
print(data2)  # 我叫桂千理,今年20岁
#  % 也可以当成模版来使用
text = '我叫%s, 今年%d岁'
data1 = text %('王不易', 18)
data2 = text %('桂千理', 20)
print(data1)  # 我叫王不易,今年18岁
print(data2)  # 我叫桂千理,今年20岁

2.3 f

more convenient than the first two

#  通过名称一一对应
name = '王不易'
age = 18
text = f'我叫{
      
      name},今年{
      
      age}岁'
print(text)  # 我叫王不易,今年18岁
#  在括号里面做算术也是可以的
text = f'我叫{
      
      name},今年{
      
      2+16}岁'
print(text)  # 我叫王不易,今年18岁
# 在上面的基础上加等于,就可以相当于整个算术
text = f'我叫{
      
      name},今年{
      
      2+16=}岁'
print(text)  # 我叫王不易,今年2+16=18岁
#  进制转换
v1 = f'嫂子今年{
      
      22}岁'
print(v1)  # 嫂子今年22岁

#  将22转换为二进制
v2 = f'嫂子今年{
      
      22: #b}岁'
print(v2)  # 嫂子今年 0b10110岁

#  将22转换为八进制
v3 = f'嫂子今年{
      
      22: #o}岁'
print(v3)  # 嫂子今年 0o26岁

#  将22转换为十六进制
v4 = f'嫂子今年{
      
      22: #x}岁'
print(v4)  # 嫂子今年 0x16岁
#  理解 
#  f的花括号里面还可以传入方法/函数
name = 'amaisi'
text = f'我叫{
      
      name},今年18岁。'
print(text)  # 得到的就是 我叫amaisi,今年18岁。
#  如果我想将name改为大写,则可以这样写(直接传入方法)
text = f'我叫{
      
      name.upper()},今年18岁。'
print(text)  # 我叫AMAISI,今年18岁。

3. Operators

3.1 Interview questions

In logic operation:and or

name = 'alex'
pwd = '123'
v1 = name == 'alex' and pwd == '123'
print(v1)  # True

name = 'alex'
pwd = '3'
v1 = name == 'alex' and pwd == '123'
print(v1)  # False

#  上面类似于
if name == 'alex' and pwd == '123'
#  面试题2
v2 = 'wmj' and 'gql'
#  问: v2返回什么?
print(v2)  # gql

'''
解:
	第一步: 将and前后的值转换为布尔值 -> True and True
	第二步: 判断本次操作取决于谁? 如果前面是False,则无需看后一个值,由于前面是True,需要去查看后面的值是什么,所以本次逻辑判断取决于后面的值。
	所以,后面的值等于多少,最终的结果就是多少。 所以v2 = 'gql'
'''

v3 = '' and 'wby'  # False and True
#  所以直接返回空
print(v3)  # 返回空

v4 = 1 or 8
#  第一步: 将or前后的值转换为布尔值 -> True and True
#  第二步: 判断本次操作取决于谁? 由于前面是True,所以本次逻辑判断取决于前面的值。
print(v4)  # 1

v5 = 0 or 8
#  第一步: 将or前后的值转换为布尔值 -> False and True
#  第二步: 判断本次操作取决于谁? 由于前面是False,所以本次逻辑判断取决于后面的值。
print(v5)  # 1

3.2 Exercises

v1 = 1 or 2
v2 = -1 or 3
v3 = 0 or -1
v4 = 0 or 100
v5 = "" or 10
v6 = "wupeiqi" or ""
v7 = 0 or ""

print(v1,v2,v3,v4,v5,v6,v7)
#  结果: 
#  v1 = 1
#  v2 = -1
#  v3 = -1
#  v4 = 100
#  v5 = 10
#  v6 = "wupeiqi"
#  v7 = ''

#  总结: or, 看第一个值,如果第一个值为真,结果就应该是第一个值,否则结果就是第二个值。
v1 = 4 and 8
v2 = 0 and 6
v3 = -1 and 88
v4 = "" and 7
v5 = "武沛齐" and ""
v6 = "" and 0
v7 = 0 and "中国"

print(v1,v2,v3,v4,v5,v6,v7)
#  结果: 
#  v1 = 8
#  v2 = 0
#  v3 = 88
#  v4 = ''
#  v5 = ''
#  v6 = ''
#  v7 = ''
#  总结: and, 看第一个值,如果第一个值为真,结果就应该是第二个值,否则就是第一个值

3.3 Ultimate interview questions (combination of or and and)

Idea: If there are multiple and and or, first calculate and and then calculate or

v1 = 0 or 4 and 3 or 7 or 9 and 6
	解:	 0 or 3 or 7 or 9 and 6
  	 0 or 3 or 7 or 6	
     3 or 7 or 6
     3 or 6
     3
v2 = 8 or 3 and 4 or 2 and 0 or 9 and 7
		 8 or 4 or 2 and 0 or 9 and 7
  	 8 or 4 or 0 or 9 and 7
     8 or 4 or 0 or 7
     8 or 0 or 7
     8 or 7
     8
v3 = 0 or 2 and 3 and 4 or 6 and 0 or 3
		 0 or 3 and 4 or 6 and 0 or 3
		 0 or 4 or 6 and 0 or 3
  	 0 or 4 or 0 or 3
     4 or 0 or 3
     4 or 3
     4

Supplement: There are not, and, and or

Idea: first calculate not, then calculate and, and finally calculate or

v4 = not 8 or 3 and 4 or 2
		 False or 3 and 4 or 2
  	 3 and 4 or 2
     4 or 2
     4

practice today

First judge (greater than, less than), and then judge (not, and, or)

  1. Judge the True and False of the following logical statements

    1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
    		False or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6  # 先处理大于、小于
        False or True or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
        False or True or False and 2 > 1 and 9 > 8 or 7 < 6
        False or True or False and True and 9 > 8 or 7 < 6
        False or True or False and True and True or 7 < 6
        False or True or False and True and True or False  # 先判断and
        False or True or True and True or False
        False or True or True or False
        True or True or False
        True or False
        True
    not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
    		not True and True or False and True and True or False  #  先处理大于、小于
      	False and True or False and True and True or False  # 处理not
        False  # 处理and
    
  2. Evaluate the following logical statements.

    8 or 3 and 4 or 2 and 0 or 9 and 7
    		8 or 4 or 0 or 7  # 先判断and
        8  # 处理not
    0 or 2 and 3 and 4 or 6 and 0 or 3
    		0 or 4 or 0 or 3  # 先判断and
        4 # 处理not
    
  3. What are the following results?

    6 or 2 > 1  # 6
    3 or 2 > 1  # 3
    0 or 5 < 4  # False
    5 < 4 or 3  # 3
    2 > 1 or 6  # True
    3 and 2 > 1  # True
    0 and 3 > 1  # True   -> 这题的答案是 0
    	0 and True
      0
    2 > 1 and 3  # 3
    3 > 1 and 0  # 0
    3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2  
    		True and 2 or True and 3 and 4 or True
      	2 or 4 or True
        2
    
  4. Realize user login system, and support direct exit after three consecutive input errors, and display the remaining number of errors each time an error is entered (hint: use string formatting).

    print('欢迎来到德莱联盟')
    times = 3  # 用于控制次数
    while True:
        name = input('请输入你的用户名: ')
        pwd = input('请输入你的密码: ')
        if name == '王不易' and pwd == '123':
            print('欢迎回家')
            break
        times -= 1
        if times == 0:
            print('你的次数已用完,即将关闭程序')
            break
        print(f'密码或用户名输入有误,剩余错误次数{
            
            times}')
    
  5. Guess the age game
    Requirements: Allow the user to try up to 3 times. If the guess is not correct for 3 times, it will exit directly. If the guess is correct, print the congratulations message and exit.

    print('来猜猜我的年龄吧~~~~')
    times = 3  # 用于控制次数
    age = 18
    while True:
        number = input('请大胆输入我的年龄: ')
        number = int(number)
        if number == age:
            print('恭喜你,答对了!!!')
            break
        times -= 1
        if times == 0:
            print('你的次数已用完,你太让我失望了')
            break
        print(f'不对不对,还有{
            
            times}次机会')
    

  6. Requirements for the upgraded version of the age guessing game: allow the user to try up to 3 times. After each 3 attempts, if the guess is not correct, ask the user if he wants to continue playing. If the answer is Y, continue to let him guess 3 times, and so on . , if you answer N, you will exit the program, if you guess right, you will exit directly.

print('来猜猜我的年龄吧~~~~')
times = 3  # 用于控制次数
age = 18
while True:
    number = input('请大胆输入我的年龄: ')
    number = int(number)
    if number == age:
        print('恭喜你,答对了!!!')
        break
    times -= 1
    if times == 0:
        print('你的次数已用完,你太让我失望了')
        n = input('再给你一次机会,你还玩不玩,回答Y,就再给你猜三次,回答N,就拜拜 : ')
        if n == 'Y':
            times = 3
        else:
            break
    print(f'不对不对,还有{
      
      times}次机会')

Guess you like

Origin blog.csdn.net/m0_48936146/article/details/127820381