About the selection of versions
According to the internet, if Python is to be used at work, choose version 2.7. Here I choose 2.7.9 version of to study;
What is Python?
is a high-level computer programming language. Wide range of applications, Google and other famous companies are in use.
Python, the English word, is the serpent, the script;
Disadvantages:
1, the execution speed is slow, because is the high-level language, translates into the machine language the cost limit, appears the speed relative to other languages to be slow;
2, unable to encrypt the code, because it is an explanatory language;
Install Python
System type |
Installation method |
Description |
MaxOS |
|
OSX 10.8 or more comes with Python |
Linux |
|
How to install the basic software |
Windows |
Https://www.python.org/ftp/python/2.7.9/python-2.7.9.msi |
Set to Path |
Python's Interpreter
Interpreter version |
Description |
CPython |
The official interpreter, written in C, is the python that runs under the command line. |
Ipython |
Interactive interpreter, based on CPython, with improved interactivity |
PyPy |
Focus on execution speed, based on JIT technology |
Jython |
Python interpreter running on the Java platform |
IronPython |
Python interpreter for the dotnet platform |
The most widely used is CPython, if you want to interact with the Java and Dotnet platforms using network tuning to interact better.
The first Python program
Open the command line, enter Python, and enter the CPython interpreter;
Command |
Description |
100+200 |
Simple calculation |
print ' Hello,world ' |
Output string |
Exit () |
Exit the Python Interpreter |
You can also use a text editor, we recommend the use of notepad++, note that the encoding type is saved as Utf-8 no BOM format;
print ' Hello, Python '; a=100+200 Print A; Exit () |
To run the command:
Operating effect:
Input and output
Output method print ' string ', using commas to separate multiple strings, comma equivalent to a space;
Input: raw_input (' input hint text '); get string;
Python Basics
Python is indented, #开头的语句数注释; When the statement ends with a colon: The indented statement is a block of code and is sensitive to case;
Data type
The data types that Python can handle directly have a centralized
Type |
Description |
|
Integral type |
Any size, plus or minus. |
Divided into decimal and hexadecimal ox |
Floating point number |
Contain decimals |
Scientific notation can be used |
String |
Use any character enclosed in ' or ' |
Use \ to identify escape characters Multiple lines can be labeled with ' ... ' |
Boolean value |
Two kinds of values for True,false tour |
Boolean operations can be performed, And,or,not |
Patyon also provides a variety of data types, such as lists, dictionaries, and also supports custom data types |
Null value |
None ID, cannot represent 0 |
|
Variable |
Can be any type, defined directly by an assignment symbol, which actually defines a pointer or address. |
Python is a dynamic language, Java is a static language |
Constant |
Variables that cannot be changed |
|
String encoding
Coding |
Range |
Number of bytes |
Advantage |
Ascii |
English letters and numbers |
1 bytes |
Simple, small storage space |
Unicode |
Characters in all languages |
4 bytes |
Wide Coverage |
UTF8 |
Characters in all languages |
Depending on the situation, letters and numbers use 1 bytes, and other characters use the number of bytes as needed |
Space saving |
File processing: Load into memory display using Unicode encoding, save to file using UTF8 encoding;
Browser processing: Server-to-Web transmission via UTF8 encoding;
ASCII-related functions:
Function |
Function description |
|
Ord (' B ') |
Get the corresponding number |
|
Chr (65) |
Get the corresponding character |
|
Unicode characters u ' hello '
Convert to UTF8 code: Encode (' Utf-8 ')
Gets the string length len (' string ')
Convert to Unicode encoding decode (' Utf-8 ')
Common comments
#!/usr/bin/env python
#-*-Coding:utf-8-*-
The first line is to tell the Linux/os system that Python is an executable program that Windows ignores;
The second line is to tell the Python interpreter to read the source file according to UTF-8 code, otherwise it may be garbled in Chinese
Formatting:
Placeholder |
Type |
%d |
Integer |
%f |
Floating point number |
%s |
String |
%x |
Hexadecimal number |
Common data structures
Data |
Characteristics |
Common operations |
Initialize method |
List |
An ordered array of variable lengths that can construct a multi-bit array |
Append, append Element Pop deletes the last one, or the element at the specified position Insert a position inserts an element Len Get length A "I" gets an element, and when I is a negative number, the inverse is obtained. |
[' A ', 123, ' shit '] |
Tuple |
Length, point to immutable ordered array, more secure |
B "I" value by position |
(' A ', 789,[1, ' end ') |
Dict |
MAP, high query efficiency, but low storage efficiency |
In is present Get getting elements Pop "key" delete element c[' key ') to get a value or add value based on key |
{' name ': ' Carter ', ' Age ': 27} |
Set |
A collection in algebra |
Add add Element Remove delete Element |
Set ([1,2,3,4]) |
Immutable objects |
Is that objects cannot be changed, pointing can change |
|
|
Condition and cycle judgment
Description
Kind |
format | TD valign= "Top" width= "280" >
Conditional judgment |
If If ... Else If...elif...else |
Elif from top to bottom As long as X is a non-0 number, not an empty string, a non-empty list, Condition can be true |
Loop judgment |
for V AR in array: while (condition): |
  |
#-*-coding:utf-8-*--*- age=3 If age >=: print ' adult ' else: print ' teenager ' sum = 0 For x in range (101): Sum+=x; Print sum; Sum = 0 N = While n>0: Sum +=n N-=2 Print U ' 100 within the even-numbered addition to get ', sum |
Range (num) a list from 0 to Num-1
int (' string ') converts the string to an integer
Getting Started with Python learning notes 1:python