search
HomeBackend DevelopmentPython TutorialIntroduction to the origin and use of Python

Python language is loved by many people in the IT industry because of its concise and clear style, as well as a large number of widely applicable class libraries and python open source frameworks that can be used. What is the origin and development history of Python? Let’s take a brief look at it.

Tracing back to the origin of the Python language, it was developed by Guido van Rossum in Amsterdam in the early 1990s as a new script interpreter. I wonder if Guido ever thought that Python would one day become one of the most popular programming languages?

Some people like to use glue language to describe Python because it can easily combine modules written in many other languages. As for the process, I won’t go into details here. If you are interested, you can find it anywhere. . What you need to know is that many universities at home and abroad also take the Python language as a required course, and the number of domestic units that use the Python language to work is also increasing. Programmers who know Python are in hot demand.

I would like to ask my friends who have learned Python language, what are its attractive features? Most people would think that it is a language that is easy to use, easy to read and easy to maintain, so many users like to use and learn it. It is really a language with a wide range of uses.

The most basic syntax of Python language is: indentation, control statements, expressions, functions, object methods, types and mathematical operations. Only after learning the basic syntax of Python can you start learning formal applications, such as: practical applications of graphics processing, mathematical processing, text processing, databases, WEB programming, crawlers, etc.

Python 3.3 is the latest version, but many people still like to start learning from python 2. Because it has been said before that the third-party support for Python 3 is not yet complete, you will encounter inexplicable problems during the learning process. It is better to start learning from python 2, which is already very complete. The transition to python 3 will be easy after that.

June 2017 TIOBE programming language rankings:

As can be seen from the above figure, the usage rate of python language is on the rise, and the usage rate of the top three languages ​​is on the rise. Downtrend.

PythonAdvantages and Disadvantages

Advantages:

  1. Python’s positioning is “elegant”, "Clear" and "simple", so Python programs always seem simple and easy to understand. Beginners learning Python are not only easy to get started, but also can write very, very complex programs if they go deeper in the future.

  2. The development efficiency is very high. Python has a very powerful third-party library. Basically, if you want to realize any function through the computer, the official Python library has corresponding modules to support it. Download it directly. After the call, development is carried out on the basis of the basic library, which greatly reduces the development cycle and avoids reinventing the wheel.

  3. High-level language————When you write a program in Python, you don’t need to think about low-level details such as how to manage the memory used by your program

  4. Portability - Due to its open source nature, Python has been ported to many platforms (with modifications to enable it to work on different platforms). If you carefully avoid using system-dependent features, then all of your Python programs can run without modification on almost any system platform on the market

  5. Scalability———— — If you need a critical piece of your code to run faster or want certain algorithms to be kept private, you can write parts of your program in C or C++ and use them in your Python program.

  6. Embeddability - You can embed Python into your C/C++ program to provide scripting functionality to your program users.

Disadvantages:

  1. Slow speed. Python’s running speed is indeed much slower than C language, and it is also slower than JAVA. Therefore, this is also the main reason why many so-called experts disdain to use Python, but in fact, the slow running speed referred to here cannot be directly perceived by users in most cases, and must be reflected with the help of testing tools. For example, if you use C It took 0.01s to run a program, and 0.1s in Python. In this way, C language is directly 10 times faster than Python, which is very exaggerated, but you cannot directly perceive it with the naked eye, because the time that a normal person can perceive is the smallest The unit is about 0.15-0.4s, haha. In fact, in most cases, Python can fully meet your program speed requirements, unless you want to write a search engine that has extremely high speed requirements. In this case, of course, it is recommended that you use C to implement it.

  2. The code cannot be encrypted because PYTHON is an interpreted language and its source code is stored in the form of text. However, I don’t think this is a disadvantage. If your project requires source code The code must be encrypted, so you shouldn't use Python to implement it in the first place.

  3. Threads cannot take advantage of the problem of multiple CPUs. This is one of the most criticized shortcomings of Python. GIL is the Global Interpreter Lock (Global Interpreter Lock), which is used by computer programming language interpreters to synchronize threads. Tools enable only one thread to be executed at any time. Python threads are native threads of the operating system. It is pthread on Linux and Win thread on Windows. The execution of the thread is completely scheduled by the operating system. A python interpreter process has a main thread and multiple user program execution threads. Even on multi-core CPU platforms, parallel execution of multi-threads is prohibited due to the existence of GIL. Regarding the compromise solution to this problem, we will discuss it in detail in the thread and process chapters later.


