1. If simple judgment
If condition:
Statement block
Else
Statement block
If 1>2:
Print (' fake ')
Else
Print (' true ')
2. Multiple if elif
If condition 1:
Statement block
Elif Condition 2:
Statement block
Else
Statement block
If a==2:
Print (' a=2 ')
Elif a==3:
Print (' A=3 ')
Else
Print (' a=0 ')
3, and and
score=input (' Please enter your score: ')
Score=int (score) #强制类型转换
if score<=100 and score>=90:
print (' excellent ')
elif score<90 and score>=80:
print (' good ')
elif score<80 and score>=60:
print (' Pass ')
elif score<60 and score>=0:
print (' fail ')
Else:
print (' Please enter the correct number ')
4, or OR
Sex=input (' Please enter your gender: ')
If sex== ' male ' or sex== ' female ':
Print (' Sex legal ')
Else
Print (' Sex not legal ')
5. DateTime
Import the DateTime module using import first
Import datetime
Set a user name (used with datetime concatenation)
user= ' xxx '
class that represents the date and time
Today=datetime.datetime.today ()
Convert datetime to String type
TODAY=STR (today)
Welcome XX login, today's date is xxx example:
notation 1: msg= ' welcome ' +user+ ' login ' + ' Today's date is ' +today (The way to stitch strings)
Print (msg)
notation 2: msg= ' Welcome%s login, today's date is%s '% (user,today)
Print (msg)
%s placeholder preceded by several%s after parentheses write several variables
%s represents a string type (others are also available) %d integers%f decimals retain several decimals, plus%.2f in front of F
Retain two decimal places you can also use round (score,2) to retain decimals after several (rounding 0)
\ nthe line break
Python If condition judgment, datetime