Detailed explanation of Python variables and data types

高洛峰
Release: 2017-02-18 10:12:17
Original
1400 people have browsed it

This article mainly introduces Python variables and data types in detail. It has a very good reference value. Let’s take a look at it with the editor

Python variables and data types

  1. Data types in Python

  2. Print statement in Python

  3. Notes on Python

  4. What are variables in Python

  5. Definition of strings in Python

  6. raw strings and multi-line strings in Python

  7. Unicode strings in Python

  8. Integers and floating point numbers in Python

  9. Boolean types in Python

Data types in Python

As the name suggests, a computer is a machine that can do mathematical calculations. Therefore, computer programs can naturally handle various numerical values. However, computers can process much more than just numerical values. They can also process a variety of data such as text, graphics, audio, video, web pages, etc. Different data require the definition of different data types. In Python, the following data types can be processed directly:

1. Integers

Python can handle integers of any size, including negative integers of course. In Python programs, integers The representation method is exactly the same as the mathematical writing method, for example: 1, 100, -8080, 0, etc.

Since computers use binary, it is sometimes more convenient to use hexadecimal to represent integers. Hexadecimal is represented by the 0x prefix and 0-9, af, for example: 0xff00, 0xa5b4c3d2, etc.

2. Floating point numbers

Floating point numbers are also decimals. They are called floating point numbers because when expressed in scientific notation, the decimal point position of a floating point number is variable. For example, 1.23x10^9 and 12.3x10^8 are equal. Floating point numbers can be written mathematically, such as 1.23, 3.14, -9.01, etc. But for very large or very small floating point numbers, they must be expressed in scientific notation. Replace 10 with e. 1.23x10^9 is 1.23e9, or 12.3e8, 0.000012 can be written as 1.2e-5, and so on.

The way integers and floating-point numbers are stored inside the computer are different. Integer operations are always accurate (is division also accurate? Yes!), while floating-point operations may have rounding errors.

3. String

A string is any text enclosed in '' or "", such as 'abc', "xyz", etc. Please note that '' or "" itself is just a way of expression, not part of the string. Therefore, the string 'abc' only has 3 characters: a, b, c.

4. Boolean value

The representation of Boolean value and Boolean algebra is exactly the same. A Boolean value has only two values: True and False, either True or False. In Python, you can Use True and False directly to represent Boolean values ​​(please pay attention to the case), or they can also be calculated through Boolean operations.

Boolean values ​​can be operated with and, or and not.

The and operation is an AND operation. Only when everything is True, the result of the AND operation is True.

The or operation is an OR operation. As long as one of them is True, the result of the or operation is True.

The not operation is a non operation. It is a unary operator that turns True into False and False into True.

5. Null value

The null value is a special value in Python, represented by None. None cannot be understood as 0, because 0 is meaningful, and None is a special null value.

In addition, Python also provides a variety of data types such as lists and dictionaries, and also allows the creation of custom data types. We will continue to talk about it later

Practice code

1. 计算十进制整数45678和十六进制整数0x12fd2之和。

 print 45678 + 0x12fd2

2. 请用字符串表示出Learn Python in imooc。

 print "Learn Python in imooc"

3. 请计算以下表达式的布尔值(注意==表示判断是否相等):

 print 100 < 99
 print 0xff == 255

 结果:

 False
 True
Copy after login


For more detailed explanations of Python variables and data types, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!