In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "what are the use of python data types and variables". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the ways to use python data types and variables?"
Data type
As the name implies, a computer is a machine that can do mathematical calculations, so computer programs can naturally deal with all kinds of numerical values. However, the computer can deal with much more than numerical values, but also can deal with text, graphics, audio, video, web pages and other kinds of data, different data, need to define different data types. In Python, there are several types of data that can be processed directly:
Integer number
Python can handle integers of any size, including negative integers, and its representation in the program is exactly the same as that in mathematics, such as: 1meme 100memlle 8080ju, etc.
Because computers use binary, it is sometimes convenient to use hexadecimal to represent integers. Hexadecimal is denoted by 0x prefix and 0-9 meme, such as 0xff00.0xa5b4c3d2, and so on.
Floating point number
Floating-point numbers are decimal numbers. They are called floating-point numbers because the position of the decimal point of a floating-point number is variable according to scientific notation. For example, 1.23x109 and 12.3x108 are exactly equal. Floating-point numbers can be written mathematically, such as 1.23, 2.14, 3.14, 9.01, etc. But for very large or very small floating-point numbers, it must be expressed by scientific counting, replacing 10 with e, 1.23x109 is 1.23 billion, or 12.3e8 0.000012 can be written as 1.2e-5, and so on.
Integers and floating-point numbers are stored differently inside the computer, and integer operations are always accurate (is division also accurate? Right! While floating-point operations may have rounding errors
String
A string is any text enclosed in single or double quotation marks, such as' abc', "xyz, and so on. Note that''or''is only a representation in itself, not part of a string, so the string 'abc' has only three characters: a _ line _ b _ line _ c. If 'itself is a character, then it can be enclosed with "". For example, the characters contained in "Isimm OK" are six characters, I. E., I. E., I. M., spaces, and O.
What if the string contains both 'and'? it can be identified by the escape character\, such as:
'i\ m\ "OK\"!'
The contents of the string represented are:
Ichimm "OK"!
The escape character\ can escape many characters, such as\ n for line feeds,\ t for tabs, and the character\ itself needs to be escaped, so the character represented by\\ is\. You can print the string with print () on the interactive command line of Python:
> print ('I\'m ok.') I\ m ok. > print ('I\ m learning\ nPython.') I\ m learningPython. > print ('\\ n\\)\\
If there are many characters in the string that need to be escaped, you need to add a lot of\. To simplify, Python also allows the string inside the string to be unescaped by default. You can try it yourself:
> print ('\ t\\)\ > print (r'\ t\\)\\ t\\
If there are many line breaks inside the string, it is difficult to read on one line with\ n.For simplicity, Python allows the use of''...''. Represents multiple lines of content, you can try it for yourself:
> print (''line1... line2... line3''') line1line2line3
Multiline string''.'' You can also add r to the front,
> print (r'''hello,\ n... world... qw123123''') hello,\ nworldqw123123
The language in which the variable itself is not fixed is called the dynamic language, which corresponds to the static language. A static language must specify a variable type when defining a variable, and an error will be reported if the type does not match when assigning. For example, Java is a static language, and the assignment statement is as follows (/ / for comments):
Int a = 123; / / an is an integer type variable a = "ABC"; / / error: cannot assign a string to an integer variable
This is why dynamic languages are more flexible than static languages.
Please do not equate the equal sign of an assignment statement with the equal sign of mathematics. For example, the following code:
X = 10x = x + 2
If it is mathematically understood that x = x + 2 is not true anyway, in the program, the assignment statement first evaluates the expression x + 2 on the right, gets the result 12, and then assigns it to the variable x. Since the previous value of x is 10, after re-assignment, the value of x becomes 12.
Finally, it is also important to understand the representation of variables in computer memory. When we write:
A = 'ABC'
The Python interpreter does two things:
A string of 'ABC'' is created in memory
Create a variable named an in memory and point it to 'ABC'.
You can also assign one variable a to another variable b, which actually points the variable b to the data that the variable a points to, such as the following code:
#-*-coding: utf-8-*-
Run
The last line prints out whether the content of the variable b is' ABC' or 'XYZ'?. If we understand it mathematically, we will mistakenly conclude that b is the same as a, and it should also be 'XYZ', but in fact, the value of b is' ABC',. 'let's execute the code line by line, and we can see what's going on:
The execute a = 'ABC', interpreter creates the string' ABC' and the variable a, and points a to 'ABC':
Executing b = a, the interpreter creates the variable b and points it to the string 'ABC':' that a points to.
Executing a = 'XYZ', interpreter creates the string' XYZ', and changes the point of a to 'XYZ', but b does not change:
So, in the end, the result of printing the variable b is naturally 'ABC'.
Constant
The so-called constant is an immutable variable. For example, the commonly used mathematical constant π is a constant. In Python, constants are usually represented by all uppercase variable names:
PI = 3.14159265359
But in fact, PI is still a variable, and Python does not have any mechanism to guarantee that PI will not be changed, so using all uppercase variable names to represent constants is just an idiomatic usage, and if you have to change the value of the variable PI, no one can stop you.
Finally, explain why the division of integers is also accurate. In Python, there are two kinds of division, one of which is /:
> 10 / 33.333333333333333335
The result of / division is a floating point number, and even if two integers are exactly divided, the result is a floating point number:
> > 9 / 33.0
Another division is /, which is called floor division, and the division of two integers is still an integer:
> > 10 / / 33
You read it correctly, the floor division of an integer is always an integer, even if it cannot be divided. To do precise division, you can use /.
Because / / division takes only the integer part of the result, Python also provides a remainder operation to get the remainder of the division of two integers:
> > 10% 31
Whether an integer does / / divide or take the remainder, the result is always an integer, so the result of an integer operation is always accurate.
At this point, I believe that everyone on the "python data types and variables of the use of what" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 206
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.