search
HomeBackend DevelopmentPython TutorialAutomated testing: several common programming patterns in Python
Automated testing: several common programming patterns in PythonApr 13, 2023 pm 09:04 PM
pythontype of dataprogramming mode

Automated testing: several common programming patterns in Python

This chapter updates the content related to "Python syntax specifications and data types", mainly to let everyone understand what types of programming modes Python has, master the basic syntax of Python, and understand How to output and the basic application of command line parameters. After understanding the data types of Python, you can do more related operations.

Common programming patterns

①Python interactive command programming.

②Python script programming.

③Chinese encoding processing.

1. Interactive command programming mode

The interactive command programming mode is a typical line-by-line reading execution mode.

This programming mode is a typical application when the program has only one line or less.

The following figure uses the PythonIDLE editor for programming, and the programming mode of this editor is a typical interactive command encoding symbol.

>>> is the prompt for entering interactive commands. Each time you press Enter after completing the input, the command will be executed by the Python parser.

Automated testing: several common programming patterns in Python

2. Script programming mode

When we need to write more complex or large sections of code, imperative programming is not convenient enough.

Therefore, Python provides a script programming mode. You can create a script file with the suffix *.py and write a large amount of code into the file, which facilitates the maintenance and update of the code. You can then use interactive commands or IDE tools to run it.

Automated testing: several common programming patterns in Python

3. Character programming

String is a data type. However, strings have a special encoding problem.

Because computers can only process numbers, if you want to process text, you must first convert the text into numbers before processing.

Supplement: History of the development of character encoding

The earliest computers used 8 bits as a byte during design. Therefore, the largest integer that can be represented by a byte That is 255 (binary 11111111 = decimal 255). If you want to represent a larger integer, you must use more bytes. For example, the maximum integer that can be represented by two bytes is 65535, and the maximum integer that can be represented by 4 bytes is 4294967295.

Since the computer was invented by Americans, only 127 characters were encoded into the computer at first, that is, uppercase and lowercase English letters, numbers and some symbols. This encoding table is called ASCII encoding, such as uppercase letters The code for the letter A is 65, and the code for the lowercase letter z is 122.

Automated testing: several common programming patterns in Python

Extension: unicode character set

The reason why Python3 can solve the Chinese garbled problem well is that it uses unicode for all strings Character Encoding.

● Unicode unifies all languages ​​into one set of codes so that there will be no garbled characters.

● Unicode is also constantly developing, but the most commonly used one is to use two bytes to represent a character (if you encounter a very rare character, you need 4 bytes). Most operating systems and most programming languages ​​we see now support unicode.

ASCII encoding is 1 byte, while Unicode encoding is usually 2 bytes.

Extension: UTF-8 character set

New problems arise again: If unified into Unicode encoding, the garbled code problem will disappear. However, if the text you write is basically all in English, Unicode encoding requires twice as much storage space as ASCII encoding, which is very uneconomical in terms of storage and transmission.

The birth of the solution: The UTF-8 encoding that converts Unicode encoding into "variable length encoding" appeared again.

● UTF-8 encoding encodes a Unicode character into 1-6 bytes according to different number sizes. Commonly used English letters are encoded into 1 byte, and Chinese characters are usually 3 bytes. Only Very rare characters will be encoded into 4-6 bytes.

● If the text you want to transmit contains a large number of English characters, using UTF-8 encoding can save space.

● UTF-8 encoding has an additional benefit, that is, ASCII encoding can actually be regarded as part of UTF-8 encoding. Therefore, a large number of historical legacy software that only supports ASCII encoding can be encoded in UTF-8 Keep working while coding.

Special note: Unicode encoding is used uniformly in computer memory.

python3 character encoding

In the Python3 version, strings are encoded in Unicode, which means that Python strings support multiple languages.

For the encoding of a single character, Python provides the ord() function to obtain the decimal integer representation of a single character, and the chr() function to convert the encoding into the corresponding character.

Example:

>>> ord(‘A’)
65
>>> ord(‘中’)
20013
>>> chr(66)
‘B’
>>> chr(25991)
‘文’

Python source code is also a text file, so when your source code contains Chinese, you need to specify the UTF-8 encoding when saving the source code. When the Python interpreter reads source code, in order for it to be read in UTF-8 encoding, we usually write this line at the beginning of the file.

#-*- coding:utf-8 *-

The comment is to tell the Python interpreter to read the source code according to UTF-8 encoding. Otherwise, the Chinese output you write in the source code may be garbled.

The above is the detailed content of Automated testing: several common programming patterns in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)