Data type of Day1_python

Source: Internet
Author: User
Tags true true

I. What mutable data types and immutable data types

Variable data type: value change, id value unchanged, immutable data type: value changed, id value changed.

how to determine whether a data type is mutable or immutable:

Depending on the concept of the mutable data type and the immutable data type, it is only possible to know whether the data type is mutable or immutable by changing the value of the value, using the ID () function to see if the variable id value changes.

Core tips:

mutable type Vs Immutable type

mutable type (mutable): list, dictionary

Immutable types (unmutable): Numbers, strings, tuples

The immutable variable here refers to whether the contents of the memory (value) can be changed

Second, what is the data type?

We humans can easily distinguish between numbers and characters, but the computer does not, although the computer is very powerful, but from a certain point of view and very silly, unless you explicitly tell it, 1 is the number, "Han" is the text, otherwise it is not clear 1 and ' Han ' difference, therefore, in each programming language will have a called data type of East, in fact, is the common data types are clearly divided, you want the computer to do numerical operations, you will pass the numbers to it, you want him to deal with the text, send him a string type.

2.1, int (integral type)

On a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647

On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807

Long (integer)

Unlike the C language, Python's long integers do not refer to the positioning width, that is, Python does not limit the size of long integer values, but in fact, because of limited machine memory, we use a long integer value can not be infinite.

Note that, since Python2.2, Python automatically converts integer data to long integers if an integer overflows, so it does not cause any serious consequences if you do not add the letter L after long integer data.

Note: There is no longer a long type in Python3, all int>>> a= 2**64>>> type (a)  #type () is the way to view data types <type ' long ' > >>> B = 2**60>>> type (b) <type ' int ' >
2.2. String

In Python, the quoted characters are considered to be strings!

>>> name = "Alex Li" #双引号 >>> age = "       #只要加引号就是字符串 >>> age2 =          #int >>>>& gt;> msg = ' My name is Alex, I am years old! '  #我擦, 3 quotes can also >>>>>> hometown = ' Shandong '   #单引号也可以

What's the difference between single quotes, double quotes, and multiple quotes? Let me tell you aloud that there is no difference between single and double quotes, only the following situation you need to consider a single pair of mates

msg = "My name is Alex, I ' m years old!"

What does a multi-quote function do? The function is that multiple lines of string must be in multiple quotes

msg = "" Today I want to write a poem, praise my deskmate, you see his black short hair, like a fried chicken. "Print (msg)

string concatenation

The numbers can be subtraction, and so on, what about strings? Let me tell you aloud, too, can I? What? Yes, but only the "add" and "multiply" operations can be performed.

>>> name ' Alex Li ' >>> age ' all ' >>>>>> name + age  #相加其实就是简单拼接 ' Alex Li22 ' >> >>>> name * #相乘其实就是复制自己多少次, then stitch together ' Alex lialex lialex lialex lialex lialex lialex lialex lialex lialex Li '

Note that the concatenation of strings can only be a string between the two, and cannot be spliced with numbers or other types

>>> type (name), type (age2) (<type ' str;, <type ' int ' >) >>>>>> name ' Alex Li ' > >> age222>>> name + age2traceback (most recent call last):  File "<stdin>", line 1, in <module& Gt Typeerror:cannot concatenate ' str ' and ' int ' objects #错误提示数字 and characters cannot be spliced
2.3. Boolean type (BOOL)

Boolean type is simple, two values, a true (true), a false (false), mainly in mind logic

But you don't really understand, do you? Let me explain, I now have 2 values, a=3, b=5, I say a>b you say set up? Of course we know it doesn't, but the question is how does a computer describe it as a non-establishment? Or that a< B is set up, how does the computer describe it?

Yes, the answer is, with a Boolean type

>>> a=3>>> b=5>>>>>> a > B #不成立就是False, i.e. false false>>>>>> a < b # The establishment is true, that is true true

Why do computers describe this condition? Because then you can do different things according to the result of the condition! Like what

If a > B   print (A is bigger than B) Else   print (A is smaller than B)

  

  

  

  

Data type of Day1_python

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.