Judgment (if) statement
if to determine the basic syntax of the statement
if to judge the condition:
Things to do when the conditions are true
Judgement sentence exercise:
demand:
1. Define an integer variable
2. Determine whether you are over 18 years old (> =)
> =: Comparison operator
Comparison relation operators: ==,! =,>, <, <=,> =
3. If you are over 18, you are allowed to enter the Internet cafe
else:
age = 19
if age> = 18:
print 'You are an adult, welcome to the Internet cafe'
print 'Welcome to welcome'
print '~~~~~~~~'
==============================================================
Thinking:
When using if judgment, you can only do what you need to do when the conditions are met, then if you need to do when the conditions are not met
When we should do something, how should we do it?
else, the format is as follows #Note that else must be used with if, and the else is not followed by the condition
if to judge the condition:
Things to do when the conditions are true
......
else: Things to do when the condition is not established
......
#if and else statements and their indented parts are a complete block of code
Example:
demand
1. Enter the user's age
2. Determine whether you are over 18 years old (> =)
3. If you are over 18, you are allowed to enter the Internet cafe
4. If you are under 18 years of age, you will be prompted to do your homework
logic operation
age = raw_input ('Please enter the age:')
if age> = 18:
print 'You are an adult, welcome to the internet cafe'
else:
print 'Prompt to write homework'
print '~~~~~~~~'
=======================================================
In program development, usually when judging conditions, you will need to judge multiple conditions at the same time
Only if multiple conditions are met, can the subsequent code be executed, this time you need to use logical operators
Logical operators can logically connect multiple conditions into more complex conditions
Logical operators include: short circuit with && or / || short circuit with /! Take the opposite structure. Three kinds
&&
Condition 1 and Condition 2
With / and
Both conditions are met, return True
As long as there is one that is not satisfied, it returns False
||
Condition 1 or Condition 2
Or / or
As long as one of the two conditions is met, return True
If neither condition is met, return False
!
Reverse a condition: for example, if I am male, then the value will be the opposite: I am female.