Start Python learning, the introduction of Python and its development history here is not much to say. Can Baidu a bit.
I am also just beginning to learn, knowledge point follow-up will be added.
First, we face the choice: Which version to start learning (can go to the official website, there is a specific introduction in English)
One, Python2 or Python3
There are many differences between the 2 versions, encoding, syntax, strings, data types, libraries, classes, modules, and so on. The following 2 points are not understood here first.
1. Default encoding
Python2 By default is ASCII, so when reading Chinese will be garbled, the solution is to add the beginning of the file:#-*-Coding:utf-8-*-. The code that contains Chinese will need to specify this encoding on the head.
Python3 the encoding used by default is Utf-8.
2. Syntax
Grammar changes a lot, the beginning of learning, first of all to say a print change. The print statement in Python2 becomes the print () function, meaning that python3 must be enclosed in parentheses. Otherwise the error.
Second, installation
1. Download the installation package from the official websitehttps://www.python.org/downloads/
2. Configure the environment variable path.
Open "Control Panel \ All Control Panel items \ System" → "Advanced system settings" → "Advanced" tab → click on "Environment variables" → find "system variables" in the path variable or the current user variable path→ change the value of the variable path, at last add "; Python's absolute Installation path "→ Click OK
3. Verify that the environment variable is in effect
Open the cmd window and enter: Python-verion display as follows:
Third, the first procedure
Create a file helloworld.py, content:
Print ("Hello world!")
Then, in cmd, enter the directory where the file is located and execute
Python helloworld.py
#执行结果
c:\users\mi\pycharmprojects\py_15\day1>python firstprogram.py Hello world!
If you want to run a script in a Linux system, you need to declare it first and add it to your head:
# !/usr/bin/env python
After you install Python, an idle is generated, as follows
Python 3.5.2 (V3.5.2:4DEF2A2901A5, June, 22:18:55) [MSC v.1900"copyright" " credits " or " license () " for More information. Print("Hello world! " ) Hello world! >>>
Four, character encoding
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-1, so the ASCII code can only represent a maximum of 255 symbols.
About Chinese
To deal with Chinese characters, programmers designed GB2312 for Simplified Chinese and big5 for traditional Chinese.
GB2312 (1980) contains a total of 7,445 characters, including 6,763 Kanji and 682 other symbols. The inner code range of the Chinese character area is high byte from B0-f7, low byte from A1-fe, occupy code bit is 72*94=6768. 5 of these seats are d7fa-d7fe.
GB2312 supports too few Chinese characters. The 1995 Chinese character extension specification GBK1.0 contains 21,886 symbols, which are divided into Chinese characters and graphic symbol areas. The Chinese character area consists of 21,003 characters. The 2000 GB18030 is the official national standard for replacing GBK1.0. The standard contains 27,484 Chinese characters, as well as Tibetan, Mongolian, Uyghur and other major minority characters. Now the PC platform must support GB18030, the embedded products are not required. So mobile phones, MP3 generally only support GB2312.
From ASCII, GB2312, GBK to GB18030, these coding methods are backwards compatible, meaning that the same character always has the same encoding in these scenarios, and the latter standard supports more characters. In these codes, English and Chinese can be handled in a unified manner. The method of distinguishing Chinese encoding is that the highest bit of high byte is not 0. According to the programmer, GB2312, GBK, and GB18030 belong to the double-byte character set (DBCS).
Some Chinese Windows default internal code or GBK, you can upgrade to GB18030 through the GB18030 upgrade package. But GB18030 relative GBK increases the character, the ordinary person is difficult to use, usually we still use the GBK to refer to the Chinese Windows inside code.
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 ...
V. Notes
Single-line comments at the beginning of #
Multi-line annotation with
""".............
................
............."""
VI. User input
Simple user input
1 # !/usr/bin/env python 2 name = input ("pls input your name")
3 Print ("Hello"+name)
Seven, variable
A container for storing data in memory
Assigning values to variables
#将第一天的收益赋值给变量day1
day1=2000
#将第二天的收益赋值给变量day2
day2=200
Add a variable
#两天的收益相加, assign the value to total
Total=day1+day2
1 # !/usr/bin/env python 2 day1=20003 day2=2004 total=day1+day256print( " 2-day total revenue: "
Eight, judge
Scenario 1. User Authentication Login
1Username=input ("Username:")2Password=input ("Password:")3 4 ifusername=="Xiaohong" andpassword=="123456":5 Print("welcome!")6 Else:7 Print("user or passwd error!")
Scenario 2. Guess Age Game
1 #!/usr/bin/env python3 #-*-coding:utf-8-*-6My_age = 289user_input = Int (input ("input your guess num:") A ifUser_input = =My_age: - Print("Congratulations, you got it!") - elifUser_input <My_age: - Print("Oops,think bigger!") - Else: A Print("Think smaller!")
Outer variables, can be used by the inner layer of the code inside the variable, should not be used by the outer code nine, canonical output
1 #!/usr/bin/env python2 3Name=input ("Name:")4Age=input ("Age :")5Job=input ("Job:")6Hobby=input ("Hobby:")7 8info="""9 ---------Info of%s------------Ten Name:%s One Age :%s A Job:%s - Hobby:%s - --------------End-------------- the """% (Name,name,age,job,hobby)
Python3 Day1-python Foundation