In [1]:
x = 5
print(x)
5
In [ ]:
 
In [2]:
print("Hello World")
Hello World
In [3]:
x = "Hello World"
print(x)
Hello World
In [7]:
x = 5
y = 7.5
print(str(x)+str(y))
57.5
In [10]:
x = '23'
y = 5
z = '75.3'
print(int(x))
print(float(z)+int(x))
23
98.3
In [13]:
x = int(input("Please enter your age: "))
print(x,type(x))
Please enter your age: 60
60 <class 'int'>
In [16]:
x = 4
y = 7
if x > y:
    print(x,' is greater than ',y)
elif x < y:
    print(x,' is less than ',y)
else:
    print(x,' is equal to ',y)
4  is less than  7
In [17]:
for x in range(10):
    print(x)
0
1
2
3
4
5
6
7
8
9
In [18]:
for x in range(1,10):
    print(x)
1
2
3
4
5
6
7
8
9
In [21]:
for x in range(10,1):
    print(x)
In [23]:
sum = 0
for x in range(1,11):
    sum = sum + x
print(sum)
1
3
6
10
15
21
28
36
45
55
In [26]:
x = 0
while x < 11:
    print(x)
    x = x + 1
0
1
2
3
4
5
6
7
8
9
10
In [27]:
names = ['John', 'Jim']
for x in names:
    print(x)
John
Jim
In [28]:
s = 'John'
for x in s:
    print(x)
J
o
h
n
In [29]:
names.append('Donald')
In [30]:
print(names)
['John', 'Jim', 'Donald']
In [37]:
counts = {'A':10, 'B':20, 'C':6, 'A':50}
In [38]:
print(counts)
{'A': 50, 'B': 20, 'C': 6}
In [33]:
print(counts['A'])
10
In [36]:
for x in counts:
    print(x,counts[x])
A 10
B 20
C 6
In [39]:
def square(x):
    return x*x
In [40]:
square(5)
Out[40]:
25
In [41]:
square(7)
Out[41]:
49
In [43]:
s = "Hello World"
s.count('a')
Out[43]:
0
In [46]:
letters = 'abcdefghijklmnopqrstuvwxyz'
counts = {}
for x in letters:
    counts[x] = s.count(x)
a 1
b 1
c 1
d 1
e 1
f 1
g 1
h 1
i 1
j 1
k 1
l 1
m 1
n 1
o 1
p 1
q 1
r 1
s 1
t 1
u 1
v 1
w 1
x 1
y 1
z 1
In [7]:
def get_counts(fname):
    # this function takes the name of a file as input parameter, fname
    # it then reads the file into a string variable
    # then proceeds to process the string for the count of letters
    # answer should be returned in a dictionary of 26 numbers
    with open(fname) as f:
        data = f.read().lower()
    letters = 'abcdefghijklmnopqrstuvwxyz'
    #counts = {}
    counts = []
    for x in letters:
        #counts[x] = data.count(x)
        counts.append(data.count(x))
    return counts
In [8]:
get_counts('ra.txt')
Out[8]:
[158,
 25,
 55,
 55,
 246,
 52,
 51,
 91,
 158,
 1,
 11,
 85,
 33,
 153,
 175,
 27,
 1,
 99,
 136,
 153,
 67,
 21,
 39,
 5,
 74,
 2]
In [49]:
len("abc")
Out[49]:
3
In [50]:
"abc".count('a')
Out[50]:
1
In [6]:
import matplotlib.pyplot as plt
%matplotlib inline 

def main():
  fall_enrollments = [489, 547, 705, 879, 1075, 1228, 1404, 1550, 1773, 1376]
  terms =["F2010","F2011","F2012","F2013","F2014","F2015","F2016","F2017","F2018","F2019"]
  xaxis = range(len(terms))
  plt.figure(figsize=(16,8)) 
  plt.title("Fall Undergrad Enrollments in Computer Science ")
  plt.xlabel("Term")
  plt.ylabel("Undergrad Enrollment")
  plt.bar(xaxis, fall_enrollments, width=0.5)
  plt.xticks(xaxis, terms)
  for a,b in zip(xaxis, fall_enrollments):
    plt.text(a, b+30, str(b), horizontalalignment='center', verticalalignment='center')
  plt.show()

main()
In [ ]: