2D Lists & Nested Loops
number_grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [0], ] for row in number_grid: for col in row: print(col) //Output : 1 // 2 // 3 // 4 // 5 // 6 // 7 // 8 // 9 // 0
def translate(phrase): translation = "" for letter in phrase: if letter.lower() in "aeiou": if letter.isupper(): translation += "G" else: translation += "g" else: translation += letter return translation print(translate(input("Enter a phrase: "))) //Output : Enter a phrase: apple // gpplg
num1 = input("Enter a number: ") num2 = input("Enter another number: ") result = int(num1) + int(num2) print(result) //Output : Enter a number: 1 // Enter another number: 2 // 3
num1 = input("Enter a number: ") num2 = input("Enter another number: ") result = float(num1) + float(num2) print(result) //Output : Enter a number: 1 // Enter another number: 2 // 3.0
num1 = float(input("Enter first number : ")) op = input("Enter operator : ") num2 = float(input("Enter second number : ")) if op == "+": print(num1 + num2) elif op == "-": print(num1 - num2) elif op == "*": print(num1 * num2) elif op == "/": print(num1 / num2) else: print("Invalid operator.") //Output : Enter first number : 1 // Enter operator : + // Enter second number : 1 // 2.0
secret_word = "giraffe" guess = "" guess_count = 0 guess_limit = 3 out_of_guesses = False while guess != secret_word and not out_of_guesses: if guess_count < guess_limit: guess = input("Enter guess: ") guess_count += 1 else: out_of_guesses = True if out_of_guesses: print("Out of Guesses, YOU LOSE !") else: print("You win!") //Output : Enter guess: lion // Enter guess: tiger // Enter guess: giraffe // You win!
Question.py
class Question: def __init__(self, prompt, answer): self.prompt = prompt self.answer = answer
from Question import Question question_prompts = [ "What color are apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n\nAnswer : ", "What color are bananas?\n(a) Teal\n(b) Magenta\n(c) Yellow\n\nAnswer : ", "What color are strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n\nAnswer : " ] questions = [ Question(question_prompts[0], "a"), Question(question_prompts[1], "c"), Question(question_prompts[2], "b") ] def run_test(questions): score = 0 for question in questions: answer = input(question.prompt) if answer == question.answer: score += 1 print("You got " + str(score) + "/" + str(len(questions)) + " correct.") run_test(questions) //Output : What color are apples? // (a) Red/Green // (b) Purple // (c) Orange // // Answer : a // // What color are bananas? // (a) Teal // (b) Magenta // (c) Yellow // // Answer : c // // What color are strawberries? // (a) Yellow // (b) Red // (c) Blue // // Answer : b // // You got 3/3 correct.
s = input("enter a string : ") # reverse a given string def palindrome(string): x = "" for i in string: x = i + x return x if s == palindrome(s): print("it is a palindrome") else : print("it is not a palindrome")
s = input ("enter a string : ") # function to check the palindrome def palindrome(string): for i in range(1, int(len(string) / 2)): if string[i] == string[len(string) - i - 1]: return True return False print(palindrome(s))
Student.py
class Student: def __init__(self, name, major, gpa, is_on_probation): self.name = name self.major = major self.gpa = gpa self.is_on_probation = is_on_probation def on_honor_roll(self): if self.gpa >= 3.5: return True else: return False
from Student import Student student1 = Student("Jim", "Business", 3.1, False) student2 = Student("Pam", "Art", 2.5, True) print(student1.name) //Output : Jim print(student1.gpa) //Output : 3.1 print(student2.gpa) //Output : 2.5
month_conversions = { "Jan": "January", "Feb": "February", "Mar": "March", "Apr": "April", "May": "May", "Jun": "June", "Jul": "July", "Aug": "August", "Sep": "September", "Oct": "October", "Nov": "November", "Dec": "December" } print(month_conversions["Oct"]) //Output : October print(month_conversions.get("Oct")) //Output : October print(month_conversions.get("xxx", "Not a valid Key.")) //Output : Not a valid Key.