Python basics: a first-time introduction to Python
Python Introduction
The founder of python is Guido van rosum ). During the Christmas period of 1989, Guido fansoum was determined to develop a new script interpreter to pass the time in Amsterdam as an inheritance of the ABC language.
Comparison between Python and other languages:
C, Python, Java, and C #
C language: the code is compiled to obtain the machine code. The machine code is directly executed on the processor, and each command controls CPU work.
Other languages: the code is compiled to get the bytecode. The virtual machine executes the bytecode and converts it to the machine code, and then runs it on the processor.
Python and C Python are developed by C.
For usage: Python class libraries are complete and simple to use. To implement the same function, Python 10 lines of code can solve the problem, and C may need 100 lines or more.
For speed: Compared with C, Python runs slowly.
Python, Java, and C #
For use: the original Linux Python is not available in other languages; The above languages have very rich class library support
For speed: Python may be slightly inferior in speed
Therefore, there is no essential difference between Python and other languages. The other difference is that Python is good at a certain field, rich talents, and preemptible.
Python type
• Cpython
Python official version, which is implemented in C language and most widely used. CPython converts the source file (py file) into a bytecode file (pyc file) and runs on a Python virtual machine.
• Jyhton
For Java Implementation of Python, Jython dynamically compiles Python code into Java bytecode and runs it on JVM.
• IronPython
IronPython compiles Python code into C # bytecode and runs it on the CLR. (Similar to Jython)
• PyPy (special)
Python implements Python, and then encodes Python bytecode into machine code.
• RubyPython, Brython...
Install Python
Windows:
1. Download the installation package
Https://www.python.org/downloads/
2. Installation
Default installation path: C: \ python27
3. Configure Environment Variables
Right-click the computer and choose Properties> advanced system Settings> advanced environment variables> Path in the second content box. one row, double-click] --> [append the Python installation directory to the variable value and use; Separate]
For example: the original value; C: \ python27; remember that there is a semicolon before
Linux:
No installation required, original Python Environment
Ps: if it comes with 2.6, update it to 2.7.
Update Python
Windows:
Uninstall and reinstall
Linux:
View the default Python version
Python-V
1. Install gcc for compiling Python source code
Yum install gcc
2, download the source package, https://www.python.org/ftp/python/
3. decompress and enter the source code file.
4. Compile and install
./Configure
Make all
Make install
5. view the version
/Usr/local/bin/python2.7-V
6. Modify the default Python version.
Mv/usr/bin/python/usr/bin/python2.6
Ln-s/usr/local/bin/python2.7/usr/bin/python
7. Prevent yum execution exceptions and modify the Python version used by yum.
Vi/usr/bin/yum
Set the header #! /Usr/bin/python #! /Usr/bin/python2.6
Getting started with Python
I. Interpreter
If you want to execute a python script like a shell script, for example,./hello. py, You need to specify the interpreter in the header of the hello. py file, as shown below:
#!/usr/bin/env python print "hello,world"
Ps: You must grant the "hello. py" execution permission before execution. chmod 755 hello. py
Ii. Content Encoding
When the python interpreter loads the code in the. py file, it will encode the content (ascill by default)
ASCII (American Standard Code for Information Interchange) is a computer coding system based on Latin letters. It is mainly used to display modern English and other Western European languages, it can only be expressed in 8 bits (one byte) at most, that is, 2 ** 8 = 256. Therefore, the ASCII code can only represent 256 symbols at most.
Obviously, ASCII codes cannot represent all types of characters and symbols in the world. Therefore, you need to create a new encoding that can represent all characters and symbols, that is, Unicode.
Unicode (unified code, universal code, Single Code) is a character encoding used on a computer. Unicode is generated to address the limitations of traditional character encoding schemes. It sets a uniform and unique binary encoding for each character in each language, it is specified that some characters and symbols are represented by at least 16 bits (2 bytes), that is, 2*16 = 65536,
Note: here we will talk about at least 2 bytes, maybe more
UTF-8 is the compression and optimization of Unicode encoding, which no longer uses at least 2 bytes, but classifies all characters and symbols: the content in the ascii code is saved in 1 byte, the European characters are saved in 2 bytes, and the East Asian characters are saved in 3 bytes...
Therefore, when the python interpreter loads the code in the. py file, it will encode the content (ascill by default), if it is the following code:
Error: ascii code cannot indicate Chinese Characters
#! /Usr/bin/env python print "Hello, world"
Correct: the following code should be displayed to the python Interpreter:
#! /Usr/bin/env python #-*-coding: UTF-8-*-print "Hello, world"
3. Notes
When watching: # commented content
Multi-line comment: "commented content """
Iv. input parameters by executing the script
Python has a large number of modules, making it very simple to develop Python programs. There are three class libraries:
• Python internal modules
• Open-source modules in the industry
• Modules developed by programmers themselves
Python provides a sys module, where sys. argv is used to capture the parameters passed in when the python script is executed.
#!/usr/bin/env python# -*- coding: utf-8 -*- import sys print sys.argv
V. pyc File
When you run the Python code. py file, a file with the same name is automatically generated during execution. pyc file, which is the bytecode generated after the Python interpreter is compiled.
Ps: After the code is compiled, it can generate bytecode. After the bytecode is decompiled, the code can also be obtained.
Vi. Variables
1. Declare Variables
#!/usr/bin/env python# -*- coding: utf-8 -*- name = "zhangyanlin"
The code above declares a variable named: name. The value of the variable name is: "zhangyanlin"
Rule for variable definition:
The variable name can only be the first character of any combination of letters, numbers, or underscores. It cannot be a number or a keyword. It cannot be declared as a variable name.
['And', 'as', 'assert ', 'Break', 'class', 'contine', 'def', 'del ', 'elif ', 'else', 'Got t', 'exec ', 'Finally', 'for ', 'from', 'global', 'if', 'import', 'in ', 'Is ', 'lambda', 'not ',' or ', 'pass', 'print', 'raise ', 'Return', 'try', 'while ', 'with', 'yield ']
VII. Input
#! /Usr/bin/env python #-*-coding: UTF-8-*-# assign the user input content to the name variable name = raw_input ("Enter the User name :") # print the input content print name
When entering the password, if you want to be invisible, you need to use the getpass method in the getpass module, that is:
#! /Usr/bin/env python #-*-coding: UTF-8-*-import getpass # assign the user input content to the name variable pwd = getpass. getpass ("Enter Password:") # print the entered content print pwd
8. Process Control and indentation
Requirement 1: user login verification
#! /Usr/bin/env python #-*-coding: encoding-*-# enter the user name and password # verify the user name and password # if an error occurs, the output username or password is incorrect # If it succeeds, the output is welcome, XXX! Import getpass name = raw_input ('Enter the User name: ') pwd = getpass. getpass ('enter password: ') if name = "zhangyanlin" and pwd = "123456": print "Welcome, zhangyanlin! "Else: print" incorrect user name and password"
9. while Loop
1. Basic cycle
While condition: # loop body # If the condition is true, the loop body is executed # If the condition is false, the loop body is not executed
2. break
Break is used to exit all loops.
while True: print 123 break
3. continue
Continue is used to exit the current loop and continue the next loop
while True: print 123 continue
4. # list all odd numbers in 100
odd = 1while odd <= 100: print(odd) odd += 2
5. # list all the even numbers in 100
even = 0while even <= 100: print (even)even += 2
6. Calculate the sum of 1-2 + 3-4... + 99
# Exclude 99 remaining 49 groups 1 minus 2u1 = 49 print (u1 * (1-2) + 99)
Or
# List all numbers from 1 to 99 and start = 1sum = 0 while start <100: temp = start % 2 if temp = 1: sum = sum + start else sum = sum-start + = 1 print (sum)
The above is the basic Python article provided by the editor. The first time you get to know Python, you must take a look at all the content of the introduction. I hope you can support more help ~