Menu

Python Sunday 31

Python Sunday 31

Scratchpad #1

# print(100 + 100)

# # some message
# print(10 + 5 + 6)  # this is a comment

# num = 105

# 110
highScore = 0  # camel casing

high_score = 0  # snake casing
HighScore = 0  # Pascal casing

DAYS_IN_A_WEEK = 7

__high_Score__ = 0

Scratchpad #2

# num = 10
# num = num / 3

# age = None
# # some code
# age = 10

# age

name = "John Doe"

name.lower().find("doe").upper()

Scratchpad #3

# # print("Amount of US dollars you want to convert?")
# usd = float(input("Amount of US dollars you want to convert: "))
# inr = usd * 80
# print(f"${usd} is equal to Rs. {inr}")

# name = "Ratan Tata"

# if name == "Mukesh Ambani":
#     print("Reliance Industries")

# if name == "Ratan Tata":
#     print("Tata Sons & Group")

# if name == "Gautam Adani":
#     print("Adani Group")
# else:
#     print("Some other business person")

# if name == "Mukesh Ambani":
#     print("Reliance Industries")
# elif name == "Ratan Tata":
#     print("Tata Sons & Group")
# elif name == "Gautam Adani":
#     print("Adani Group")
# else:
#     print("Some other business person")

# logged_in_user = None

# if logged_in_user:
#     print("Welcome")
# else:
#     print("Please log in to continue")

# if True:
#     print("Hello World")

# age = int(input("How old are you? "))

# if age >= 65:
#     print("Drinks are free")
# elif age >= 21:
#     print("You can enter and drink")
# elif age >= 18:
#     print("You can enter but you can't drink")
# else:
#     print("Not allowed")

# age = int(input("How old are you? "))

# if age >= 18 and age < 21:
#     print("You can enter but you can't drink")
# elif age >= 21 and age < 65:
#     print("You can enter and drink")
# elif age >= 65:
#     print("Drinks are free")
# else:
#     print("Not allowed")

# age = input("How old are you? ")

# if age:
#     age = int(age)
#     if age >= 18 and age < 21:
#         print("You can enter but you can't drink")
#     elif age >= 21 and age < 65:
#         print("You can enter and drink")
#     elif age >= 65:
#         print("Drinks are free")
#     else:
#         print("Not allowed")
# else:
#     print("Please enter an age")

# player1 = input("Please enter player 1's choice: ")
# player2 = input("Please enter player 2's choice: ")

# if player1 == player2:
#     print("It's a tie")
# elif ((player1 == 'rock' and player2 == 'scissor')
#       or (player1 == 'scissor' and player2 == 'paper')
#       or (player1 == 'paper' and player2 == 'rock')):
#     print("Player 1 wins")
# elif ((player2 == 'rock' and player1 == 'scissor')
#       or (player2 == 'scissor' and player1 == 'paper')
#       or (player2 == 'paper' and player1 == 'rock')):
#     print("Player 2 wins")
# else:
#     print("Please enter a valid choice")

# for num in range(50):  # 0,1,22,3,4
#     print(f"{num} - Hello World")

# print("Hello Universe")
# # num = 0

# for char in "Hello World":
#     print(char)

# for num in range(10):
#     print(num)

# for num in range(4, 10):
#     print(num)

# for num in range(0, 20, 5):
#     print(num)

for num in range(0, -10, -2):
    print(num)

Scratchpad #4

# for num in range(1, 21):
#     if num == 5 or num == 16:
#         print("FizzBuzz")
#     elif num % 2 == 0:
#         print("Fizz is even")
#     else:
#         print("Fizz is odd")

# for char in "Hello World":
#     print(char)

# while True:
#     print("Hello")

# password = input("Enter your password: ")

# while password != "hello":
#     print("Incorrect Password")
#     password = input("Enter your password again: ")

# print("Welcome")

# for num in range(10):
#     print(num)

# count = 0
# while count < 10:
#     print(count)
#     count += 1

# count = 1
# password = input("Enter your password: ")

# while password != "hello":
#     print("Incorrect Password")
#     if count == 3:
#         break
#     password = input("Enter your password again: ")
#     count += 1

# password = input("Enter your password: ")

# while True:
#     if password == "hello":
#         break
#     print("Incorrect Password")
#     password = input("Enter your password again: ")

# for num in range(10):
#     if num == 4:
#         break
#     print(num)

# for num in range(10):
#     password = input("Enter your password: ")

#     while True:
#         if password == "hello":
#             break
#         print("Incorrect Password")
#         password = input("Enter your password again: ")

