官术网_书友最值得收藏!

Variables

In order to make effective programs, you will need to store values that you can access and modify across multiple lines of code. In Python, you can store a value using a variable. A variable is a sequence of characters that is made to represent some value in the program. To create a variable, you can use the assignment (=) operator. The assignment operator takes the value to the right and assigns it to the variable name on the left. The following statements will create a variable called myVariable and print it out:

>> myVariable = 1
>> print(myVariable)
It can be easy when starting out to confuse the assignment   =  operator with the logical equal "==" operator. Make a mental note that a single   =  is the assignment operator and a double '==' is the logical equal operator.

After a variable is created, using the name of a variable in a statement is the same as using the value that the variable contains. The following are some examples of statements that use variables:

>> myVariable = 4
>> print(myVariable+2)
>> myVariable = "abc"+"cba"
>> print(myVariable+"abc")
>> myVariable = True
>> print(myVariable and True)
Dynamic typing: In many languages, the value assigned to a variable needs to have a specific data type that is specified when the variable is created. However, this is not the case in Python. A value with any data type can be assigned to any variable. This is referred to as   dynamic typing.

You can also change a variable relative to its own value. This is sometimes called incrementing (in the case of addition) or decrementing (in the case of subtraction):

>> myVariable = 0
>> myVariable = myVariable + 5
>> print(myVariable)

There is a shorthand for changing a variable based on its own value that can be used for addition, subtraction, multiplication and division. The following examples demonstrate how changing the value of a variable can be expressed more concisely. First, the following creates a variable and sets it to 0:

>> myVariable = 0.
主站蜘蛛池模板: 砚山县| 赣州市| 邹平县| 大姚县| 永丰县| 图木舒克市| 巧家县| 贵南县| 镇宁| 高青县| 会宁县| 安图县| 东山县| 胶州市| 师宗县| 丰宁| 廊坊市| 伊春市| 双牌县| 富源县| 乡城县| 通海县| 清远市| 郴州市| 洛宁县| 日照市| 枣庄市| 鹤岗市| 棋牌| 临夏县| 苍梧县| 湖州市| 开封市| 开封市| 淮阳县| 青州市| 东乌| 梧州市| 彭水| 神农架林区| 香港 |