📖 preface

How will programming skills grow the most

The work is often a bit like turning a screw in a factory 🏭. Most of the work is repetitive, so imagine how much innovation and new skills you’ve learned over the past year. So at this time general in order to learn more content will buy some technical books, but! Technical books and other books are different, as long as you do not use to read it is just understatement, it is difficult to accept and understand. Just like design patterns, although you may have seen them a few times, they are still rarely used in actual coding, mostly because they are not followed seriously. Hands-on programming is the best way to learn.

Today the blogger is going to talk to you about how to use Python conditional control! Do not like spray, if you have suggestions welcome to supplement, discussion!

For more information on installation and sinification, see this post from the bloggerDownload and Install VsCodeAs well asPython series: windows10 to configure Python3.0 development environment!Restart VsCode after installation!


🚀 last Come on!

Python conditional statements are blocks of code that are executed based on the result (True or False) of one or more statements.

Here is a simple picture of the execution of conditional statements:


😎 if statement

The general form of the if statement in Python looks like this:

if condition_1:
    statement_block_1
elif condition_2:
    statement_block_2
else:
    statement_block_3
Copy the code
  • If “condition_1” is True the “statement_block_1” block statement is executed
  • If “condition_1” is False, then “condition_2” will be judged
  • Condition_2 “executes the “statement_block_2” block statement if “condition_2” is True
  • If “condition_2” is False, the “statement_block_3” block statement is executed

Python uses elif instead of else if, so the keyword for the if statement is if-elif-else.

Note:

1. Use a colon (:) after each condition to indicate that the following statement block will be executed after the condition is met. 2. Use indentation to divide statement blocks. Statements with the same indentation number together form a statement block. There are no switch-case statements in Python.

@param: @return: "a = 1 while a < 7: if(a % 2 == 0): print(a, "one") else : print(a, "two") a +=1Copy the code

Here are the results:

Example:

@param: @return: "" if var1 = 100 if var1: Print ("1 - if = true") print (var1) var2 = 0 if var2: print ("2 - if = true") print (var2) print ("Good bye!" )Copy the code

Here are the results:

The result shows that because var2 is 0, the statement inside the corresponding if is not executed.

The following example demonstrates the calculation of dog age:

@param: @return: "age = int(input(" please input the age of your dog: ") print("") if age <= 0: print("") ) age == 1: print(" age = 14 ") ) the equivalent of a person of 22 years of age. ) elif age > 2: human = 22 + (age -2)*5 print(" ", human) ###Copy the code

Here are the results:

🙌if

The operator describe
< Less than
< = Less than or equal to

| greater than = | is greater than or equal to zero

= = | equals to compare two values are equal! = | is not equal to

"" @name: Sunny Chen @test: test font @msg: this is created by Sunny Chen. X = 5 y = 8 print(x == y) x = 8 print(x == y)Copy the code

Here are the results:

🎊demo.pyThe file demonstrates a numerical comparison operation:

"" @name: Sunny Chen @test: test font @msg: this is created by Sunny Chen. "" # Guess = -1 print(" number = 7") ) while guess ! If guess == number: print(" ") if guess == number: print(" ") Elif guess < number: print(" Elif guess > number: print )Copy the code

Here are the results:


🎇 if nested

And of course there’s nesting of ifs, for those of you who know Java. In nested if statements, you can put if… elif… The else structure is placed in another if… elif… Else structure.

If expression 1: statement if expression 2: statement ELif expression 3: statement else elif expression 4: statement else: statementCopy the code

The instance

"" @name: Sunny Chen @test: test font @msg: this is created by Sunny Chen. '' num=int(input(" ")) if num%2==0: if num%3==0: print (" ") else: Print (" num%3==0 ") else: print (" num%3== 2") else: print (" num%3== 2") else: print (" num%3== 2")Copy the code

Here are the results:


Here we go: Conditional control in Python! Share finished, go to try it!


🎉 finally

  • For more references, see here:Chen Yongjia’s blog
  • Like the little friend of the blogger can add a concern, point a like oh, continue to update hey hey!