1.if basic grammar
To determine if the condition:
When the conditions are met, things to do
......
Note: The code indentation is a tab key, or 4 spaces - recommended space
if statement and the indented portion is a complete block of code
2. Operator
Operation Description
== equality
! = Are not equal
> Left is greater than the right
<Left is less than the right
Whether <= greater than or equal to the right of the left
Whether <= Less than or equal to the right of the left
3.if - else syntax
To determine if the condition:
When the conditions are met, things to do
......
else:
When the condition is not satisfied, things to do
......
Note: if else can be considered as a complete block of code.
4. The logic operation
Logical Operators:
And and / or or / non-not
5.if - elif - else
if Condition 1:
When the condition 1 holds, things to do
......
elif Condition 2:
When the condition 2 is satisfied, things to do
......
elif Condition 3:
When the conditions are set up 3 things to do
......
else:
When the above conditions are not established, things to do
......
Note: elif and else can not be used alone, and if necessary in combination.
Can be if, elif and else code and the respective indentation, as a complete block of code .
5.if nest basic grammar
if Condition 1:
When the condition 1 holds, things to do
......
if condition based on the condition 12:
When the condition 2 is satisfied, things to do
......
else:
When the condition 2 is not satisfied, things to do
......
else:
1 when the condition is not satisfied, things to do
......
6. introduction kit using a random function
c = random.randint(a, b)
- a, b are integers
- a <= b
- c >= a 且 c <= b
1 # Import random Toolkit 2 Import Random . 3 . 4 # computer to select it punches out using a random function to obtain 1- random integer between. 3 . 5 Computer the random.randint = ( 1 , . 3 ) in this way the generated random # number, to generate three probability is much lower than the 1 and 2
NOTE: The kit generally introduced at the top of the file, so that the code below, can be used at any time
7. Exercises: rock-paper-scissors
1 # import random Kit 2 Import Random 3 # input from the console to be out of boxing - a stone ( 1 ) / Scissors ( 2 ) / fabric ( 3 ) 4 Player = int (the INPUT ( " Please enter the fist you want out - stone 1/2 scissors / cloth 3 " )) 5 # computer to select it punches out using a random function to obtain 1- random integer between 3 . 6 computer = (the random.randint 1 , 3 ) in this manner # generated random number, the probability of generation 2 and 3, are much lower than 1 . 7 . 8 # input into text . 9 player_str = " stone " 10 computer_str = " stone " . 11 12 is IF== Player . 1 : 13 is player_str = " stone " 14 elif Player == 2 : 15 player_str = " scissors " 16 the else : . 17 player_str = " fabric " 18 is . 19 IF Computer == . 1 : 20 is computer_str = " stone " 21 is elif Computer == 2 : 22 is computer_str = " scissors " 23 is the else : 24 = computer_str " cloth " 25 26 # players and boxing computer output selected 27 Print ( " you choose boxing is:% s - computer out of boxing is:% S " % (player_str, computer_str)) 28 29 # judge the outcome 30 IF ((== Player . 1 and computer == 2 ) 31 is or (Player == 2 and computer == . 3 ) 32 or (== Player . 3 and computer == . 1 )): 33 is Print ( " Ouye, computer weak burst " ) 34Player == elif Computer: 35 Print ( " a coincidence, it again a ~ " ) 36 the else : 37 [ Print ( " pity, almost point win " )