Primitive Data Structures
Our most primitive data structures include 4 types:
- Strings ex: “Hello World”
- Floats ex: 1.0
- Integers ex: 1
- Booleans: ex: False
These make up the foundation of what we will work with. You can also have more complex data structures. There are many more but for succinctness, we will stick to primarily 3:
- Lists ex [0 , 1 , 3 , 4 , 2 , 3 , 4]
- Sets ex {0 , 1 , 2 , 3}
- Dictionaries ex {“a” : 1 , “b” : 2 , “c” : 3}
Note that of lists, sets, and dictionaries, the base index is at 0. The index is the position of a certain element in these structures. To reference an element in any one of these structures, just use the []
. So we can say list[0]
will reference the first element in that list.
Printing Data Structures
This is some basic code that shows you how we can print different types. The unique aspect of Python, is that unlike most other languages, we don’t have to declare variable types. It intuitively knows what the type is after assignment.
# --- Primitive Data Structures ---
v = "I am a String" # Assign a variable, v, to the String
pi = 3.14 # Here we are assigning the variable pi to a Double
numOfBooks = 50 # Here we are assigning the numOfBooks variable to an Int
iAmFemale = True # Here we are assigning the iAmFemale variable to a Bool
#Combining data types in one print statement
print("--------------------")
print("Primitive Structures")
print("--------------------")
print("I have " , numOfBooks , " at home")
print("The value of pi is equal to", pi)
print("I am a female:" , iAmFemale)
--------------------
Primitive Structures
--------------------
I have 50 at home
The value of pi is equal to 3.14
I am a female: True
More Complex Data Structures
The structures typically have more functions associated with them and add more complexity to the code.
# --- More advanced Data Structures ---
#Things in a list don't have to be unique and you can
#have repeats
myGroceryList = ["apples" , "bananas" , "milk" , "milk" , "yogurt"]
#Sets must only contain unique values
favoriteColors = ["blue" , "red" , "green"]
#Dicts have key and value pairs.
theCodeIs = {0: "I" , 1: "Love" , 3: "Pie"}
#Printing variables
print("--------------------")
print("Advanced Structures")
print("--------------------")
print("Grocery List:" , myGroceryList)
print("My favorite colors are:" , favoriteColors)
print(theCodeIs)
--------------------
Advanced Structures
--------------------
Grocery List: ['apples', 'bananas', 'milk', 'milk', 'yogurt']
My favorite colors are: ['blue', 'red', 'green']
{0: 'I', 1: 'Love', 3: 'Pie'}
Operators
Operators are a way for us to manipulate data structures to get an output. You can think about them as being like simple calculations. We will cover primarily three types of operators:
- Arithmetic
- Logical
- Relational Operators
Arithmetic Operators
There are generally 5 types of arithmetic operators that can be summarized below:
- Addition
3 + 4
- Subtraction
4 - 3
- Multiplication
5 * 4
- Division
5.4 / 4
- Modulus
7 % 6
- Floor Division
9 // 4
Most of these are pretty self explanatory. Modulus %
finds the remainder. So 7 % 6 = 1
. Floor division will round the answer down to the nearest integer. So 9 // 4 = 2
.
Relational Operators
These are used to produce either True
or False
as outputs. There are 5 logical operators:
==
(equals)!=
(does not equal)>
(greater than)<
(less than)<=
(less than or equal to)>=
(greater than or equal to)
Using these operators will help us to determine if some criterion is met.
Logical Operators
There are three main relational operators in python:
or
and
not
These help us to construct complex conditional statements. We will combine logical and relational operators next!
If Statements
If statements help us to perform an action only if a conditional returns true. Let’s look at this simple code:
iAmAFemale = True
if iAmAFemale:
print("I am a female")
What we see here is that the boolean condition has to be True in order for “I am a female” to print. If it is false, we will not enter the code block inside the if.
Here’s a more complex one:
iAmAFemale = True
i = -1
if iAmAFemale and i > 0:
print("I am a female")
Here we are using the logical operator and
, to combine two conditional statements into one big condition. and
tells us that both conditions have to be met in order for us to go inside the loop.
How about this one?
iAmAFemale = True
i = -1
if iAmAFemale or i > 0:
print("I am a female")
Here, we will print “I am a female” because the logical statement or
specifies that at least one condition must be True in order to go inside the loop.
For and While loops
Loops help us to parse through an iterable process. Something is iterable when we can step through it. For loops are often written like so:
For Loops
for i in range (0 , 5):
# Do something
Here we have an integer i
, that we increment over. The range (0, 5)
call essentially tells the computer to increment i starting from 0, and ending at 4. While the first part of the range
is inclusive, the last number is exclusive, meaning that it will stop just one integer before the max integer you have specified in the range.
While Loops
i = 0
while i < 5:
# Do something
i += 1
Here in while loops, we have to first declare the variable i
we will be incrementing outside of the loop. Then we have to increment i
inside of the loop itself. While loops have boolean conditionals as parameters. If the condition is met (i.e. i < 5
) , then it will perform the actions inside the loop. If the condition is not met, then it will break out of the loop.
# A working example of a for loop
numList = [0 , 1 , 2 , 3 , 4 , 5]
for i in range(0, len(numList)):
print("The number I am at is: " , numList[i])
The number I am at is: 0
The number I am at is: 1
The number I am at is: 2
The number I am at is: 3
The number I am at is: 4
The number I am at is: 5
In the above code, we have also manipulated lists a bit. We can reference the number of elements in the list by calling the len()
function. We can also reference the individual element at an index
Functions
We define functions using the def syntax. There are two parts to a function: (1) The function name, and (2) the function parameters. Unlike most other languages, in python, you don’t have to declare the output type of the function. Below is a simple function that incorporates what we have learned about loops, and some data structures.
def isPrime(num):
for i in range(2, num):
if(num % i == 0): # Calculate the remainder
print("The number" , num , " is not Prime")
return
print("The number" , num , " is Prime")
return
isPrime(11)
isPrime(72)
The number 11 is Prime
The number 72 is not Prime
Let’s break down the logic of this problem. We are trying to determine if an input number, in this case the parameter num
is prime or not. We know that prime numbers are always divisible by themselves and 1. So we basically need to check every number between 1 and the number itself, exclusively.
If after dividing by the iterable divisor, and our remainder is equal to 0, this means that our number has a factor that is not 1 or itself. This means that it is prime!
Note the use of return
statements in a function. A return
just tells the computer to break out of the function.
Access the associated assignments for this lesson by clicking the button below!