# if-statement
x = 20
if x > 10:
print(x)
print(x+2)
print(x+3)
# if-statement
x = 2
if x > 10:
print(x)
print(x+2)
print(x+3)
# if-else-statement 2-way branch
x = 20
if x > 10:
print(x+2)
else:
print(x+3)
# nested-if-statement multi-way branch 3 or more branches
# assign letter grades to scores
score = 74
if score >= 90:
print('A')
elif score >= 80:
print('B')
elif score >= 70:
print('C')
elif score >= 60:
print('D')
else:
print('F')
x = 'A'
if x.islower():
print("Lower")
else:
print("Not Lower")
print()
for x in range(10):
print(x)
for x in range(5,16):
print(x)
for x in range(5,16,3): # 3 is referred to as the "step"
print(x)
s = "Hello"
for x in s:
#do something with x
print(x)
s = "Hello"
for i in range(len(s)): # a way to generate indices of the string s
print(s[i])
def count(c,s):
answer = 0
for x in s:
if x == c:
answer = answer + 1
return answer
count('c','abaababab')
import random
shift = random.randint(1,25)
print(shift)
def encode(n,s):
return "Same Answer"
def main():
shift = random.randint(1,25)
plain_text = input("\nEnter plain text: ")
cipher_text = encode(shift,plain_text)
print("Coded text: "+cipher_text)
main()
with open("vote1.dat") as f1:
lines = f1.read().splitlines()
print(lines)
f1.close()
with open("vote2.dat") as f2:
lines = f2.read().splitlines()
ballots = []
for line in lines:
ballot = line.split(',')
ballots = ballots + [ballot]
print(ballots)
f2.close()
s = "aa\nbbb\nccccc\ndd\nee"
print(s)
t = s.splitlines()
print(t)
# Student Class; consists of "methods"
class Student:
# every class MUST have the __init__ method
# this method is also referred to as the "constructor" method
# self denotes the object on which the methods are called, or the objects whose
# attributes we want to refer to
def __init__(self,n):
# no return statement
self._name = n
self._courses = []
def add_course(self,cno,credits,grade):
t = (cno,credits,grade)
self._courses.append(t)
def gpa(self):
letter_to_numeric = {'A':4, 'B':3, 'C':2, 'D':1, 'F':0}
#numeric_to_letter = {4:'A', 3:'B', 2:'C', 1: 'D', 0:'F'} don't need this
grade_points = 0.0
credits = 0.0
for x in self._courses:
credits = credits + x[1]
grade_points = grade_points + x[1] * letter_to_numeric[x[2]]
return grade_points/credits
def __str__(self):
answer = "******************************\n"
answer = answer + "Name: "+ self._name + "\n\nCourses: \n"
for x in self._courses:
answer = answer + "\tCNO:\t"+x[0]+"\n"
answer = answer + "\tCREDITS:"+str(x[1])+"\n"
answer = answer + "\tGRADE:\t"+x[2]+"\n\n"
answer = answer + "GPA: "+str(round(self.gpa(),2))+"\n\n"
answer = answer + "******************************\n"
return answer
s1 = Student("John")
s1.add_course("CSC7003",1.5,'A')
s1.add_course("CSC1301",4,'B')
s1.add_course("CSC1302",4,'A')
s2 = Student("Alice")
s2.add_course("CSC7003",1.5,'B')
#print(round(s1.gpa(),2))
#print(round(s2.gpa(),2))
print(s1)
#print(s2)
print(20)
round(3.7777,2)