- Introduction to Python
Common programming languages: C, Python, Java, PHP, Go
Classification of programming languages:
Machine code: C
Bytecode: Other Languages
Note: All other programming languages (Python, Java, PHP, Go) are ultimately interpreted as C, which are also written in C.
Programming languages are divided into compiled and interpreted
Compiled type: C# Java C, compiles the entire written code, similar to a book after it is written and compiled.
Interpretation type: Python PHP read a sentence and explain a sentence
Advantages of Python: complete class library, many modules
Difficulty level: C
C# JAVA
PHP
Python
Summary: 1. Install the interpreter
2. Learn the rules of the language
3. Write code
4. The interpreter runs
2. Install the interpreter: cpython (written in C language, commonly used)
Jpython (written in Java)
Ironpython
Rubypython (written in Ruby)
Pypy interpreter (faster than cpython)
Install the cpython interpreter, two versions:
Python2.7
Python3.6
Website: python.org download interpreter
Install the interpreter Python2.7 and Python3.6 at the same time. Install version 3.6 first and then version 2.7. Set the environment variables after installation. In fact, the following environment variables will be installed by default.
Set the environment variable path (windows operating system): Right-click My Computer -> Properties -> Advanced System Settings -> Advanced -> Environment Variables -> System Variables - "Find Path
The current environment variables are: C:\Python27\;C:\Python27\Scripts;C:\Python36\Scripts\;C:\Python36\;
How to make 2.7 and 3.6 take effect at the same time?
In the installation path of 3.6, copy python.exe and name it python3.exe
What is pip? pip is a package management tool. Common commands: pip3 install XXX
3. Write the code
Create file XX.py
4. Encoding format, commonly used UTF-8
Ascii: represented by 1 byte, only part of the language information can be included
Unicode: Universal code, including all language information, requires 4 bytes. Some only require one or three bytes, so the Unicode is a waste of space. cannot be stored.
UTF-8: Compress Unicode, at least one byte. Chinese occupies 3 bytes.
gbk: Text correspondence of some Asian countries. Chinese occupies 2 bytes.
The Python2 interpreter uses ascii code to read by default. Add # -*- coding:utf-8 -*- to the file to read using utf-8
The Python3 interpreter uses UTF-8 by default for reading
5. IDE
Windows: pycharm, use the professional version, do not use Chinese
Linux、unix:vim
Pycharm settings:
1.New project->pure python->existing interpreter->system interpreter, select the path of python3.6
2. Ctrl+mouse wheel to change font size
File->setting->搜索mouse->General->change font size
3. Set the default format
File->setting->File and code Templates->Python scritp
Under Linux, you need to add the interpreter path to the code file: #!/usr/bin python
6. Input and output
Output: print()
输入:py3 input() ; py2 raw_input()
The terminal enters the password to encrypt, but the pycharm input password still cannot be encrypted
Import getpass
Pwd = getpass.getpass("Please enter your password")
7. Variable name
Rules: 1. Numbers, letters, underscores
2. Cannot start with a number
3. Cannot use python built-in keywords
Suggestion: see name by name, lowercase, separated by underscores
Comment: single line comment #
multi-line comment
"""
count = 1
while True:
"""
Add comments in batches: ctrl+?
Pycharm adjusts the format: code->reformat code
Shift+tab go left
Tab go right
8. Data type
age = 18 #integer type
name = "alex" # string type
Use triple quotes for line breaks
msg = """
Welcome to call 10086
1. Check the phone bill
2. Check the water meter
3. Manual service
"""
9. Conditional Statements
if condition:
set up go here
elif condition:
set up go here
else:
not established go here
and means and
10. Loop Statement
While condition:
Condition is fulfilled
import time
while True:
print (‘’)
time.sleep(1)
break forcibly terminates the current loop
continue to jump out of this loop and continue to the next loop
pass
11. Common data types
Integer: age = 18
String: name = "Lagerstroemia" name[0] takes purple name[1] takes wei
List user_list = ["Ertai", "Lagerstroemia", "Cup"] user_list[0] takes Ertai, user_list[2] takes cup
Dictionary: one pair for one
user_info={"name":"Xiao Liang","age":18}
user_info[name] takes Xiaoliang
user_info [age] Tori 18
for statement
for user in user_list:
print (user)