Introduction to Computer Science Through Programming
Smith Computer Science
The following are odds and ends coming in the test 2:
phrase = "hello, there, my friend. I like fruit, birds, and music." list1 = phrase.split() list2 = phrase.split(",") # find the differences print(list1) print(list2)
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])
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"]))
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)
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) )
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)
##################### ### 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
When finished, you may continue to work on your project.