A Python program is a sequence of statements
Variable
3 categories/data types of data: numbers, string, boolean
# assignment statement; this is a comment
count = 25
fullname = "John Smith"
sum = count + 5
# print statement
print(sum)
print(20)
print("20")
y = "20"
y + str(count)
True
False
45 > 36 # <, >, ==, <=, >=, !=
count+100 > 36
# and, or, not
count > 36 and sum < 100
# simple if
if count > 6:
print("Hello")
print("World")
# if-then-else
if count > 36:
print("Hello")
print("World")
else:
print("Not Hello")
grade = 98
# nested if
if grade > 90:
print("A")
elif grade > 80:
print("B")
elif grade > 70:
print("C")
elif grade > 60:
print("D")
else:
print("F")
range(10) # 0,1,2,3,4,5,6,7,8,9
range(count)
range(10,25) # 10,11,12,....,24
range(10,25,3) # 10,13,16,19,22
count = 7
for i in range(count):
print(i,i+count)
nums = [23, 32, 45, 10]
print(len(nums))
names = ["John", "James", "Smith"]
fruits = ["apple","orange","banana"]
bools = [True, False, True]
for n,f in zip(names,fruits):
print(n,f)
# counts = [i*i for i in range(26)]
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)) # 0,1,2,3,4,5,6,7,8,9
plt.figure(figsize=(15,8))
plt.title("Fall Grad Enrollments in Computer Science")
plt.xlabel("Semester")
plt.ylabel("Grad Enrollment")
plt.bar(xaxis, fall_enrollments, width=0.5)
plt.xticks(xaxis, terms)
for a,b in zip(xaxis, fall_enrollments): # (0,489), (1,547), ...
plt.text(a, b+20, str(b), horizontalalignment='center', verticalalignment='center')
plt.show()
main()
import string
import matplotlib.pyplot as plt
%matplotlib inline
#import sys
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 list of 26 numbers
def draw(counts):
# this function takes as input the "counts" list
# the function then proceeds to draw the histogram using matplotlib
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)) # 0,1,2,3,4,5,6,7,8,9
plt.figure(figsize=(15,8))
plt.title("Fall Grad Enrollments in Computer Science")
plt.xlabel("Semester")
plt.ylabel("Grad Enrollment")
plt.bar(xaxis, fall_enrollments, width=0.5)
plt.xticks(xaxis, terms)
for a,b in zip(xaxis, fall_enrollments): # (0,489), (1,547), ...
plt.text(a, b+20, str(b), horizontalalignment='center', verticalalignment='center')
plt.show()
def main():
#counts = get_counts(sys.argv[1])
counts = get_counts("lipogram.txt")
#counts = [23,34,96,....100]
#print(counts)
draw(counts)
main()
s = "14:00/the ships are in position"
shift = int(s[0:2])
time, plaintext = s.split("/")
print(time)
print(plaintext)
# s = "14:00/the ships are in position"
def encrypt(s):
# this function returns the encrypted form of s
shift = int(s[0:2])
time, plaintext = s.split("/")