Python from rookie to expert (3): declaring variables

Source: Internet
Author: User

Variables (variable) are a very important concept in the Python language. The main function of a variable is to give a name to a value in a Python program. Similar to "Zhang San", "John Doe", "King two leper" the same name, easy to remember.

?? In the Python language, the declaration of a variable needs to be assigned a value, after all, a variable that does not represent any value is meaningless, and the Python language does not allow such a variable.

?? Declaring a variable is also very simple, with the syntax structured as follows:

variable_name = variable_value

?? The left side of the equals sign (=) is the variable name, the right side is the variable value, and the Python compiler automatically recognizes the type of the variable when the value is assigned.

?? Note: Variables cannot be named arbitrarily and must conform to certain rules. Variable names usually contain letters, numbers, and underscores (_), and variable names cannot begin with a number. For example, value315 is a valid variable name, and 315value is the wrong variable name.

?? The following code declares multiple variables with data types including integers, strings, booleans, and floating-point numbers. Finally, the values of these variables are output.

x = 20                      # 声明整数类型变量y = 40                      # 声明整数类型变量s = "I love python"     # 声明字符串类型变量flag = True                 # 声明布尔类型变量u = 30.4                    # 声明浮点类型变量print(flag)                 # 输出flag变量的值print(x + y)                # 输出x和y的和print(s)                    # 输出s变量的值print(u)                    # 输出u变量的值

?? The program runs as shown in the results.

"Python from rookie to Master" has been published, buy send video lessons

Python from rookie to expert (3): declaring variables

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.