Python Basics Tutorial Python variables and functions

Source: Internet
Author: User

Variable base

Variables are important in the programming language of the basic concepts, in all kinds of high-level languages are used to represent a piece of memory area, a moment in this area of the storage of ' a ', another moment may be stored as ' C ', it is because the block memory value can change at any time, we call this memory region of the symbol as a variable.

Variable Nature

Python programs also use variables to access the data in a block of memory, but the concept of variables in Python is somewhat different from the C language variables, first from the definition of the variable to distinguish the difference, in C language to define a variable, you need to specify the variable data type, when the variable is initialized, You also need to assign values according to the data type of the variable to the left of the equals sign, otherwise there will be data conversion operations, resulting in unimaginable errors, and Python does not have this requirement, in Python defined variables do not need to specify the variable data type, you can assign all kinds of data directly to the variable on the left side of the equal sign, more free , for example.

>>>x = 12>>>print X12>>>x = 13.09>>>print x13.09>>>x = "a1309b" >> >print x ' a1309b '

From the top of the program can be seen when the assignment Python does not care about the variable x data type is what, the developers who have learned C language know that the type of C variable is to allocate a certain size of the data memory area, and Python defines the variable when the variable type is not specified, then how can the longer data storage of the next? Let's look at a small program to explain why Python does not need to specify the type of the variable or "store" any length of data.

>>> x = 12>>> type (x) <type ' int ' >>>> x = ' + ' >>> type (x) <type ' str ' >> >> x = 12.03>>> type (x) <type ' float ' >>>> x = ' 12.0300203000404 ' >>> type (x) < Type ' str ' >

Note:The type function is the data type that returns the value of an expression in parentheses

The top code x is assigned an integer 12, the string ' 12 ', floating point data 12.03, but there is no problem, can be executed correctly, very strange! What if the data is large enough to save (exceeding 4, 8 bytes)? Let's look at the following procedure to analyze the difference between Python variables and C-language variables.

>>> x = 12>>> ID (x) 10416516>>> x = 13>>> ID (x) 10416504>>> x = ' >>&G ' T ID (x) 19428056>>> y = ' >>> id (y) 19428056>>> y = 13>>> id (y) 10416504>>> y = 12>>> ID (y) 10416516

Note:The ID function is to return the parentheses in the object in memory address, readers please use the ID output value as memory address, the value to the right of the equal sign as the memory of the storage content to draw a memory map analysis.

10416516 and 10416516, 10416504 appear multiple times.

>>> Help (ID) to built-in function ID in module __builtin__:id (...)  ID (object), integer Return the identity of an object.  This was guaranteed to be unique among simultaneously existing objects. (Hint:it ' s The object's memory address.) >>>

When x and Y are assigned a value of 12, the ID return value is the same, when X and Y are assigned a value of 13, the ID return value is the same, when X and Y are assigned the value of ' 12 ' when the return value of the ID is the same, but X or Y is assigned to 12, 13, ' 12 ' The return value of the This shows that Python allocates memory space to the right of the equals sign, and can point to the data by different variables. You can also use different variables to point to the same memory space to get the same value.

>>> x = 12>>> y = 13>>> x = y>>> x13>>> ID (x) 10416504>>> ID (y) 104165 04>>>

Can you change the data in the memory space that it is pointing to by using a variable? No!

>>> x = 12>>> ID (x) 10416516>>> x = 15>>> ID (x) 10416480

From the top results visible, did not change the value of 12 in the storage area, but also allocated a memory space to the data 15, that is, the variable is re-assigned to the other memory of a place, and the C language variable is the C variables are always pointing to a location, The C language can change the value it points to in memory by a variable, and the variable in Python is "pointing", and it can be thought that the variables in Python are equivalent to the concept of pointers in the C language. So both C and Python have the concept of variables, but the "change" of the variables in two languages is different!


Python Basics Tutorial Python variables and functions

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.