Skip to content

Variables and Statements

Part of the power of a computing environment lies in the ability to store, manipulate, and recall information. This is done using variables and statements.

Variables

A variable is a name that is associated with the data stored in a memory address. One way to create variables in python is through assignment which consists of placing the variable you would like to create to the left of an equal sign =, and the expression on the right side.

Here we create a variable a, and assign to it the number 5.

a=5      # create a new variable a and assign 5 to it.
b=7      # create a new variable b and assign 7 to it.
a=3      # reassign to the variable a the number 3.
c=a+b    # assign to the variable c the sum of a and b
c        # output the value of c
Output:
10

Statements

Statements are the part of a programming language that are used to encode algorithmic logic.

Simple statements:

  • assignment:
    a=a+1
  • call:
    print(a+1), factor(24)

Compound statements:

  • if-statement:
if A>3:
    print(A-3)
else:
    print("not big enough")
  • while statement:
while x<=10:
    print(x)
    x=x+1
  • for statement:
for x in [1,2,3,4,5]:
    print(x)

We will look at if, while, and for statements more thoroughly below. But the odd one may creep into some of our examples before then.