#     print(num)

# password = input("Enter your password: ")

# while True:
#     if password == "hello":
#         break
#     print("Incorrect Password")
#     password = input("Enter your password again: ")

# for num in range(1, 11):
#     print(f"{num} time")

#     password = input("Enter your password: ")

#     if password == "exit":
#         break

#     while True:
#         if password == "hello":
#             break
#         print("Incorrect Password")
#         password = input("Enter your password again: ")

# tasks = ""

# while True:
#     todo = input("Please enter a task or 'exit': ")
#     if todo.lower() == 'exit':
#         break

#     tasks = tasks + todo + "||"

# print(tasks)

# forbidden_words = ["idiot", "nonsense", "mad", "moron", "stupid", "dumb"]

# message = input("Your message to me: ")
# words = message.split(" ")

# new_message = ""

# for word in words:
#     if word in forbidden_words:
#         new_message = new_message + "nice "
#     else:
#         new_message = new_message + word + " "

# print(new_message)

forbidden_words = ["idiot", "nonsense", "mad", "moron", "stupid", "dumb"]

# for word in forbidden_words:
#     print(word)

index = 0
while index < len(forbidden_words):
    print(forbidden_words[index])
    index += 1

Scratchpad #5

# # names = ["john", "jack", "jane", "john", "john", "jill"]

# # for name in names:
# #     if name == "john":
# #         print("CURRENT", names)
# #         names.remove("john")
# #         print("UPDATED", names)

# # print(names)

# names = ["john", "jack", "jane", "john", "john", "jill"]

# new_names = []

# for name in names:
#     if name != "john":
#         new_names.append(name)

# print(new_names)

# names = ['jack', 'jane', 'jill', 'john', 'john', 'john']

# print(names[3:])

# frameworks = ["Django", "Flask"]
# frameworks[0], frameworks[1] = frameworks[1], frameworks[0]

# print(frameworks)

# names = ['jack', 'jane', 'jill', 'john', 'john', 'john']
# upper_names = [x.upper() for x in names]

# nums = [1, 2, 3, 4, 5, 6, 7, 8]
# double = [num * 2 for num in nums]

# nums = [1, 2, 3, 4, 5, 6, 7, 8]
# str_nums = [str(num) for num in nums]

# # double = []
# # for num in nums:
# #     double.append(num * 2)

# # upper_names = []
# # for name in names:
# #     upper_names.append(name.upper())

# print(upper_names)
# print(double)
# print(str_nums)

# numbers = [1, 2, 3, 4, 5, 6]

# modified = [num * 2 if num % 2 != 0 else num / 2 for num in numbers]
# print(modified)

# modified = []

# modified = [num * 2 if num % 2 != 0 else num / 2 for num in numbers]
# for num in numbers:
#     if num % 2 != 0:
#         modified.append(num * 2)
#     else:
#         modified.append(num / 2)

# evens = [num for num in numbers if num % 2 == 0]
# odds = [num for num in numbers if num % 2 == 1]

# names = ['jacky', 'jane', 'jilly', 'john', 'johnathan', 'john']
# long_names = [name for name in names if len(name) > 4]

# print(long_names)

# message = "Hello Python!"

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

total = 0
for lst in nested_list:
    for num in lst:
        total += num

print(total)

nested_list = [[[1, 2, 3], [1, 2, 3, 4], [1, 2, 3]],
               [[1, 2, 3], [1, 2, 23, 3], [1, 2, 3], [1, 2, 3]],
               [[1, 2, 3], [1, 2, 3], [1, 2, 23, 12, 10, 3]]]

Scratchpad #6

# lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# total = 0

# for l in lst:
#     for num in l:
#         total += num

# print(total)

# product = [
#     "iPhone 14 pro max xl",
#     "Apple",
#     200000,
#     500,
#     "Some description....",
#     150,
# ]

# product = {
#     "name": "iPhone 14 pro max xl",
#     "in_stock": 500,
#     "price": 200000,
#     "description": "Some description...",
#     "no_of_reviews": 150,
#     "discount_available": True,
#     "brand": "Apple",
#     "reviews": [
#         {"user": "john", "comment": "Bakwas"},
#         {"user": "jack", "comment": "mehenga bohot"},
#     ]
# }

# song = {
#     "name": "birthday bash",
#     "artists": ["honey singh"],
#     "album": "birthday bash",
#     "release_year": "2016",
#     "track_length": 3.0,
#     "label": "t-series"
# }

# song1 = {"name": "song1", "artists": ["john doe", "jane"]}
# song2 = dict(name="song1", artists=["john doe", "jane"])

