Python 3.6
I. coding problems
- The part that involves Chinese requires UTF-8 encoding. Use PyCharm: File-> Settings-> Editor-> File Encoding to select UTF-8 Encoding in Global Encoding and Project encoding and Default Encoding for properties file.
- Add #-*-coding: UTF-8-*-at the beginning to specify the UTF-8 encoding.
Ii. Basic Knowledge 2.1 identifier
In Python, an identifier consists of letters, numbers, and underscores. In Python, all identifiers can contain English letters, numbers, and underscores (_), but cannot start with a number. The identifiers in Python are case sensitive.
2.2 Script Programming
- PyCharm directly writes scripts
- Command Line input code
print("Hello, Python!");
- You can write a. py file and run it using the command line.
python HelloWorld.py
2.3 reserved characters
All Python keywords only contain lowercase letters
and exec notassert finally orbreak for passclass from printcontinue global raisedef if returndel import tryelif in whileelse is withexcept lambda yield
2.4 rows and indentation
The biggest difference between Python and other languages is that the Python code block does not use braces {} to control classes, functions, and other logic judgments. Python uses indentation to write modules. The number of white spaces for indentation is variable, but all code block statements must contain the same number of white spaces for indentation, which must be strictly executed. This error is caused by shrinking: IndentationError: unexpected indent
2.5 multi-line statements
In a Python statement, a new line is generally used as the statement Terminator. However, you can use a slash (\) to split a line of statements into multiple lines for display. If the statement contains [], {}, or () brackets, you do not need to use multiline connectors.
2.6 Use of quotation marks
Python can use quotation marks ('), double quotation marks ("), three quotation marks (''' or") to represent strings. the start and end of quotation marks must be of the same type. The three quotation marks can be composed of multiple lines. They are used to compile the quick Syntax of multi-line text. They are often used as comments in the specific location of a file.
2.7 annotation syntax
Single line comment: Add # At the beginning to comment multiple lines comment:
''' This is a multi-line comment, which uses single quotation marks. This is a multi-line comment, using single quotes. This is a multi-line comment, using single quotes. '''
2.8 empty line syntax
Empty lines are part of the program code, to facilitate code maintenance and structure.
2.9 Input and Output
username = input("username:")print(username)
Multi-line formatting output:
info = '''------------info %s -----------name:%s age:%sjob:%s''' % (name,name,age,job)info2 = '''------------info {name} -----------name:{name} age:{age}job:{job}'''.format(name=name, age=age, job=job)Iii. Variable type 3.1 basic type
Counter = 100 # assign a value to the integer variable miles = 1000.0 # Floating Point name = "John" # string a, B, c = 100,100 0.0, "John" # multi-variable assignment
3.2 numeric type
The numeric data type is used to store numeric values. They are unchangeable data types, which means that changing the numeric data type will allocate a new object. Python supports four different numeric types: int (signed integer) long (long integer [can also represent octal and hexadecimal]) float (float) complex (plural) use complex (a, B)
You can use del to delete object references.
del vardel var_a, var_b
3.3 string
S = "hello world" # Or s = 'Hello world' print (s) # output full string print (s []) # print (s * 2) of the third to fifth output strings # print (s + "TEST") of the output string twice # output the connected string
The plus sign (+) is a String concatenation operator, and the asterisk (*) is a repeated operation.
3.4 List
List = ['runob', 786, 2.23, 'john', 70.2] tinylist = [123, 'john'] print (list) # print (list [0]) of the output complete list # print (list []) of the first element in the output list # output print (list [2:]) for the second to third elements # print (tinylist * 2) for all elements from the third element to the end of the list) # print the output list twice (list + tinylist) # print the combined list
3.5 tuples
Tuple = ('runoob', 786, 2.23, 'john', 70.2) tinytuple = (123, 'john') print (tuple) # print (tuple [0]) # print (tuple []), the first element of the output tuples # output print (tuple [2:]) for the second to third elements # print (tinytuple * 2) for all elements from the third to the end of the list) # print Output tuples twice (tuple + tinytuple) # print the combination of tuples3.6 dictionary
Dict = {} dict ['one'] = "This is one" dict [2] = "This is two" tinydict = {'name': 'john ', 'code': 6734, 'dept': 'sales'} print (dict ['one']) # print (dict [2]) of the output key 'one' # print (tinydict) # output the complete dictionary print (tinydict. keys () # print (tinydict. values () # output all values3.7 data type conversion
| Function |
Description |
| Int (x [, base]) |
Converts x to an integer. |
| Long (x [, base]) |
Converts x to a long integer. |
| Float (x) |
Convert x to a floating point number. |
| Complex (real [, imag]) |
Create a plural number |
| Str (x) |
Convert object x to a string |
| Repr (x) |
Convert object x to an expression string |
| Eval (str) |
Used to calculate a valid Python expression in a string and return an object |
| Tuple (s) |
Converts the sequence s into a single tuples. |
| List (s) |
Converts sequence s to a list. |
| Set (s) |
Convert to a variable set |
| Dict (d) |
Create a dictionary. D must be a sequence (key, value) tuples. |
| Frozenset (s) |
Convert to an unchangeable set |
| Chr (x) |
Converts an integer to a character. |
| Unichr (x) |
Converts an integer to a Unicode character. |
| Ord (x) |
Converts a character to an integer. |
| Hex (x) |
Converts an integer to a hexadecimal string. |
| Oct (x) |
Converts an integer to an octal string. |
Iv. Operators
Operators in Python: Arithmetic Operators, comparison (relational) operators, value assignment operators, logical operators, bitwise operators, member operators, identity operators, and operator priorities.
4.1 Arithmetic Operators
+,-, *,/, % ,*(Power,* B, B power of a), // () operator, 10 // 4, returns 2)
4.2 comparison Operators
= ,! =, <>,>, <, >=, <=
4.3 value assignment operator
=, + =,-=, * =,/=, % =, ** =, // =
4.4-bit operator
&, |, ^ (Bitwise XOR or operator ,~ (Bitwise inverse operator), <,>
4.5 logical operators
And, or, not
4.6 member operators
In (if the value is found in the specified sequence, True is returned; otherwise, False is returned .), Not in (True is returned if no value is found in the specified sequence; otherwise, False is returned .)
4.7 identity Operators
Is (is used to determine whether two identifiers reference an object), is not (is not used to determine whether two identifiers reference different objects)
4.8 operator priority
The operation order is determined by the highest priority.
| Operator |
Description |
| ** |
Index (highest priority) |
| ~ +- |
Flip by bit, one-dollar plus sign and minus sign (the last two methods are named + @ and -@) |
| */% // |
Multiplication, division, modulo and Division |
| +- |
Addition and subtraction |
| >>< < |
Right Shift, Left Shift Operator |
| & |
AND' |
| ^ | |
Bitwise operators |
| <=<>>= |
Comparison Operators |
| <>==! = |
Equal to operator |
| = % =/= // =-= + = * = ** = |
Value assignment operator |
| Is, is not |
Identity Operators |
| In, not in |
Member Operators |
| Not, or and |
Logical operators |