Python Practical Notes (3) Conditional Judgment

Multiple statements can be executed, relying on the principle of indentation, and it looks more rigid (note the colon)

age = 3
if age >= 18:
    print('adult')
elif age >= 6: print('teenager') else: print('kid')

As long as it xis a non-zero value, a non-empty string, a non-empty list, etc., it is judged as True, otherwise it isFalse

if x:
    print('True')

About input()

birth = input('birth: ')
if birth < 2000:
    print('00前') else: print('00后')

The program will report an error. The reason is that the type is a character when typing, and it cannot be directly compared with the shaping. It needs to be improved as follows:

s = input('birth: ')
birth = int(s)
if birth < 2000:
    print('00前') else: print('00后')

Off topic:

When writing code, add:

# -*- coding: utf-8 -*-

Using UTF-8 encoding can save space, it seems so, anyway I plan to do it

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325273969&siteId=291194637
Recommended