Lecture 02 Variables

Joseph Haugh

University of New Mexico

Free Recall

  • Get out a sheet of paper or open a text editor
  • For 2 minutes write down whatever comes to mind about the last class
    • This could be topics you learned
    • Questions you had
    • Connections you made

Setting Up CS Circles

  • Sign up for a free account on CS Circles
  • This is mandatory
  • Next set jhaugh as your “guru”
    • Click “Edit my profile” in the top right menu
    • Set the guru field to jhaugh
  • I will now be able to see your progress and give you credit in Canvas

Coding Exercise: Bonjour

  • Now take a few minutes to read chapter 0
  • Then complete Coding Exercise: Bonjour
  • I will give you about 10 minutes to do this

Assignment Statement

  • An assignment statement is an operation which creates or modifies a variable
  • It takes the following form: name = value
  • For example: abc = 123
    • Creates a variable named abc containing the value 123

Variables

  • Variables are used to store data the program needs
  • They need to have an initial value
  • They can change over time

Variable Example

x = 5
print(x)
x = 6
print(x)

Visualize

Variables Can Depend On Other Variables

What will the following code print?

x = 5
y = x + 1
print(x)
print(y)

Visualize

Variables Can Depend On Other Variables

What will the following code print?

x = 5
y = x + 1
x = 6
print(x)
print(y)

Visualize

Code Is Executed Top To Bottom

Code

x = 5
y = x + 1
x = 6
print(x)
print(y)

Output

6
6

Table

Statement x y
x = 5 5
y = x + 1 5 6
x = 6 5 6 6

Key Idea: Each line of code effects lines of code after it

Try It: Three Variables

What does the following code print? Try it by hand first.

a = 9
b = a * 3
a = 10
c = a + b
print(a)
print(b)
print(c)

Visualize

Try It: Three Variables

Code

a = 9
b = a * 3
a = 10
c = a + b
print(a)
print(b)
print(c)

Output

10
27
37

Table

Statement a b c
a = 9 9
b = a * 3 9 27
a = 10 9 10 27
c = a + b 10 27 37

Terminology: Statements vs Expressions

  • We have seen several mathematical expressions so far
    • 9
    • a * 3
    • a + b
  • We have only seen the assignment statement so far (we will see many more soon)
    • a = 9
    • b = a * 3
    • c = a + b
  • Expressions are units of code which always have a value
  • Statements are units of code that effect the state of the code (illustrated in the table on the previous) but have no value

Other Values

  • Variables can save more than just numbers
  • Variables can hold any value
  • Some other types of values we will see in this class:
    • Strings
    • Floats
    • Lists

Strings

  • Strings are a sequence of characters
  • Creation of strings takes one of two forms:
    • Single Quotes (’)
    • Double Quotes (“)
  • You must end the string with the same quote type you started it with
  • For example, both of the following are valid in Python:
    • 'Hello world'
    • "Hello world"
  • However, the following are invalid:
    • 'Hello world"
    • "Hello world'
  • You must be consistent

String Variable

As stated before variables can hold strings:

msg = "mon ami"
print(msg)

Visualize