Below are some pieces of code discussed during lessonsSIMPLE PRINT
Print "Hello World" SIMPLE VARIABLE CALCULATION Variable1 = 10 Variable2 = 20 print = Variable1 * Variable2 GETTING PEOPLE TO INPUT A VARIABLE Variable = Input "Enter your name " Print = Variable THIS WILL WORK OUT AREA OF CIRCLE + MESSAGE AT END PI = 3.142 Rad = input("Enter radius of circle ") Rad_squared = Rad ** 2 Area = Rad_squared * PI print "Here is the area of your circle is " + str(Area) + " cm2" WORK OUT TAX AND TIP ON A MEAL meal1 = 4.99 meal2 = 8.99 meal3 = 7.00 total = meal1 + meal2 + meal3 tax = total * 0.2 totalwithtax = total + tax print "Your bill with no tip included " + str(totalwithtax) totalwithtip = totalwithtax * 0.1 + totalwithtax print "Your bill with tip included " + str(totalwithtip) ONE DIMENSIONAL LISTS (ARRAYS) - THIS WILL RETURN ITEM 1 INT THE LIST - BILL (NOT BOB (THAT IS 0)my_list = ("bob","bill","ben","jon","tom") name = my_list[1] print name PHONE CHARGER = less than iCharge = input("What is the charge?") if (int(iCharge) < 100): print("Phone not charged") else: print("Fully charged!") HEATING SYSTEM = less than or equal to Temp = input("What is the temperature in the house?") if (int(Temp) <= 15): print ("The temperature is ", Temp, "degrees. Time to put the heating on") else: print ("The heating can stay off", Temp, "degrees. The heating is going off") |
Some additional code:VARIOUS OPERATORS
print (3 + 4) print (3 - 4) print (3 * 4) print (3 / 4) print (3 % 2) print (3 ** 4) # 3 to the fourth power print (3 // 4) #floor division WORKING WITH LISTS (ARRAYS)
SELECTION: IF, THEN ELSE - character counter Month = “September” If len(month) > 10 print(month + “ has more than 10 characters”) Else: print(month + “ has less than ten characters”) CASE or ELIF - Grade predictor exam = int(input("enter mark ")) if exam > 90: print ("A*") elif exam > 80: print("A") elif exam > 70: print ("B") elif exam > 60: print("C") else: print ("D") FOR loop - examples month = 'september' for letter in month: print(letter) N = int(input("people")) for a in range(N): print(a) a = "password" for x in a: print (0) sum = 0 for i in range(10): sum = sum+i print(sum) message = input("Enter a message: ") new_message = "" VOWELS = "aeiou" print() for letter in message: if letter.lower() not in VOWELS: new_message += letter print("A new string has been created:", new_message) print("\nYour message without vowels is:", new_message) input("\n\nPress the enter key to exit.") for letters in ("Cheese"): print(letters) # some sample FOR loops print ("\nCounting") for i in range(10): print (i, end=" ") print ("\ncounting by fives:") for i in range(0, 50, 5): print (i, end=" ") print ("\nCounting backwards:") for i in range(10, 0, -1): print (i, end=" ") sampleList = [1,2,3,4,5,6,7,8] for a in sampleList: print (a) # now get them to change all the numbers in the array a = ('cat', 'dog', 'frog') for x in a: print (x, len(x)) WHILE loop - 3 year old simulator response = "" while response != "Because": response = input("Why?") print ("Oh. Okay.") WHILE loop - guessing game guess = input("Make a guess") Guesses = 10 while guess != "computer": guess = input("Guess again") Guesses = Guesses - 1 if Guesses == 0: print ("out of guesses") break else: print ("Winner") WHILE LOOP - 99 bottles of beer on the wall countdown=99 while countdown != 0: print ("There are ",countdown," bottles of beer on the wall", countdown,"bottles of beer") countdown = countdown - 1 print ("They are all smashed") |