-
Integers, Floats and Strings 3
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
-
If Statements 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Lists, Sets and Tuples 5
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
-
Loops 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Functions 3
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
-
Dictionaries 3
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
-
Assertions 2
-
Lecture7.1
-
Lecture7.2
-
-
Classes 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
-
Matplotlib 2
-
Lecture9.1
-
Lecture9.2
-
Variables Introduction
What is a Variable¶
The first thing to learn about coding is naturally variables. Variables in programming are quite similar to those in your basic math class. They are symbols that hold some value for us. This notebook goes through examples of integers, floating point numbers and strings which are some of the basic building blocks in python. For each cell in this notebook, when run it will execute any of the code which we have in that cell. Anything that has # before it will not be run though, because this is a comment in the code. Comments are recommended for clean coding and are added to explain some of the code better. Below, you will see first a comment explaining what the code does followed by a line of code which declares our first variable. In this case, the first variable we make is the number 5. The way we set the variable is to say x = 5, the variable name goes on the right, followed by the equal sign, followed by the value we give it.
#Create a variable x and set it equal to 5
x = 5
The Print Function¶
So now we have our first variable, but how do we actually see the value? The print function in python prints out the value of whatever we pass it. To do so, we need to call print() and then within the parentheses pass in the variable to print out. In this case that variable is going to be x, so we call it like print(x)
#Print out the value for x
print(x)
5
Strings¶
Strings are variables for textual data. We use quotes when we define a string, for example:
#Create a string variable
string_example = "Hello World"
print(string_example)
Hello World
It is also not necessary always to declare a variable object. If we wanted to just print "Hello World", the following will work.
#Print "Hello World"
print("Hello World")
Hello World
If you want to check the type of a variable, all one needs to do is use type() and pass in as an argument the variable.
#Print the types of the variables
print(type(string_example))
print(type(x))
<class 'str'>
<class 'int'>
Variables can be re-used. In the following block of code we are going to set the variable x equal to 5 first, print it out, set it equal to 10 after, then print it again.
#Set x equal to 5
x = 5
print(x)
#Set x equal to 10
x = 10
print(x)
5
10
Mathematical functions are supported in python as well. Addition for example is done by just simply using the plus sign.
#Add 2 + 2
print(2 + 2)
4
#Set x equal to 5
x = 5
#Add 1 to x
x = x + 1
print(x)
6
If we have a variable that we want to add a number to, we could do it like before with x = x + 1, or the the operation += adds whatever is on the right hand side to the left hand side. Below, the code is going to add 1 to x.
#Add 1 to x
x = 5
x += 1
print(x)
6
Variables may also be added together. If we have x and y, and want to find the sum of the two as z, we can do it like below:
#Add x + y to create z
x = 5
y = 10
z = x + y
print(z)
15
Mathematical Functions¶
The following are some of the basic math functions that python has:
- Add A and B: A + B
- Subtract B from A: A - B
- Divide A by B: A / B
- Raise A to the Bth power: A ** B
#Subtract
print(10 - 5)
#Multiply
print(2 * 5)
#Divide
print(10 / 5)
#Raise 2 to the 3rd power
print(2 ** 3)
5
10
2.0
8
Parentheses are used the same way as in math.
print((2+3)**2)
25
More String Functionality¶
As long as you are consistent with what you use on the left and the right, you are allowed to use either single or double quotes to declare a string.
#Both ways of initializing work
a = 'string1'
b = "string2"
print(a)
print(b)
string1
string2
If of course you want either single or double quotes to be part of the string, you can include them together where the inner one is the one that you want for your string and the outer one declares the string.
a = "'Single Quote'"
b = '"Double Quote"'
print(a)
print(b)
'Single Quote'
"Double Quote"
Using the plus sign between two strings will lead to the two of them being added together (also referred to as concatenated).
#Adding strings together concatenates them
first = "John "
last = "Doe"
print(first+last)
John Doe
String Formatting¶
To insert a value into a string, we can use curly braces where we want to be able to substitute in variables. Then we call format() and pass in whatever variables we are substituting.