Posts

Exercise 2 (Time Module)

#Create a python program capable of gretting you with Good Morning, Good Afternoon, and Good Evening. Your program should use time module to get the current hour. #Solution 1 import time t = time.strftime('%H: %M: %S') timestamp = int(time. strftime('%H')) minutes = int(time. strftime('%M')) seconds = int(time. strftime('%S')) print("Hours", "=", timestamp) print("Minutes", "=", minutes) print("Seconds", "=", seconds) if timestamp >0 and timestamp <12:     print("Good Morning") elif timestamp >=12 and timestamp <17:     print("Good Afternoon") elif timestamp >=17 and timestamp <20:     print("Good Evening") else:     print("Good Night") # output =  # Hours = 21 # Minutes = 22 # Seconds = 38 # Good Night

Basic Calculator(Exercise 1)

num1 = int(input("Enter first number")) num2 = int(input("Enter second number")) operator = str(input("Enter the operator")) if operator == "+":     print(num1 + num2) elif operator == "-":     print(num1 - num2) elif operator == "*":     print(num1 * num2) elif operator == "/":     print(num1 / num2) elif operator == "%":     print(num1 % num2) elif operator == "//":     print(num1 // num2) else:     print("Invalid Input") #output =  # Enter first number100 # Enter second number20 # Enter the operator+ # 120 # output = # Enter first number100 # Enter second number20 # Enter the operator- # 80 # output =  # Enter first number100 # Enter second number20 # Enter the operator* # 2000 # output = # Enter first number100 # Enter second number20 # Enter the operator/ # 5.0 # output = # Enter first number100 # Enter second number20 # Enter the operator% # 0

Raising Custom Error

''' Raising Custom Error: In Python, we can raise custom error by using the "raise" keyword. ''' #Example: a = int(input("Enter a value between 5 and 9")) if(a<5 or a>9):     raise ValueError("Value should be between 5 and 9") # output = # Enter a value between 5 and 9 10 # Traceback (most recent call last): #   File "c:\Users\sindh\OneDrive\Desktop\Python Codes\Raising Custom Error.py", line 4, in <module> #     raise ValueError("Value should be between 5 and 9") # ValueError: Value should be between 5 and 9 salary = int(input("Enter the salary amount: ")) if not 50000 < salary < 70000:     raise ValueError("Not a valid salary") # output = # Enter the salary amount: 10000 # Traceback (most recent call last): #   File "c:\Users\sindh\OneDrive\Desktop\Python Codes\Raising Custom Error.py", line 18, in <module> #     raise ValueError("Not a valid salary...

Finally Clause

''' Finally Clause: It is also a part of exception handling. When we use exception using try and except block, we can include a finally block at the end. The finally block is always executed, so it is generally used for doing the concluding tasks like closing file resources or closing database connection or maybe ending the program execution with a delightful message. Syntax: try:     statements which could generate exceptions except:     solution of generated exception finally:     Block of code which is going to execute at any situation  The finally block is executed irrespective of the outcome of the try...except...else block. ''' try:     l = (1, 5, 7, 13)     i = int(input("Enter the index: "))     print(l[i]) except:     print("Some error occurred") finally:     print("I am always executed") # output =  # Enter the index: 0 # 1 # I am always executed # output =  # Enter the index: 4 # So...

Exception Handling

''' Exception Handling: It is the process of responding to unwanted or unexpected events when a computer program runs. It deals with these events to avoid the program or system crashing. Without this process, exception would disrupt the normal operation of the program. ''' a = 5 print(f"Multiplication table of {a} is: ") for i in range(1, 11):   print(f"{int(a)} X {i} = {int(a) * i}") #output = Multiplication table of 5 is:  # 5 X 1 = 5 # 5 X 2 = 10 # 5 X 3 = 15 # 5 X 4 = 20 # 5 X 5 = 25 # 5 X 6 = 30 # 5 X 7 = 35 # 5 X 8 = 40 # 5 X 9 = 45 # 5 X 10 = 50 a = input("Enter the number") print(f"Multiplication table of {a} is: ") for i in range(1, 11):   print(f"{int(a)} X {i} = {int(a) * i}") #output = # Enter the number12 # Multiplication table of 12 is: # 12 X 1 = 12 # 12 X 2 = 24 # 12 X 3 = 36 # 12 X 4 = 48 # 12 X 5 = 60 # 12 X 6 = 72 # 12 X 7 = 84 # 12 X 8 = 96 # 12 X 9 = 108 # 12 X 10 = 120 # a = input("Ente...

for Loop with else

''' else statement in Loop: We have learnt before that else clause is used with if-statement. But Python allows the else keyword to be used with for and while loops too. The else block appears after the body of the loop. The statements in the else block will be executed after all the iterations are completed. The program exits the loop after else block is executed. ''' #Example: for i in range(5):     print(i) else:     print("Sorry no i") #output = 0 1 2 3 4 #Sorry no i for i in[]:     print(i) else:     print("Sorry no i") #output = Sorry no i for i in range(6):     print(i)     if i == 4:         break else:     print("Sorry no i") # output =  # 0 # 1 # 2 # 3 # 4 #Here else-statement didn't execute because loop is completed after the if-statement. i = 0 while i<7:     print(i)     i = i + 1     if i == 4:         break else:    ...

Dictionary Methods

''' Dictionary Methods: Dictionary uses several built-in methods for manipulation. 1.update() 2.clear() ''' ''' 1.update() This method updates the value of the key provided to it if the item already exists in the dictionary, else it creates a new value-pair. ''' ep1 = {2402: 92, 1001: 88, 1806: 45, 2810: 57} ep2 = {3583: 61, 9361: 90, 2539: 73, 3748: 44} ep1.update(ep2) print(ep1)              #output = {2402: 92, 1001: 88, 1806: 45, 2810: 57, 3583: 61, 9361: 90, 2539: 73, 3748: 44} ''' Removing items from dictionary: 2.clear() This method clears all the items from the dictionary. 3.pop() This method removes the key-value pair whose key is passed as a parameter. 4. popitem() This method removes the last key-value pair from the dictionary. 5.del() This keyword is used to remove a dictionary item. ''' ep1 = {2402: 92, 1001: 88, 1806: 45, 2810: 57} ep1.clear() print(ep1)            #output = {} ep1 = {2402: 92, 1001: ...