Now that we have some basic skills, it's important for us to define the conditions in which they can be executed. This is where control comes into play.
And as before, very easy topic!
In plain English:
And that's all there is to it. Now we just need to learn the Python-way of expressing the above phrases.
In [1]:
collection = [1,2,3,4,5]
len(collection)
Out[1]:
In [2]:
if len(collection) == 5:
print("Woohoo!")
In [3]:
collection[1]
Out[3]:
In [4]:
if collection[0] % 2 == 0:
print("Divisible")
else:
print("Not Divisible")
So now we can deal with a scenario where there are two possible decisions to be made. What about more than two decision?
Say hello to "elif"!
In [5]:
collection = [1,2,3,4,5]
if collection[0] == 0:
print ("Zero!")
elif collection[0] == 100:
print ("Hundred!")
else:
print("Not Zero or Hundred")
In [6]:
x = ["George", "Barack", "Donald"]
test = "Richard"
if test in x:
print(test, "has been found.")
else:
print(test, "was not found. Let me add him to the list." )
x.append(test)
print(x)
In [7]:
# Your code here
How about adding your own input and checking against that? This doesn't come in too handy in a data science environment since you typically have a well defined dataset already. Nevertheless, this is important to know.
In [8]:
age = int(input("Please enter your age:"))
if age < 18:
print("You cannot vote or buy alcohol.")
elif age < 21:
print("You can vote, but can't buy alcohol.")
else:
print("You can vote to buy alcohol. ;) ")
In [9]:
mr_prez = ["Bill", "George", "Barack", "Donald"]
name = input("Enter your name:") # Don't need to specify str
In [10]:
type(name)
Out[10]:
In [11]:
if name in mr_prez:
print("You share your name with a President.")
else:
print("You too can be president some day.")
Time to supercharge our Python usage. Loops are in some ways, the basis for automation. Check if a condition is true, then execute a step, and keep executing it till the condition is no longer true.
In [12]:
numbers = [1,2,3,4,5,6,7,8,9,10]
for number in numbers:
if number % 2 == 0:
print("Divisible by 2.")
else:
print("Not divisible by 2.")
In [13]:
numbers = {1,2,3,4,5,6,7,8,9,10}
for num in numbers:
if num%3 == 0:
print("Divisible by 3.")
else:
print("Not divisible by 3.")
When using dictionaries, you can iterate through keys, values or both.
In [14]:
groceries = {"Milk":2.5, "Tea": 4, "Biscuits": 3.5, "Sugar":1}
print(groceries.keys())
print(groceries.values())
In [15]:
# item here refers to the the key in set name groceries
for a in groceries.keys():
print(a)
In [16]:
for price in groceries.values():
print(price)
In [17]:
for (key, val) in groceries.items():
print(key,val)
In [18]:
groceries.items()
Out[18]:
In [19]:
groceries.keys()
Out[19]:
In [20]:
groceries.values()
Out[20]:
Enter your responses in the fields below. This is solved for you if you scroll down, but you can't cheat yourself!
In [21]:
data = {
"Richard": {
"Title": "CEO",
"Employees": ["Dinesh", "Gilfoyle", "Jared"],
"Awards": ["Techcrunch Disrupt"],
"Previous Firm": "Hooli",
"Board Seat":1,
"Net Worth": 100000
},
"Jared": {
"Real_Name": "Donald",
"Title": "CFO",
"Previous Firm": "Hooli",
"Board Seat":1,
"Net Worth": 500
},
"Erlich": {
"Title": "Visionary",
"Previous Firm": "Aviato",
"Current Firm": "Bachmannity",
"Incubees": ["Richard", "Dinesh", "Gilfoyle", "Nelson", "Jian Yang"],
"Board Seat": 1,
"Net Worth": 5000000
},
"Nelson": {
"Title": "Co-Founder",
"Current Firm": "Bachmannity",
"Previous Firm": "Hooli",
"Board Seat": 0,
"Net Worth": 10000000
},
}
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [22]:
# Name of people in the dictionary
data.keys()
Out[22]:
In [23]:
# Alternate way to get the name of the people in the dictionary
for name in data.keys():
print(name)
In [24]:
# Name of people who have incubees
for name in data.items():
if "Incubees" in name[1]:
print (name[0])
In [25]:
# Name and networth of people with a networth greater 500000
for name in data.items():
if "Net Worth" in name[1] and name[1]["Net Worth"]>500000:
print (name[0], name[1]["Net Worth"])
In [26]:
# Name of people who don't have a board seat
for name in data.items():
if "Board Seat" in name[1] and name[1]["Board Seat"] == 0:
print (name[0])
In [27]:
# Generate a list on the fly
nums = list(range(10))
print(nums)
In a defined range, the lower number is inclusive, and upper number is exclusive. So 0 to 10 would include 0 but exclude 10. So if we need a specific range, we can use this knowledge to our advantage.
In [28]:
nums = list(range(1,11))
print(nums)
We can also specify a range without explicitly defining an upper or lower range, in which case, Python does it's magic: range will be 0 to one less than the number specified.
In [29]:
nums = list(range(10))
print(nums)
We can also use the range function to perform mathematical tricks.
In [30]:
for i in range(1,6):
print("The square of",i,"is:",i**2)
Or to check for certain other conditions or properties, or to define how many times an activity will be performed.
In [31]:
for i in range(1,10):
print("*"*i)
In [32]:
# Your Code Here
Print the square of the first 10 natural numbers.
In [33]:
# Your Code Here
In [34]:
for i in range(1,100):
print("The square of",i,"is:",i**2)
if i >= 5:
break
print("Broken")
In [35]:
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
for letter in letters:
print("Currently testing letter", letter)
if letter == "e":
print("I plead the 5th!")
continue
print( letter)
Remember lists? Now here's a way to power through a large list in one line!
As a Data Scientist, you will need to write a lot of code very efficiently, especially in the data exploration stage. The more experiments you can run to understand your data, the better it is. This is also a very useful tool in transforming one list (or dictionary) into another list.
Let's begin by some simple examples
First, we will write a program to generate the squares of the first 10 natural numbers, using a standard for loop. Next, we will contrast that with the List Comprehension approach.
In [36]:
# Here is a standard for loop
numList = []
for num in range(1,11):
numList.append(num**2)
print (numList)
So far, so good!
In [37]:
# Now for List Comprehension
sqList = [num**2 for num in range(1,11)]
print(sqList)
In [38]:
[num**2 for num in range(1,11)]
Out[38]:
How's that for speed?!
Here's the format for List Comprehensions, in English.
ListName = [Expected_Result_or_Operation for Item in a given range]
print the ListName
In [39]:
cubeList = [num**3 for num in range(6)]
print(cubeList)
List comprehensions are very useful when dealing with an existing list. Let's see some examples.
In [40]:
nums = [1,2,3,4,5,6,7,8,9,10]
In [41]:
# For every n in the list named nums, I want an n
my_list1 = [n for n in nums]
print(my_list1)
In [42]:
# For every n in the list named nums, I want n to be squared
my_list2 = [n**2 for n in nums]
print(my_list2)
In [43]:
# For every n in the list named nums, I want n, only if it is even
my_list3 = [n for n in nums if n%2 == 0]
print(my_list3)
How about calculating the areas of circles, given a list of radii? That too in just one line.
In [44]:
radius = [1.0, 2.0, 3.0, 4.0, 5.0]
import math
# Area of Circle = Pi * (radius**2)
area = [round((r**2)*math.pi,2) for r in radius]
print(area)
In [45]:
data = {
"Richard": {
"Title": "CEO",
"Employees": ["Dinesh", "Gilfoyle", "Jared"],
"Awards": ["Techcrunch Disrupt"],
"Previous Firm": "Hooli",
"Board Seat":1,
"Net Worth": 100000
},
"Jared": {
"Real_Name": "Donald",
"Title": "CFO",
"Previous Firm": "Hooli",
"Board Seat":1,
"Net Worth": 500
},
"Erlich": {
"Title": "Visionary",
"Previous Firm": "Aviato",
"Current Firm": "Bachmannity",
"Incubees": ["Richard", "Dinesh", "Gilfoyle", "Nelson", "Jian Yang"],
"Board Seat": 1,
"Net Worth": 5000000
},
"Nelson": {
"Title": "Co-Founder",
"Current Firm": "Bachmannity",
"Previous Firm": "Hooli",
"Board Seat": 0,
"Net Worth": 10000000
},
}
In [46]:
# Print all details for people who have incubees
[(k,v) for k, v in data.items() if "Incubees" in v ]
Out[46]:
In [47]:
for name in data.items():
if "Net Worth" in name[1] and name[1]["Net Worth"]>500000:
print (name[0], name[1]["Net Worth"])
In [48]:
high_nw = [(name[0], name[1]["Net Worth"]) for name in data.items() if "Net Worth" in name[1] and name[1]["Net Worth"]>500000]
print(high_nw)
In [49]:
type(high_nw)
Out[49]:
In [50]:
type(high_nw[0])
Out[50]:
We can also use dictionary comprehension to create new dictionaries
In [51]:
name = ['George HW', 'Bill', 'George', 'Barack', 'Donald', 'Bugs']
surname = ['Bush', 'Clinton', 'Bush Jr', 'Obama', 'Trump', 'Bunny']
full_names = {n:s for n,s in zip(name,surname)}
full_names
Out[51]:
In [52]:
# What if we want to exclude certain values?
full_names = {n:s for n,s in zip(name, surname) if n!='Bugs'}
print(full_names)