Python data types and variables

Source: Internet
Author: User
Tags mathematical constants null null

Data type a computer is a machine that can do mathematical calculations as the name implies, so that computer programs can handle a variety of values. However, the computer can handle far more than the numerical value, but also can deal with text, graphics, audio, video, web 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 python can handle integers of any size, including negative integers, which are represented in the program in the same way as mathematically, for example: 1,100,-8080,0, and so on. Computer because of the use of binary, so, sometimes hexadecimal notation is more convenient, hexadecimal with 0x prefix and 0-9,a-f, such as: 0XFF00,0XA5B4C3D2, and so on. Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation, the decimal position of a floating-point number is variable, for example, 1.23x109 and 12.3x108 are exactly equal. Floating-point numbers can be written in mathematical notation, such as 1.23,3.14,-9.01, and so on. But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23x109 is 1.23e9, or 12.3e8,0.000012 can be written 1.2e-5, and so on. Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors. string strings are arbitrary text enclosed in single quotes ' or double quotes, such as ' abc ', ' XYZ ', and so on. Note that the ' or ' itself is only a representation, not a part of the string, so the string ' abc ' only a,b,c these 3 characters. If ' itself is also a character, then you can use "", such as "I m OK" contains the characters are I, ', M, space, o,k these 6 characters. What if the inside of a string contains both ' and contains '? You can use the escape character \ To identify, for example:
‘I\‘m \"OK\"!‘
The string content represented is:
I‘m "OK"!
The escape character \ can escape a lot of characters, such as \ n for newline, \ t for tab, character \ itself to escape, so \ \ means the character is \, you can print the string in Python's interactive command line () to see: >>> 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 \, in order to simplify, Python also allows to use R ' ' to indicate that the string inside of ' is not escaped by default, You can try it yourself: >>> print (' \\\t\\ ') \       \>>> print (R ' \\\t\\ ') \\\t \ \ If there is a lot of line wrapping inside the string, written in a line is not good to read, in order to simplify, Python allows the "" ... "format to express multiple lines of content, you can try: >>> print (" "Line1 ... line2 Line3 ") Line1line2line3 above is entered in the interactive command line, note that when you enter multiple lines of content, the prompt changes from >>> to ..., prompting you to enter on the previous line. If written as a program, it is:  print ("" Line1line2line3 ") multi-line string" "..." can also be used in front of the use of R, please test yourself. The   Boolean Boolean value is exactly the same as the Boolean algebra, with a Boolean value of only true, false two, or true, or false, in Python, which can be used to denote a Boolean value directly with True and false (note case). can also be calculated by Boolean operations: >>> truetrue>>> falsefalse>>> 3 > 2true>>> 3 > 5False Boolean values can be operated with and, or, and not. The  and operation is associated with an operation, and only the result of all True,and operations is true: >>> true and truetrue>>> true And falsefalse>>> False and falsefalse>>> 5 > 3 and 3 > 1Trueor operations are or operations, as long as one of them is the result of a true,or operation is TRUE:&N Bsp;>>> true or truetrue>>> true or falsetrue>>> False or falsefalse>>> 5 > 3 or 1 > 3Truenot operation is a non-operation, it is a monocular operator, turning true into False,false to true: >>> not truefalse>>> not falsetrue >>> not 1 > 2True Boolean values are often used in conditional judgments, such as: Age=?if Age >= 18:    print (' adult ') else:  & nbsp Print (' teenager ') NULL null value is a special value in Python, denoted by none. None cannot be understood as 0, because 0 is meaningful, and none is a special null value.   Python also provides a variety of data types, such as lists, dictionaries, and so on, and allows you to create custom data types that we'll continue to talk about later. The concept of   variable variables is basically the same as the equation variables in junior algebra, but in computer programs, variables can be not only numbers, but also arbitrary data types.   variables are represented by a variable name in the program, and the variable name must be a combination of uppercase and lowercase English, numeric, and _, and cannot start with a number, such as: 
a = 1
Variable A is an integer.
t_007 = ‘T007‘
The variable t_007 is a string.
Answer = True
The variable answer is a Boolean value of true. In Python, equals = is an assignment statement, you can assign any data type to a variable, the same variable can be repeatedly assigned, and can be different types of variables, for example: a = 123 # A is an integer print (a) a = ' ABC ' # A changed to a string print (a) This type of variable itself is called Dynamic language, which corresponds to static language. Static languages must specify the variable type when defining the variable, and if the type does not match, an error is given. For example, Java is a static language, the assignment statement is as follows (//For comment): int a = 123; A is an integer type variable a = "ABC"; Error: You cannot assign a string to an integer variable compared to a static language, which is why dynamic languages are more flexible. Do not equate an equal sign of an assignment statement with a mathematical equal sign. For example, the following code: x = 10x = x + 2 If mathematically understood x = x + 2 That is not true in any case, in the program, the assignment statement first calculates the right expression X + 2, obtains the result 12, and assigns to the variable x. Since the value before X is 10, the value of X becomes 12 after the value is re-assigned. Finally, it is important to understand the representation of variables in computer memory. When we write:
a = ‘ABC‘
, the Python interpreter did two things:
    1. A string of ' ABC ' was created in memory;

    2. A variable named A is created in memory and points to ' ABC '.

You can also assign a 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: A = ' abc ' b = AA = ' xyz ' Print (b) The last line prints out the contents of variable B is ' ABC ' or ' xyz '? If we understand mathematically, we will mistakenly conclude that B and a are the same, and should be ' XYZ ', but actually the value of B is ' abc ', let us execute the code in one line, we can see exactly what happened: execute a = ' abc ', the interpreter created the string ' ABC ' and variable A, and pointed a to ' ABC ': Execute B = A, the interpreter creates the variable B and points B to the string ' abc ' that points to a: execute a = ' xyz ', the interpreter creates the string ' xyz ' and changes the point of a to ' XYZ ', but B does not change: So, the result of printing variable B is naturally ' abc '. Constant constants are immutable variables, such as the usual mathematical constants π is a constant. In Python, constants are typically represented in all uppercase variable names:
PI = 3.14159265359
But in fact pi is still a variable, Python does not have any mechanism to ensure that PI will not be changed, so, using all uppercase variable names to represent constants is only a customary usage, if you must 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 divisions, one division is/: >>> 10/33.3333333333333335/Division computes a floating-point number, even if two integers are evenly divisible, the result is also a floating-point number: >>> 9/ 33.0 There is also a division is//, called the division of the floor, two integers are still integers: >>> 10//33 You did not read wrong, the whole number of the floor except//is always an integer, even if not endless. To do the exact division, use/can. Because//division takes only the integer part of the result, Python also provides a remainder operation, which gives the remainder of dividing two integers: >>> 10 31 The result is always an integer, regardless of whether the integer is//division or the remainder, so the result of the integer operation is always accurate. Summary Python supports a variety of data types, within a computer, any data can be regarded as an "object", and variables are used in the program to point to these data objects, the variable is assigned to the data and variables to associate. Note: There is no size limit for python integers, and integers for some languages are limited by size depending on their storage length, such as Java's range of 32-bit integers is limited to-2147483648-2147483647. Python also has no size limit for floating-point numbers, but is represented as INF (infinity) beyond a certain range.

Python data types and variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.