1. Comment
Comments are explanations and code reviews, posters, python interpreter does not recognize
There are two ways python Notes: (1) single-line comments # (2) '' '' '' multi-line comments triple quotes
2. User interaction
input () using the following method python3.x
name = input("what is your name?") print(name)
In python 2.x 2 kinds of methods:
(1) must be considered as input to tell what type of data input
name = input("what is your name?") print(name)
D:\python2.7\python.exe D:/py/11.py what is your name?yanglingyao # 输入交互信息 Traceback (most recent call last): File "D:/py/11.py", line 1, in <module> name = input("what is your name?") File "<string>", line 1, in <module> NameError: name 'yanglingyao' is not defined # 报错
D: \ python2.7 \ python.exe D: / Py / 11 .py the What IS your name? ' Yanglingyao ' # information statement input is a string type yanglingyao Process Finished with Exit code 0
(2)raw_input()
name = raw_input("what is your name?") print(name)
PS: Python3.x in input () and raw_input Python2.x in () method using the same entered information is automatically saved to a string
3. Output Formatting
(1)% s: you can receive any type of value
name = 'yang' age = 18 print('my name is %s,my age is %s' % (name, age))
D:\python3\python.exe D:/py/11.py my name is yang,my age is 18
(2)% d: can receive digital values
name = 'yang' age = 18 print('my name is %d,my age is %d' % (name, age))
D: \ to python3 \ python.exe D: / Py /. 11 .py Traceback (MOST Recent Last Call): File " D: /py/11.py " , Line. 3, in <Module1> Print ( ' My name IS% D, D My Age IS% ' % (name, Age)) TypeError: % D the format: A number IS required, Not STR # error,% d can receive the digital type
The correct way is as follows:
name = 'yang' age = 18 print('my name is %s,my age is %d' % (name, age)) D:\python3\python.exe D:/py/11.py my name is yang,my age is 18
= 123 A Print ( " % 08D " % A) # integer is 8, the incoming number is not eight, then by default 0 completions, exceeding 8, is how much is D: \ python3 \ python.exe D: / Py / 11 .py 00,000,123
4. Data Type
1. Plastic (int)
Role: qq number, phone number, etc.
Definition method:
a = 11
Nature:
a = int(11)
2. Float (float) is commonly known as fractional
Role: record weight. Salaries and other
Definition method:
b = 1.1
Nature:
b = float(1.1)
3. The string (str)
Role: record some descriptive information
Definition method:
C = ' I string ' # single quotes defined D = ' I' string ' " # double quotes defined, used for nesting, the present application contain a single string or double quotes E = ' '' I string you can save multiple lines ' ''
Nature:
str = A ( ' I'm a character ' )
python strings support adding and multiplying operations: adding a new string application memory and copy the new string added to the space, the efficiency is not high
a = 'yang' b = 'lingyao' c = a + b d = a * 3 print(c) print(d) D:\python3\python.exe D:/py/11.py yanglingyao yangyangyang
4. list (list),
Action: recording a plurality of values can be easily taken out of position specified by one or more of
The method defined: a plurality of values separated by commas of any type within []
= l [ ' Q ' ,. 1, 1.2, [ ' Q ' , ' W ' ]] # The value of the index, the first index 0 L1 = l [0] # remove the first element of the list l l2 = l [ 3] [1] # remove the second element of the four elements Print (L1, L2) # QW
The dictionary (dict)
Action: accessing a plurality of sets of data, there are stored data corresponding to the description
The method defined: {} are defined by the format of key elements: value, also referred to as a key-value pair, with each of the intermediate key value, wherein the key must be separated immutable type, value may be any type, such as:
= D1 { ' name ' : ' yanglingyao ' , # one key-pair ' Age ' : 18 is , ' addr ' : ' Shanxi ' , " Hobby " : [ " songs " , " tourism " ] }
Note: the dictionary is disordered, it is not only the index value ,, by a key (key) to access the value (value), such as:
Print (D1 [ ' age ' ]) # remove key is the dictionary age value Print (D1 [ ' hobby ' [. 1]]) # remove hobby dictionary key value in the second element
18
tourism
6. Boolean value (bool)
Role: represents something right or wrong, true and false, True (true) False (false)
Defined method:
t = False
t1 = True
5. Operators
1: basic operators:
"+" (Plus) "-" (minus) "*" (multiplication) "/" (addition) "//" (except the floor, i.e. the integer value of a reserved bit, regardless of the rounding) '**' (exponentiation ) "%" modulo arithmetic, i.e. take the remainder
Print (. 4 +. 5) # . 9 Print (. 5 -. 4) # . 1 Print (. 5 *. 4) # 20 is Print (. 6 /. 3) # 2.0 regardless of the operands are not floating point, floating point division is in the python2.x 3 corresponds to the floor except Print (//. 6. 4) # . 1 rounding down, rounding and other independent floor Print (2 **. 6) # 2 366 of the power Print (. 6%. 4) # 2 modulo calculation , that is, take the remainder
2. Comparison operators:
">" (Greater than), "<" (less than), "<=" (less than or equal to), "> =" (equal), "==" (determination values are equal), "! =" (Not equal) are the result is True or False
print(1 > 3) # False print(1 < 3) # True print(1 >= 3) # False print(1 <= 3) # True print(1 == 3) # False print(1 != 3) # True
On the distinction and == == is the comparison of the value of equality, is the comparison that the same memory address
. 17 = A B =. 17 Print (A == B, A IS B) # True True because the pool concept relates to a small integer, -5 to 256 are fixed to both the memory address so True C = 1000 D = 1000 Print (C D ==, C IS B) # True False
Thus: the same memory address, the same value must have the same value, not necessarily the same memory address
3. The logical operators:
"And" (with: one condition is False, compared False, conditions are True, it is True)
"Or" (or: either of the conditions is True, compared True, conditions are False, it is False)
"Not" (non-: inverted)
print(1 > 3 and 4 > 3) # False print(4 > 3 and 5 > 3) # True print(1 < 3 or 4 < 3) # True print(1 > 3 or 2 > 3) # False print(not 1 < 3) # False print(not 1 > 3) # True
4. assignment operator:
- Augmented assignment:
Age = 18 Age Tasu = 1 # equivalent Yu Age = Age Tasu 1 Print (Age) # 19 Age * = 1 # equivalent Yu Age = Age * 1 Print (Age) # 19 Age - = 1 # equivalent Yu age = age - 1 Print (Age) # 18 Age / = 1 # equivalent Yu = Age Age / 1 Print (Age) # 18.0
-
= Y = Z = X 18 is '' ' corresponds to X = 18 is Y = X Z = X ' '' Print (X, Y, Z) # 18 is 18 is 18 is
- Cross assignment:
18 is = X Y =. 19 X, Y = Y, X # syntax provided Python Print (X, Y) # 19, 18
The method can also achieve this purpose are:
x = 18 y = 19 z = x x = y y = z print (x, y)
- Decompression assignment:
= List1 [. 1, 2,. 3,. 4,. 5 ] L1, L2, L3, L4, L5 = List1 # number to the right of the equal sign with the value of a variable name must be the same number of left Print (L1, L2, L3, L4, L5) # . 1 2. 5. 4. 3
When some values do not need to use it, use the '_' (underscore) acts as a variable name
= List1 [. 1, 2,. 3,. 4,. 5 ] L1, _, _, _, L5 = List1 # digits to the right of the equal sign with the value of a variable name must be consistent with the number on the left does not need to store the extracted underline value Print (L1, L5) # l, 5
When you need to remove the head and tail do not know how many of the values of the right time with the "* _" instead
= List1 [. 1, 2,. 3,. 4,. 5 ] L1, * _, L5 = List1 # number to the right of the equal sign and the value must be consistent with the number of variable names to store value need not be left with an underscore taken Print ( L1, L5) # 1, 5 Print (* _) # 2,3,4