Home  >  Article  >  Backend Development  >  Introduction to the origin and use of Python

Introduction to the origin and use of Python

零下一度
零下一度Original
2017-07-18 15:16:402195browse

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