# Loops # Make a for loop that prints 1..10 including the 1 and 10. for i in range(1, 11): print(i) # Make a while loop that prints 1..10 including the 1 and 10. i = 1 while i <= 10: print(i) i = i + 1 # Make a while loop that prints 1,2,4,8,16,32,64,128,256,512, 1024, 2048. i = 1 while i <= 2048: print(i) i = i * 2 # Make a for loop that prints -3,0,3,6,9,12,15,18. for i in range(-3, 19, 3): print(i) # Make a program that prints this: 256,128,64,32,16,8,4,2,1 i = 256 while i >= 1: print(i) i = i/2 for i in range(8,-1,-1): print(2**i) # Make a loop that prints 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5...(forever) while 2!=3: print(1,2,3,4,5) # Do it again a different way i =1 while True: print(i) if i == 5: i = 1 else: i+=1 # Do it again a different way with '*' ########################################################################### # Lists # Make a list that contains 1,2,3, and 4. l = [1, 2, 3, 4] # Make a list that contains 1,â€appleâ€, 3, and “pearâ€. l2 = [1, "apple", 3, "pear"] # Make a for loop that prints all the items in one of the lists above. for i in l: print(i) # Make a list. Print “yes†or “no†depending on if “apple†is in the list. if "apple" in l: print("Yes") else: print("no") # Using slicing, print the last item in one of the lists above. print(l[-1]) # Using slicing, print a list that is the second-to-end and last item in one of the lists above. print(l[-2:]) # Using two lists, merge them any reasonable way you want. l.extend(l2) # -OR- l = l + l2 # Given the list l=[1,12,3,12,5,6,12] count the number of 12's. print(l.count(12)) c = 0 for item in l: if item == 12: c += 1 print(c) # Compute the sum of the not-12s. You should get 15. total = 0 for item in l: if item != 12: total += item print(total) # Give the list above, make a list of the non-12 elements. # You should get [1,3,5,6] total = [] for item in l: if item != 12: total.append(item) print(total) # (BONUS) Given a list, print it backwards print(l.reversed()) for i in range(len(l), -1, -1): print(l[i]) ############################################################################# # Files # Open some file and print the first line file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") line = file.readline() print(line) # Open some file and print all the lines. file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") for line in file: print(line) # Open some file and print the last line file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") print(file.readlines()[-1]) lines = [] file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") for line in file: lines.append(line) print(lines[-1]) # Open some file, and print the lines in sorted order. # Hint: Put the lines in a list, sort the list, and print the list file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") lines = [] for line in file: lines.append(line) lines = sorted(lines) print(lines) # -OR- file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") lines = file.readlines() lines = sorted(lines) print(lines) # Print every OTHER line of a file. file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") lines = file.readlines() for i in range(0, len(lines), 2): print(line[i]) file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") print(file.readlines()[::2]) file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") c = 0 for line in file: if c % 2 == 0: print(line) c += 1 file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") for line in file: junk = readline() print(junk) # Make a file that is just a bunch of numbers, one per line. # Write a program that adds them up. file = open("C:\\Users\\rappleto\\Desktop\\numbers.txt", "r") total = 0 for number in file: total += double(number) print(total) # Make a program that prints the first char of the file file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") print(file.read(1)) file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") firstline = file.readline() print(firstline[0]) file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") lines = file.readlines() print(lines[0][0]) # Make a program that prints chars 5..15 of each line of the file. file = open("C:\\Users\\rappleto\\Desktop\\passwd.txt", "r") for line in file: print(line[5:16]) # If Statements # Make an if statement that prints “Yes†if the variable ‘a’ is 5. a = 3 b = 7 if a == 5: print("Yes") # Make an if statement that prints “Yes†if the variable ‘a’ is 5 and ‘b’ is 10. if a == 5 and b == 10: print("Yes") # Make an if statement that prints ‘A’ is the variable ‘grade’ is in the range 100-90, ‘B’ if it is in the range 89-80, ‘C’ if it is in the range 79-70, and “Bad†otherwise. grade = 100 if 90 <= grade <= 100: print("A") elif grade >= 80: print("B") elif grade >= 70: print("C") else: print("Bad")