First, that is non-empty true:
Python programming language specify any non-empty and non-0 (null) is true, 0 is false, or null
- Boolean, False representation False, the other is True
- Integer and floating-point numbers, 0 for False, the other is True
- String String type and class (including bytes and unicode), empty string indicates False, the other is True
- Sequence type (including tuple, list, dict, set, etc.), empty indicates False, True indicates a non-empty
- None always expressed False
Example a:
S = '' IF S: Print ( ' walking IF ' ) else : Print ( " go else " ) # go else Example II: username = INPUT ( ' username: ' ) IF username: Print ( " User name is S% " % username) # If you enter a name to go IF else : Print ( " Please enter your user name " ) # If you enter a user name to go else example III: A= 0 if a : print("go if") else: print("go else")#走else
Second, the list of formula:
python inside [] represents a list, the list can be used to quickly generate a range () function is generated.
Inside the list of data operations and operations to generate a new list of the most efficient and rapid way, that is the formula list generation.
1、range()
A sequential list of numeric type, such as [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], can be used to generate range
= b the Range (1, 11 ) Print (b) # result range (1, 11) python3 direct printing b displays range (1, 11) objects should not directly display the list, if you want to display a list, you can use list ( ) on the next Print (type (B)) # results <class 'Range'> Print (List (B)) # turn List, output is [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10]
2, a list of formula
(1). If you want to regenerate the data of the list which calculates a new list, such as [11, 2 2 33 ... 1010 ], according to the usual definition of a list of thinking is C, then one by one for loop operation , and then append a goner Add to c, c is the last of the new list
# Logarithm of the square of the list B = Range (1. 11 ) C = [] for I in B: c.append (I * I) Print (C) # Results: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
(2) Next to see a list of how to achieve the formula
# Log listing squares B = Range (1. 11 ) C = [X * X for X in B] Print (C) # Results: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
(3) a list of fixed syntax formula, [] which is in front of arithmetic operation for the inside data list, followed sequentially as usual for traversing to read. It will automatically generate a new list after running
3, if the judge with
1. If you want to filter data inside a list, for example: [1, 3, -3, 4, -2, 8, -7, 6], to find the number is greater than 0 and to be one by one for normal cycle thinking Analyzing , qualified into a new list
= C [. 1,. 3, -3,. 4, -2,. 8, -7,. 6] D = [] for I in C: IF I> 0: d.append (I) added to the list # D Print (D ) # results: [1, 3, 4, 8, 6]
2. Next you can see a list of the formula, write code more simple and elegant, fast and efficient!
# Extracted number greater than 0 C = [1,. 3, -3,. 4, -2,. 8, -7,. 6 ] D = [X for X in C IF X> 0] Print (D) # Run Results [1 , 3, 4, 8, 6]
Another example:
= result3 [STR (I +. 1) .zfill (2) for I in Range (1,10 )] Print (result3) # Ê equivalent result4 = [] for I in Range (1,10 ): J = STR (I +. 1) .zfill (2 ) result4.append (J) Print (result4) # print the results [ '01', '02', '03', '04', '05', '06', ' 07 ',' 08 ',' 09 ']
Third, a triplet of expressions
# Ternary expressions with # ID 17 bits represent gender, male odd, even-F id_card = ' 211105200103052312 ' IF int (id_card [-2])% 2 == 0: Sex = ' F ' the else : Sex = ' M ' Print (Sex) # above code is equivalent to the following code Sex = ' M ' if int (id_card [-2])% 2 == 0 the else ' M ' # # look if, if if satisfied, if compared preceding value, or if the value taken back Print (Sex)