Skip to content
Home » PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 7 – Variables

PYTHON JUMPSTART COURSE Section 1 – Introduction, Lesson 7 – Variables

Spread the love

We’ve used the term VARIABLE a couple times so far. But what exactly are variables and how to use them in Python?

Variables in Python

A variable is a way of referring to a memory location used by a computer program.

Variables hold references to data (numbers, strings and other types). In Python we don’t declare variables but we must initialize them before they can be used.

We don’t bind variables to any particular data type. So one variable may refer to a number at one time and to a string or whatever at another:

>>> a = 7   # variable a holds reference to the integer object 7
>>> a
7
>>> b = a   # now b holds reference to the same integer as a
>>> b
7
>>> a = "abc"   # now a dumps the reference to the integer and references the string "abc"
>>> a
'abc'
>>> b   # while b still holds reference to the integer
7
>>> b = 2.5 # now b references a newly created float variable 2.5
>>> b
2.5

After this the integer object 7 will be orphaned: no other variable will be referencing it. It will be removed by Python.

Variables in Python

Multiple Assignment

Python allows multiple assignment. You can assign one value to multiple variables at the same time:

>>> a = b = c = 6
>>> a
6
>>> b
6
>>> c
6

It’s possible to assign multiple comma-separated values to multiple comma-separated variables in one go.  They are assigned in order of appearance:

>>> a, b, c = "hello", 6, 21.75

>>> a
'hello'

>>> b
6

>>> c
21.75

Deleting References to Objects

We can delete the reference to an object by using the del keyword:

>>> a = 5
>>> a
5
>>> del a

Now a’s reference to the number 5 is deleted:

>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

Identity

We can use the id() function to check if two variables reference the same object:

>>> a = 70
>>> b = a
>>> id(a)
140733195541696
>>> id(b)
140733195541696   # the same

Now we create two different integer objects 8 and 1. The variables x and y are assigned those two objects. This time the identities are different:

>>> x = 8
>>> y = 1
>>> id(x)
1598316992
>>> id(y)
1598316768    # different

QUIZ

1. We can use the … function to check if two variables reference the same object.
    A) reference()
    B) ref()
    C) id()
 
2. We can delete the reference to an object by using the … keyword:
    A) del
    B) delete
    C) remove
 

TRUE OR FALSE?

1) A variable is a way of referring to a memory location used by a computer program.
2) In Python we must declare variables before they are initialized.
3) Each variable is bound to a particular data type.
4) Python allows multiple assignment.

WHAT’S THE OUTPUT?

1)
>>> a = 5
>>> b = “abc”
>>> a = b
>>> print(a, b)  
 
2)
>>> a = b = c = “abc”
>>> b  
 
3)
>>> a, b, c = “yes”, “no”, 0.5
>>> c  
 
4)
>>> a, b, c = “yes”, “no”, 0.5
>>> a, b, c = b, b, a
>>> a  
 
5)
>>> a = 5
>>> del a
>>> a  
 
6)
>>> a, b, c = 3, 6, 9
>>> del a, c
>>> b  

SOLUTION

QUIZ

1. We can use the … function to check if two variables reference the same object.
    A) reference()
    B) ref()
    C) id()
 
2. We can delete the reference to an object by using the … keyword:
    A) del
    B) delete
    C) remove
 

TRUE OR FALSE?

1) A variable is a way of referring to a memory location used by a computer program.
True
2) In Python we must declare variables before they are initialized.
False
3) Each variable is bound to a particular data type.
False
4) Python allows multiple assignment.
True

WHAT’S THE OUTPUT?

1
>>> a = 5
>>> b = “abc”
>>> a = b
>>> print(a, b)  
Output:
abc abc  
 
2
>>> a = b = c = “abc”
>>> b  
Output:
‘abc’  
 
3
>>> a, b, c = “yes”, “no”, 0.5
>>> c  
Output:
0.5  
 
4
>>> a, b, c = “yes”, “no”, 0.5
>>> a, b, c = b, b, a
>>> a  
Output:
‘no’  
 
5
>>> a = 5
>>> del a
>>> a  
Output:
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
NameError: name ‘a’ is not defined  
 
6
>>> a, b, c = 3, 6, 9
>>> del a, c
>>> b  
Output:
6  

PROJECT

'''
Variables - Project

ABRACADABRA
__________________________________________________________

Your task is to write a program that acts like a magician. The magician
puts an apple to one hat and an orange to another. Then he swaps the two 
objects. Finally he makes the apple disappear.

And here's how you should write this program:

#1. Create two string objects: "apple" and "orange" and assign them to
the variables hat1 and hat2 respectively. The assignment should be on
one line.


#2. Now the program should print the location of the objects. It should look
like this:

The apple is in hat 1.
The orange is in hat 2.

Use the variables defined before and concatenation. After the message is 
printed, there should be an empty line.

#3. Now the magician is doing his magic. The program should print:

Abracadabra... I'll swap the two objects!

and then it should wait until you hit Enter. To make the computer wait
you can use an input function without any prompt, and you don't have to 
assign the value returned from the input function to any variable because
you won't need it.

#4. After you hit Enter the program should print the following message:

I've swapped the two objects:
The apple is now in hat 2.
The orange is now in hat 1.

First reassign the variables. Do it on one line. Don't do it like so:

hat2, hat1 = "apple", "orange"

or like so:

hat1, hat2 = "orange", "apple"

Use just the variables (hat1 and hat2).

Hint: If you have any background in another programming language like C++,
C# or Java, you know that in order to swap the values of two variables
you have to use a temporary variable. In Python you don't need it.


Then print the message shown above with the new values assigned 
to the variables. Use the two variables in your strings and concatenation.

After the message is printed, there should be an empty line.

#5. Now the magician is doing his magic again. The program should print:

Abracadabra... I'll make the apple disappear!

and then it should wait until you hit Enter. 

#6. After you hit Enter the program should print the following message:

The apple has disappeared.
The orange is still in hat 1.

First delete the apple. What you have to delete is actually the variable
the string "apple" is assigned to. Then print the message shown above using 
the variable hat1 and concatenation.
'''

PROJECT SOLUTION

##1. 

hat1, hat2 = "apple", "orange"


##2. 

print("The " + hat1 + " is in hat 1.")
print("The " + hat2 + " is in hat 2.")
print()


##3. 

print("Abracadabra... I'll swap the two objects!")
input()


##4. 

hat1, hat2 = hat2, hat1
print("I've swapped the two objects:")
print("The " + hat2 + " is now in hat 2.")
print("The " + hat1 + " is now in hat 1.")
print()


##5. 

print("Abracadabra... I'll make the apple disappear!")
input()


##6. 

del hat2
print("The apple has disappeared.")
print("The " + hat1 + " is still in hat 1.")

Spread the love

Leave a Reply