Getting started with zero foundation to learn Python (6)-common operators

Arithmetic operator

+- * / (real division)% (take the remainder) ** // (division)
In Python, you can write: a = a + 3 is equivalent to a += 3,
Insert picture description here
it can also be like this: a = b = c = d = 10
Python3's division has become a real division, that is, it will return a floating-point value.//Floor division is
Insert picture description here
performed, that is , it returns an integer that is less than the quotient
Insert picture description here
%: take the remainder
Insert picture description here
** exponential operator
Insert picture description here

Comparison operator

Return the value of Boolean type according to the true or false of the expression
<; <=; >; >=; ==; !=

Logical operator

and : When the left and right sides are both True, the result is True; as long as there is a False on the left and right sides, the result is False
or : Only when the left or right is True, the result is True; only both sides are at the same time When it is False, the result is False.
not : unary operator, whichever is the opposite of the value of the Boolean type, all
non-zero numbers are True
Insert picture description here

Insert picture description here
In Python, 3<4<5 is equivalent to (3<4) and (4<5)

Priority issues

When there are multiple operators in an expression at the same time, there may be a priority problem. Multiplication and division followed by addition and subtraction. Comparison operators are higher than logical operators . If necessary, add () to increase the readability of the code.
Insert picture description here
**The exponent operator has higher precedence than the unary operator on the left side, but lower precedence than the unary operator on the right side
As shown below
Insert picture description here
Exponentiation (**)>sign (+-)> arithmetic operator (+-* / //)> comparison operator (<; <=;>; >=; ==; != >) logical operation Symbol (not; and; or)
not> and> or

Task

  1. Python's floor division is now implemented using "//". What content will you visually display for 3.0 // 2.0?
    1

  2. a <b <c is actually equal to?
    (a<b) and (b<c)

  3. Without IDLE, can you easily say the value of 5 ** -2?

0.04
3. How to simply judge whether a number is odd or even?
Take the remainder to judge%

while 1:#保证程序可以运行n次
    number = input("请输入一个非0的数:")
    a = int(number)
    b = a % 2
    if b == 1:
        print("是奇数")
    else:
        print('是偶数')

Insert picture description here

  1. 请用最快速度说出答案:not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9
    ((not 1)or 0) and (1 or 3) and (4 or 5) and (6 or 7) and (8 or 9)= 0 and 1 and 4 and 6 and 8 = 0(错了)
    (not 1) or (0 and 1) or (3 and 4) or (5 and 6) or (7 and 8 and 9)= 0 or 0 or 4 or 6 or 9=4
    Short circuit logic: 3 and 4 == 4, and 3 or 4 == 3
  2. Remember our homework for seeking leap years in last class? If you haven't learned the "surplus" operation, still remember what method can be used to "request for completeness" instead of the "%" function?
    See if the division is equal to the real division if int(a/400) == a/400
    Use the remainder method:
year = input('请输入一个年份:')
while year.isdigit() == 1:
    a = int(year)
    b = a % 400
    c = a % 4
    d = a % 100 
    if b == 0:
        print('闰年')
    else:
        if c == 0 and d != 0:
            print('闰年')
        else:
            print("不是闰年")
    year = input('请输入一个年份:')
if year.isdigit() == 0:
    print('输入不合法') 
    year = input('要输入整数哦:')
    while year.isdigit() == 1:
        a = int(year)
        b = a % 400
        c = a % 4
        d = a % 100
        if b == 0:
            print('闰年')
        else:
            if c == 0 and d != 0:
                print('闰年')
            else:
                print("不是闰年")
        year = input('请输入一个年份:')

Insert picture description here
6. Please write a program to print out all odd numbers from 0 to 100.

print('0-100内的所有奇数:')
i = 0
while i <= 100:
    if i % 2 == 1:
        print(i)
        i = i + 1
    else:
        i += 1

Insert picture description here
7. We said that the current Python can calculate very large data, but... the real big data calculation depends on the hardware just now. How about writing a small code to make your computer crash?
Insert picture description here
8. Einstein’s difficult problem
Einstein once came up with such an interesting mathematical problem: there is a long ladder, if each step is 2 steps, 1 step is left; if each step is 3 steps, 2 steps are left; if Each step goes up to 5 steps, and 4 steps remain at the end; if each step goes up to 6 steps, 5 steps remain at the end; only each step goes up to 7 steps, and there is exactly one step left.
(Little turtle warm reminder: if the steps are too big, it is easy to pull the eggs~~~) Please program to find at least how many steps the ladder has?

Little turtle code:

x = 7
i = 1
flag = 0

while i <= 100:
    if (x%2 == 1) and (x%3 == 2) and (x%5 == 4) and (x%6==5):
        flag = 1
    else:
        x = 7 * (i+1) # 根据题意,x一定是7的整数倍,所以每次乘以7
    i += 1

if flag == 1:
    print('阶梯数是:', x)
else:
    print('在程序限定的范围内找不到答案!')

Guess you like

Origin blog.csdn.net/qq_44520665/article/details/112594537