A python in the comments:
1.1 Single-line comments:
# After the beginning of the line code comments empty grid 2, a space after the #
eg:
Print (type (name)) # <class 'STR'> String type
1.2 multi-line comments:
With three marks '' '' '' or "" "" "Input" (three single quotes, or three double quotes) three marks may also be used in multi-line string
Two python interaction with the user:
2.1 Input:
a. python3 input as input ()
User input information acquired as a unified input string
b. In python2 1.input enter into the need for human input, and wherein raw_input tell function input is defined by what type of data is the same as raw_input and python3
eg:
>>> name=input('name:') name:zhang Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'zhang' is not defined >>> name=input('name:') name:'zhang' >>> type(name) <type 'str'>
2.2. Formatted output
% S placeholder arbitrary data
% D numeric placeholder placeholder can only be used with the digital
print ( '% 04d'% 123) wherein the number of outputs 0123 of the number of bits occupied by the variable 04 representing the output data is below standard, once the number of bits exceeds the zero-padded data output will faithfully placeholder must be consistent
A print per line input comes wrap
eg:
print('my name is %s,my age is %s %(name,age))
Three data types
What is the data: a measure or record the state of things or features
What is Type: There are different types of different data to record
3.1: Integer (int)
Plastic is used to measure or record a person's age, a bunch of qq number, a phone number and other string of pure digital shaping corresponding numbers. Long int and shaping into shaped long in the python2 python3 but not distinguished.
>>> a=1111111111 >>> type(a) <type 'int'> >>> b=111111111111111111111111111111111111111111111111111111111111111111111111111111 >>> type(b) <type 'long'> >>> exit() C:\Users\何晓东>python Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a=11111111111111111111111111111111111111111111111111111111111111111111111111111111111 >>> type(a) <class 'int'>
3.2: Float (float)
Float is used to measure a person's weight, height, salary, etc. (3.1). It is the decimal.
>>> a=3.1 >>> type(a) <class 'float'>
3.3: a plurality (complx):
>>> xushu=1+2j >>> type(xushu) <class 'complex'> >>> xushu.real # 实部 1.0 >>> xushu.imag # 虚部 2.0
3.4: string (str)
Strings are used to keep the descriptive information such as a person's personal profile and the like. In quotes is called a string in python.
3.4.1 Single-quoted strings
>>> a='字符串1' >>> type(a) <class 'str'>
3.4.2 double-quoted strings
B = >>> " I string " >>> type (B) < class ' STR ' >
3.4.3 three quoted strings (lines of information may be used to describe multiple)
= S3 '' ' I is a string and a plurality of rows can be written ' '' S4 = "" " I is a string and a plurality of rows can be written " "" Print (type (S3)) Print (type (S4))
ps:. 1 triple quotes can also be used as comments, which difference is triple quotes made the comment when opening quote is not defined variables
2. Three quotes must not be used in the string during use, it can not be used behind the front double quotes single quotes
3.5: List (list)
List is enclosed in brackets [] enclosed, internal data separated by a comma, the internal data may be any data type
List is used to store a plurality of data and facilitate removal of one or more of them.
info=[ ['zhang',23,['play','game','yunger sister']], ['wang',24,['play','sleep','woman hanzi']] ] I1 = info[0] I2 = I1[2] res = I2[2] print(res) print(I1) print(I2) print(info[0][2][2])
3.6: Dictionary (dict)
Dictionary that can store a plurality of data, each data has a corresponding description. Which has a format in which various data braces format key: value (key-value pairs), wherein the data key information is described generally string, value information corresponding to the data is described, it may be of any type. Each set of data separated by commas.
student_info={ 'name':'zhang', 'age':23, 'sex':'male', 'school_info':{ 'school_name':'oldboy', 'student_count':200, 'addr':'SH' } } d1 = student_info['school_info'] print(d1) res = d1['addr'] print(res) print(student_info['school_info']['addr'])
ps:. 1 dictionary provides us with the only key, value can not be obtained directly
2. A dictionary is unordered
3.7: bool (bool)
Boolean value is used to determine the feasibility identify things (right or wrong) of. bool value is 2 for True (on) and False (error) in python.
print(1>2) print(1<2)
IV. Operators
4.1 Digital operator
The following variables assuming a = 10, b = 20
Addition: + Add - two objects are added a + b the output results 30
Subtraction: - minus - minus the other a number of result output 10 ba
Multiplication: * Multiplication - two objects a * b multiplication results output 200
Division: / inter - two objects division result a / b is outputted 0.5
Remainder:% Remainder - Returns the remainder of the division remainder is a% b 10
Take divisible: // Take divisible - returns the quotient of the integer part of the output is 4 9 // 2
Power: ** - a return to a b-th output is a ** b b
4.2 Assignment Operators
The following variables assuming a = 10, b = 20
= Simple assignment operator will C = a + b a + b is assigned to the operation result c (30)
+ = Addition complex assignment operator is c + = a c = c + a
- = subtraction assignment operator is c- = a c = ca
* = Multiplication assignment operator is c * = a c = c * a
/ = Division assignment operator c / = a is c = c / a
% = Remainder assignment operator is c% = a c = c% a
// List taken assignment operator = c // = a c = c // a is
** = power assignment operator is c ** = a C = C A
4.3 Comparison of operation
The following variables assuming a = 10, b = 20
== equal - equality comparison output is False a == b
! = (<>) Is not equal to - if the comparison of two numbers not want to wait for the output of a = b True!
Similarly there are>, <,> =, <=
4.4 Logical Operators
and with the conditions and both sides must be true, the result returned is True. If there is not a true return is False
OR, or both sides, or as long as a condition is true, the result is returned to True. If both sides are false, the result is returned as False
After not negated (NOT), if the object is determined prior to True, False is the inverse
V. Cross assignment
x = 1 and = 2 c = x x = and and = c print (x, y) x = 1 and = 2 x, y = y, x print (x, y)
VI. Chain assignment
x = 100 y = 100 z = 100 x = y = z = 100
VII. Decompression assignment
= X [1,2,3,4,5,6 ] A, B, C, D, E, F = X # number of preceding elements in the list must be the same number of elements a, b, _, _, _ , X = F # number of intermediate elements omitted to underline how much instead of A, * _, X = F # if excessive omitted _ * can be used instead of Print (A, B, C, D, E, F) Print (A, B, F) Print (A, F)