#2. Python installation

windows

1、下载安装包

2、安装
安装在C:\Python36
3、配置环境变量
【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用;分割】
linux:

1.解压
tar xf Python-3.6.2.tgz
2.编译安装
cd Python-3.6.2
./configure --prefix=/usr/local/Python3.6
make
make install
3.配置环境变量
vim /etc/profile
PATH=/usr/local/Python3.6/bin:$PATH
source /etc/profile
4.测试
python3.6

3. The first program (hello, world)

Open pycharm, create a new python project, and create a new python file.

#!/usr/binl/env python
#encoding: utf-8
#author: YangLei

print("hello,world")

4. Variables

Variables come from mathematics and are abstract concepts in computer language that can store calculation results or represent values. Variables can be accessed by variable name. In imperative languages, variables are usually mutable; in purely functional languages ​​(such as Haskell), variables may be immutable. In some languages, variables may be explicitly referred to as abstractions with storage space that can represent mutable state (such as in Java and Visual Basic); but other languages ​​may use other concepts (such as objects in C) to refer to such variables. Abstract, but not strictly define the precise denotation of "variable".

Declare variables

#!/usr/binl/env python
#encoding: utf-8
#author: YangLei

name = "yanglei"
print(name)
The above code declares a variable, the variable name is: name, and the value of variable name is: "yanglei"

Rules for variable definition:

    • Variable names can only be any combination of letters, numbers or underscores

    • The first character of the 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']

5. User input

#!/usr/binl/env python
#encoding: utf-8
#author: YangLei

input_name = input("Please enter your name: ")
print("hi,%s" % input_name)

6. Data type

1. Number

int (integer type)

On a 32-bit machine, the number of bits in the integer The number is 32 bits, and the value range is -2**31~2**31-1, that is, -2147483648~2147483647. On a 64-bit system, the number of integers is 64 bits, and the value range is -2**63 ~2**63-1, that is, -9223372036854775808~9223372036854775807

long (long integer)
Unlike C language, Python’s long integer does not specify the bit width, that is: Python has no limit The size of the long integer value, but in fact due to limited machine memory, the long integer value we use cannot be infinite.
Note that since Python 2.2, if an integer overflow occurs, Python will automatically convert the integer data to a long integer, so now not adding the letter L after the long integer data will not cause serious consequences.
 
Note: There is no longer long type in Python3, all are int
float (floating point type)
 Floating point numbers are used to process real numbers, that is, numbers with decimals. Similar to the double type in C language, it occupies 8 bytes (64 bits), of which 52 bits represent the base, 11 bits represent the exponent, and the remaining bit represents the symbol. 2. Boolean value
True or false
1 or 0
3. String
"hello world"
String splicing:
  python中的字符串在C语言中体现为是一个字符数组,每次创建字符串时候需要在内存中开辟一块连续的空,并且一旦需要修改字符串的话,就需要再次开辟空间,万恶的+号每出现一次就会在内从中重新开辟一块空间。
字符串格式化输出
#!/usr/binl/env python
#encoding: utf-8
#author: YangLei

name = "yanglei"
print("hi,%s" % name)

#输出hi,yanglei

注:字符串是 %s;整数 %d;浮点数%f

字符串常用功能:
  • 移除空白

  • 分割

  • 长度

  • 索引

  • 切片

4、列表
创建列表:
#!/usr/binl/env python
#encoding: utf-8
#author: YangLei

name = ["yanglei","jack","marry","andy"]

基本操作:

  • 索引

  • 切片

  • 追加

  • 删除

  • 长度

  • 切片

  • 循环

  • 包含

5、字典(无序)

创建字典:
#!/usr/binl/env python
#encoding: utf-8
#author: YangLei

user_info = {"name":"yanglei","age":23,"job":"IT"}

常用操作:

  • 索引

  • 新增

  • 删除

  • 键、值、键值对

  • 循环

  • 长度

 

七、数据运算  

算数运算:

比较运算:

赋值运算:

逻辑运算:

成员运算:

身份运算:

位运算:

运算符优先级:

  

八、if判断

场景一、用户登陆验证

#!/usr/binl/env python
#encoding: utf-8
#author: YangLei

input_user = input("Please enter your user name: ")
input_password = input("Please enter your password: ")
if input_user == "yanglei" and input_password == "123456":
    print("\033[32;1m%s login successfully\33[0m" % input_user)
