Interact with the user:
Input:
python2: input一定要声明你输入的类型 >>> input(">>:") >>:sean Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'sean' is not defined >>> input(">>:") >>:"sean" 'sean' >>> input(">>:") >>:1 1 >>> input(">>:") >>:[1,2] [1, 2] >>> ------------------------------- >>> raw_input(">>:") >>:sean 'sean' >>> raw_input(">>:") >>:12 '12'
python3: name = the INPUT ( " Please enter your name: " ) # receiving input from users, regardless of what type of user input, the final return must be a string Print (name) Print (of the type (name))
the Result:
SEAN
<class 'str'>
Summary: raw_input and the same input python3 role in python2
Basic data types:
Data: description, measure the state of the data
Type: different things require different types of storage
int
python2:
In this interval [-24xxxxxxxx, 24xxxxxxxx]: int
If not in this range: long
python3:int
float
# The f2 converted to an integer, float switch must then transferred shaping f2 = ' 1.9 ' f2 = a float (f2) f2 = int (f2)
complex (plural)
>>> a = 1-2j >>> a (1-2j) >>> type(a) <class 'complex'> >>> a.real #实部 1.0 >>> a.imag #虚部 -2.0
str
python2: 8 bit bit sequence is the nature str
python3: unicode sequence is the essence str
= A ' the Hello ' B = ' World ' Print (A + B) Result: the HelloWorld # string concatenation, is to open up a new memory space , the value after splicing deposit into
list
= Y [ ' elephant ' , ' beautiful ' , [ ' Read ' , ' Study ' ]] Print (Y [0]) # print a list of the first element Print (Y [2] [0]) # printing of the third a list of elements, the first element read
dict:
Dictionary type #
# define methods: by braces store data by key: value for this mapping relationship defined key,
# key-value pairs each separated by a comma
D2 = { ' name ' : ' Tank ' , ' Age ' : 73 is, " Hobby " : [ " basketball " , " sister " ]} Print (D2 [ ' name ' ]) # print name corresponding to the value of Tank Print ( D2 [ ' hobby ' ] [0]) # print hobby corresponding to a first list element basketball
bool
Boolean #
# is mainly used to determine right and wrong things
# generally does not define a separate boolean
"" " When the value equal to, not necessarily equal id id case of equality, the value is equal to the constant " "" Tag = True # Tag = BOOL (True) Tag1 = False # Tag = BOOL (False) Print (. 1 <2 ) a =. 1 B =. 1 Print (a == B) # equals the comparison value Print (a iS B) # iS comparison is id (address)
Formatted output:
Longhand
print("my name is",name,'my age is',age)
1,% s,% d placeholder:
% s: you can receive any type of variable
% d: receiving a digital only type
according to the delivery order, traditional values
Print ( " My name IS% S " % name) Print ( " My name IS% S,% S IS My Age " % (name, Age)) # If two or more parameters, enclosed in braces I = 1,232,314,241,412 Print ( "% .2f" I%) # 2 decimal places
Print ( "% 0.2f"% I) # 2 decimal places
2、.format
print("my name is {} ,my age is {}".format(age,name)) print("my name is {name} ,my age is {age}".format(age=age,name=name))
. 3, F-String # python3.6 the characteristic
print(f"my name is {name} ,my age is {age}")
Operator:
Arithmetic operators:
# Arithmetic operators A =. 9 B = 2 Print (A // B) # rounding. Print (A% B) # modulo Print (A ** B) # Power ^ 2 9 # Comparison operators Print (A == B) # left and right sides of the determined value of the operator are equal Print (A! = B) # determination value left sides of the operator are not equal Print (a> B) Print (a> = B) Print (a < B) Print (a <= B) # assignment operator # incremental assignment a. 1 = a + = 1 # . 1 = A + A Print (A) A - =. 1 Print (A) A * = 2 Print (A) A / = 2 Print (A) # chain assignment X. 1 = Y =. 1 Z =. 1 X = Y = Z =. 1 Print (X, Y, Z) # crossover assignment A, b = b, A # A and b swap value Print (F " A: {A}, b: {b} " ) # decompression assignment l1 = [ 1,2,3,4,5,6 ] A = L1 [0] B = L1 [. 1 ] C = L1 [2] D = L1 [. 3 ] Print (A, B, C, D) A, B, C, D, * _ = L1 # * _ may receive an overflow element Print (A, B, C, D) # . 1, 2,3,4 * _, A, B, C, D = L1 Print (A, B, C, D) # 3,4,5,6
logic operation:
And non-#
# and or not
. 1 = A B = 2 C =. 3 Print (A <B and B> C) # and: if there is a formula does not meet the conditions, whole expression are False Print (A <B or B <C) # or : as long as there is a qualified expression, whole expression are True Print (Not a <B) # negated Print (a <B and B <C or a> C) # True # first determines (a <b) and (b <c or a> c ) and Print (A> B or B <C and A> C) # False