Input and output
1) Input
Use input ("prompt information input")
2) Output
print ("output content")
print ("% s% d"% ("Output is", 99))
type of data
1) There are six types of python data types
- Number
- String
- List
- Tuple (tuple)
- Set
- Dictionary
2) Data type initialization
Python will automatically identify the data type and initialize when data is assigned
- Number num = 0
- String str = "asd"
- List list = [1,2,3]
- Tuple(元组) tup = (1,2,2) #tup = (1,)
- Set (set) set = {1,2,2,3} #equivalent to {1,2,3}
- Dictionary(字典) dic = {"num1":"ren1","num2":"ren2","num3":"ren3"}
3) Methods of commonly used data types
List
Range (5) # Create a list of less than 5 [0,1,2,3,4] for I in Range (5 ): Pass
Tuple
= TUP (1,2,2 )
T1, T2, T3 = TUP # tuple access
def c (* x): # pass what to define what print (* x) x = (1,2,3 ) c ( * x) # * x is equal to break up tuples, then pass the parameter 1 2 3 def c ( the X-*): # pass What is the definition of what Print (the X-) c ( * the X-) ( , 2, 3)
Dictionary
# Dictionary traversal two ways # for I, V in av_catalog [ 'Western'] .items (): # print key value and key # Print (I, V) for I in av_catalog: Print (I, av_catalog [ i])
4) Variable type and immutable type
- Immutable data (3): Number (number), String (string), Tuple (tuple)
- Variable data (3): List (List), Dictionary (Dictionary), Set (Collection)
Control statement
1)for
Statement format
for i in [1,2,3 ]: pass for i in range (3 ): pass # range is a list generator
2)while
n = 1 while n < 11: if n == 7: pass else: print(n) n = n+1
3)break和continue
continue means to jump out of the current loop and no longer execute the following code
break means to jump out of the entire loop
4) if statement
format
if condition: pass elif : pass else : pass
5) Python's ternary operator
format
When the result of true if the determination conditions else is false results
example
1 if 5> 3 else 0 output 1, if 5 is greater than 3, otherwise output 0
Use in a list with for
= the conf (11,2,3 ) [I for I in the conf IF I> 2 ] >>> [. 11,. 3 ] # obtain a list of the number of qualifying
= the conf (11,2,3 ) [I for I, C in the enumerate (the conf) IF C> 2 ] >>> [0, 2 ] # obtain a qualified list index number # I, in the enumerate C (conf) Can get subscript and value at the same time