Python basics

Contents of this chapter:

Types of Python

The official version of Python is implemented in C language and is the most widely used. The CPython implementation converts source files (py files) into bytecode files (pyc files), and then runs on the Python virtual machine.

The Java implementation of Python, Jython will dynamically compile Python code into Java bytecode, and then run it on the JVM.

The C# implementation of Python, IronPython compiles Python code into C# bytecode, and then runs on the CLR. (Similar to Jython)

Python implemented by Python compiles Python bytecode bytecode into machine code.

The correspondence and execution process of Python are as follows:

PyPy further processes Python bytecode on the basis of Python, thereby improving the execution speed!

Python environment

Windows:

Download link: https://www.python.org/downloads/

Linux:

Built-in python environment

# python -V View python version
nick-suo@ubuntu:~$ python -V
Python 2.7.6
nick-suo@ubuntu:~$ python3 -V
Python 3.4.0
nick-suo@ubuntu:~$

Update python environment

1、 Install gcc, used to compile Python source code
 yum install gcc
2、 Download the source code package, https://www.python.org/ftp/python/3. Unzip and enter the source code file
4、 Compile and install
 . /configure
 make all
 make install
5、 View 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 from executing abnormally and modify the Python version used by yum
 vi /usr/bin/yum
 Move the head#!/usr/bin/python is modified to#!/usr/bin/python2.6
Getting started with Python

1.'Hello World!'

# How to write python2
print "Hello World!"
# How to write python3
print("Hello World!")

2. Execute (interpreter), exit

It is clearly pointed out that the hello.py script is executed by the python interpreter.

nick-suo@ubuntu:/blogs$ cat hello.py
#! /usr/bin/env python
print("Hello World!")
nick-suo@ubuntu:/blogs$ sudo chmod +x hello.py 
nick-suo@ubuntu:/blogs$ ./hello.py
Hello World!
nick-suo@ubuntu:/blogs$
#########################
The program can request to exit by throwing SystemExit exception.
>>> raise SystemExit

Three, coding

When the python interpreter loads the code in the .py file, it encodes the content (ascill by default)

ASCII (American Standard Code for Information Interchange) is a computer coding system based on the Latin alphabet. It is mainly used to display modern English and other Western European languages. It can only be represented by 8 bits at most (one byte ), that is: 2**8 = 256, so the ASCII code can only represent 256 symbols at most.

Obviously, the ASCII code cannot represent all the various characters and symbols in the world, so a new encoding that can represent all characters and symbols is needed, namely: Unicode

Unicode (unicode, universal code, single code) is a character encoding used on computers. Unicode was created to solve the limitations of traditional character encoding schemes. It sets a unified and unique binary encoding for each character in each language, and stipulates that although some characters and symbols are represented by at least 16 bits (2 Bytes), that is: 2 **16 = 65536,
Note: What is mentioned here is at least 2 bytes, possibly more

UTF-8 is the compression and optimization of Unicode encoding. It no longer uses at least 2 bytes, but classifies all characters and symbols: the content in the ascii code is saved with 1 byte, and European characters Use 2 bytes to store, East Asian characters store in 3 bytes...

Tell the python interpreter what encoding to use to execute the code:

nick-suo@ubuntu:/blogs$ cat hello.py
#! /usr/bin/env python
#- *- coding:utf-8-*-print("Hello world!")
nick-suo@ubuntu:/blogs$ python3 hello.py
Hello world!
nick-suo@ubuntu:/blogs$

Four, notes

Single-line comment: #Nick

Multi-line comment: ``'Nick'''

# Nick
'''
Nick
Nick
Nick
'''

Five, pyc file

When executing Python code, if other .py files are imported, a .pyc file with the same name will be automatically generated during execution, which is the bytecode generated by the Python interpreter.

nick-suo@ubuntu:/blogs$ ls a.py*
a.py  a.pyc
nick-suo@ubuntu:/blogs$

**Six, step into the parameters **

Python has a large number of modules, which makes the development of Python programs very concise. The class library includes three:

Python provides a sys module, where sys.argv is used to capture the parameters passed in when executing the python script

#! /usr/bin/env python
#- *- coding:utf-8-*-import sys
print("sys.argv")

Seven, variables

1、 Declare variable

#! /usr/bin/env python
#- *- coding:utf-8-*-
# Declare variable name with value"Nick"
name ="Nick"

The role of variables: nickname refers to the content stored in an address in memory

Rules for variable definition:

2、 Variable assignment

#! /usr/bin/env python
#- *- coding:utf-8-*-
name1 ="Nick"
name2 ="Suo"

#! /usr/bin/env python
#- *- coding:utf-8-*-
name1 ="Nick"
name2 = name1

8. Input

Enter your user name

#! /usr/bin/env python
#- *- coding:utf-8-*-
# Assign the content entered by the user to the name variable
name =raw_input("please enter user name:")
print name
# How to write python3
name =input("please enter user name:")print(name)

When entering the password, if you want to be invisible, you need to use the getpass method in the getpass module, namely:

#! /usr/bin/env python
#- *- coding:utf-8-*-import getpass
pwd = getpass.getpass("Please enter password:")print(pwd)

Nine, process control and indentation

User login verification and output corresponding content

#! /usr/bin/env python
#- *- coding:utf-8-*-import getpass
name =input("please enter user name:")
pwd = getpass.getpass("Please enter password:")if name =="nick" and pwd =="nick":print("Welcome, nick.")
elif name =="Suo" and pwd =="Suo":print("Welcome, Suo.")
elif name =="test":print("Hi, test.")else:print("Sorry, please try angin.")

Ten, while loop

1、 Basic cycle

while condition:
 # Loop body
 # If the condition is true, then the loop is executed
 # If the condition is false, then the loop is not executed

2、 break

break is used to exit the current layer loop

#! /usr/bin/env python
#- *- coding:utf-8-*-
num =1while num <6:print(num)
 num+=1breakprint("end")

3、 continue

continue is used to exit the current loop and continue to the next loop

#! /usr/bin/env python
#- *- coding:utf-8-*-
num =1while num <6:print(num)
 num+=1continueprint("end")

Recommended Posts

Python basics
Python basics 2
Python basics
Python basics 3
Python basics 4
Python basics 5
Python object-oriented basics
Python custom function basics
Basics of Python syntax
Python multi-process and multi-thread basics
Python multithreading
Python CookBook
Python FAQ
Python3 dictionary
Python3 module
python (you-get)
Python string
Python descriptor
Python exec
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
07. Python3 functions
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Learn the basics of python interactive mode
Two days of learning the basics of Python
Centos6 install Python2.7.13
Python answers questions
Python basic syntax (1)
Python exit loop
Ubuntu16 upgrade Python3
Centos7 install Python 3.6.
ubuntu18.04 install python2
Python classic algorithm
Relearn ubuntu --python3
Python2.7 [Installation Tutorial]
Python string manipulation
Python 3.9 is here!
Python study notes (1)
python learning route
CentOS7 upgrade python3
Python review one
linux+ubuntu solve python
Functions in python