My objective for this lesson is to make you aware of python and its usefulness in wrangling with data.
We are going to start slow with data types and expressions. Eventually, by the end of the lesson, we will programmatically genrerate data and then use a sophisticated library to help us manage and visualize it all.
In [4]:
1
Out[4]:
In [5]:
2 + 4
Out[5]:
In [7]:
2 - 3
Out[7]:
In [8]:
100 * 5
Out[8]:
In [9]:
4 / 2
Out[9]:
In [10]:
4 / 3
Out[10]:
In [11]:
4 % 3
Out[11]:
In [14]:
type(3)
Out[14]:
In [15]:
type(3.5)
Out[15]:
In [16]:
"Cats"
Out[16]:
In [17]:
'Dogs'
Out[17]:
In [18]:
'Hello World'
Out[18]:
In [19]:
"I can't wait to program for the rest of my life"
Out[19]:
In [46]:
"My name is " + "Andre"
Out[46]:
In [20]:
type("Cats")
Out[20]:
In [21]:
type("Doge")
Out[21]:
In [23]:
[1, 2, 3]
Out[23]:
In [25]:
[100, 4, 12, 45]
Out[25]:
In [26]:
["Cat", "Dog", "Zebra"]
Out[26]:
variable_name_here = expression_here
In [5]:
x = 4
In [6]:
x
Out[6]:
In [2]:
x + 2
Out[2]:
In [3]:
my_sweet_ride = "ferrari"
In [4]:
my_sweet_ride
Out[4]:
In [47]:
my_name = "Andre"
"My name is " + my_name
Out[47]:
In [7]:
type(x)
Out[7]:
In [8]:
type(my_sweet_ride)
Out[8]:
In [9]:
x = 4
y = 100
z = x * y
In [10]:
z
Out[10]:
In [14]:
my_array = [100, 67, 4, 101]
In [16]:
my_array[0]
Out[16]:
In [17]:
my_array[1]
Out[17]:
In [18]:
my_array[2]
Out[18]:
In [22]:
# This will error out because the index is asking for a location that is not in the
# array (out of range).
my_array[4]
In [23]:
# Access an index using a var!
pointless_var = 2
my_array[pointless_var]
Out[23]:
In [33]:
my_array[0:3]
Out[33]:
In [30]:
string_1 = "We are so lame."
string_1[0]
Out[30]:
In [31]:
string_1[0:10]
Out[31]:
In [32]:
new_string = string_1[0:10] + "cool."
new_string
Out[32]:
There are many ways to change the contents of lists besides assigning new values to individual elements:
In [19]:
odds = [1, 3, 5, 7]
print('odds are:', odds)
In [20]:
odds.append(11)
print('odds after adding a value:', odds)
In [21]:
del(odds[0])
print('odds after removing the first element:', odds)
In [22]:
odds.reverse()
print('odds after reversing:', odds)
In [24]:
odds = [1, 3, 5, 7]
print('odds are:', odds)
odds = odds + [11]
print('odds are:', odds)
In [35]:
first_dict = {}
In [36]:
first_dict
Out[36]:
In [37]:
first_dict["x"] = "some_var"
In [38]:
first_dict
Out[38]:
In [40]:
first_dict[x] = 4
In [41]:
first_dict
Out[41]:
In [42]:
second_dict = {"key_value": "value", "second_key_value": "second_value"}
In [43]:
second_dict
Out[43]:
In [44]:
second_dict["key_value"]
Out[44]:
In [45]:
second_dict["second_key_value"]
Out[45]:
Create two python variables that will add up to 100.
In [ ]:
Add two python strings together to create a sentence.
In [ ]:
Create an array with number values only and assign it to a variable name. Then access the first element of that array.
In [ ]:
Create a dictionary and access a few values from it
In [ ]:
In [49]:
True
Out[49]:
In [50]:
False
Out[50]:
In [51]:
1 == 1
Out[51]:
In [52]:
2 == 1
Out[52]:
In [53]:
1 < 10
Out[53]:
In [54]:
1 <= 10
Out[54]:
In [55]:
1 > 10
Out[55]:
In [56]:
1 >= 10
Out[56]:
In [57]:
"dogs are better than cats" == "dogs are better than cats"
Out[57]:
In [59]:
"dogs" == "cats"
Out[59]:
In [60]:
"abc" < "def"
Out[60]:
In [61]:
"abc" > "def"
Out[61]:
In [62]:
"a" < "b"
Out[62]:
In [63]:
"a" > "b"
Out[63]:
Logic Flow
In [5]:
x = 5
if x < 10:
print("This workshop is okay")
In [6]:
x = 11
if x < 10:
print("This workshop is okay")
else:
print("This workshop is lame")
In [7]:
str_1 = "Andre"
if str_1 == "Andre":
print("Welcome to the danger zone")
In [12]:
my_var = 50
if my_var > 100:
print("Large value")
y = 10 # Example of doing different types of expressions and statements
elif my_var > 10:
print("Medium value")
y = 5
else:
print("Small value")
y = 0
In [14]:
list_of_elements = [1, 2, 3, 4]
# This could take a while to add everything by hand.
# This could take a while to add everything by hand.
for value in range(0, 10):
print(value)
In [15]:
total_sum = 0
for value in range(0, 10):
total_sum = total_sum + value
total_sum
Out[15]:
In [16]:
list_of_elements = []
for value in range(0, 10):
list_of_elements.append(value)
list_of_elements
Out[16]:
In [17]:
# We can also start looping through all these values
for new_value in list_of_elements:
print(new_value * 2)
In [18]:
word = 'lead'
for char in word:
print(char)
In [ ]: