One, the first Python code
Create the hello.py file under the/home/dev/directory, as follows:
1 [[email protected] scripts]#2#!/usr/bin/env python3 4print("Hello world! ")
Output Result:
1 [[email protected] scripts]#2 Hello world!
Second, the Interpreter
When executing python/home/dev/hello.py in the previous step, it is clear that the hello.py script is executed by the Python interpreter.
If you want to execute a python script like executing a shell script, for example, you ./hello.py
would need to specify the interpreter at the head of the hello.py file, as follows:
# !/usr/bin/env python Print " Hello,world "
So, execute:. /hello.py
Can.
PS: Need to give hello.py execution permission before execution, chmod 755 hello.py otherwise will error!
Third, Content coding
The Python interpreter encodes the content when it loads the code in the. py file (default Ascill)
ASCII (American Standard Code for Information interchange, United States Standards Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, which can be used up to 8 Bit to represent (one byte), that is: 2**8 = 256, so the ASCII code can only represent a maximum of 256 symbols.
It is clear that the ASCII code cannot represent all the words and symbols in the world, so it is necessary to create a new encoding that can represent all the characters and symbols, namely: Unicode
Unicode (Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more
UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ...
Therefore, when the Python interpreter loads the code in the. py file, it encodes the content (the default ascill), if it is the following code:
Error: ASCII code cannot be expressed in Chinese
1 # !/usr/bin/env python 2 3 Print " Hello, World "
Correction: What should be shown tells the Python interpreter what code to use to execute the source code, i.e.
1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3 4 Print " Hello, World "
Iv. notes
When the line stares: # is annotated content
Multiline Comment: "" "Annotated Content" ""
V. Execute script incoming parameters
Python has a large number of modules, which makes developing Python programs very concise. The class library includes three:
- Python-supplied modules
- Industry-Open Source modules
- Modules developed by programmers themselves
Python internally provides a SYS module where the SYS.ARGV is used to capture the parameters passed in when executing a python script.
1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3 4 Import SYS 5 6 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 after the Python interpreter was compiled.
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
=
"nulige"
The code above declares a variable named: Name, and the value of the variable name is: "Nulige"
The role of a variable: a nickname that refers to what is stored in an address in memory
Rules for variable definitions:
- Variable names can only be any combination of letters, numbers, or underscores
- The first character of a variable name cannot be a number
- 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 ']
2. Assigning values to variables
1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3 4 " Nulige " 5 " Alex "
1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3 4 " Nulige " 5 name2 = name1
八、输入
1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3 4 # assigning user-entered content to the name variable 5 name = Raw_input (" Please enter user name:")6 7 # Print the input content 8 Print Name
When entering a password, if you want to be invisible, you need to take advantage of the Getpass method in the Getpass module, namely:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 ImportGetpass5 6 #assigning user-entered content to the name variable7PWD = Getpass.getpass ("Please enter your password:")8 9 #Print What you have enteredTen PrintPwd
Ix. Process Control and indentation
Requirement One, user login verification
1 #!/usr/bin/env python2 #-*-coding:encoding-*-3 4 #prompt to enter user name and password5 6 #Verify user name and password7 #if error, the output user name or password is incorrect8 #If successful, the output is welcome, xxx!9 Ten One ImportGetpass A - -Name = Raw_input ('Please enter user name:') thePWD = Getpass.getpass ('Please enter your password:') - - ifName = ="Alex" andPWD = ="cmd": - Print "Welcome, alex!. " + Else: - Print "incorrect user name and password"
Demand two, according to the user input content output its permissions
1 #print their permissions based on what the user has entered2 3 #Alex--Super Admin4 #Eric--General Administrator5 #Tony,rain--- business executives6 #other--Ordinary users7 8Name =Raw_input('Please enter user name:')9 Ten ifName = ="Alex": One Print "Super Admin" A elifName = ="Eric": - Print "General Administrator" - elifName = ="Tony" orName = ="Rain": the Print "Business Supervisor" - Else: - Print "Normal User"
Ten, while loop
1. Basic cycle
1 while Conditions: 2 3 # Loop Body 4 5 # if the condition is true, then the loop body executes 6 # if the condition is false, then the loop body does not perform
2. Break
Break to exit all loops
1 while True: 2 Print " 123 " 3 Break 4 Print " 456 "
Exercises
1. Use while loop input 1 2 3 4 5 6 8 9 10
2. For all numbers of 1-100
3. All odd numbers in output 1-100
4. All even numbers in output 1-100
5, Beg 1-2+3-4+5 ... 99 of all numbers of the and
6. User Login (three chance retry)
Getting started with Day1-python -01