Python Programming (May 2022)
Python Programming (May 2022)
Scratchpad #3
# # Currency Convert # # print("Amount of USD you want to convert") # usd = input("Amount of USD you want to convert: ") # inr = float(usd) * 75.43 # inr = round(inr, 2) # print(f"USD {usd} is equal to Rs. {inr}") # # ----------------- # city = input("Enter your city: ") # # city = city.lower() # if city == "bejing": # print("You are not welcome") # elif city == "mumbai": # print("You are always welcome") # elif city == "delhi": # print("Nahi aana hai!") # else: # print("You are also welcome") # --------------------------------------- # city = "askdmkdsa" # if city: # print(f"{city} is a great place") # else: # print("Please enter something") # loggedInUser = "" # if loggedInUser: # print("Welcome") # else: # print("Please log in") # ------------------- # age = int(input("Please enter your age: ")) # if age >= 65: # print("Drink are free!") # elif age >= 21: # print("You are allowed to enter and drink") # elif age >= 18: # print("You can enter, but you cannot drink") # else: # print("Entry denied!") # if age >= 18 and age < 21: # print("You can enter, but you cannot drink") # elif age >= 21 and age < 65: # print("You are allowed to enter and drink") # elif age >= 65: # print("Drink are free!") # else: # print("Entry denied!") isLoggedIn = False user_role = "moderator" if isLoggedIn: if user_role == "admin": print("You have access to everything") else: print("You can access fewer things") else: print("Please log in first")
Scratchpad #4
# for char in "hello": # print(char) # for num in range(0, 1000): # print(num) # print(f"{num}: Hello World") # for num in range(1, 11): # print(num) # print("Goodbye World") # for char in "Hello World": # print(char) # for num in range(14, 0): # print(num) # for num in range(0, 10, 3): # print(num) # for num in range(11, 0, -2): # print(num) # for num in range(0, 10, 2): # print(num) # for num in range(1, 21): # if num == 5 or num == 16: # print(f"{num} - FizzBuzz") # elif num % 2 == 0: # print(f"{num} - Fizz is even") # elif num % 2 != 0: # print(f"{num} - Fizz is odd") # password = input("Please enter your password: ") # while password != "helloworld": # print("ACCESS DENIED!") # password = input("Please enter your password again: ") # while password == "helloworld": # if password == "helloworld": # print("Welcome") # elif password != "helloworld": # print("Access Denined") # print("Welcome") # while password != "helloworld": # print("ACCESS DENIED!") # password = input("Please enter your password again: ") # for num in range(0, 10): # print(num) # count = 0 # while count < 11: # print(count) # count += 1 # count = 3 # password = input("Please enter your password: ") # while True: # if count < 1: # print("All your chances are over! Bhag ja!") # break # if password == "helloworld": # break # print("ACCESS DENIED") # print(f"{count} chances remaining") # password = input("Please enter your password again: ") # count -= 1 # ----------------------------- player1 = input("Enter player1's choice: ") player2 = input("Enter player2's choice: ") if ( (player1 == "rock" and player2 == "scissors") or (player1 == "paper" and player2 == "rock") or (player1 == "scissors" and player2 == "paper") ): print("Player 1 wins") elif ( (player2 == "rock" and player1 == "scissors") or (player2 == "paper" and player1 == "rock") or (player2 == "scissors" and player1 == "paper") ): print("Player 2 wins") elif player1 == player2: print("It's a tie") else: print("Something went wrong")
Scratchpad #5
# todo = [] # while True: # task = input("Add task to list. (q to quit) ") # if task == "": # print("Please enter a valid task") # elif task == "q": # break # else: # todo.append(task) # print(f"You have {len(todo)} tasks in your list.") # print("Your TODOs are as follows: ") # for task in todo: # print("- " + task) # # print(todo) numbers = [1, 2, 3, 4] count = 0 while count < len(numbers): print(numbers[count]) count += 1 for num in numbers: print(num) # rahul@rstforum.co.in
Scratchpad #6
# numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] # # numbers # # None # # print("Hello World") # # print(10000) # numbers[1][1] + 10 # numbers = [1, 2, 3, 4, 5, 6] # # numbers2 = [] # # for num in numbers: # # numbers2.append(num / 2) # # numbers3 = [num / 3 for num in numbers] # # print(numbers2) # # print(numbers3) # numbers = [1, 2, 3, 4.5, 5.3, 6] # n2 = [n * 2 for n in numbers] # print(n2) nested_list = [ [[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3]], ] # for lst in nested_list: # for num in lst: # print(num)
Scratchpad #7
product = ["iPhone 13", "Apple", "Some description...", 100000, 500, True] # products = [ # ["iPhone 13", "Apple", "Some description...", 100000, 500, True], # ["iPhone 13", "Apple", "Some description...", 100000, 500, True], # ["iPhone 13", "Apple", "Some description...", 500, 100000, True], # ] # product[0] # product[1] # product = { # 2: "Apple", # 5: 100000, # 4: 500, # 1: "Some description...", # 3: "iPhone 13", # 0: "Hello", # } # product2 = ["Hello", "Some description...", "Apple", "iPhone 13", 500, 100000] # print(product2[0]) # print(product[0]) # product[3] # print(product["name"]) # print(product[0]) # print(product2[0]) # profile1 = { # "name": "John Doe", # "age": 20, # } # profile2 = dict(name="John Doe", age=20) # print(profile1) # print(profile2) # book = { # "name": "The theory of everything", # "author": "Stephen Hawking", # "pages": 140, # "language": "English", # "in_stock": True, # 73: 1001001, # } # print(book["author"]) # print(book["author"]) # print(book["description"]) # for key in book: # print(book[key]) # for value in book.values(): # print(value) # for item in book.items(): # print(item[0]) # print(item[1]) # for key, value in book.items(): # print(key, value) song = { "name": "Parabola", "artist": "Tool", "album": "Lateralus", "released": 2001, "genres": ["Progressive/Art Rock", "Progressive metal"], } # >>> song = { # ... "name": "Parabola", # ... "artist": "Tool", # ... "album": "Lateralus", # ... "released": 2001, # ... "genres": ["Progressive/Art Rock", "Progressive metal"], # ... } # >>> # >>> song # {'name': 'Parabola', 'artist': 'Tool', 'album': 'Lateralus', 'released': 2001, 'genres': ['Progressive/Art Rock', 'Progressive metal']} # >>> # >>> # >>> "Tool" in song # False # >>> # >>> "Tool" in song.keys() # False # >>> # >>> "Tool" in song.keys() # False # >>> # >>> "Tool" in song.values() # True # >>> # >>> "artist" in song.keys() # True # >>> # >>> "artist" in song # True # >>> # >>> song # {'name': 'Parabola', 'artist': 'Tool', 'album': 'Lateralus', 'released': 2001, 'genres': ['Progressive/Art Rock', 'Progressive metal']} # >>> # >>> # >>> # >>> x = {"a": "1", "b": "2", "c": "3"} # >>> # >>> # >>> x # {'a': '1', 'b': '2', 'c': '3'} # >>> # >>> # >>> x.clear() # >>> # >>> x # {} # >>> # >>> # >>> x = {"a": "1", "b": "2", "c": "3"} # >>> # >>> # >>> y = x # >>> # >>> y # {'a': '1', 'b': '2', 'c': '3'} # >>> x # {'a': '1', 'b': '2', 'c': '3'} # >>> x.clear() # >>> # >>> y # {} # >>> x = {"a": "1", "b": "2", "c": "3"} # >>> # >>> # >>> y = x.copy() # >>> y # {'a': '1', 'b': '2', 'c': '3'} # >>> x # {'a': '1', 'b': '2', 'c': '3'} # >>> x.clear() # >>> x # {} # >>> y # {'a': '1', 'b': '2', 'c': '3'} # >>> x # {} # >>> # >>> x = {"a": "1", "b": "2", "c": "3"} # >>> # >>> # >>> x # {'a': '1', 'b': '2', 'c': '3'} # >>> # >>> y = x # >>> # >>> y # {'a': '1', 'b': '2', 'c': '3'} # >>> x # {'a': '1', 'b': '2', 'c': '3'} # >>> # >>> x is y # True # >>> y = x.copy() # >>> # >>> y # {'a': '1', 'b': '2', 'c': '3'} # >>> x # {'a': '1', 'b': '2', 'c': '3'} # >>> y is x # False # >>> x is y # False # >>> x == y # True # >>> y = x # >>> x == y # True # >>> x is y # True # >>> x # {'a': '1', 'b': '2', 'c': '3'} # >>> y # {'a': '1', 'b': '2', 'c': '3'} # >>> # >>> # >>> # >>> # >>> # >>> # >>> {}.fromkeys("hello", True) # {'h': True, 'e': True, 'l': True, 'o': True} # >>> # >>> # >>> {}.fromkeys(["name", "age", "address", "phone"], None) # {'name': None, 'age': None, 'address': None, 'phone': None} # >>> # >>> # >>> {}.fromkeys(True, None) # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # TypeError: 'bool' object is not iterable # >>> # >>> # >>> {}.fromkeys(["name", "age"], None) # {'name': None, 'age': None} # >>> # >>> {}.fromkeys("abcd", None) # {'a': None, 'b': None, 'c': None, 'd': None} # >>> # >>> {}.fromkeys(100, None) # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # TypeError: 'int' object is not iterable # >>> # >>> # >>> # >>> # >>> # >>> {} # {} # >>> # >>> type({}) # <class 'dict'> # >>> # >>> {"name": "John Doe"} # {'name': 'John Doe'} # >>> # >>> # >>> song = { # ... "name": "Sober", # ... "artist": "Tool", # ... "album": "Undertow", # ... } # >>> # >>> # >>> # >>> song # {'name': 'Sober', 'artist': 'Tool', 'album': 'Undertow'} # >>> # >>> # >>> song["name"] # 'Sober' # >>> # >>> song["artist"] # 'Tool' # >>> # >>> song["length"] # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # KeyError: 'length' # >>> # >>> # >>> song # {'name': 'Sober', 'artist': 'Tool', 'album': 'Undertow'} # >>> # >>> song.get("name") # 'Sober' # >>> song.get("artist") # 'Tool' # >>> song.get("length") # >>> # >>> result = song.get("length") # >>> # >>> result # >>> print(result) # None # >>> # >>> song # {'name': 'Sober', 'artist': 'Tool', 'album': 'Undertow'} # >>> # >>> song["name"] # 'Sober' # >>> # >>> song.get("name") # 'Sober' # >>> song["hello"] # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # KeyError: 'hello' # >>> song.get("hello") # >>> # >>> # >>> song = { # ... "name": "Vicarious", # ... "album": "10,000 Days" # ... } # >>> # >>> # >>> song.pop() # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # TypeError: pop expected at least 1 argument, got 0 # >>> # >>> song.pop("name") # 'Vicarious' # >>> # >>> song # {'album': '10,000 Days'} # >>> # >>> # >>> song = { # ... "name": "Vicarious", # ... "album": "10,000 Days" # ... } # >>> # >>> song # {'name': 'Vicarious', 'album': '10,000 Days'} # >>> # >>> # >>> song.pop("hello") # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # KeyError: 'hello' # >>> # >>> song.pop("name") # 'Vicarious' # >>> song # {'album': '10,000 Days'} # >>> # >>> # >>> song = { # ... "name": "The Pot", # ... "artist": "Tool", # ... "album": "10,000 Days" # ... } # >>> # >>> song # {'name': 'The Pot', 'artist': 'Tool', 'album': '10,000 Days'} # >>> # >>> song.popItem() # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # AttributeError: 'dict' object has no attribute 'popItem'. Did you mean: 'popitem'? # >>> song.popItem() # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # AttributeError: 'dict' object has no attribute 'popItem'. Did you mean: 'popitem'? # >>> # >>> # >>> song.popitem() # ('album', '10,000 Days') # >>> # >>> # >>> # >>> # >>> # >>> song.popitem("artist") # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # TypeError: dict.popitem() takes no arguments (1 given) # >>> # >>> song = {'name': 'The Pot', 'artist': 'Tool', 'album': '10,000 Days'} # >>> # >>> # >>> # >>> song # {'name': 'The Pot', 'artist': 'Tool', 'album': '10,000 Days'} # >>> # >>> # >>> # >>> song2 = {} # >>> # >>> song # {'name': 'The Pot', 'artist': 'Tool', 'album': '10,000 Days'} # >>> song2 # {} # >>> song2.update(song) # >>> # >>> song2 # {'name': 'The Pot', 'artist': 'Tool', 'album': '10,000 Days'} # >>> # >>> song2["name"] # 'The Pot' # >>> # >>> song2["name"] = "Sober" # >>> # >>> song2 # {'name': 'Sober', 'artist': 'Tool', 'album': '10,000 Days'} # >>> # >>> # >>> # >>> song2.update({"length": 2.45}) # >>> # >>> song2 # {'name': 'Sober', 'artist': 'Tool', 'album': '10,000 Days', 'length': 2.45} # >>> # >>> song2["name"] = "Hello" # >>> # >>> song2 # {'name': 'Hello', 'artist': 'Tool', 'album': '10,000 Days', 'length': 2.45} # >>> # >>> song2.update({"name": "World"}) # >>> song2 # {'name': 'World', 'artist': 'Tool', 'album': '10,000 Days', 'length': 2.45} # >>> # >>> # >>> song2.update({"name": "Sober", "album": "Something"}) # >>> # >>> song2 # {'name': 'Sober', 'artist': 'Tool', 'album': 'Something', 'length': 2.45} # >>> # >>> # >>> # >>> song # {'name': 'The Pot', 'artist': 'Tool', 'album': '10,000 Days'} # >>> # >>> # >>> {key for key in song} # {'album', 'artist', 'name'} # >>> # >>> # >>> {key:True for key in song} # {'name': True, 'artist': True, 'album': True} # >>> # >>> # >>> # >>> {key:value for key,value in song.items()} # {'name': 'The Pot', 'artist': 'Tool', 'album': '10,000 Days'} # >>> # >>> # >>> song # {'name': 'The Pot', 'artist': 'Tool', 'album': '10,000 Days'} # >>> # >>> {key:value.upper() for key,value in song.items()} # {'name': 'THE POT', 'artist': 'TOOL', 'album': '10,000 DAYS'} # >>> {key.upper():value.upper() for key,value in song.items()} # {'NAME': 'THE POT', 'ARTIST': 'TOOL', 'ALBUM': '10,000 DAYS'} # >>> # >>> # >>> # >>> {num:True for num in range(10)} # {0: True, 1: True, 2: True, 3: True, 4: True, 5: True, 6: True, 7: True, 8: True, 9: True} # >>> # >>> {num:num for num in range(10)} # {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9} # >>> # >>> {num:num*2 for num in range(10)} # {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18} # >>> {num:num**2 for num in range(10)} # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81} # >>>
Scratchpad #8
# def hello(): # print("Hello World") # print("Hello World") # hello() # def greet(): # print("TEST") # return "Hello John" # message = greet() # print(greet()) # print(greet()) # print(message) import random # def flip_coin(): # if random.random() > 0.5: # return "HEADS" # else: # return "TAILS" # print(flip_coin()) # print(flip_coin()) # print(flip_coin()) # def greet(person): # parameter # return f"Hello, {person}" # print(greet("James")) # argument # print(greet("John")) # def greet(person, message): # parameter # return f"{message}, {person}" # print(greet("James", "Hello")) # def add(a, b): # return a + b # print(add(10, 5.4)) # def sum_odd_nums(nums): # total = 0 # for num in nums: # if num % 2 == 1: # total += num # return total # print(sum_odd_nums([1, 2, 3, 4, 5])) # def is_odd_num(num): # if num % 2 == 0: # return False # return True # print(is_odd_num(2)) # def greet(message, name="John"): # return f"{message.upper()}, {name}" # print(greet("John Doe", "hi")) # print(greet("James")) # def add(a, b): # return a + b # def sub(a, b): # return a - b # def math(a, b, fn=add): # return fn(a, b) # print(math(10, 50)) # def full_name(first_name="A", last_name="B"): # return f"{first_name} {last_name}" # print(full_name("john", "doe")) # print(full_name(last_name="doe", first_name="john")) # print(full_name()) # name = "James Smith" # def greet(): # name = "John Doe" # print(name) # print(name) # greet() # total = 50 # global scope # def math(a, b): # total = 10 # local scope # print(a + b + total) # math(10, 20) # total = 0 # def static(): # global total # total += 1 # return total # def increment(num): # num += 1 # return num # total = increment(total) # print(static()) # 0 # print(total) # def outer(): # count = 0 # def inner(): # nonlocal count # count += 1 # return count # return inner() def greet(message, name): """ This function takes a message and a name. It concatenates it and upper-cases it and return the value ## Example greet("Hi", "John") @params message: str name: str """ return f"{message}, {name}".upper() # print(greet("Hi", "John")) # print(greet.__doc__) # print(help(greet)) # import random
Scratchpad #9
# def add(*nums): # total = 0 # for num in nums: # total += num # return total # print(add( # 10, # 5, # 10, # 4, # )) # def add(special_num, *nums): # print(nums, special_num) # add(20, 1, 12, 12) # def profile(**kwargs): # print(kwargs) # profile(name="John Doe", age=20, profession="Programmer") # def show_info(a, b, *args, role="moderator", **kwargs): # return [a, b, args, role, kwargs] # print( # show_info(1, # 2, # 3, # 4, # True, # role="programmer", # first_name="John", # last_name="Doe")) # def add_all_values(*args): # total = 0 # for num in args: # total += num # print(total) # # Array / List # nums = [1, 2, 3, 4, 5, 6, 7, 8] # add_all_values(*nums) # def say_name(first, last): # print(f"My name is {first} {last}.") # data = {"first": "John", "last": "Doe"} # say_name(**data) # def square(num): # return num * num # cube = lambda num: num * num * num # print(cube(10)) # something = 10 # something = square # print(something(2)) # def math(a, b, fn): # return fn(a, b) # print(math(10, 2, lambda a, b: a + b)) # print(math(10, 2, lambda a, b: a - b)) # add = lambda a, b, c: a + b + c # def add(a, b): # return a + b # def sub(a, b): # return a - b # math(10, 2, sub) # print(math(10, 10, add)) # nums = [1, 2, 3, 4, 5, 6, 7, 8] # doubles = map(lambda num: num * 2, nums) # doubles2 = [num * 2 for num in nums] # # print(list(doubles)) # # print(doubles2) # for num in doubles: # print(num) # nums = [1, 2, 3, 4, 5, 6, 7, 8] # evens = filter(lambda num: num % 2 == 0, nums) # print(list(evens)) # even_doubles = map(lambda num: num * 2, filter(lambda num: num % 2 == 0, nums)) # print(list(even_doubles)) # data = [ # { # "name": "Product #1", # "price": 1000 # }, # { # "name": "Product #2", # "price": 2000 # }, # { # "name": "Product #3", # "price": 4000 # }, # { # "name": "Product #4", # "price": 5000 # }, # { # "name": "Product #5", # "price": 10000 # }, # ] # filtered_data = filter(lambda product: product["price"] > 4000, data) # data_in_rupees = map( # lambda product: { # "name": product["name"], # "price": product["price"] * 75 # }, filtered_data) # print(list(data_in_rupees)) # print(all([0, 1, 2, 3])) # nums = [2, 4, 6, 8] # all_evens = map(lambda num: num % 2 == 0, nums) # print(all(nums)) # people = ['John', 'Jack', 'James', 'Jason', 'Aane', 'Jacob'] # print(any(map(lambda name: name[0] == 'A', people))) numbers = [2, 3, 7, 3, 9, 1] sorted_numbers = sorted(numbers) print(sorted_numbers) print(numbers)