Variables

Variables are object that store data somewhere. Let's see an example.


In [10]:
a=45
print(a)


45

In [11]:
b=25
print(b)


25

Okay, neat. Can I only make variables that hold numbers?


In [12]:
x="Hello Everybody!"
print(x)


Hello Everybody!

In [13]:
Amiright=True
print(Amiright)


True

Wait a minute, the numerical variables above are only integers...can we use decimal points?


In [14]:
fl = 0.54
print(fl)


0.54

Wow look at all those variables! What do I call them all?

Terminology

Integer:Any whole number:42,2635
Float:Any number at all
String:Anything with letters in it
Boolean:Any True or False statement

These will become useful when you get an error and you are wondering what it means.

Operators

So now that we have these fandangled new variables, what can we do with them?

Well, that's where operators come in handy. If you want to do arithmetic on two numerical variables or compare two variables, operators are how you do it. Let's see some examples.


In [15]:
print(a)
print(b)
c=a+b
print(c)


45
25
70

Note that our a and b didn't go away. Variables will stick around until you change them.


In [16]:
c=a/b
print(c)
c=c+a
print(c)


1.8
46.8

We have the usual arithmetic operators, $+, -, /, *$. On top of that we also have the power operator, $**$, the modulo operator $\%$ which returns the remainder from a quotient, the additive equality operator $+=$, and the equality operator, $=$. We also have the comparison operators; $>,<,==, !=, and, or$. Which mean, greater than, less than, is equal to, is not equal to, and, or.


In [28]:
#addition
print(2+2)
#division or quotient
print(42/42)
#multiplication
print(2*3)
#subtraction
print(2-8)
#exponent or power
print(2**3)
#modulo or remainder
print(10%7)
#additive equality
a=56
a+=10
print(a)
#greater than
print(10>5)
#less than
print(10<5)
#is equal to
print(11==10)
#is not equal to
print(10!=10)
#and
print(11==11 and 12>10)
#or
print(10==11 or 5<7)


4
1.0
6
-6
8
3
66
True
False
False
False
True
True

Exercises

  1. Let a=5 and b=17634, compute a+b, a-b, a*b, a/b, b**a, a**b and a%b
  2. Write four Boolean statements(a>b and b<a). Two should be True and two should be False.
  3. Let a=4637 and b=8467. Is a**b<b**a? Is a%b**3==0?

Notes about Strings

Let's explore how a computer thinks about strings. Beginning with some natural ideas.

  1. Does the computer know how long a string is?
  2. What if you want only part of a string?
  3. What does it mean to add two strings?
  4. How does a computer "see" the string?

In [20]:
string="hiya"
print(len(string))


4

In [21]:
print(string[0])


h

In [25]:
string1=" there!"
print(string+string1)


hiya there!

In [26]:
print(string-string1)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-0d4ad426fbe1> in <module>()
----> 1 print(string-string1)

TypeError: unsupported operand type(s) for -: 'str' and 'str'

The computer sees the string as a list of individual letters. In the next section, we will discuss lists in a more general sense. In the previous example, we had two strings, "heya", and " there!". The computer sees "heya" as

$$\left["h", "e", "y", "a"\right]$$

Exercises

  1. Type out how the computer sees " there!"
  2. Construct the string, "Python is the best!" from three different strings.

In [ ]: