2.4. StatementsΒΆ
A statement is a unit of code that the Python interpreter can execute. We have seen two kinds of statements: print being an expression statement and assignment.
When you type a statement in interactive mode, the interpreter executes it and displays the result, if there is one.
A script usually contains a sequence of statements. If there is more than one statement, the results appear one at a time as the statements execute.
For example, run this code to see what it does.
The assignment statement produces no output.
- dog
- The value of var3 is first set to "bird" but then changed to be the value of var1. The value of var1 is first set to "cat" but later changed to the value of var2 which was set to "dog".
- fish
- Only var2 has the value of fish. When you assign the value of a variable to the value of another variable the value is copied to the new variable. No relationship is created between the two variables.
- cat
- The value of var3 is first set to "bird" but then changed to be the value of var1. However, the value of var1 also is changed after it is originally set.
- bird
- While var3 is originally set to "bird" the value is changed later.
csp-10-2-2: What is the value of var3 after the following code executes?
var1 = "cat"
var2 = "dog"
var3 = "bird"
var1 = var2
var3 = var1
var2 = "fish"
- dog
- The value of var2 is first set to "dog" but then it changes. What does it change to?
- fish
- The value of var2 is first set to "dog" then changed to "fish". Even though var1 is reassigned to the value of var2 it does not change the value of var2.
- cat
- Var1 is first assigned to the value "cat", what is var2 assigned to?
- bird
- Var3 is initially set to "bird", but "bird" has no relationship to var2. What is var2 assigned to?
csp-10-2-3: What is the value of var2 after the following code executes?
var1 = "cat"
var2 = "dog"
var3 = "bird"
var1 = var2
var3 = var1
var2 = "fish"
csp-10-2-5: Click the assignments in this codeblock.Remember that an assignment gives value to a variable and does not produce output.
minutes = 60 print(minutes) seconds = minutes * 60 print(seconds)
You have attempted of activities on this page