Python tutorial - day01 - computer foundation and environment construction

day01 computer foundation and environment construction

Course objective: let everyone understand the basic knowledge of computer and complete the construction of Python environment.

Course summary:

  • Fundamentals of computer
  • The essence of programming
  • Introduction to Python
  • Construction of Python environment

1. Fundamentals of computer

1.1 basic concepts

  • Composition of computer

    A computer is a combination of multiple hardware. Common hardware include: CPU,Hard disk, memory, network card, display, chassis, power supply....
    
    Note: mechanical parts are combined together, and they cannot cooperate with each other.
    
  • operating system

    It is used to coordinate the various hardware of the computer and make the hardware work together to achieve a certain goal.
    Common operating system categories:
    - windows,Advantages: ecological cow force and many tools; Disadvantages: slightly slow and charging. [individual]
    	- xp
    	- win7
    	- win10
    	...
    - linux,Advantages: less resource consumption and free of charge (installed on the servers of many companies) Linux);Disadvantages: few tools, bid farewell to the game. [enterprise server]
    	- centos
    	- ubuntu
    	- redhat
    	...
    - mac,Advantages: good ecology, almost all tools, user experience and interaction; Disadvantages: can't play games
    
  • Software (application)

    After installing the operating system, we will install some common software on our computer, such as: QQ,Antivirus, wechat...
    
    The question is: who developed these software? It was developed by programmers from major companies.
    
    You must write in the future`Software`,Software can be understood as a lot of code (an article).
    

    [the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-0NC7duig-1622358897058)(assets/image-20201021185430525.png)]

1.2 programming language

Software is a collection of a lot of code developed by programmers using programming language. There are more than 2500 programming languages in the world. Common programming languages: Java, C#, Python, PHP, C

Composition is a collection of a lot of words written by primary school students in Chinese / English / French / Japanese.

In essence, learning programming language is to learn its grammar, and then write the corresponding functions in the software according to the grammar.

  • Syntax rules for output in Python language

    print("I am Alex My second uncle")
    
  • Syntax rules for output in Golang language

    fmt.Println("I am Alex Second uncle")
    

1.3 compiler / interpreter

A compiler / interpreter is a translator who translates code into commands that the computer can recognize.

A use Python Developed a software 1000              B use Golang Developed a software 2000

       Python interpreter                        Golang compiler

                    exercise    do    system    Unified

               CPU    Hard disk    network card    Memory    Power Supply .....

Why are some called interpreters? Some are called compilers?

  • Interpreter, real-time translation. After getting 1000 lines of code, give an explanation to the operating system.
  • Compiler, full text translation. After getting 2000 lines of code, it will compile it into a temporary file (the computer can recognize the command), and then hand over the file to the operating system to read.

Python, PHP, JavaScript, Ruby... Are generally called interpretative languages.

C. C + +, Go, Java... Are generally called compiled languages.

2. Learn the essence of programming

Learning programming is essentially three things:

  • Select a programming language and install the compiler / interpreter related to this programming language on your computer.
  • Learn the grammar rules of programming language, design and develop your software (code set) according to the grammar rules + business background.
  • Use the compiler / interpreter to run your own code.

3. Introduction to Python

3.1 classification of languages

  • Dimensions of translation

    • Interpretive languages, Python, Ruby
    • Compiled language, C, C + +, Golang
  • High and low dimensions

    • Low level programming language, the code written can be directly recognized by the computer.

      Machine language, 101 001 00001 00010 0010001, machine code, handed over to the computer for execution.
      Assembly language, MOV INC ... ,Instructions are given to the computer to execute.
      
    • High level programming language, the code written cannot be directly recognized by the computer, but it can be converted into a language recognized by the computer in a certain way.

      C,C++,Java,Python,Ruby...,This kind of programming language almost writes English composition when writing code.
      It is translated into machine code by the relevant compiler or interpreter, and then handed over to the computer for execution.
      

Note: now basically all use high-level programming languages.

3.2 Python

Python was founded by Guido van Rossum. During Christmas in 1989, Guido began to write a compiler for the python language. The name Python comes from Guido's favorite TV series Monty Python's flying circle. He hopes that this new language, called python, can meet his ideal: to create a language between C and shell that is comprehensive, easy to learn, easy to use and extensible.

With many programming languages around the world, why can Python stand out and become a hot programming language in the industry? At present, it ranks third in the TIOBE ranking list and has been on the rise.

Python is so popular for the following reasons:

  • Compared with other industries, Python can provide people with the ability to process files and even learn the language of Excel quickly. For ex amp le, compared with other industries, it is very low-cost to learn the language of Excel.
  • Class libraries are powerful. Python has grown naturally since its birth. After years of development, it has accumulated many convenient class libraries in many fields. Naturally, it has also become the preferred programming language for operation and maintenance automation, data analysis and machine learning.
  • The development efficiency is very high. Combined with the characteristics of Python syntax refinement and complete class library, it can complete more functions with less code and greatly improve the development efficiency. For example, the function realized by Python in 10 lines of code may require 100 + lines in other languages.

3.3 Python interpreter types (understand)

Want to learn a programming language: install Python interpreter, learn Python syntax and write code, and use Python interpreter to execute the written code.

Python is very popular in the world, and many companies will want to have a layer of heat.

Because Python is so popular, many companies have developed Python interpreters (used to translate Python code into commands that can be recognized by computers).

  • CPython [mainstream], the bottom layer is a Python interpreter developed by C language.
  • Jython is a Python interpreter developed by java language, which is convenient to integrate Python and Java code.
  • IronPython is a Python interpreter developed based on C# language, which is convenient to integrate Python and C# code.
  • RubyPython,...
  • PyPy is an optimization of CPython, and its execution efficiency is improved. The function of compiler is introduced, which essentially compiles Python code and then executes the compiled code.
  • ...

