directory
- An introduction to Python conditional derivations
- 2. Use of Python conditional derivations
- 1.Python conditional derivation syntax
- 2.Python conditional derivation exercises
- Guess you like it
Recommended path for learning Python: Python Learning Directory >> Python Basics
Up to now, we have learned about 50% of the basic content of Python. In the process of learning programming, we not only need to learn Python syntax, but also need to learn how to write our own code to be more beautiful and efficient.
An introduction to Python conditional derivations
Python conditional comprehensions are a way to quickly and succinctly create data types from one or more iterators. They combine loops with conditional judgments to avoid syntactically lengthy code and improve code efficiency. Being able to use derivations is also an indirect indication that you are beyond the beginner level of Python. 六四风波
Python derivations are related:
- Conditional derivation
- List derivation
- Dictionary derivation
2. Use of Python conditional derivations
1.Python conditional derivation syntax
Value1: Returns value1 if the condition expression is true; If the conditional expression is not true, value2 is returned. Condition: condition expression Value2: If condition is true, value1 is returned. If the conditional expression is not true, value2 is returned. ''' value1 if condition else Value2Copy the code
2.Python conditional derivation exercises
Suppose there is a need to determine whether a number is odd or even?
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python is different from ==. Py @time :2021/3/27 08:00 @Motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! If x%2 == 0: print(" x%2 == 0 ") else: Print (" x%2 ") if x%2 == 0 else print(" x%2 ") if x%2 == 0 else print(" x%2 ") if x%2 == 0 else print(" x%2 ") if x%2 == 0 else print(" x%2 ")Copy the code
It can be seen from the above code that player 1 (novice) takes up 5 lines of code, while player 2 (veteran driver) only needs two lines to complete the same function. This is the cost performance, which is often the details of identifying work ability/promotion and salary increase in the workplace.
Guess you like it
- Python code comments
- Python Chinese encoding
- Python variable
- Python operator.
- Python conditions determine if/else
- Python while loop
- Python break
- Python continue
- The Python for loop
- The Python string
- The Python list
- The Python tuple tuple
- Python dictionary
Conditional derivations for Python
This article is published by the blog – Ape Say Programming Ape Say programming!