Python Basics

A Python program is a sequence of statements

Variable

3 categories/data types of data: numbers, string, boolean

Assignment Statement

In [1]:
# assignment statement; this is a comment
count = 25
In [2]:
fullname = "John Smith"
In [3]:
sum = count + 5
In [4]:
# print statement
print(sum)
30
In [7]:
print(20)
print("20")
20
20
In [11]:
y = "20"
In [13]:
y + str(count)
Out[13]:
'2025'

Conditional Statement

In [14]:
True
Out[14]:
True
In [15]:
False
Out[15]:
False
In [16]:
45 > 36   # <, >, ==, <=, >=, !=
Out[16]:
True
In [20]:
count+100 > 36
Out[20]:
True
In [22]:
# and, or, not
count > 36 and sum < 100
Out[22]:
False
In [28]:
# simple if
if count > 6:
    print("Hello")
    print("World")
Hello
World
In [30]:
# if-then-else
if count > 36:
    print("Hello")
    print("World")
else:
    print("Not Hello")
Not Hello
In [ ]:
grade = 98
In [ ]:
# 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")

For Loop Statement

In [34]:
range(10) # 0,1,2,3,4,5,6,7,8,9
Out[34]:
range(0, 10)
In [32]:
range(count)
Out[32]:
range(0, 25)
In [33]:
range(10,25) # 10,11,12,....,24
Out[33]:
range(10, 25)
In [35]:
range(10,25,3) # 10,13,16,19,22
Out[35]:
range(10, 25, 3)
In [52]:
count = 7
In [53]:
for i in range(count):
    print(i,i+count)
0 7
1 8
2 9
3 10
4 11
5 12
6 13

Functions

functions are used to encapsulate lines of code as an independent module

Lists of values

In [58]:
nums = [23, 32, 45, 10]
print(len(nums))
4
In [56]:
names = ["John", "James", "Smith"]
In [74]:
fruits = ["apple","orange","banana"]
In [75]:
bools = [True, False, True]
In [77]:
for n,f in zip(names,fruits):
    print(n,f)
John apple
James orange
Smith banana
In [87]:
# counts = [i*i for i in range(26)]

Bar Chart

In [85]:
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()
In [ ]:
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()
In [94]:
s = "14:00/the ships are in position"
shift = int(s[0:2])
time, plaintext = s.split("/")
print(time)
print(plaintext)
14:00
the ships are in position
In [ ]:
# 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("/")