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

  • 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.
主站蜘蛛池模板: 营口市| 望谟县| 松原市| 策勒县| 武夷山市| 镇江市| 宁德市| 瑞昌市| 剑川县| 富平县| 阳原县| 晋州市| 右玉县| 达拉特旗| 永丰县| 浮梁县| 本溪市| 永新县| 荥阳市| 卢湾区| 泰兴市| 华池县| 徐水县| 弥勒县| 东至县| 乃东县| 莱阳市| 新龙县| 北安市| 凭祥市| 镶黄旗| 绥宁县| 盘锦市| 合川市| 舒兰市| 六盘水市| 崇仁县| 阜阳市| 安龙县| 襄汾县| 新郑市|