Python2 Learning data types and variables

Source: Internet
Author: User
Tags mathematical constants

Data types and variable data types

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"!

An escape character \ can escape many characters , such as a \n newline, a tab, and the \t character itself, \ so \\ the character that is \ represented is You can look at the interactive command line in Python with a print() printed string:

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 inside of the default is not escaped , you can try it yourself:

>>> print(‘\\\t\\‘)\ >>> print(r‘\\\t\\‘)\\\t\

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

>>> 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 enter the previous line. If a program is written, it is:

print(‘‘‘line1line2line3‘‘‘)

Multi-line strings ‘‘‘...‘‘‘ can also be used in front of each other r , please test yourself.

Boolean value

A Boolean value is exactly the same as a Boolean algebra, with a Boolean value of only True False two values, either, True or False , in Python, you can directly use True and False represent a Boolean value (note case). It can also be computed by Boolean operations:

>>> 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 :

>>> True and TrueTrue>>> True and FalseFalse>>> False and FalseFalse>>> 5 > 3 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 :

 >>> true or truetrue>>>  True or falsetrue> >> false or false false>>> 5 > 3 span class= "keyword" >or 1 > 3true   

An operation is a not 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 >= 18:    print(‘adult‘)else: print(‘teenager‘)
Null value

A null value is a special value in Python, None denoted by . None cannot 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.

t_007 = ‘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:

123 # a是整数print(a)a = ‘ABC‘ # a变为字符串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, and assignment statements are as follows (//For comments):

int a = 123; // a是整数类型变量a = "ABC"; // 错误:不能把字符串赋给整型变量

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 = 10x = 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:

a = ‘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 b b a the data that the variable points to, such as the following code:

‘ABC‘b = aa = ‘XYZ‘print(b)

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.

Practice

Please print out the values of the following variables:

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 .

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, but beyond a certain range it is represented as inf (infinitely Large).

Feel this site content is good, after reading have harvest?

Python2 Learning 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.