Python study notes (54-punch in on time-QQ) 7.23

Task2 conditional statement

1.
If statement The internal code block of the if statement is executed only when the result of the conditional expression expression is true, otherwise it will continue to execute the statement immediately following the code block. The expression conditional expression in a single if statement can realize multiple conditional judgments through the Boolean operators and, or and not.

  1. The if-else statement
    Python provides the else used with if. If the Boolean value of the conditional expression of the if statement is false, the program will execute the code after the else statement.
    The if statement supports nesting, that is, one if statement is embedded in another if statement to form a different level of selection structure. Python uses indentation instead of curly braces to mark code block boundaries, so pay special attention to the hanging problem of else.

  2. if-elif-else statement

if expression1:
    expr1_true_suite
elif expression2:
    expr2_true_suite
    .
    .
elif expressionN:
    exprN_true_suite
else:
    expr_false_suite

The elif statement is the else if, used to check whether multiple expressions are true, and execute the code in a specific code block when it is true.

  1. The assert keyword
    assert is a keyword we call "assertion". When the condition behind this keyword is False, the program automatically crashes and throws an AssertionError exception.
    During unit testing, it can be used to put checkpoints in the program. Only the condition is True can the program work normally.

2. Loop statement
1. While loop The
most basic form of the while statement includes a boolean expression at the top and one or more indented statements belonging to the while code block.
The code block of the while loop will continue to execute in a loop until the value of the Boolean expression is Boolean false. If the Boolean expression does not contain <, >, ==,! Operators such as =, in, not in, etc., are also possible to give only conditions such as values. When a non-zero integer is written after while, it is regarded as a true value and the loop body is executed; when 0 is written, it is regarded as a false value and the loop body is not executed. You can also write str, list, or any sequence. If the length is not zero, it is regarded as a true value and the loop body is executed; otherwise, it is regarded as a false value and the loop body is not executed.

2. While-else loop

while 布尔表达式:
    代码块
else:
    代码块

When the while loop is executed normally, the else output is executed. If a statement that jumps out of the loop is executed in the while loop, such as break, the content of the else code block will not be executed. (Break is executed in order and will jump out when encountered)

3. The for loop
for loop is an iterative loop, which is equivalent to a general sequence iterator in Python. It can traverse any ordered sequence, such as str, list, tuple, etc., as well as any iterable object, such as dict.

for 迭代变量 in 可迭代对象:
    代码块

In each loop, the iteration variable is set to the current element of the iterable object and provided to the code block.

4. For-else loop

for 迭代变量 in 可迭代对象:
    代码块
else:
    代码块

When the for loop is executed normally, the else output is executed. If the statement that jumps out of the loop is executed in the for loop, such as break, the content of the else code block will not be executed, just like the while-else statement.

5. The range() function
range([start,] stop[, step=1])
This BIF (built-in function) has three parameters, two of which are enclosed in square brackets indicate that these two parameters are optional. step=1 means that the default value of the third parameter is 1. The function of the range BIF is to generate a sequence of numbers starting with the value of the start parameter and ending with the value of the stop parameter. The sequence contains the value of start but not the value of stop.

6. enumerate() function
enumerate(sequence, [start=0])
sequence-a sequence, iterator or other object that supports iteration. start-the starting position of the subscript. Return an enumerate (enumeration) object.
Using enumerate(A) not only returns the element in A, but also gives the element an index value (starting from 0 by default). In addition, use enumerate(A, j) to determine the starting value of the index j.

7. Break statement and continue statement The
break statement can jump out of the loop at the current level.
continue terminates the current cycle and starts the next cycle.

8. Pass statement The
pass statement means "do nothing". If you don't write any statement where the statement is needed, the interpreter will prompt an error, and the pass statement is used to solve these problems. Pass is an empty statement, does not do any operation, only plays the role of a placeholder, its role is to maintain the integrity of the program structure. Although the pass statement does nothing, if you are not sure what kind of code to put in a location for the time being, you can place a pass statement first to make the code run normally.

9. Deduction
(1) List comprehension

[ expr for value in collection [if condition] ]
如:
x = [-4, -2, 0, 2, 4]
y = [a * 2 for a in x]
print(y)
# [-8, -4, 0, 4, 8]

(2) Tuple derivation

( expr for value in collection [if condition] )
如:
a = (x for x in range(10))
print(a)

# <generator object <genexpr> at 0x0000025BE511CC48>

print(tuple(a))

# (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

(3) Dictionary deduction

{
    
     key_expr: value_expr for value in collection [if condition] }
如:
c = {
    
    i for i in [1, 2, 3, 4, 5, 5, 6, 4, 3, 2, 1]}
print(c)
# {1, 2, 3, 4, 5, 6}

Practice questions:
1.

class:solution:
  def findNumbers()
  x=[i for i in range(1500,2700) if (i%5)==0 and (i%7)==0]
  return x
if(v1<=100 and v2<=100 and t<=300 and s<=10 and I<=10000)
  i=0
  j=0
  l1=0
  l2=0
  while l1<=l and l2<l
    if l1-l2>=t
      for j<5
      l2=l2+v2
      j++
      i++
      if l1>=l or l2>l
        break
      j=0
    else
    l1=l1+v1
    l2=l2+v2
    i++
    if l1>=l or l2>l
       break
 if l1>l2
   printf("R"); 
 elif l2>l1
   printf("T");
 else 
   printf("D")
 printf(i)

Method Two:

import math
v1,v2,t,s,l=map(int ,input().split())
'''
v1 兔子的速度
v2 乌龟的速度
t 兔子领先t米以上
s 一旦任一秒结束后假如兔子发现自己领先t米以上,就休息s秒
l 总路程
'''
l1=0#兔子走的路程
l2=0#乌龟走的路程
l1time=0#兔子现在走了的时间
l2time=0#乌龟现在走了的时间
while True:
  if l1-l2>=t:
        l2+=s*v2
        l1time+=s
        l2time+=s
  dt1=l1time+(l-l1)/v1 #兔子走到重点所需要的时间
  dt2=l2time+(l-l2)/v2 #乌龟走到重点所需要的时间
  l1+=v1
  l2+=v2
  l1time+=1
  l2time+=1
  if l1>=l or l2>=l:

    if dt1>dt2:
        print('T')
        print(math.ceil(dt2))
        #print('l2',l2)
        break
    elif dt1==dt2:
        print('D')
        print(math.ceil(dt1))
        break
    else:
        print('R')
        print(math.ceil(dt1))
        break

Guess you like

Origin blog.csdn.net/m0_45672993/article/details/107544729