Python First day
A Why learn Python
as a Linux OPS engineer, it's getting more and more uncomfortable. No status, but also to blame, and operation and maintenance of automation development so fast, the general operation of the road is increasingly narrow (because I am very food), and several colleagues to discuss learning Python, of course, I am also very interested. No nonsense, I think you are similar to my idea.
Two Python Introduction
It is said that the birth of Python was made by a Dutchman during Christmas (1989), in order to pass the time, developed a new script interpreter, namely Python. (Are the founders so cool?) Pass time can make a language come out, worship), in recent years Python development too fast, in the Tiobe leaderboard beyond PHP occupy five, his code clear, concise, apply to all fields. Major Internet enterprises are in use, such as: Google,yahoo,youtube, know, watercress and so on.
Python type:
- Cpython
A) use of the C language, the use of more extensive, official adoption.
b) Principle: CPython Converts the source file (. py file) into a bytecode file (pcy file) and then runs on the Python virtual machine.
2. Jpython
A) Use Java implementations.
b) Principle: Jpython dynamically compiles python code into Java code, which is then run by the JVM
3. IronPython
A) Use C # implementation.
b) Ronpython compiles the code into C # bytecode, which is then run by the CLR
4. PyPy
A) Python implementation by Python
b) Compile the Python bytecode again into machine code.
。。。。。。。。。。。。。。 Wait a minute.................
The process that is specifically implemented for Python:
Four Use 2.0 or 3.0?
For this question online discussion is very enthusiastic Ah, with 2.0 despise 3.0. With 3.0 contempt for 2.0, which one do you belong to? (I look at 2.0 of the book, with 3.0 knock code, OH, just rookie start, don't think too much).
Five Programmer's first program: Hello,world
A) Use the interactive interpreter to launch Python directly, showing the program version and operating system.
Version 2.0:
Version 3.0:
b) The Linux system comes with Python, creating files:
The first Python program in life.
Note: You can use pycharm software to write programs under Windows
Six Character encoding and variables
When the Python interpreter loads the code in the. py file, the content is encoded and the default is ASCII
Version 2.0 requires coding in the script, as follows:
Version 3.0 can ignore this problem, the parser will automatically encode
A) variable naming rules:
1. Can only contain letters, numbers and underscores.
2. Letter Case Sensitivity
3. Prohibit the use of reserved words
Note: Python2 differs from Python3 's reserved words
b) Command conventions:
- Variable names starting with a single underscore (_x) are not imported by the form module import * statement
- The variable name (_x_), preceded by an underscore, is a system-defined variable name that has special meaning for the Python interpreter.
- Variable names (__x) that start with two underscores, but end without underscores are local variables of the class
- In interactive mode, the variable name "_" is used to save the result of the last expression
Note: The variable name has no type and the object has
- The following 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 ', ' whil E ', ' with ', ' yield ']
c) Variable assignment:
>>> a = 1>>> B = 2>>> a1>>> b2>>> c = "This was a string" >>> C ' this I S a string '
" Zhao " = nameprint"Hong"print(name, name2) Zhao Zhaohong Zhao
Seven User input:
user = input ("Please enter your user name:")Print("your user name is:", user) passwd=input ("Please enter your password.")Print("your password is:", passwd)#Display the password you entered, if you want to hide the password as follows:ImportGetpasspassword= Getpass.getpass ("Please enter your password")Print(password)
Eight Module
Python is composed of a series of modules, each module is a py suffix file, and the module is also a namespace, thus avoiding the problem of variable name conflict. Module we can be understood as Lib library, if you need to use a module in a function or object, you want to import this module can be used, in addition to the system default module does not need to import outside. The import directly uses the following syntax:
Import Module name (does not need to add. py suffix)
Import SYS Print (SYS.ARGV) # output # python module.py helloworld ["module.py", "Hello "," World "] # Get parameters to execute script get Import os -H") # command to invoke the system
Practice:
Write a module that can be auto-complete with tab:
Tab completion code on Linux:
#!/usr/bin/env python#python startup fileImportSYSImportReadLineImportRlcompleterImportatexitImportOS#Tab CompletionReadline.parse_and_bind ('Tab:complete')#History FileHistfile= Os.path.join (os.environ['HOME'],'. Pythonhistory')Try: Readline.read_history_file (histfile)exceptIOError:PassAtexit.register (Readline.write_history_file, Histfile)delOS, Histfile, ReadLine, Rlcompleter
Save and exit to use.
Note: Now the module is stored in the current directory, if you want to use other parts of the system, then we need to add environment variables, the general storage path is: python/2.7/site-packages
The above is what Python learned on the first day, seemingly simple but still need more practice.
After-school exercises:
1 Write a blog
2 Write a user login interface, the user password incorrectly entered three times after locking the account
3. Level three menu (geographical location, provincial and county)
Python first day