# print(song1, song2)

# book = {
#     "name": "The theory of everything",
#     "author": "Stephen Hawking",
#     "pages": 140,
#     "language": "English",
#     "in_stock": True,
#     73: 1001001
# }

# print(book["pages"])
# print(book["language"])
# print(book[73])

# book["hello"] = "world"
# book["pages"] = 200
# book["universe"] = 123123123

# print(book)

# book = {
#     "name": "The theory of everything",
#     "author": "Stephen Hawking",
#     "pages": 140,
#     "language": "English",
#     "in_stock": True,
# }

# for key in book:
#     print(key, book[key])

# print(book["name"])

# for key in book.keys():  # ["The theory of everything", "Stephen Hawking"]
#     print(key)

# for value in book.values():  # ["The theory of everything", "Stephen Hawking"]
#     print(value)

# for key in book.keys():  # ["The theory of everything", "Stephen Hawking"]
#     print(key, book[key])

# for item in book.items():
#     print(item)

# for key, value in book.items():
#     print(key, value)

song = {
    "name": "Parabola",
    "artist": "Tool",
    "album": "Lateralus",
    "released": 2001,
    "genres": ["Progressive/Art Rock", "Progressive metal"],
}

print(song.get("track_length"))

print("SOME MORE CODE")

Scratchpad #7

# def hello():
#     print("Hello World")
#     print("Hello World")
#     print("Hello World")
#     print("Hello World")

# hello()
# hello()
# hello()
# hello()

from random import randint, random

# def guess():
#     print(randint(1, 7))

# guess()

# def toss():
#     num = random()
#     if num > 0.5:
#         print("Heads")
#     else:
#         print("Tails")

# toss()

# def toss():
#     num = random()
#     if num > 0.5:
#         return "Heads"
#     else:
#         return "Tails"

# def toss(times):
#     for _ in range(0, times):
#         num = random()
#         if num > 0.5:
#             print("Heads")
#         else:
#             print("Tails")

# toss(5)

# def add(a, b):
#     return a + b

# print(add(10, 3))

# def full_name(first_name, last_name):
#     return f"Your full name is {first_name} {last_name}"

# print(full_name("John", "Doe"))
# print(full_name("Jack", "Smith"))

# def sum_odd_numbers(numbers):
#     total = 0
#     for num in numbers:
#         if num % 2 != 0:
#             total += num
#     return total

# print(sum_odd_numbers([1, 2, 3, 4, 5, 6, 7, 8]))

# def is_odd_number(number):
#     if number % 2 != 0:
#         return True
#     return False

# print(is_odd_number(3))

# def mul(a=1, b=1):
#     return a * b

# print(mul(10))

# def toss(times=1):
#     for _ in range(0, times):
#         num = random()
#         if num > 0.5:
#             print("Heads")
#         else:
#             print("Tails")

# toss(3)

# def add(a, b):
#     return a + b

# def sub(a, b):
#     return a - b

# def math(a, b, fn=sub):
#     return fn(a, b)

# result = math(10, 20, add)
# print(result)

# def print_full_name(first_name, last_name, age=10):
#     print(f"My name is {first_name} {last_name} and I am {age} years old.")

# print_full_name(last_name="doe", age=20, first_name="jane")

# name = "John Doe"

# def greet():
#     name = "Jane Smith"
#     print(f"Hello {name}")

# greet()
# print(name)

# total = 0

# def static():
#     global total
#     total += 10
#     print(total)

# static()
# print(total)

# def outer():
#     count = 0

#     def inner():
#         nonlocal count
#         count += 10
#         print(count)

#     inner()

# outer()

# def add(a, b):
#     """This functions accepts two numbers and add them
#     @param a: int
#     @param b: int
#     @return int
#     """
#     return a + b

# print(add.__doc__)
# print(help(add))

# def add(a, b):
#     return a + b

# def add(*nums):
#     total = 0
#     for num in nums:
#         total += num

#     return total

# print(add(10, 2, 12, 124, 54, 65, 7, 567, 7889, 5433, 21))

# def names(name1, name2, *names):
#     print(name1)
#     print(name2)
#     print(names)

# names("John", "Jane", "Jack", "Jim", "Jill", "Janam")

# def profile(**details):
#     print(details)

# profile(first_name="John", last_name="Doe", age=20)


def show_info(a, b, *args, role="moderator", **kwargs):
    return [a, b, args, role, kwargs]


print(
    show_info(10,
              20,
              30,
              40,
              50,
              role="Dev",
              first_name="John",
              last_name="Doe"))