Jeeqs

Submit

Variables

1.3 > A Gentle Introduction to Programming Using Python > EECS > MIT

Recall that variables are containers for storing information. For example,
Program Text:

a = "Hello, world!"
print a

Output:

Hello, world!
The = sign is an assignment operator which tells the interpreter to assign the value ‘‘Hello, world!’’ to the variable a.

Program Text:

a = "Hello, world!"
a = "and goodbye..."
print a

Output:

and goodbye...

Taking this second example, the value of $\verb!a!$ after executing the first line above is $\verb!‘‘Hello, world!’’!$. But, after executing the second line, the value of $\verb!a!$ changes to ‘‘and goodbye...’’. Since we ask the program to print out $\verb!a!$ only after the second assignment statement, that is the value that gets printed. If you wanted to save the values of both strings, you should change the second variable to another valid variable name, such as $\verb!b!$. Variables are useful because they can cut down on the amount of code you have to write. Write a program that prints out the tic-tac-toe board from exercise 1.2 , but which uses variables to cut down on the amount of typing you have to do. Hint - how many different variables should you need?

Course
MIT, EECS : A Gentle Introduction to Programming Using Python

Attribution
Canelake, Sarina. 6.189 A Gentle Introduction to Programming Using Python, January IAP 2011. (Massachusetts Institute of Technology: MIT OpenCourseWare), http://ocw.mit.edu (Accessed 08 Mar, 2012). License: Creative Commons BY-NC-SA

No submission found! Please Submit a solution!
Markdown and MathJax are supported.