Python path: Python BASICS (1) and python path Basics
1. The first Python code
Create the hello. py file in the/home/dev/directory. The content is as follows:
print "hello,lenliu"
Run the hello. py file:python hello.py
The internal execution process of python is as follows:
Ii. Interpreter
When running python hello. py in the previous step, it is clearly pointed out that the hello. py script is executed by the python interpreter.
If you want to execute a python script like a shell script, for example:./hello.py
First, you must have the execution permission, and then specify the interpreter in the header of the hello. py file, as shown below:
#!/usr/bin/env pythonprint "hello,world"
In this way, run :./hello.py
You can.
Ps: Give "hello. py" execution permission, chmod 755 hello. py
Iii. 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 pythonprint "Hello, lenliu"
Correct: the following code should be displayed to the python Interpreter:
#! /Usr/bin/env python #-*-coding: UTF-8-*-print "Hello, lenliu"
Iv. Notes
When watching: # commented content
Multi-line comment: "commented content """
5. Execute the script to input parameters
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.
Ps: After the code is compiled, it can generate bytecode. After the bytecode is decompiled, the code can also be obtained.VII. Variables
1. Declare Variables
#! /Usr/bin/env python #-*-coding: UTF-8-*-name = "lenliu" the above Code declares a variable named: name, and the value of the variable name is: "lenliu"
The role of a variable: nickname, which represents the content stored in an address in the memory.
#! /Usr/bin/env python #-*-coding: UTF-8-*-name1 = "wupeiqi" name2 = "lenliu"
Name1 = "wupeiqi" name2 = name1
8. Input
Name = raw_input ("Enter the User name:") # assign the user input content to the name variable print name # print the input content
To make the password invisible, you must use the getpass method in the getpass module, that is:
Import getpasspwd = getpass. getpass ("enter the password:") print pwd
9. Process Control and indentation
Requirement 1: user login verification
# Prompt for entering the user name and password # verify the user name and password # If the user name or password is incorrect, output the user name or password is incorrect # If the user name is successful, output welcome, XXX!
#! /Usr/bin/env python #-*-coding: encoding-*-import getpassname = raw_input ('enter your Username: ') pwd = getpass. getpass ('enter password: ') if name = "lenliu" and pwd = "cmd": print "Welcome, lenliu! "Else: print" incorrect user name and password"
Requirement 2: Output permissions based on user input
# Print the permission according to the user input # lenliu --> super administrator # amy --> common administrator # Others --> common user
#! /Usr/bin/env python #-*-coding: encoding-*-name = raw_input ('enter your Username: ') if name = "lenliu ": print "Super administrator" elif name = "amy": print "normal administrator" else: print "normal user"
PS: the outer variable, which can be used by the inner variable, but the inner variable cannot be used by the outer variable.
10. Basic data types first recognized
1. Number
Int (integer)
On a 32-bit machine, the number of digits of an integer is 32 bits and the value range is-2 ** 31 ~ 2 ** 31-1, I .e.-2147483648 ~ 2147483647
In a 64-bit system, the number of digits of an integer is 64-bit, and the value range is-2 ** 63 ~ 2 ** 63-1, that is,-9223372036854775808 ~ 9223372036854775807
Long (long integer)
Different from the C language, the Length Integer of Python does not specify the Bit Width. That is, Python does not limit the size of the Length Integer. However, due to limited machine memory, the length integer value we use cannot be infinitely large.
Note: Since Python2.2, if an integer overflows, Python will automatically convert the integer data to a long integer. Therefore, without the letter L after the long integer data, it will not cause serious consequences.
Float (float type)
Floating point numbers are used to process real numbers, that is, numbers with decimals. Similar to the double type in C, it occupies 8 bytes (64-bit), where 52 bits represent the bottom, 11 bits represent the index, and the remaining one represent the symbol.
PS: 3.23 and 52.3E-4 are floating point numbers. E indicates the power of 10. Here, 52.3E-4 indicates 52.3*10-4
Complex (plural)
A complex number is composed of a real number and a virtual number. Generally, x + yj is used. x is the real number of a complex number, and y is the virtual number of a complex number. Here, x and y are both real numbers.
PS: (-5 + 4j) and (2.3-4.4.7) are complex numbers.
Note: there is a small number pool in Python:-5 ~ 257
2. Boolean Value
True or false (1 or 0)
3. String
"Lenliu"
String concatenation:
The string in python is embodied in the C language as a character array. Each time you create a string, you need to open up a continuous space in the memory. Once you need to modify the string, you need to open up space again, every time the "+" icon appears, it will open up a new space.
String formatting
Name = "lenliu" print "I am % s" % name output: I am lenliu
PS: the string is % s integer % d Floating Point Number % f
Common functions of strings:
Remove blank, split, length, index, and slice
4. List
Creation list:
Name_list = ['lenliu', 'seven'] Or name_list = list (['lenliu', 'seven']) basic operations:
Index, slice, append, delete, length, slice, cycle, and contain
5. Ancestor (cannot be modified or deleted)
Create a ancestor:
Ages = (11, 22, 33, 44, 55) or ages = tuple (11, 22, 33, 44, 55 ))
Basic operations:
Index, slice, loop, length, and inclusion
6. Dictionary (unordered)
Create a dictionary:
Person = {"name": "lenliu", 'age': 18} or person = dict ({"name": "lenliu", 'age': 18 }) common Operations:
Index, add, delete, key, value, key-value pair, cycle, Length
PS: loop, range, continue, and break
XI. Operations
Arithmetic Operation:
Comparison:
Assignment operation:
Logical operation:
Member calculation:
Identity calculation:
Bitwise operation:
#!/usr/bin/pythona = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0c = a & b; # 12 = 0000 1100print "Line 1 - Value of c is ", cc = a | b; # 61 = 0011 1101 print "Line 2 - Value of c is ", cc = a ^ b; # 49 = 0011 0001print "Line 3 - Value of c is ", cc = ~a; # -61 = 1100 0011print "Line 4 - Value of c is ", cc = a << 2; # 240 = 1111 0000print "Line 5 - Value of c is ", cc = a >> 2; # 15 = 0000 1111print "Line 6 - Value of c is ", c
Operator priority:
More: http://www.runoob.com/python/python-operators.html
12. Basic operations on the first recognized text
Open the file:
File_obj = file ("file path", "Mode") or file_obj = open ("file path", "Mode ")
File opening modes include:
- R. open the file in read-only mode.
- W. Open a file for writing only. If the file already exists, overwrite it. If the file does not exist, create a new file.
- A. Open a file for append. If the file already exists, the file pointer is placed at the end of the file. That is to say, the new content will be written to the existing content. If the file does not exist, create a new file for writing.
- W +, open a file for reading and writing. If the file already exists, overwrite it. If the file does not exist, create a new file.
Read File Content:
Obj. read () # Load all content to the memory obj at a time. readlines () # Load all content to the memory at a time and split it into strings for line in obj: # Read Only one row of data at a time print line
Write File Content:
Obj. write ('content ')
Close the file handle:
obj.close()
Link: http://www.cnblogs.com/wupeiqi/articles/4906230.html Wu sir