Python environment
Python installation
1. Download the installation package
https://www.python.org/downloads/
2. Installation
Default installation path: C:\PYTHONG27
3. Configure Environment variables
"Right-click Computer"-"Properties"-"Advanced system Settings"-"Advanced"-"Environment variables"-"" the Second content box found a row of the variable named path, double-click "-" Python installation directory appended to the variable value, use; Split "
such as: the original value; C:\python27, remember that there's a semicolon in front
Getting Started with Python
One, the first Python code
Create a hello.py file
Print "Hello,world"
Second, the Interpreter
To execute a python script, you need to add an interpreter to the file header:
#!/usr/bin/env python
Print "Hello,word"
Third, Content coding
UTF-8 can be expressed in Chinese, so the switch plus:
#!/usr/bin/env python
#-*-Coding:utf-8-*-
Print "Hello,world"
Iv. notes
When line comment:# is commented content
Multiline Comment: "" "Annotated Content " ""
V. Execute script incoming parameters
Python itself has a large number of modules, development concise. The class library has three: 1. Internal; 2. Industry open source modules; 3. Programmers develop their own modules
Execution parameters
#!/usr/bin/env python
#-*-Coding:utf-8-*-
Import sys
Print SYS.ARGV
Vi.. pyc file
When you execute Python code, if you import a different. py file, a . PYc file with the same name is automatically generated during execution, which is the bytecode generated by the Python interpreter after compilation.
PS: Code is compiled to generate bytecode, and bytecode can be obtained by decompile.
Seven, variable
1. Declaring variables
#!/usr/bin/env python
#-*-Coding:utf-8-*-
Name = "Lzylogo"
Variable definition rules: 1. Can only be any combination of letters, numbers, or underscores; 2. The first character cannot be a number; 3. Keywords cannot be declared as variable names (' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' def ', ' Del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' from ', ' global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ' , ' Pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' with ', ' yield ')
2. Assigning values to variables
#!/usr/bin/env python
#-*-Coding:utf-8-*-
#名字1 = "Lzylogo"
name1 = "Lzylogo"
#名字2 = "Kitty"
name2 = "Kitty"
#!/usr/bin/env python
#-*-Coding:utf-8-*-
#名字1 = "Lzylogo"
name1 = "Lzylogo"
#名字2 = First Name 1
Name2 = name1
Eight, input
Wait for user input
#!/usr/bin/env python
#-*-Coding:utf-8-*-
# Assign user-entered content to the name variable
Name = Raw_input ("Please enter user name:")
# Print What you have entered
Print Name
Make password invisible
#!/usr/bin/env python
#-*-Coding:utf-8-*-
# import Getpass Code
Import Getpass
# Assign user-entered content to the name variable
PWD = Getpass.getpass ("Please enter password:")
# Print What you have entered
Print pwd
Ix. Process Control and indentation
1. User Login Verification
#!/usr/bin/env python
#-*-Coding:utf-8-*-
# Prompt for user name and password
Import Getpass
Name = Raw_input ("Please enter user name")
PWD = Getpass.getpass ("Please enter password")
# Verify user name and password
# If an error occurs, the output user name or password is incorrect
# If correct, just enter "Welcome, kitty"
If name = = "Lzylogo" and pwd = = "Kitty":
Print "Welcome, kitty"
Else
Print "User name and password error"
2. export Their permissions based on user input
# kitty--Super admin
# Eric--general manager
# Tony,rain---Business leader
# Other--Ordinary users
Name = Raw_input ("Please enter user name:")
if name = = "Kitty":
Print "Super admin"
elif name = = "Eric":
Print "Normal admin"
elif name = = "Tony" or name = = "Rain"
Print "Business leader"
Else
Print "Normal user"
Ten, while loop
1. Basic cycle
While condition:
# Loop Body
# If the condition is true, then the loop body executes
# If the condition is false, then the loop body does not perform
2.break for exiting all loops
#代码执行到break那行, the loop exits, and all of the following lines will no longer execute
While True:
Print "123"
Break
Print "456" (exited, will not be executed)
3.continue exits the current loop and continues the next cycle
#代码执行到continue, the current loop is exited and the next loop is executed again
While True:
Print "123"
Continue
Print "456"
Exercise 1: Using while loop input 1 2 3 4 5 6 8 9 10
Start = 1
While True:
If start = = 7:
Start + = 1
Continue
Print (start)
If start = = 10:
Break
Start + = 1
Python development: First knowledge of Python (note-taking)