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

  • Practical Data Wrangling
  • Allan Visochek
  • 356字
  • 2021-07-02 15:16:09

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.
主站蜘蛛池模板: 上犹县| 广平县| 商河县| 柘城县| 沛县| 海南省| 建昌县| 工布江达县| 永嘉县| 进贤县| 昌宁县| 义乌市| 新田县| 郴州市| 内黄县| 理塘县| 旬阳县| 道孚县| 客服| 金平| 定西市| 宝坻区| 黑龙江省| 海兴县| 宁德市| 木兰县| 安龙县| 滕州市| 虹口区| 南开区| 榕江县| 九台市| 雷波县| 苏州市| 鄯善县| 永胜县| 阳曲县| 孟津县| 故城县| 宾川县| 石林|