Note: the commonly used Python interpreter refers to the CPython interpreter by default.

3.4 version of Cpython interpreter

CPython's interpreter has two major versions:

  • 2.x, the latest Python 2 7.18. (no maintenance after 2020)

    Being the last of the 2.x series, 2.7 received bugfix support until 2020. Support officially stopped January 1 2020, and 2.7.18 code freeze occurred on January 1 2020, but the final release occurred after that date.
    
  • 3.x, the latest version of 3.9.0 (Teaching).

4. Environmental construction

  • Python interpreter, which translates the Python code written by the programmer into instructions that can be recognized by the computer.
    • Mainstream CPython
    • Version 3.9.0
  • Three essential things about learning programming
    • Install Cpython version 3.9.0 interpreter
    • Learn Python syntax and write code
    • The interpreter runs the code

4.1 installing the Python interpreter

4.1.1 mac system

  • Go to the official Python website to download the Python interpreter (version 3.9.0)

    https://www.python.org/
    
  • install

    default Python Interpreter installation directory: /Library/Frameworks/Python.framework/Versions/3.9
    
    have bin There is one in the directory python3.9 File, he is Python The startup file of the interpreter.
    Interpreter path:/Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 
    
  • Write a simple Python code and let the interpreter run.

    name = input("enter one user name:")
    print("Welcome NB System:",name)
    

    Save the file in: document / Hello py[/Users/wupeiqi/Documents/hello.py]

    Next, let the interpreter run the code file:

    - Open terminal
    - Input: interpreter code file at terminal
      /Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 /Users/wupeiqi/Documents/hello.py
    
  • [supplement] system environment variables

    - Suppose you have 30 Python File to run
       /Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 /Users/wupeiqi/Documents/hello1.py
       ...
       /Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 /Users/wupeiqi/Documents/hello30.py
    
    - Python The interpreter path doesn't have to be written so long at a time.
        - take  /Library/Frameworks/Python.framework/Versions/3.9/bin Add to the environment variable of the system.
        - Use later Python Interpreter to run python Code, you can do this:
        	 python3.9 /Users/wupeiqi/Documents/hello1.py
        	 ...
        	 python3.9 /Users/wupeiqi/Documents/hello2.py
    
    - How will /Library/Frameworks/Python.framework/Versions/3.9/bin Add to the environment variable of the system?
    	- You don't need to add it by default Python The interpreter has been added for you during the installation process.
    	- Manually add:
    		 - Open the under the user directory  .bash_profile Documents(.zprofile)
    		 	- Terminal:~ % cat ~/.zprofile
    		 - Write the following in the document
    
    # Setting PATH for Python 3.9
    # The original version is saved in .zprofile.pysave
    PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
    export PATH
    
    		- Exit after entering: esc+':wq'+enter
    

4.1.2 windows system

  • Download Python interpreter from Python official website

    https://www.python.org/downloads/release/python-390/
    
  • Install on your own computer

    python Interpreter installation directory: C:\Python39
    python Interpreter path: C:\Python39\python.exe
    
  • Write a Python code and give it to the Python interpreter to run

    name = input("enter one user name")
    print("Welcome NB system",name)
    

    And save the file in: Y: \ hello py

    How to let the interpreter run the written code file?

    - Open terminal
    - Enter: interpreter path code path at the terminal
    
  • Optimize configuration (make it easier to operate Python interpreter to run code in the future)

    - Wrote 30 Python Code, want to use the interpreter to run.
        C:\Python39\python.exe Y:\hello1.py
        C:\Python39\python.exe Y:\hello2.py
        ...
        C:\Python39\python.exe Y:\hello10.py
    
    - However, you can easily run the code in the future without writing any more Python The path where the interpreter is located.   
        As long as you will C:\Python39 The path is added to the environment variable of the system. In the future, you can:
            python.exe Y:\hello1.py
            
    - How will C:\Python39 Add to environment variables? [by default, it has been automatically added to the environment variable when the interpreter is installed]
    

4.2 installing pychar editor (mac)

Help us write code quickly. Using pychar can greatly improve the efficiency of our code writing+ Use the interpreter to run the code.

print("asdfasdf")
  • Download pychar

    https://www.jetbrains.com/pycharm/
    
  • install

  • Quick use, write code + run code

  • Crack pychar (professional version)

4.3 installing pychar editor (win)

  • Download pychar

    https://www.jetbrains.com/pycharm/download/other.html
    
  • install

  • Quick use: write code + run code

  • Crack pychar (professional version)

summary

  1. Understand the relationship between hardware, operating system and software (application system).
  2. Learn about common operating systems.
  3. Understand the difference and function between compiler and interpreter.
  4. Classification of programming languages
  5. Understand the types of Python interpreters
  6. Understand the version of CPython interpreter
  7. Learn how to install Python interpreter.
  8. Understand what environmental variables do.
  9. Learn the difference between Python and pychar.

task

  1. Briefly describe the relationship between hardware, operating system and software (application system).
  2. List the common operating systems.
  3. Briefly describe the difference and function between compiler and interpreter.
  4. Classification of programming languages
  5. What are the types of Python interpreters?
  6. What are the versions of CPython interpreter? Which version are you using now?
  7. What is the role of system environment variables?
  8. What is the difference between Python and pychar?

Keywords: Python

Added by Cogen on Tue, 08 Feb 2022 06:03:21 +0200