Learning is better than seeing, seeing is better than practicing

Process control

judge

score = 92

if score > 90:
    print( "A" )
elif score >= 80:
    print("B")
else:
    print( "E" )
    
if score == 92:
  print("The score is 92") elseif score ! =92:
  print("The score is not 92")
elseif notscore ! =92:
  print("The score is 92")

scorelist = [ score ]
if 92 in list( scorelist ):
  print("Score contains 92")
Copy the code

traverse

for i in range( 1.5) :print( i )

# the while loop
n = 1
while n < 10:
    print( n )
    n += 1
else:
    print( "End of cycle" )


# Iterate over nesting
for i in range( 1.10) :for j in range( 1, i + 1) :print( f'{i}*{j}={i*j}', end=' ' )
    print(a)# infinite loop
while True:
    s = input( 'Enter 0 to exit:' )
    if s == '0':
        break
    print( "You typed in:", s )

Copy the code

Exception handling

The code structure

try:
    # Code where exceptions may occur
except ValueError:
    # Code that handles exceptions
Copy the code

Practical example

while True:
    try:
        # Code that may be abnormal, such as when a number is not entered
        
        x = int(input("Please enter a number:"))
        print( x )
    except ValueError:
        print("You did not enter a number, please try again!")
Copy the code

object-oriented

Object to build employee.py

# -*- coding: utf-8 -*-
__author__ = 'Abo'

class Employee:
    'Base class for all employees'
    empCount = 0

    # Object initialization method, instance 'Employee('hah', 100)' is called
    def __init__(self, name, salary) :
        self.name = name
        self.salary = salary
        Employee.empCount += 1

    def displayCount(self) :
        # Assign the Employee. EmpCount value to the %d position in the string
        return "Total Employee %d" %Employee.empCount

    def displayEmployee(self) :
        return "Name : ", self.name, ", Salary: ", self.salary
Copy the code

Call object

if __name__ == '__main__':
    employee = Employee('hah'.100)
    print( employee.displayCount() )
    print( employee.displayEmployee() )
Copy the code

Bytes Type Description

The Bytes type in Python is essentially an 8-bit binary number arranged in order

For example, if the file is read in binary mode, the string with the prefix b is all bytes, for example

The bytes type is different from the ASCII or string type

Bytes and ASCII

  • Bytes is an 8-bit array
  • ASCII is the decoding method for parsing bytes, similar to GB2312, UTF-8, etc
demoString1 = b'\x61\x62\x63\x64'  # represents the four beyte hexadecimal numbers, 0x61 0x62 0x63 0x64, 97 to 100
demoString2 = b'abcd'              # represents the ASCII digits of abcd, 97, 98, 99, and 100
for i in demoString2:
    print(i)             # 97, 98, 99, 100

print( demoString1 == demoString2 )  #True
Copy the code