Python If Statement
# Python2.x Python Conditional Statements\\
\\
Python conditional statements determine which block of code to execute based on the result (True or False) of one or more statements.\\
\\
You can get a simple understanding of the execution process of conditional statements through the following diagram:\\
\\
!(#)\\
\\
Python programming language specifies any non-zero and non-empty (null) value as true, and 0 or null as false.\\
\\
In Python programming, the `if` statement is used to control program execution. The basic form is:\\
\\
if Condition: Execute statements... else: Execute statements...\\
Where "Condition" (the condition) is true (non-zero), the subsequent statements are executed. The executed content can span multiple lines, distinguished by indentation to indicate the same scope.\\
\\
`else` is an optional statement. It can be executed when you want to run content when the condition is not met.\\
\\
GIF demonstration:\\
\\
!(#)\\
\\
Specific examples are as follows:\\
\\
## Example\\
\\
#!/usr/bin/python# -*- coding: UTF-8 -*-# Example 1: Basic usage of if flag = False name = 'luren'if name == 'python': # Check if the variable is 'python' flag = True# Set flag to true if condition is met print'welcome boss'# And print welcome message else: print name# Print variable name if condition is not met\\
\\
The output is:\\
\\
luren# Output result\\
The condition in an `if` statement can use `>` (greater than), `=` (greater than or equal to), `<=` (less than or equal to) to represent its relationship.\\
\\
When there are multiple values to check, you can use the following form:\\
\\
if Condition 1: Execute statements 1... elif condition 2: Execute statement2...elif Condition3: Execute statements 3... else: Execute statements 4...\\
Example:\\
\\
## Example\\
\\
#!/usr/bin/python# -*- coding: UTF-8 -*-# Example 2: Usage of elif num = 5 if num == 3: # Check the value of num print'boss'elif num == 2: print'user'elif num == 1: print'worker'elif num= 0 and num<= 10: # Check if value is between 0 and 10 print'hello'# Output: hello num = 10 if num10: # Check if value is less than 0 or greater than 10 print'hello'else: print'undefine'# Output: undefine num = 8# Check if value is between 0-5 or 10-15 if(num>= 0 and num= 10 and num` (greater than) and ` 0 ) and ( b / a > 2 ): print "yes"else : print "no"\\
However, the following code will cause an error:\\
\\
a=0 b=1if ( a > 0 ) or ( b / a > 2 ): print "yes"else : print "no"(javascript:;)Shenxian Clan\\
\\
a72***@qq.com 9 years ago (2017-05-20) \\
2. #0 Tao\\
\\
num***10@163.com [](#)68 #!/usr/bin/env python# -*- coding: UTF-8 -*-# A simple conditional loop statement to implement the Tower of Hanoi problem def my_print(args): print args def move(n, a, b, c): my_print ((a, '-->', c)) if n==1 else (move(n-1,a,c,b) or move(1,a,b,c) or move(n-1,b,a,c)) move (3, 'a', 'b', 'c')\\
> For more Tower of Hanoi problems, see: [\\
\\
(javascript:;)Tao\\
\\
num***10@163.com 9 years ago (2017-07-03) \\
3. #0 sherlockzak\\
\\
for***rpoetry@mail.com [](#)101 Python does not have a **switch/case** statement. When encountering many situations, writing many **if/else** statements is not easy to maintain. In this case, you can consider using dictionary mapping as an alternative:\\
\\
#!/usr/bin/env python# -*- coding: utf-8 -*-import os def zero(): return "zero"def one(): return "one"def two(): return "two"def num2Str(arg): switcher={ 0:zero, 1:one, 2:two, 3:lambda:"three" } func=switcher.get(arg,lambda:"nothing") return func()if __name__ == '__main__': print num2Str(0)(javascript:;)sherlockzak\\
\\
for***rpoetry@mail.com 8 years ago (2018-03-30) \\
4. #0 John\\
\\
oyw***@126.com [](#)52 Simple conditional judgment with `if` in one line:\\
\\
#!/usr/bin/python# -*- coding: UTF-8 -*- a = [1,2,3] b = a if len(a) != 0 else ""print(b) c=[] d = c if len(c) != 0 else "c is an empty list"print(d)\\
The output is:\\
\\
[1, 2, 3] c is an empty list(javascript:;)John\\
\\
oyw***@126.com 8 years ago (2018-05-22) \\
5. #0 Linlangyue\\
\\
z46***0448@gmail.com [](#)59 Move duplicate data in a list to the end and return the count of unique elements in the list:\\
\\
def deduplication(nums): # write your code here exist_nums = {} pointer = 0 for num in nums: if num not in exist_nums: exist_nums = True nums = num pointer += 1 return pointer print(deduplication([1,1,1,1,1,1,2,2,2,2,2,2,2,2]))(javascript:;)Linlangyue\\
\\
z46***0448@gmail.com 7 years ago (2019-07-13) \\
\\
YouTip