else:
    print("\033[31;1mThe user name or password error,please try again\033[0m")

场景二、猜年龄游戏

在程序里设定好你的年龄,然后启动程序让用户猜测,用户输入后,根据他的输入提示用户输入的是否正确,如果错误,提示是猜大了还是小了

#!/usr/binl/env python
#encoding: utf-8
#author: YangLei

guess_age = 50
input_age = int(input("Please enter your guess age: "))
if input_age > guess_age:
    print("\033[31;1mCan you guess what big\33[0m")
elif input_age < guess_age:
    print("\033[31;1mCan you guess what small\33[0m")
else:
    print("\033[32;1mYou guessed it\33[0m")

外层变量,可以被内层代码使用

内层变量,不应被外层代码使用
 

九、break和continue的区别

continue:

#!/usr/binl/env python
#encoding: utf-8
#author: YangLei

count = 1
while count <= 10:
    if count == 5:
        count += 1
        continue
    print(count)
    count += 1

break:

#!/usr/binl/env python
#encoding: utf-8
#author: YangLei

count = 1
while count <= 10:
    if count == 5:
        count += 1
        break
    print(count)
    count += 1

 由此可以看出continue是跳出当前循环,而break是跳出本层循环。

 

十、while循环

循环语句,计算机的一种基本循环模式。当满足条件时进入循环,不满足跳出。while语句的一般表达式为:
while(表达式)
{
循环体
}

场景一、用户登陆验证升级

#!/usr/bin/env pyhon
#encoding: utf-8
#auth: yanglei

count = 0
while count < 3:
    input_user = input("Please enter your user name: ")
    input_password = input("Please enter your password: ")
    if input_user == "yanglei" and input_password == "123456":
        print("\033[32;1m%s login successfully\33[0m" % input_user)
        break
    elif count == 2:
        print("\033[31;1mThe user name or password mistake,three chances to use up,the program exits\33[0m")
        break
    else:
        count += 1
        print("\033[31;1mThe user name or password error,please try again\033[0m")

场景二、猜年龄游戏升级

 

#!/usr/bin/env pyhon
#encoding: utf-8
#auth: yanglei

guess_age = 50
count = 0
while count <= 3:
    if count == 3:
        input_choose = input("Do you want to continue to play?(Y or y|N or n)")
        if input_choose == "Y" or input_choose == "y":
            count = 0
            continue
        elif input_choose == "N" or input_choose == "n":
            break
        else:
            print("\033[31;1mAre you input errors!\33[0m")
            continue
    input_age = int(input("Please enter your guess age: "))
    if input_age > guess_age:
        print("\033[31;1mCan you guess what big\33[0m")
        count += 1
    elif input_age < guess_age:
        print("\033[31;1mCan you guess what small\33[0m")
        count += 1
    else:
        print("\033[32;1mYou guessed it\33[0m")
        break

 

The above is the detailed content of Introduction to the origin and use of Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Main Purpose of Python: Flexibility and Ease of UseThe Main Purpose of Python: Flexibility and Ease of UseApr 17, 2025 am 12:14 AM

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python: The Power of Versatile ProgrammingPython: The Power of Versatile ProgrammingApr 17, 2025 am 12:09 AM

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Learning Python in 2 Hours a Day: A Practical GuideLearning Python in 2 Hours a Day: A Practical GuideApr 17, 2025 am 12:05 AM

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.

Python vs. C  : Pros and Cons for DevelopersPython vs. C : Pros and Cons for DevelopersApr 17, 2025 am 12:04 AM

Python is suitable for rapid development and data processing, while C is suitable for high performance and underlying control. 1) Python is easy to use, with concise syntax, and is suitable for data science and web development. 2) C has high performance and accurate control, and is often used in gaming and system programming.

Python: Time Commitment and Learning PacePython: Time Commitment and Learning PaceApr 17, 2025 am 12:03 AM

The time required to learn Python varies from person to person, mainly influenced by previous programming experience, learning motivation, learning resources and methods, and learning rhythm. Set realistic learning goals and learn best through practical projects.

Python: Automation, Scripting, and Task ManagementPython: Automation, Scripting, and Task ManagementApr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Python and Time: Making the Most of Your Study TimePython and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Games, GUIs, and MorePython: Games, GUIs, and MoreApr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft