Introduction to Python variable types and naming conventions

Source: Internet
Author: User
The initials are English and underlined, others can be English, numbers, and underscores (that is, _), while variable names are case-sensitive, that is, the variable temp and temp are different variables. The basic usage of variables is as follows:

Copy the Code code as follows:


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

The above examples are the use of variables to operate, Python variables can be divided into numbers, characters and three kinds of objects.

Numbers: It is possible to make mathematical arithmetic numbers, and the types of numbers are divided into integral type, floating point type and complex number. Integer refers to a number without a decimal point, and floating point refers to the number of decimal points, the plural is the mathematics of the resume, in which floating-point numbers can be expressed in scientific notation, the specific difference is as follows:

Copy the Code code as follows:


# Example: Using variables
A = 10
Print A/3
>>> 3 # Output variable a divided by the value of integer 3
Print a/3.0
>>> 3.33333333333 # Output A divided by the value of the floating-point number 3.0
b = 1e-2 # scientific notation
Print B
>>> 0.01 # value of output B
Print b*10
>>> 0.1 # Output B*10 value
F1 = (1+2j)
F2 = (5+3j)
>>> (6+5j) # Outputs the value of a complex f1+f2

In the above example, the number variable A is defined as shaping, and when divided by the shape, the value is considered to be shaped, so the output programmed is integer, and when the divisor is floating point, the value that is removed is considered floating point. The number operation symbol has + (plus),-(minus), * (multiply),/(except),% (remainder), but does not support the operator of self-increment minus + + 、--.

Characters: That is, the string of content expressed in different text symbols, the strings need to be enclosed in single quotation marks, double quotation marks, as follows:

Example: Define a character type variable.

Copy the Code code as follows:


s = ' python ' # variable assignment string Python
s = "17jo.com" # variable assignment string 17jo.com
s = "' Hello world!
Hello python! ' # variable is assigned a value of two lines: Hello World!hello python!
s = "" "Hello world!
Hello python! "" # Variable assignment two line: Hello World!hello python!
s = ' it\ ' Python ' # variable assignment: It ' s python!
s = "\" python\ "" # Variable Assignment: "Python"
s = ' python ' ' # variable assignment: ' Python '
s = ' Hello \ n python ' # \ n is a newline escape character
The value of print S # output S
>>> Hello # two lines of output
>>> python

The above example is an example of defining a string variable, where the "/" "" "quotation mark can define a multiline string, if you need to use a single quotation mark or double quotation marks in the string to escape the representation, but the single quotation mark can use the word Fu Di quotation marks, and double quotation marks can also use character single quotes without escaping.

The scope of a variable refers to the valid range of variables, in Python, except for variables defined in the function or class, the variables defined in the program will be valid after the first occurrence, i.e. the same name will be considered 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.