Python is an easy-to-learn and powerful programming language. It has an efficient high-level data structure and is able to do object-oriented programming in a simple and efficient manner. Python's elegant syntax and dynamic type, combined with its explanatory nature, make it an ideal language for scripting or developing applications in many areas of most platforms.
I. Data type
Python3 supports three different types including: shaping (int), float (float), complex number (complex).
1. Shaping:
m=233print m
Output M is 233
2. Float Type:
m=3.3*102print m
Output M is 336.59999999999997
3. Plural: The complex number is composed of real and imaginary parts, which can be represented by A+BJ or complex (a, b), and the real and imaginary parts of the complex numbers are floating point types.
Because the plural period that stage uses with less contact so does not do the specific introduction, has the interest to be possible to consult the relevant information.
4. Data type conversion: For data built-in type conversions, you only need to use the data type as a function name.
int (x) converts X into shaping
print int (22.3)
The output is 22.
Float (x) converts x to floating-point
Print float (223)
The output is 223.0.
Data type conversions can also be nested using such as:
print float (int (233.3))
The output will also be 233.0.
Complex (x) converts x into a complex real part of X, and the imaginary part is divided into 0
The complex (x, y) converts the Xs and y to a complex number, the real part is x, the imaginary part is divided into y,x and y is the expression of the number.
5. Constants:
There are two more common constants in Python, Pi and E.
PI: Digital constant Pi (π)
E: Book-order constant E, which is the natural logarithm.
Two. Variables and keywords
The same variable can be repeatedly assigned, and can be different types of variables , enter as follows:
a=123print aa='ABC'print A
If the former output of A is 123, the latter also output ABC, the latter does not overwrite the former value.
When you cannot determine the data or variable type, you can use the interpreter built-in function type for confirmation, such as:
Print ('Hello word! ' ) a=123print aa=1.23print A
The compiler sequentially outputs <class ' str ' >,<class ' int ' >,<class float ' >
Variable name:
Python has 33 keywords, but it can't be used as a variable name, so you can check your books yourself.
Three. Operators and manipulating objects
Python supports the following seven types of operators
1. Arithmetic operator: + (plus), * (minus), * (multiply),/(except),% (remainder: return division), * * (Power: 10**5 meaning 10 of 5 times, output 100000),//(divisible: Returns the integer portion of the quotient)
2. Comparison operator: = = (equals),! = (not equal to),> (greater than),< (less than), >= (greater than or equal), <= (less than equals) The return value of the comparison operator is true or false
3. Assignment Operator: = (simple assignment operator), + = (addition assignment operator),-= (subtraction assignment operator), *= (multiplication assignment operator),/= (Division assignment operator),%= (modulo assignment operator),//= (divisible assignment operator)
4. The bitwise operator calculates the number as a binary.
&: Bitwise AND operator, |: Bitwise OR OPERATOR, ^: bitwise XOR operator, ~: Bitwise negation operator, <<: Left move operator, >>: Right Move operator
A=60b=13C=0c=a&bPrintC# Ac=a|bPrintC# Ac=a^bPrintC# thec=~aPrintC#-61C=a<<2PrintC# -C=a>>2PrintC# the
5. Logical operators: and (with), or (or), not (non)
a=10b=20print and b #printor b # print not a #falseprintnot b #falseprintnot -1 #false
6. Member operator: In (within the specified sequence), not in (no specified sequence)
a=10b=5list=[1,2,3,4,5]print in list) #false Print not in list) #false
7. Identity operator: is (to determine whether two identifiers refer to from an object), is not (to determine whether two identifiers refer to from different objects)
a=10b =10print (a is b) # true print (a is not b) # false b=20print (a is b) # Span style= "COLOR: #008000" >false print (a is not b) # true
8. Bitwise operator precedence (high to Low): * *, (~,+,-), (*,/,%,//), (+,-), (>>,<<), (&), (^,|), (<=,<,>,>=), (< >,==,! =), (=,%=,/=,//=,+=,-=,*=,**=), (Is,is not), (In,not in), (Not,or,and)
Four. String manipulation
We can use quotation marks (' or ' to create a string), and usually the string cannot be mathematically manipulated even if it looks like a number.
Concatenation of strings:
1. Connect directly by using ' + ':
A='hello'b=' word'c='! ' Print (A+B+C)
The output is: Hello word!
2.join method
a=['hello','word','! ' ]b='print b.join (a)
The output is also: Hello word!
3. Replacement method
Print ('name:%s,age:%d,sex:%s'% (angle,12, female))
Output is: Name:angle,age:12,sex: Female
4. Single quotation mark ("'") or double quotation mark ("" "" "")
Name=' angle 'age =12Sex=' female ' message='name:%sage:%dsex:%s' (name,age,sex)print message
The result of the output is also: Name:angle age:12 sex: Female
Five. Notes
Multiline comment with ("" or "" ") Single-line Comment (# (shortcut key ctrl+/))
Self-hing Artificial intelligence-------------Python Primer Basics (1)