SmithLogo

CSC 111

Introduction to Computer Science Through Programming

Smith Computer Science



Lecture Notes 34: Prep for Test 2 +Project Work


Outline

This class we'll discuss:




Announcements




Prep for Test 2

The following are odds and ends coming in the test 2:





General Review targets:





Mutable vs Immutable types

immutable

Once created, you can only overwrite, not modify.
Examples: int, boolean, float, String, Tuple

mutable

These types contain explicit methods to allow modification (inserting, removing, extending, etc).
Examples: Classes with non-private attributes, list, dictionary



String/list processing



Study string splitting


Replit Test 2 Study Code:
study string-and-lists.py (PART 1)
run it in the shell with: python3 string-and-lists.py

phrase = "hello, there, my friend. I like fruit, birds, and music."
list1 = phrase.split()
list2 = phrase.split(",")
# find the differences
print(list1)
print(list2)


Study list slicing


Replit Test 2 Study Code:
study string-and-lists.py (PART 2)
run it in the shell with: python3 string-and-lists.py

myList = ['A', 'B', 'C', 'D', 'E', 'F', 'G','H']
print(myList[1:-2:2])
print(myList[-1])
print(myList[2:-3])
print(myList[-2:5])




Randomness example

Study random modules: rand, randInt, choice, seed


Replit Test 2 Study Code:
study random-choice.py
run it in the shell with: python3 random-choice.py

from random import choice

# What do you need to modify to fix the randomn sequence!

num_houses = int(input("How many stops? "))
for n in range(num_houses):
  print("House", n, "-", choice(["Yellow", "Red", "Blue"]))


Here, the changes are:
  1. restructure to include all of random or choice and seed
  2. set fixed seed




Private attributes in classes

defining private attributes and what does that mean


Replit Test 2 Study Code:
study private.py
run it in the shell with: python3 random-choice.py

class P:
   def __init__(self, name, alias):
      self.name = name       # public
      self.__alias = alias   # private

   def who(self):
      print('name  : ', self.name)
      print('alias : ', self.__alias)




Returns in a function

Number of returns vs placement of returns vs end of execution at return


Replit Test 2 Study Code:
study returns.py
run it in the shell with: python3 returns.py

def fun1(a, b):
  if a < b:
    return a
  elif b < a:
    return b
  else:
    return 0
    print("no difference")


def fun2(n):
  for i in range(n):
    return i*i

# predict what happens
out1a = fun1(2,7)
print(out1a)

out1b = fun1(7,2)
print(out1b)

out1c = fun1(2,2)
print(out1c)

print( fun2(5) )




Function calls with assigned attribues

You can specify the parameters for each argument


Replit Test 2 Study Code:
study assigning.py
run it in the shell with: python3 assigning.py

def my_color(name, age, color):
    return f"I am {name}, and for {age} years, I've loved {color} "

def my_game(name="meh", age="0", game="hide-and-seek"):
    return f"I am {name}, and for {age-12} years, I've played {game}"  


out_color1 = my_color("Pablo",42, "Viridian")
print(out_color1)

out_color2 = my_color(name="Pablo",age = 42, color="Viridian")
print(out_color2)

out_color3 = my_color(age = 42, color="Viridian",name="Pablo")
print(out_color3)



out_game1 = my_game("Pablo",42, "chess")
print(out_game1)

out_game2 = my_game(name="Pablo",age = 42, game="chess")
print(out_game2)

out_game3 = my_game(age = 42, game="chess",name="Pablo")
print(out_game3)




Read-Write position and "r+"


Replit Test 2 Study Code:
study rw-position.py
run it in the shell with: python3 rw-position.py

#####################
### PART 1 : invisible characters that count
print("### PART 1 #########")
print()
# standard opening for reading
f = open("example.txt", "r")
lines = f.readlines() 
f.close()

# verify why each line has 10 characters 
# and why it prints like that
for li in lines:
  print(li,f"has {len(li)} characters")

#####################
### PART 2: editor look and actual number of lines
print()
print("### PART 2 #########")
print()
# standard opening for reading and counting lines
f = open("example2.txt", "r")
x = len(f.readlines())
f.close()

# note the last line in the editor is NOT the #of lines
# why is that?
print(f"file example2.txt has {x} lines")
print()

#####################
### PART 3: printing complete lines is tricky...(extra \n)
print()
print("### PART 3 #########")
print()
# standard opening for writing: creating file
f = open("example3.txt", "w")
for i in range(3):
  f.write(f"li {i}: 789\n")
f.close()

# standard opening for reading and printing
f = open("example3.txt", "r")
lines = f.readlines() 
f.close()
# why does this print like it does?
for li in lines:
  print(li)

### PART 4: read and write with update
print()
print("### PART 4 #########")
print()
# standard opening for writing: creating file
f = open("example4.txt", "w")
for i in range(3):
  f.write(f"{i}\n")
f.close()

# standard opening for reading with update (at the end)
f = open("example4.txt", "r+")
lines = f.readlines() 
x = len(lines)
# notice: not closing
for i in range(x,x+3):
  f.write(f"{i}\n")  
f.close()  
 
# standard opening for reading and printing
f = open("example4.txt", "r")
lines = f.readlines() 
f.close()
# why does this print like it does?
for li in lines:
  print(li, end = "") # newlines already in read lines


### PART 5: read and write with update
print()
print("### PART 5 #########")
print()
# standard opening for writing: creating file
f = open("example5.txt", "w")
for i in range(3):
  f.write(f"{i}\n")
f.close()

# standard opening for reading to count
f = open("example5.txt", "r")
lines = f.readlines() 
x = len(lines)
f.close() ## <--- closed!

# separate opening for reading with update (overwrites)
f = open("example5.txt", "r+")
for i in range(x,x+3):
  f.write(f"{i}\n")  
f.close()  
 
# standard opening for reading and printing
f = open("example5.txt", "r+")
lines = f.readlines() 
f.close()
# why does this print like it does?
for li in lines:
  print(li, end = "") # newlines already in read lines




Using: from graphics import *

This will be used in the test to keep formatting and writing simple.






Mock Test 2 Results

Reviewing the results



Quick Survey about Mock Test 2 JMCQ Format

Please fill it out before Friday: https://forms.gle/uP4FmpqsPWJyupRp7




In-Class project work:

When finished, you may continue to work on your project.