day2-operator and variable job

day02 operator and branch structure

1. Operator

The operators supported in python are divided into several categories: mathematical operators, comparison operators, logical operators, and bitwise operations.

A. Mathematical operator: + (addition operation)-(subtraction operation) * (multiplication operation) / (division operation)% (remainder) // (division operation) ** (power operation)

Addition, subtraction, multiplication and division are exactly the same as the functions in mathematics, so there is no need to talk about it.

a.% (take the remainder) means to take the remainder in mathematics, which can be divided into several applications

1. Determine whether a number can be divisible.

2. Take the low digit of the integer;

(X% 10-get the single digit of X) (X% 100-get the last two digits of X) and so on.

b.// (division) X // y represents the quotient of x divided by y and rounded to the small

Apply to numbers to remove low digits

c. (Exponentiation operation) x**y means finding x to the y power, if the power is 1/N, it means opening to the N power.

B. Comparison operator:> .<. >=. <=. ==. !=
C. Logical operator: and (logical and) or (logical or) not (logical negation)

1.and is equivalent to life and when multiple conditions can be met.

2.or is equivalent to or in life, it is used when only one of multiple conditions is required.

3.not negate a certain condition

D. auxiliary operators

(=)( +=)( -=)( *=)( /=)( %=)( //=)( **=)

* The precedence of operators

Mathematical Operators> Comparison Operators> Logical Operators> Assignment Operator

Mathematical operators: **> *>%> //> ±

2. Branch structure

if single branch structure:

​ if conditional statement:
​ code segment

Description:
1. if-keyword, fixed writing
2. Conditional statement-any expression with a result: any type of data, operator expressions other than assignment statement,
if followed by an assignment statement
3. Colon-fixed writing. (The colon in python generally needs to be indented after a line break to indicate a code block)
4. Code segment-one or more statements that maintain an indentation with if (press tab); the code will only be executed when the conditions are met

Execution process:
first judge whether the conditional statement is True (if it is not a Boolean value, convert it to Boolean and then judge),
if it is True, execute the code segment.

if double-branch structure-perform an operation if the condition is met, and perform another operation if the condition is not met

Syntax:
if conditional statement:
code block 1 (code to be executed if the condition is met)
else:
code block 2 (code to be executed if the condition is not met)
Execution process:
first judge whether the conditional statement is True, if it is, execute code block 1 otherwise Just execute code block 2

3) If multi-branch structure-perform different operations according to different conditions

Syntax:
if conditional statement 1:
code block 1
elif conditional statement 2:
code block 2
elif conditional statement 3:
code block 3
. . .
elif:
Code block N
execution process:
first judge whether conditional statement 1 is True, if it is directly executed code block 1, then the positive if structure ends directly; if it is not True, judge whether conditional statement 2 is True, if it is executed Code block 2, and then the entire if structure ends; if it is not True, judge whether the conditional statement is True. . .
By analogy, if the previous conditions are not met, the code segment after elie is executed

Multiple choice

  1. print(100 - 25 * 3 % 4) What should be output? (B)

    A. 1

    B. 97

    C. 25

    D. 0

  2. Which of the following statements is wrong (A).

    A. 除字典类型外,所有标准对象均可以⽤于布尔测试

    B. 空字符串的布尔值是False

    C. 空列表对象的布尔值是False

    D. 值为0的任何数字对象的布尔值是False

  3. The value of the following expression is True is (C).

    A. 3>2>2

    B. 1 and 2 != 1

    C. not(11 and 0 != 2)

    D. 10 < 20 and 10 < 5

  4. Data types not supported by Python are (A).

    A. char

    B. int

    C. float

    D. list

  5. (Multiple choice) n = 6784, the following ways to get 7 are (C, D). Qianfeng Python Artificial Intelligence Academy

    A. n / 1000 % 100

    B. n % 1000 / 100

    C. n // 100 % 10

    D. n // 10 % 100 // 10

  6. Run the following program, when you enter 12 from the keyboard, the result is (B).

x = (input())
print(type(x))

A. <class 'str'>

B. <class 'int'>

C. 出错

D. <class 'dict'>

  1. The operation result of the following expression is (D).

    a = 100
    b = False
    print(a * b > -1)
    

    A. False

    B. 1

    C. 0

    D. True

Fill in the blank

  1. The function name for viewing the type of data in the variable is (type).
  2. Knowing that x = 3 == 3, after the execution ends, the value of the variable x is (True).
  3. Given that x = 3, then after executing the statement x += 6, the value of x is (9).
  4. The value of expression 3 ** 2 is (9), the value of expression 3 * 2 is (6), and the value of expression 4 ** 0.5 is (2).

Programming questions

  1. Write a conditional statement that judges whether a number can be divisible by 2 and 5 at the same time, and print the corresponding result.

    num = int(input('输入一个数:'))
    print('能否同时被2和5整除', num % 5 == 0 and num % 2 == 0 )
    输入一个数:10
    能否同时被25整除 True
    
  2. Write a conditional statement that judges whether a number can be divisible by 2 or 5, but cannot be divisible by 2 or 5 at the same time, and print the corresponding

    the result of.

    num = int(input('输入一个数:'))
    if (num % 2 or num % 5 == 0) and num % 10 != 0:
        print(num, '能被2或者5整除,但是不能同时被2或者5整除')
    else:
        print(num,'未满足条件')
    输入一个数:50
    50 未满足条件
    
    
  3. Assuming that today’s class time is 15678 seconds, how many hours, how many minutes, and how many seconds are programming to calculate today’s class time;

    Hour XX minutes XX seconds' formula is expressed.

    For example: 100 seconds is represented as 0:1:40

    num = 15678
    print(num//3600,'小时', num // 60  % 60,'分钟' , num % 60,'秒')
    
  4. Define two variables to save a person's body and weight, and program to determine whether the person's body is normal!

    Formula: The average value of weight (kg)/body (m) between 18.5 and 24.9 is normal.

    Output format: Is it normal: True/Fals

    
    
    
    

Guess you like

Origin blog.csdn.net/XXXtentacion777/article/details/108761808