b1 = True and True
b2 = True and False
b3 = False and False
b4 = False and False
print(b1)
print(b2)
print(b3)
print(b4)
b5 = True or True
b6 = True or False
b7 = False or False
b8 = False or False
print(b5)
print(b6)
print(b7)
print(b8)
print(not True)
print(not False)
import random
random.seed(1)
rlist = random.sample(range(1,21), 10)
print(rlist)
for i in range(0, len(rlist)):
if ( (rlist[i] > 5) and (rlist[i] < 15) ):
print(rlist[i], "is between.")
print()
for i in range(0, len(rlist)):
if ( (rlist[i] <= 5) or (rlist[i] >= 15) ):
print(rlist[i], "is not between.")
print()
for i in range(0, len(rlist)):
if ( not((rlist[i] > 5) and (rlist[i] < 15)) ):
print(rlist[i], "is not between.")
print(True & True)
print(True & False)
print(False & True)
print(False & False)
print(True | True)
print(True | False)
print(False | True)
print(False | False)
n = 5
bool1 = (n > 9 or n < 7)
bool2 = (n > 9 | n < 7)
print(bool1)
print(bool2)
print(9 | 5)
In this example, we will explore a dataset containing information about the 887 passengers on the first (and last) voyage of the RMS Titanic.
The following two code cells loads two packages needed for this example.
import pandas as pd
import matplotlib.pyplot as plt
The next cell reads the data from the titanic.csv
file into our Python session and creates five lists, each with 887 elements:
name
- Contains the names of the titanic passengers.sex
- Contains the sex of each of the passengers. Each element is either the string 'male'
or the string 'female'
. age
- Contains the age of each of the passengers, as an integer.pclass
- Contains the ticket class of each of the passengers. Each element is an integer 1, 2, or 3. Lower numbers indicate a higher class. surv
- Explains whether each of the passengers lived or died in the disaster. Each element is either a 1, indicating that the passenger survived, or a 0, indicating that the passenger died. Note that any particular index position refers to the same passenger in all three list. In other words, the passenger whose name is in name[37]
has their sex recorded in sex[37]
, has their age recorded in age[37]
, and so on.
df = pd.read_csv('titanic.csv')
name = list(df['Name'])
sex = list(df['Sex'])
age = list(df['Age'])
pclass = list(df['Pclass'])
surv = list(df['Survived'])
The following cell will print the record for a randomly selected passenger each time it is ran.
n = random.choice(range(0, len(name)))
print("Name:", name[n])
print("Sex:", sex[n])
print("Age:", age[n])
print("Class:", pclass[n])
print("Survived:", surv[n])
female_survived = 0
female_died = 0
male_survived = 0
male_died = 0
for i in range(0, len(sex)):
if((sex[i] == "female") and (surv[i] == 1)):
female_survived += 1
if((sex[i] == "female") and (surv[i] == 0)):
female_died += 1
if((sex[i] == "male") and (surv[i] == 1)):
male_survived += 1
if((sex[i] == "male") and (surv[i] == 0)):
male_died += 1
female_total = female_survived + female_died
male_total = male_survived + male_died
print("Total Female Passengers:", female_total)
print("Female Passengers who Survived:", female_survived)
print("Female Passengers who Died:", female_died)
print()
print("Total Male Passengers:", male_total)
print("Male Passengers who Survived:", male_survived)
print("Male Passengers who Died:", male_died)
female_survival_rate = round(female_survived / female_total, 4)
male_survival_rate = round(male_survived / male_total, 4)
print("The proportion of females who survived the Titanic voyage is equal to " + str(female_survival_rate) + ".")
print("The proportion of males who survived the Titanic voyage is equal to " + str(male_survival_rate) + ".")
survival_rates = [female_survival_rate, male_survival_rate]
death_rates = [1-female_survival_rate,1- male_survival_rate]
plt.bar(range(2),survival_rates, label='Survived', color='cornflowerblue')
plt.bar(range(2),death_rates, label='Died', bottom=survival_rates, color='Salmon')
plt.legend(loc="center left", bbox_to_anchor=(1.03,0.5))
plt.xticks(range(2), ['Female', 'Male'])
plt.ylabel('Proportion')
plt.title('Survival Rate by Sex')
plt.show()
c1_survived = 0
c2_survived = 0
c3_survived = 0
c1_total = 0
c2_total = 0
c3_total = 0
for i in range(0, len(pclass)):
if(pclass[i] == 1):
c1_total += 1
if(surv[i] == 1):
c1_survived += 1
elif(pclass[i] == 2):
c2_total += 1
if(surv[i] == 1):
c2_survived += 1
else:
c3_total += 1
if(surv[i] == 1):
c3_survived +=1
c1_survival_rate = round(c1_survived / c1_total, 4)
c2_survival_rate = round(c2_survived / c2_total, 4)
c3_survival_rate = round(c3_survived / c3_total, 4)
print("The proportion of 1st class passengers who survived the Titanic voyage is equal to " + str(c1_survival_rate) + ".")
print("The proportion of 2nd class passengers who survived the Titanic voyage is equal to " + str(c2_survival_rate) + ".")
print("The proportion of 3rd class passengers who survived the Titanic voyage is equal to " + str(c3_survival_rate) + ".")
survival_rates_by_class = [c1_survival_rate, c2_survival_rate, c3_survival_rate]
death_rates_by_class = [1-c1_survival_rate, 1-c2_survival_rate, 1-c3_survival_rate]
plt.bar(range(3),survival_rates_by_class, label='Survived', color='cornflowerblue')
plt.bar(range(3),death_rates_by_class, label='Died', bottom=survival_rates_by_class, color='Salmon')
plt.legend(loc="center left", bbox_to_anchor=(1.03,0.5))
plt.xticks(range(3), ['Class 1', 'Class 2', 'Class 3'])
plt.ylabel('Proportion')
plt.title('Survival Rate by Class')
plt.show()