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

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.
主站蜘蛛池模板: 台东市| 稷山县| 崇明县| 绍兴县| 永福县| 南溪县| 霞浦县| 历史| 连城县| 古田县| 鹤岗市| 白河县| 子长县| 民乐县| 裕民县| 常德市| 灌阳县| 伊春市| 郁南县| 灵寿县| 高要市| 开原市| 巴塘县| 印江| 噶尔县| 安丘市| 昌吉市| 泽州县| 永登县| 法库县| 彭州市| 平利县| 泗阳县| 望城县| 甘泉县| 吉木乃县| 龙胜| 明溪县| 崇礼县| 石首市| 二手房|