Describe how to define a variable type in Python?

Source: Internet
Author: User
This article mainly describes how to define a python variablesvariablesThe concept is basically consistent with the equation variables of the junior algebra, just in the computer program, variablesNot only can it be a number, it can also be any data type

Variables are represented by a variable name in the program, and the variable name must be a combination of uppercase and lowercase English, numeric, and _, and cannot start with a number, such as:

A = 1

Variable a is an integer.

t_007 = ' T007 '

The variable t_007 is a string.

Answer = True

The variable Answer is a Boolean value of True.

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


#-*-Coding:utf-8-*-a = 123 # A is an integer print (a) a = ' ABC ' # a changed to string 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 is an integer type variable a = "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 = 10x = x + 2

If mathematically understood x = x + 2 that is not true anyway, in the program, the assignment statement first calculates the right expression x + 2, gets the result, and assigns it to the variable x. Since the value before x is re-assigned, the value ofx is changed to a number of ten.

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. Create a string of ' ABC ' in memory;

2. Create a variable named a in memory and point it to ' ABC '.

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.