Zero basic learning of python (type of Python + environment of Python + introduction to Python)

Contents of this chapter:

  • Types of Python
  • Python environment
  • Introduction to Python (interpreter, coding, pyc file, step input parameters, variables, input, process control and indentation, while loop)
  • Exercises

Types of Python

  • Cpython

The official version of Python is implemented in C language, which is the most widely used. CPython implementation will convert the source file (py file) into bytecode file (pyc file), and then run on the python virtual machine.

  • Jyhton

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

  • IronPython

C# implementation of Python. IronPython compiles Python code into c# bytecode and then runs it on CLR. (similar to Jython)

  • PyPy (special)

Python implemented by Python recompiles the bytecode of Python into machine code.

  • RubyPython,Brython ....

The correspondence and execution process of Python are as follows:

PyPy further processes the bytecode of Python on the basis of python, so as to improve the execution speed!

Python environment

Windows:

Download address: 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,For compilation Python Source code
    yum install gcc
2,Download the source package, https://www.python.org/ftp/python/
3,Unzip and enter the source file
4,Compile and install
    ./configure
    make all
    make install
5,View version
    /usr/local/bin/python2.7 -V
6,Modify default Python edition
    mv /usr/bin/python /usr/bin/python2.6
    ln -s /usr/local/bin/python2.7 /usr/bin/python
7,prevent yum Execution exception, modification yum Used Python edition
    vi /usr/bin/yum
    Put the head #!/usr/bin/python Change to #!/usr/bin/python2.6

##Getting started with Python

1, 'Hello World!'

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

2, Execute (interpreter), exit

Clearly point out hello The 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 throw SystemExit Exception to request exit.
 
>>> raise SystemExit

3, Code

The python interpreter is loading When the code in the py file is, the content is encoded (default ASCII)

ASCII (American Standard Code for Information Interchange) is a set of computer coding system based on 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. Therefore, ASCII code can only represent 256 symbols at most.

Obviously, ASCII code can not represent all kinds of characters and symbols in the world. Therefore, a new code that can represent all characters and symbols is needed, that is, Unicode

Unicode (unified code, universal code, single code) is a character code used on computers. Unicode is produced to solve the limitations of the traditional character coding scheme. It sets a unified and unique binary coding for each character in each language. It stipulates that although some characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 * * 16 = 65536,
Note: at least 2 bytes are mentioned here, and there may be more

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

Tell the python interpreter what code 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$

4, Notes

Single line note: #Nick

Multiline comment: 'Nick'

#Nick
 
'''
Nick
Nick
Nick
'''

5, pyc file

When executing Python code, if you import other code py file, a file with the same name will be automatically generated during execution pyc file, which is the bytecode generated after the Python interpreter compiles.

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

6, Footstep incoming parameters

Python has a large number of modules, which makes the development of Python programs very simple. Class libraries include three types:

  • Modules provided internally in Python
  • Open source modules in the industry
  • Modules developed by programmers themselves

Python internally provides a sys module, in which sys Argv is used to capture the parameters passed in when executing Python scripts

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

7, Variable

1. Declare variable

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

Function of variable: nickname refers to the content stored in an address in memory

Rules for variable definition:

  • Variable names can only be any combination of letters, numbers, or underscores
  • The first character of a variable name cannot be a number
  • The following keywords cannot be declared as variable names
  • ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

2. Assigned variable

#!/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 one user name

#!/usr/bin/env python
#-*- coding:utf-8 -*-
 
#Assign the user input to the name variable
name = raw_input("enter one user name:")
print name
 
#How to write Python 3
name = input("enter one 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, that is:

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

9, Process control and indentation

User login verification and output corresponding content

#!/usr/bin/env python
#-*- coding:utf-8 -*-
 
import getpass
 
name = input("enter one user name:")
pwd = getpass.getpass("Please input a 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.")

10, while loop

1. Basic cycle

while condition:
      
    # Circulatory body
  
    # If the condition is true, the loop executes
    # If the condition is false, the loop does not execute

2,break

break is used to exit the current layer loop

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

3,continue

Continue is used to exit the current cycle and continue the next cycle

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

Exercises

Find the sum of all numbers of 1-2 + 3-4 + 5... 99

Method 1:

num = 1
num2 = 2
num3 = num - num2
while True:
    num+=2
    num2+=2
    num3 = num3 + num
    if num == 99:
        break
    num3 = num3 - num2
print(num3)

Method 2:

sum = 1
s = 0
while True:
    s = s + sum
    if sum == 99:
        break
    sum+=2
 
sum1 = 0
s1 = 0
while True:
    s1 = s1 + sum1
    if sum1 == 98:
        break
    sum1 += 2
 
print(s - s1)

Method 3:

sum = 0
start = 1
while start < 100:
    temp = start % 2
    if temp == 1:
        sum = sum +start
    else:
        sum = sum - start
    start += 1
print(sum)

+View Code

Method 4:

sum=1
s=0
while sum<100:
    s=s+sum*(-1)**(sum+1)
    sum+=1
print(s)

Method 5:

#Exclude 99, remaining 49 groups 1-2
print(49*(1-2)+99)

Keywords: Python Back-end

Added by gchouchou on Wed, 02 Feb 2022 22:24:37 +0200