How to use python variable data types and operators
1 Data type
Different types of variables can perform different operations, so the type of the variable must be understood. Data types in python can be divided into:
Built-in types :
Numeric type: Integer type int, floating point type float, complex number (complex) 3 5j
str:String
bool:Boolean value [True, False]
None: Null value, indicating that the variable has no determined value
list: List
tuple:tuple
dict:dict
set:Collection
Custom type:
##class: Class
Numeric type:
Integer type (int): There is only one type of int in python3, which can represent integers, for example: 10, -5, 10000
Floating point type (float): represents a real number with a decimal point, There are two representations:
- Decimal representation: 1.9 .23
- Scientific notation: Use e to represent the exponent of 10 , 1e2 represents 100. Note that e must be preceded by a numerical value, and e must be followed by an integer
Complex: represents an irrational number in mathematics , in the form: a bj
Boolean type (bool): represents the two states of the transaction, male and female, cloudy and sunny, light and dark Wait, it has only two values: True, False
None: represents an empty object, generally used for judgment, different from 0 and empty characters
String (str): In python, use quotes (single quotes, double quotes, triple quotes) to represent strings
Representation of string:
# Use single quotes to express: 'hello'# Use double quotes to express: "I use python"
# Use 3 A single quotation mark means: it can represent multi-line text, for example:
'''The great
's
motherland
'''
# It means 3 double quotation marks: it can represent multi-line text , for example:
"""Be optimistic about life and death,
Just do it if you don't accept it"""
Escape characters: Some special characters cannot Input from the keyboard can be represented by escape characters. In addition, whether it is a single quotation mark, double quotation mark or triple quotation mark string, the quotation mark is the string delimiter, and the quotation mark is not the content of the string. So how to enter a single quotation mark string? Indicates a single quote, which can also be expressed using escape characters. Common escape characters
Escape characters | Description | Escape characters | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
##\' | represents an ordinary character single quote \n | line break |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\" | represents an ordinary character double quote \r | ##Enter | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
\\ |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
\a | Ring | ##\t |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\b | Delete one character back |
The computer cannot store it directly String, but we can encode the characters, for example, use 65 to represent uppercase character A, 66 to represent uppercase character B... etc. This representation method is the American ASCII code, which can only represent 127 characters, but for Americans That's enough. Once we can use integers to represent characters, we can easily represent integers in binary, and strings can be easily stored in the computer. 1.2 Type judgmentWe can use type and isinstance to test and judge the data type #type用法: type(obj) 功能:返回obj的数据类型 参数:obj是你要测试变量或数值 示例: age = 10 name = 'hello' print(type(name),type(age)) #判断变量是否是指定类型 if type(age) is int: print('是') else: print('否') #isinstance用法: isinstance(obj,typename) 功能:判断obj是否是指定类型,是返回True,否返回False 参数: objobj是你要判断的变量或数值 typename是指定数据类型,可以是int,float,str等。也可是一个 类型的元组,例如:(int,float) 示例: age = 10 name = 'hello' print(isinstance(age,int)) print(isinstance(name,(str,int)) #只要name是str或int的一种就返回True if isinstance(age,int): print('是') else: print('否') #type和isinstance的区别 type判断基本类型是没问题的,但无法判断子类对象是父类的一种 isinstance可以判断子类对象是父类的一种 class A: pass class B(A): pass objA = A() objB = B() #输出否 if type(objB) is A: print('是') else: print('否') print(isinstance(objB,A)) #True Copy after login Conclusion:Use first isinstance 2. Operators and expressionsIn order to calculate the results in mathematics, we will write some formulas to calculate, for example:
This is a calculation formula in mathematics. There are similar formulas in python for calculation, called expressions. In the expression, 30 and 5 are called operands, called operators. The purpose of an expression is to compute a result. Expression composition:
Based on the above, the so-called expression is composed of operands and operators An expression that conforms to python syntax. To write expressions, you first need to learn operators. There are two things to know about operators
2.1 Arithmetic operatorsa = 20 b = 10 Copy after login
注意:
2.5 赋值运算符
注意:
a = 2 b = 3 a *= b + 2 #等价于 a = a * (b + 2) print(a) # a = 10 Copy after login 2.6 关系运算关系运算就是比较运算,如果表达式成立,返回True,否则返回False。关系运算的结果是布尔值。
注意:
2.7 逻辑运算逻辑运算符可以用于构造复杂条件。逻辑运算符包括:
在逻辑运算中,False、None、0、0.0、‘’(空字符串)被看做假(False),其它的看做真(True) 2.7.1 逻辑与
2.7.2 逻辑或
2.7.3 Logic Not
Summary: If a is true, the expression is False, otherwise the expression is True 2.8 Short-circuit calculation
2.9 Notes
|
The above is the detailed content of How to use python variable data types and operators. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

MySQL download file is corrupt, what should I do? Alas, if you download MySQL, you can encounter file corruption. It’s really not easy these days! This article will talk about how to solve this problem so that everyone can avoid detours. After reading it, you can not only repair the damaged MySQL installation package, but also have a deeper understanding of the download and installation process to avoid getting stuck in the future. Let’s first talk about why downloading files is damaged. There are many reasons for this. Network problems are the culprit. Interruption in the download process and instability in the network may lead to file corruption. There is also the problem with the download source itself. The server file itself is broken, and of course it is also broken when you download it. In addition, excessive "passionate" scanning of some antivirus software may also cause file corruption. Diagnostic problem: Determine if the file is really corrupt

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

MySQL refused to start? Don’t panic, let’s check it out! Many friends found that the service could not be started after installing MySQL, and they were so anxious! Don’t worry, this article will take you to deal with it calmly and find out the mastermind behind it! After reading it, you can not only solve this problem, but also improve your understanding of MySQL services and your ideas for troubleshooting problems, and become a more powerful database administrator! The MySQL service failed to start, and there are many reasons, ranging from simple configuration errors to complex system problems. Let’s start with the most common aspects. Basic knowledge: A brief description of the service startup process MySQL service startup. Simply put, the operating system loads MySQL-related files and then starts the MySQL daemon. This involves configuration
