Python variable Usage Details

Source: Internet
Author: User

When using Python variables, you need to learn in detail, because in many places you need to use related variables. If you can understand this sentence, you have a good understanding of Python variables and data types.

Let's look at the differences between C # And Python in terms of variables and data types. If you still have doubts, read this chapter.

Python variable declaration and definition

Unlike C #, Python does not need to define its type before using a variable. Run the following example:

 
 
  1. i = 12 print i 

We can see from the above that variable I does not need to be defined before use, but must be declared and initialized. Run the following example:

 
 
  1. i = 1 
  2. print i + j 

The above code will generate an exception: "NameError: name 'J' is not defined". The Python prompts that the variable j is not defined. This is different from weak languages such as BASIC. In BASIC, no exception is generated when the above code is executed. You can try it in the excel vba development environment and change print to MsgBox. The result is 1. This shows that Python is not a weak language similar to BASIC.

On the other hand, there is a big difference between Python and C #, that is, when the program is running, the same variable name can be in different stages) represents different types of data. Let's take a look at the following example:

 
 
  1. i = 1   
  2. print i,type(i),id(i)  
  3. i = 10000000000 
  4. print i,type(i),id(i)  
  5. i = 1.1  
  6. print i,type(i),id(i) 

The type of variable I has undergone int, long, and float changes during program execution, which is very different from that of static type languages such as C. As long as a variable obtains a data type, the static language will always be of this type. The variable name represents the memory location used to store data. The variable names used in Python are only references of various types of data and objects, and the memory location for storing data is obtained by using id, the input data 1, 10000000000, and 1.1 are stored in the memory locations indicated by id, until the Garbage Collector pulls it away when the system determines you no longer use it ). This is a typical feature of dynamic language. It determines the type of a variable when assigning values to it.

On the other hand, Python is strongly typed. Try to run the following example:

 
 
  1. #-*-Coding: UTF-8 -*-
  2. I = 10; j = 'ss'
  3. Print I + j
  4. # Print str (I) + j, output 10ss

An exception occurs: "TypeError: unsupported operand type (s) for +: 'int' and 'str '". In BASIC and other weak languages, the above example will run normally and return results, although sometimes unexpected.

Therefore, Python is both a dynamic language and a strong language, which is different from C. C # programmers may spend some time adapting to the declaration, definition, and usage of Python variables, but I believe you will soon like it, because it makes things easier and insecure ). In addition, C #4.0 has begun defining and using variables in a similar way by adding the keyword dynamic before the variable name). If you have learned Python variables first, it will be able to adapt to the dynamic programming features of c #4.0 faster.

  • Python display UTF-8 Chinese Text specific operation method
  • Main features of Python objects
  • Summary of Python experience
  • Python scripts solve difficulties in Game Development
  • Adaptation and processing of related files in Python

Naming rules for Python Variables

The naming rules for Python and C # variables, functions, classes, and other identifiers are basically the same, and are also case sensitive. The difference is that the identifier that begins or ends with an underscore in Python usually has a special meaning. For example, an identifier (such as _ foo) Starting with an underscore cannot be imported using the from module import * Statement. The identifier with two underscores (_ init __) is retained by special methods. The front side has two underline identifiers, such as _ bar, which are used to implement class private attributes. This will be mentioned in "class and object-oriented programming.

Finally, the keyword of Python cannot be used as the identifier.) But the keyword of Python is much less than that of C #. You can google it and it will not be listed here.

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.