5 Python Basics-Data types and variables

Source: Internet
Author: User
Tags mathematical constants

Data type

A computer is a machine that can do mathematical calculations as the name implies, so a computer program 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, of course, negative integers, which are represented in the program in the same way as mathematically, for example:,,, 1 100 , and so on -8080 0 .

Because the computer uses binary, it is sometimes convenient to use hexadecimal notation for integers, and hexadecimal is 0x represented by prefixes and 0-9,a-f, for example: 0xff00 ,, and 0xa5b4c3d2 so on.

Floating point number

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 , and -9.01 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

A string is any text enclosed in single or double quotation marks " , such as ‘abc‘ , and "xyz" so on. Note that ‘‘ or "" itself is just a representation, not part of a string, so the string is ‘abc‘ only a , b c this 3 characters. If it is also a character, it can be "" enclosed, such as the character that contains it,,, the space, the "I‘m OK" I m O K 6 characters.

What if the inside of a string contains and contains both " ? Can be identified by an escape character \ , such as:

' i\ ' m \ "Ok\"! '

The string content represented is:

I'm "OK"!

The

escape character \ can escape many characters, such as \ n for newline, \ t for tabs, and the character \ itself to be escaped, so \ \ The represents the character \ , which can be printed on the interactive command line of Python using print () :

Print ('i\ ' m OK. ' ) I'm OK. Print ('i\ ' m learning\npython. ' ) I'm learningPython. Print ('\\\n\\') \

If there are a lot of characters in the string that need to be escaped, you need to add a lot \ , in order to simplify, Python also allows to use r‘‘ ‘‘ A string that represents the internal default is not escaped:

Print ('\\\t\\') \        Print (R'\\\t\\') \\\t\\

If there is a lot of line wrapping inside the string, \n writing in one line is not good to read, in order to simplify, Python allows ‘‘‘...‘‘‘ the format to represent multiple lines of content:

Print ("'line1 ... line2 ... line3") Line1line2line3

The above is entered in the interactive command line, note that when you enter multiple lines of content, the prompt >>> becomes ... , prompting you to follow the previous line of input, note that the ... prompt is not part of the code:

Print ("'line1 ...                                      line2 ...                                               line3")                                           line1                                                   line2                                                   line3                                                           

```after entering the Terminator and parentheses ) , execute the statement and print the result.

If a program is written as a .py file, it is:

Print ('line1line2line3')

Multi-line strings ‘‘‘...‘‘‘ can also be preceded by the r use of:

Print (R' 'hello,\nworld')
Hello,\nworld
Boolean value

Boolean values are exactly the same as Boolean algebra, with a Boolean value of only two values, either, or, True False True False in Python, you can directly use True , represent a False boolean value (note case), or you can calculate it by Boolean:

>>> truetrue>>> falsefalse>>> 3 > 2True>>> 3 > 5False

Boolean values can be used and , or and not operations.

andOperations are and operations, and only all are True , the result of the and operation is True :

 and truetrue  and Falsefalse  and Falsefalse  and 3 > 1True

orAn operation is or an operation, as long as one of them is True , the result of the or operation is True :

or truetrue or falsetrue or Falsefalse or 1 > 3True

notAn operation is a non-operation, which is a single-mesh operator that turns True into False False True :

 not TrueFalse  not falsetrue  not 1 > 2True

Boolean values are often used in conditional judgments, such as:

if age >=:    print('adult')Else :     Print ('teenager')
Null value

A null value is a special value in Python, None denoted by. Nonecannot be understood as 0 , because 0 it is meaningful, and None is a special null value.

In addition, Python provides a variety of data types such as lists, dictionaries, and also allows you to create custom data types, which we'll continue to talk about later.

Variable

The concept of 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, a number, and _ cannot start with a number, such as:

A = 1

aA variable is an integer.

' T007 '

t_007A variable is a string.

Answer = True

AnswerA variable is a Boolean value True .

In Python, the equals sign = is an assignment statement that assigns any data type to a variable, the same variable can be repeatedly assigned, and can be a variable of different types, for example:

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, and assignment statements are as follows (//for comments):

int a = 123;  "ABC"; Error: Cannot assign string to integer variable

This is why dynamic languages are more flexible than static languages.

Do not equate an equal sign of an assignment statement with a mathematical equal sign. For example, the following code:

x = Ten= x + 2

If mathematically understood x = x + 2 that is not tenable in any case, in the program, the assignment statement evaluates the right expression first x + 2 , obtains the result 12 , and assigns the variable x . Since x the previous value is 10 , the value becomes after the value is re-assigned x 12 .

Finally, it is important to understand the representation of variables in computer memory. When we write:

' ABC '

, the Python interpreter did two things:

    1. A string is created in memory ‘ABC‘ ;

    2. A variable named in memory is created a and pointed to ‘ABC‘ .

You can also assign a variable a to another variable, which actually refers to the data that the variable points to b b a , such as the following code:

Results:

Abc

The last line prints out b the contents of the variable. ‘ABC‘ ‘XYZ‘ If it is mathematically understood, it will be wrong to draw the b same, and it a should be ‘XYZ‘ , but b the actual value is ‘ABC‘ , let us execute the code in one line, we can see exactly what happened:

Execution a = ‘ABC‘ , the interpreter creates a string ‘ABC‘ and a variable a , and a points to ‘ABC‘ :

Executes b = a , the interpreter creates the variable b and points to b a the string that points to ‘ABC‘ :

Execute a = ‘XYZ‘ , the interpreter creates the string ' XYZ ' and puts a the pointer instead ‘XYZ‘ , but b does not change:

So, the b result of the last print variable is naturally ‘ABC‘ .

Constant

So-called 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 it is still a variable, Python does not have any mechanism to ensure that it PI will not be changed, so, using all uppercase variable names to represent constants is only a customary usage, and if you must change PI the value of the variable, no one can stop you.

Finally, explain why the division of integers is also accurate. In Python, there are two types of division, one of which is / :

>>> 10/33.3333333333333335

/The division evaluates to a floating-point number, even if it is exactly divisible by two integers, and the result is a floating-point number:

>>> 9/33.0

There is also a division that is // called a floor divide, where the division of two integers is still an integer:

>>> 10//33
You are not mistaken, the whole number of floors apart // is always an integer, even if not endless. To do the exact division, you / can use it. 

Because // division takes only the integer part of the result, Python also provides a remainder operation that can be used to divide the remainder of two integers:

>>> 10% 31

The result is always an integer, regardless of whether the integer // divides or takes the remainder, so the result of the integer operation is always accurate.

Summary

Python supports a variety of data types, within a computer, you can think of any data 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.

Assigning x = y A variable is a pointer to a real object, which is the object to which the variable is x y pointing. Subsequent assignment of the variable y does not affect x the direction of the variable.

Note: There is no size limit for python integers, and integers in some languages are limited by size depending on their storage length, for example, Java has a limit of 32-bit integers -2147483648 2147483647 .

Python also has no size limit for floating-point numbers, but it is represented inf (infinitely large) beyond a certain range.

5 Python Basics-Data types and variables

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.