Introduction to Python variable types and naming rules

Source: Internet
Author: User
In Python programming, variables are containers used to store values or objects. Variable names can be customized. The basic naming rules are as follows: English letters and underscores :_), the variable name is case sensitive, that is, the variables temp and Temp are different variables. The basic usage of variables is as follows:

The code is as follows:


# Example: use variables
A = 10
B = 20
Print a + B
>>> 30 # output the value of A plus B
A = 'Hello'
B = 'Python'
Print a + ''+ B
>>> Hello python # output the value of A plus B

The preceding examples use variables for calculation. python variables can be divided into numbers, characters, and objects.

Number: it can be used for mathematical operations, and the number types are classified into integer, floating point, and plural. Integer refers to a number without a decimal point, while floating point refers to a number with a decimal point, and the plural is the resumption of mathematics. floating point numbers can be expressed by scientific notation. the specific differences are as follows:

The code is as follows:


# Example: use variables
A = 10
Print a/3
>>> 3 # dividing output variable a by the value of integer 3
Print a/3.0
>>> 3.33333333333 # The value of dividing output a by floating point number 3.0
B = 1e-2 # Scientific notation
Print B
>>> 0.01 # output the value of B
Print B * 10
>>> 0.1 # output the value of B * 10
F1 = (1 + 2j)
F2 = (5 + 3j)
>>> (6 + 5j) # output the value of the plural f1 + f2

In the preceding example, the numeric variable a is defined as an integer. when the value is divided by an integer, the value is considered an integer. Therefore, the output value night is an integer. when the divisor is a floating point, the value is considered as a floating point. Numeric operators include + (plus),-(minus), * (multiplication),/(except), and % (remainder ), however, the auto-increment and auto-subtraction operators such as ++ and -- are not supported.

Character: it refers to the content string represented by different characters. the string must be enclosed by single quotation marks and double quotation marks. the usage is as follows:

For example, define a variable of the sequence type.

The code is as follows:


S = 'Python' # variable assignment string Python
S = "17jo.com" # Variable value assignment string 17jo.com
S = ''' hello world!
Hello Python! ''' # Two rows of variable value assignment: hello world! Hello python!
S = "hello world!
Hello Python! "# Assign values to variables in two rows: hello world! Hello python!
S = 'it \'s Python' # Variable value assignment: It's Python!
S = "\" Python \ "" # variable assignment: "Python"
S = '"Python"' # variable assignment: "Python"
S = 'Hello \ n Python' # \ n is the escape character for line breaks
Print s # output s value
>>> Hello # output in two rows
>>> Python

The above example is an example of defining a string variable. The three quotation marks '''/"" can be used to define a multi-line string, if you want to use single or double quotation marks in a string, you can escape them with \ '/\ ", but double quotation marks can be used in single quotation marks, and single quotation marks can also be used in double quotation marks without escaping.

The scope of a variable refers to the valid range of the variable. in python, except for the variables defined in functions or classes, the variables defined in the program will remain valid after the first appearance, that is, the same name will be considered as the same variable in the subsequent program.

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.