Conditional Statements

In [1]:
# if-statement
x = 20
if x > 10:
    print(x)
    print(x+2)
print(x+3)
20
22
23
In [2]:
# if-statement
x = 2
if x > 10:
    print(x)
    print(x+2)
print(x+3)
5
In [3]:
# if-else-statement  2-way branch
x = 20
if x > 10:
    print(x+2)
else:
    print(x+3)
22
In [10]:
# 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')
C
In [17]:
x = 'A'
if x.islower():
    print("Lower")
else:
    print("Not Lower")
Not Lower
In [15]:
print()

range function

In [19]:
for x in range(10):
    print(x)
0
1
2
3
4
5
6
7
8
9
In [20]:
for x in range(5,16):
    print(x)
5
6
7
8
9
10
11
12
13
14
15
In [21]:
for x in range(5,16,3):  # 3 is referred to as the "step"
    print(x)
5
8
11
14
In [22]:
s = "Hello"
for x in s:
    #do something with x
    print(x)
H
e
l
l
o
In [23]:
s = "Hello"
for i in range(len(s)):  # a way to generate indices of the string s
    print(s[i])
H
e
l
l
o
In [24]:
def count(c,s):
    answer = 0
    for x in s:
        if x == c:
            answer = answer + 1
    return answer
In [27]:
count('c','abaababab')
Out[27]:
0
In [31]:
import random

shift = random.randint(1,25)
print(shift)
4
In [32]:
def encode(n,s):
    return "Same Answer"
In [34]:
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()
Coded text:  Same Answer

Read from File

In [37]:
with open("vote1.dat") as f1:
    lines = f1.read().splitlines()
print(lines)
f1.close()
['Red', 'Blue', 'Green', 'Blue', 'Blue', 'Red', 'Red', 'Red', 'Red', 'Red', 'Green', 'Purple']
In [40]:
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()
[['Red', 'Green'], ['Blue'], ['Green', 'Red', 'Blue'], ['Blue', 'Green', 'Red'], ['Green']]
In [44]:
s = "aa\nbbb\nccccc\ndd\nee"
print(s)
t = s.splitlines()
print(t)
aa
bbb
ccccc
dd
ee
['aa', 'bbb', 'ccccc', 'dd', 'ee']

Classes and Objects

In [72]:
# 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
In [73]:
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)
******************************
Name: John

Courses: 
	CNO:	CSC7003
	CREDITS:1.5
	GRADE:	A

	CNO:	CSC1301
	CREDITS:4
	GRADE:	B

	CNO:	CSC1302
	CREDITS:4
	GRADE:	A

GPA: 3.58

******************************

In [45]:
print(20)
20
In [53]:
round(3.7777,2)
Out[53]:
3.78
In [ ]: