A Crash Course In Python And Manipulation of Data

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.

Data Types

Numbers

In [4]:
1


Out[4]:
1

In [5]:
2 + 4


Out[5]:
6

In [7]:
2 - 3


Out[7]:
-1

In [8]:
100 *  5


Out[8]:
500

In [9]:
4 / 2


Out[9]:
2.0

In [10]:
4 / 3


Out[10]:
1.3333333333333333

In [11]:
4 % 3


Out[11]:
1

In [14]:
type(3)


Out[14]:
int

In [15]:
type(3.5)


Out[15]:
float
Strings

In [16]:
"Cats"


Out[16]:
'Cats'

In [17]:
'Dogs'


Out[17]:
'Dogs'

In [18]:
'Hello World'


Out[18]:
'Hello World'

In [19]:
"I can't wait to program for the rest of my life"


Out[19]:
"I can't wait to program for the rest of my life"

In [46]:
"My name is " + "Andre"


Out[46]:
'My name is Andre'

In [20]:
type("Cats")


Out[20]:
str

In [21]:
type("Doge")


Out[21]:
str
Arrays

In [23]:
[1, 2, 3]


Out[23]:
[1, 2, 3]

In [25]:
[100, 4, 12, 45]


Out[25]:
[100, 4, 12, 45]

In [26]:
["Cat", "Dog", "Zebra"]


Out[26]:
['Cat', 'Dog', 'Zebra']
Variables

variable_name_here = expression_here


In [5]:
x = 4

In [6]:
x


Out[6]:
4

In [2]:
x + 2


Out[2]:
6

In [3]:
my_sweet_ride = "ferrari"

In [4]:
my_sweet_ride


Out[4]:
'ferrari'

In [47]:
my_name = "Andre"
"My name is " + my_name


Out[47]:
'My name is Andre'

In [7]:
type(x)


Out[7]:
int

In [8]:
type(my_sweet_ride)


Out[8]:
str

In [9]:
x = 4
y = 100
z = x * y

In [10]:
z


Out[10]:
400

In [14]:
my_array = [100, 67, 4, 101]

In [16]:
my_array[0]


Out[16]:
100

In [17]:
my_array[1]


Out[17]:
67

In [18]:
my_array[2]


Out[18]:
4

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]


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-22-e59fad3b875c> in <module>()
      1 # This will error out because the index is asking for a location that is not in the erray (out of range)
----> 2 my_array[4]

IndexError: list index out of range

In [23]:
# Access an index using a var!
pointless_var = 2
my_array[pointless_var]


Out[23]:
4

In [33]:
my_array[0:3]


Out[33]:
[100, 67, 4]

In [30]:
string_1 = "We are so lame."
string_1[0]


Out[30]:
'W'

In [31]:
string_1[0:10]


Out[31]:
'We are so '

In [32]:
new_string = string_1[0:10] + "cool."
new_string


Out[32]:
'We are so cool.'

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)


odds are: [1, 3, 5, 7]

In [20]:
odds.append(11)
print('odds after adding a value:', odds)


odds after adding a value: [1, 3, 5, 7, 11]

In [21]:
del(odds[0])
print('odds after removing the first element:', odds)


odds after removing the first element: [3, 5, 7, 11]

In [22]:
odds.reverse()
print('odds after reversing:', odds)


odds after reversing: [11, 7, 5, 3]

In [24]:
odds = [1, 3, 5, 7]
print('odds are:', odds)
odds = odds + [11]
print('odds are:', odds)


odds are: [1, 3, 5, 7]
odds are: [1, 3, 5, 7, 11]
Dictionaries

In [35]:
first_dict = {}

In [36]:
first_dict


Out[36]:
{}

In [37]:
first_dict["x"] = "some_var"

In [38]:
first_dict


Out[38]:
{'x': 'some_var'}

In [40]:
first_dict[x] = 4

In [41]:
first_dict


Out[41]:
{4: 4, 'x': 'some_var'}

In [42]:
second_dict = {"key_value": "value", "second_key_value": "second_value"}

In [43]:
second_dict


Out[43]:
{'key_value': 'value', 'second_key_value': 'second_value'}

In [44]:
second_dict["key_value"]


Out[44]:
'value'

In [45]:
second_dict["second_key_value"]


Out[45]:
'second_value'
Exercise Block #1

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 [ ]:

Boolean Logic

In [49]:
True


Out[49]:
True

In [50]:
False


Out[50]:
False

In [51]:
1 == 1


Out[51]:
True

In [52]:
2 == 1


Out[52]:
False

In [53]:
1 < 10


Out[53]:
True

In [54]:
1 <= 10


Out[54]:
True

In [55]:
1 > 10


Out[55]:
False

In [56]:
1 >= 10


Out[56]:
False

In [57]:
"dogs are better than cats" == "dogs are better than cats"


Out[57]:
True

In [59]:
"dogs" == "cats"


Out[59]:
False

In [60]:
"abc" < "def"


Out[60]:
True

In [61]:
"abc" > "def"


Out[61]:
False

In [62]:
"a" < "b"


Out[62]:
True

In [63]:
"a" > "b"


Out[63]:
False

Logic Flow


In [5]:
x = 5

if x < 10:
    print("This workshop is okay")


This workshop is okay

In [6]:
x = 11

if x < 10:
    print("This workshop is okay")
else:
    print("This workshop is lame")


This workshop is lame

In [7]:
str_1 = "Andre"

if str_1 == "Andre":
    print("Welcome to the danger zone")


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


Medium value
Looping

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)


0
1
2
3
4
5
6
7
8
9

In [15]:
total_sum = 0
for value in range(0, 10):
    total_sum = total_sum + value
total_sum


Out[15]:
45

In [16]:
list_of_elements = []
for value in range(0, 10):
    list_of_elements.append(value)
list_of_elements


Out[16]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [17]:
# We can also start looping through all these values
for new_value in list_of_elements:
    print(new_value * 2)


0
2
4
6
8
10
12
14
16
18

In [18]:
word = 'lead'
for char in word:
    print(char)


l
e
a
d
For loops take the form of
for variable in collection:
    do things with variable
Exercise Block #2

Use a for-loop to convert the string “hello” into a list of letters:

["h", "e", "l", "l", "o"]

Hint: You can create an empty list like this:

my_list = []

In [ ]: