Basic operators, process control if, while

Operator

Arithmetic operator

operator describe Example
+ plus a + b
- reduce a - b
* ride a * b
/ except a / b
% Remainder a % b
// To be divisible by a // b
** exponentiation a ** b 2**3=8

Comparison operator

Assuming variables a = 10, b = 20

operator describe Example
== Equivalent or not. A = B returns False
!= Not equal to, whether the objects of comparison are not equal A!= B Returns True
< less than A < B returns to True
<= Less than or equal to A <= B Returns True
> greater than A > B returns to False
>= Greater than or equal to A >= Return to False

Assignment Operators

= += -= /= *= %= //= **=

Logical Operator

operator Example
and Both left and right conditions are True, True, or False.
or If only one of the left and right conditions is satisfied, it is True, otherwise it is False.
not No, if the condition is True, it is False, if the condition is False, it is True.
name = 'cwz'
height = 180
weight = 140

print(name == 'cwz' and height == 180)  # True
print(name == 'cwz2' and weight == 140)  # False

print(name == 'cwz2' or weight == 140)  # True
print(name == 'cwz2' or weight == 120)  # False

print(not name == 'cwz')   # False

Identity Operator

operator describe Example
is is determines whether two identifiers are referenced from an object x is y, if referenced from the same object, return True, otherwise return False
is not is not to determine whether two identifiers are referenced from different objects x is not y, if the reference comes from different objects, return True, otherwise return False

The difference between is and ==: is is used to determine whether two variable reference objects are the same (in the same memory space), === is used to determine whether the value of the reference variable is equal.

member operator

operator describe Example
in If a value is found in the specified sequence, return True, otherwise False X = 1, y = [1, 2, 3, 4], X in Y - > Returns True
not in If no value is found in the specified sequence, return True, otherwise False X = 1, y = [1, 2, 3, 4], x not in Y - > Returns False

Bitwise Operators

Bit-by-bit operators treat numbers as binary to perform calculations.

Variables a and b are 60 and 13 in the following table. The binary format is as follows:

a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a  = 1100 0011

python operator priority

Priority is high, just enclosed in parentheses...

if judgment of process control

Process control is a direction of controlling variable change.

Single branching structure

if condition: # condition set to execute the following code
    Code

Bifurcation structure

if condition 1: # condition 1 establishes execution code 1, not execution code 2
    Code 1
else:
    Code 2

Multi-branch structure

if condition 1:   
    Code 1
 Elf condition 2:  if the latter condition holds, execute code 1, will not take the next step. Code 2 will be executed only if the latter condition of if is not established.
    Code 2
 elif condition 3:
    Code 3
 elif condition 4:
    Code 4
    ......
(elif can have many)
else:
    Code   

Practice:

'''
//If the score >= 90, print "Excellent"
//If score > = 80 and score < 90, print "good"
//If score > = 70 and score < 80, print "normal"
//Others: poor printing
'''

grade = input('Please enter your score:')

grade_int = int(grade)

if grade_int>=90:
    print('excellent')
elif grade_int >=80:
    print('good')
elif grade_int >=70:
    print('ordinary')
else:
    print('difference')

The Way to Find a bug, Print Variables, View the Change of Variables --"Deug's Source

while cycle of process judgment

Cycle - > Do one thing over and over regularly

grammar

The while condition: # condition holds the running code, but it does not end the while loop
    Code After the code is executed, it enters the next cycle.
while True:
    print(1)

This program will print out 1 indefinitely. What do we need to stop?

while + break

Print 1-100:

count = 0
while True:
    if count == 100:   
        break      # break terminates the loop
    count += 1
    print(count)

while + continue

Print 1-100, not 50

count = 0
while True:
    if count == 100:   
        break
    count += 1
    if count == 50:
        continue    # continue jumps out of this loop and does not execute the following code
    print(count)

Print the sum of even numbers of 1-100 (excluding [22,46,68,98])

count = 0
sum_count = 0
while 1:
    if count == 100:
        break
    count += 2
    if count in [22,46,68,98]:
        continue
    sum_count += count
    print(count)
print('And:', sum_count )

tag intermediate variable controls while loop

Improve the code:

count = 0
sum_count = 0
while count < 100:
    count += 2
    if count in [22,46,68,98]:
        continue
    sum_count += count
    print(count)
print('And:', sum_count )

Practice:

# Guess Age Game, Three Opportunities

age = 19
count = 0
while count < 3:
    age_inp = input('Please enter your age:').strip()
    age_inp_int = int(age_inp)
    if age_inp_int == age:
        print('Guess right.')
        break
    elif age_inp_int < age:
        print('Guess it's small.')
    else:
        print('Guess it's small.')

    count += 1
    print(f'You still have{3 - count}Second chance')

while + else

count = 0
while count < 5:
    count += 1
    print(count)
else:
    print('Not being break Interrupt me and I'll be able to come out.')
    
    
# Print results:
1
2
3
4
5
//I can come out without break ing in.

while... else... the loop will execute the code behind the else if it is not interrupted by break, otherwise the code behind the else will not execute.

Keywords: Python less

Added by tonbah on Wed, 11 Sep 2019 15:41:13 +0300