Python basics 1. Run Python code
Use Win+r to quickly open a command prompt and enter the cmd execution Command box.
In the Command box, enter the file path that you want to run after Python to execute.
2. Content encoding
The PYTHON2 interpreter encodes the default Ascill code, which is not recognized for Chinese, so add #-*-encoding:utf-8-*-before the top.
Python3 encoding the content by default is Utf-8 so there is no need to interpret the direct encoding.
3. Notes
Make it easy for others to understand your code.
Single-line Comment: # commented content
Multi-line Comment: ' Annotated content ', or ' "' annotated Content '" "
4. Variables
Variables: The intermediate results of running the program are temporarily present in memory for subsequent code calls.
The role of a variable: a nickname, which refers to what is stored in an address in memory.
Rules for variable definitions:
1 variable names can only be letters, numbers, or underscores the first character of any combination variable name cannot be a number
2 The following keywords cannot be declared as variable names
[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']
3 The definition of a variable should be descriptive.
Assignment of variables:
There are some differences from mathematical operations, where the value to the right of the equal sign in Python is assigned to the variable to the left of the equals sign.
The value or value occupies a certain amount of memory in memory, and the equal sign indicates that the pointer points to the or value. If the variable = new value or value is defined behind it, then the pointer of the variable points to the new variable value by the original variable value.
Variables define what to note:
1 variable named Chinese, pinyin
2 Variable name too long
3 variable nouns are not expressive
5. Constants
Constants are represented in Python by any combination of uppercase alphanumeric underscores to express the amount that does not change in the operation.
such as π= 3.141592653 ...
6. Program Interaction
Program interaction is used to make the user's data input to the computer, which is the computer completes the operation.
Typical interaction types are:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 #assigning user-entered content to the name variable5Name = input ('Please enter user name:')6Age = Input ('Please enter your age:')7 8 #Print What you have entered9 Print(Name,age)
It is important to note that input content is recognized by the computer as the str (string) type, especially when the digital input is also recognized as a string!!!
7. Data type (base)
Numeric, String, Boolean
1 numbers
int (integral type) (no longer in Python3)
On a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647
On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807
2 string
In Python, the quoted characters are considered to be strings!
There is no difference between single and double quotes, but in practice you should avoid using single quotes and abbreviations mixed in English.
1 " I ' am a boy " 2 ' I 'am a boy'
The second type of computer will only recognize I is the content within the string.
MultiRow reference using ' triple quote '
1 " " 2 today I want to write a little poem, 3 sing the praises of my deskmate, 4 you see his black short hair, 5 It's like a fried chicken. 6"7print(msg)
string concatenation
Strings can only be "added" and "multiplied" operations.
1 >>> name = " alex Li " 2 >> > Age = " 22 " 3 >>> name + age # add is simply stitching 4 " Span style= "COLOR: #800000" >alex Li22 " 5 >>> Name * 5 # Multiplying is actually how many times you copy yourself, and stitch them together. 6 " alex lialex lialex lialex lialex Li "
String can only and string concatenation, and other types of stitching, will be error.
3 Boolean value
True,false two values, mainly in mind logic judgment.
You can also use a or a relationship to express true or False.
1 a=32 b=53# not set is false, that is False 4false5# Set is true, that is true 6 True
8. Process Control-conditions
Process Control via if-if, elif-or, else-otherwise these three elements are implemented.
1 if Conditions: 2 Code to execute when the condition is met
1 """2 If condition:3 meet conditional execution Code4 Else:5 if the condition is not satisfied, go this passage.6 """7Ageofoldboy = 488 9 ifAgeofoldboy > 50 :Ten Print("Too old, time to retire.") One Else: A Print("It can be a few years!")
Note that indentation if elif else will be indented 4 spaces to ensure that the content is within the process control.
There are several principles for Python indentation:
1 The top-level code must write the top line, meaning that if a line of code itself does not depend on any conditions, it must not be indented.
2 The same level of code, indentation must be consistent.
3 The official recommendation is to indent with 4 spaces.
If...else ... can have multiple branch conditions
1 ifConditions:2 meet conditional execution Code3 elifConditions:4 the above conditions are not satisfied .5 elifConditions:6 the above conditions are not satisfied .7 elifConditions:8 the above conditions are not satisfied .9 Else:TenAll the above conditions are not satisfied, go this part.
When one of these conditions is met, it does not go down.
9. Process Control-cycle
1 Basic Loops
1 while Conditions: 2 # Loop Body 3 # if the condition is true, then the loop body executes 4 # if the condition is false, then the loop body does not perform
2 Cycle termination
There are 2 types of cyclic termination
1. Break
2. Continue
Break is used to completely end a loop and jump out of the loop body to execute the statement following the loop.
Continue just terminates the loop and then executes the next loop.
Example: Break
1Count =02 whileCount <= 100:#as long as count<=100 is constantly executing the following code3 Print("Loop", Count)4 ifCount = = 5:5 Break6Count +=1#each time it is executed, the count+1 is turned into a dead loop, because count is always 0 .7 8 Print("-----out of a while loop------")
Output
1 Loop 02 loop 13 loop 24 loop 35 Loop 46 loop 57 while loop------
Example: Continue
1Count =02 whileCount <= 100 : 3Count + = 14 ifCount > 5 andCount < 95:#as long as count is between 6-94, do not go to the following print statement, go directly to the next loop5 Continue 6 Print("Loop", Count)7 8 Print("-----out of a while loop------")
Output
1 Loop 1 2 loop 2 3 loop 3 4 loop 4 5 loop 5 6 Loop 7 loop 8 Loop 9 Loop 98 Loop 101 Loop, loop, and loop ------
Python Day-1