def displaydata(data) :
for item in data :
print(item)
names = ["John", "Bob", "David", "Carl", "Alan"]
displaydata(names)
John Bob David Carl Alan
def display(data) :
# check if data is list or dictionary
if isinstance(data, list) :
for item in data :
print(item)
elif isinstance(data, dict) :
for key, value in data.items() :
print(key, "=>", value)
names = ["John", "Bob", "David", "Carl", "Alan"]
display(names)
John Bob David Carl Alan
mydict = { 'carl' : 40, 'alan' : 30, 'bob' : 2, 'danny' : 1 }
display(mydict)
carl => 40 alan => 30 bob => 2 danny => 1
# open text file for reading
file = open("data/word_count.tst", 'r')
# read the file context
text = file.read()
# display text
text
'Python provides a complete set of control flow elements, \nincluding while and for loops, and conditionals. \nPython uses the level of indentation to group blocks \nof code with control elements.'
# create an empty dictionary
occurences = {}
# read each line and split
for word in text.split() :
occurences[word] = occurences.get(word, 0) + 1
# display the results
for word in occurences :
print("The word", word, "occurs", occurences[word], "times in file")
The word Python occurs 2 times in file The word provides occurs 1 times in file The word a occurs 1 times in file The word complete occurs 1 times in file The word set occurs 1 times in file The word of occurs 3 times in file The word control occurs 2 times in file The word flow occurs 1 times in file The word elements, occurs 1 times in file The word including occurs 1 times in file The word while occurs 1 times in file The word and occurs 2 times in file The word for occurs 1 times in file The word loops, occurs 1 times in file The word conditionals. occurs 1 times in file The word uses occurs 1 times in file The word the occurs 1 times in file The word level occurs 1 times in file The word indentation occurs 1 times in file The word to occurs 1 times in file The word group occurs 1 times in file The word blocks occurs 1 times in file The word code occurs 1 times in file The word with occurs 1 times in file The word elements. occurs 1 times in file
# dictionary to store data
data = { 'Omar':2.5, 'Ali':3.5, 'Osama':3.0 }
data
{'Omar': 2.5, 'Ali': 3.5, 'Osama': 3.0}
import pandas as pd
# convert data into series
data_series = pd.Series(data)
data_series
Omar 2.5 Ali 3.5 Osama 3.0 dtype: float64
import pandas as pd
# create dictionary to store student's grades
stud_grades = { 'Omar':[90, 50, 89], 'Ali':[78, 75, 73], 'Osama':[67, 85, 89]}
stud_grades
{'Omar': [90, 50, 89], 'Ali': [78, 75, 73], 'Osama': [67, 85, 89]}
# convert dictionary to dataframe
df = pd.DataFrame(stud_grades, index=['Course1', 'Course2', 'Course3'])
df
| Omar | Ali | Osama | |
|---|---|---|---|
| Course1 | 90 | 78 | 67 |
| Course2 | 50 | 75 | 85 |
| Course3 | 89 | 73 | 89 |
# calculate mean mark and add new column
df['Mean'] = (df['Omar'] + df['Ali'] + df['Osama'])/3
df
| Omar | Ali | Osama | Mean | |
|---|---|---|---|---|
| Course1 | 90 | 78 | 67 | 78.333333 |
| Course2 | 50 | 75 | 85 | 70.000000 |
| Course3 | 89 | 73 | 89 | 83.666667 |