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

  1. Python code comments
  2. Python Chinese encoding
  3. Python variable
  4. Python operator.
  5. Python conditions determine if/else
  6. Python while loop
  7. Python break
  8. Python continue
  9. The Python for loop
  10. The Python string
  11. The Python list
  12. The Python tuple tuple
  13. Python dictionary

Conditional derivations for Python

This article is published by the blog – Ape Say Programming Ape Say programming!