Posts

Recursion in Python

The recursion concept in Python is explained through the factorial and the Fibonacci sequence. # factorial(0) = 1 # factorial(1) = 1 # factorial(7) = 7*6*5*4*3*2*1 # factorial(7) = 7*factorial(6) # factorial(7) = 7*6*factorial(5) # factorial(7) = 7*6*5*factorial(4) # factorial(7) = 7*6*5*4*factorial(3) # factorial(7) = 7*6*5*4*3*factorial(2) # factorial(7) = 7*6*5*4*3*2*factorial(1) # factorial(7) = 7*6*5*4*3*2*1 # factorial(4) = 4*3*2*1 # factorial(n) = n*factorial(n-1) def findFactorial(n): if n==0 or n==1: return 1 else: return n * findFactorial(n-1) print(findFactorial(5)) # fibonachi numbers sequence = 0 1 1 2 3 5 8 13 .... def findFibonachiNo(n): if n==0: return 0 elif n==1: return 1 else: return findFibonachiNo(n-1) + findFibonachiNo(n-2) print(findFibonachiNo(6))

Create Basic "Search Engine" in Python

The following script is a very basic search engine in python. It takes user input, checks it against the so-called indexed content and returns the results in decreasing order of match cases. def matches(userQuery, indexedData): count = 0 for wordQ in userQuery.split(" "): for wordS in indexedData.split(" "): if wordQ == wordS.lower(): count += 1 # return f"{count} Result(s) found!" return count # print(f"{count} Result(s) found!") if __name__ == "__main__": sentences = ["python is a beginner-friendly programming language", "it is good to start with python your coding journey", "There are many programming languages", "youtube shorts is a great way to create videos instantly", "car is goood for convenience, bike is better when you are solo"] q

Do While Loop Emulation in Python

i = 5 while i != 45: print(i) i+=1 if i == 33: break

Match Case Statements in Python

print("\nThe capital of the following countries can be known.\nPakistan\nIndia\nTurkey\nIran\n") cName = input("Plz enter country name: ") match cName: case "Pakistan": print("The capital is Islamabad.\n") case "India": print("The capital is New Delhi.\n") case "Turkey": print("The capital is Ankara.\n") case "Iran": print("The capital is Tehran.\n") case _: #default case print("Sorry! This country is not in our database.\n")

Greetings Program in Python

Just enter your name and the program will greet you according to the time of the day. Note: If you run this program on your computer, it will respond according to your system time. In case you run this program on any online platform, it may respond according to the server time on which that platform is hosted. import time name = input("Plz enter your name: ") # timeNow = time.strftime("%H:%M:%S") # print(timeNow) hr = int(time.strftime("%H")) # print(hr) mints = int(time.strftime("%M")) # print(mints) secs = int(time.strftime("%S")) # print(secs) if hr in range(6, 12): if mints in range(0, 60): if secs in range(0, 60): print("Good Morning", name) elif hr in range(12, 14): if mints in range(0, 60): if secs in range(0, 60): print("Good Noon", name) elif hr in range(14, 17): if mints in range(0, 60): if secs in range(0, 60):

.find() and .index() string methods in Python

 The difference between these two methods is that .index()  method raises error while the other does not. str1 = "My name is Abdur." print(str1.find("is")) # gives index of first character occurence print(str1.find("iss")) #does not raises error, returns -1 print(str1.index("is")) # gives index of first character occurence print(str1.index("iss")) # raises error

Guess The Number Game - You vs. Your Friend

Guess a Random Number Game import random print("\nWelcome to the Guess a random number Game!") print("Its a 2 player game.") print("The Winner is the one with the lowest attempts.\n") lower_limit = int(input("Plz choose the lower limit? ")) upper_limit = int(input("Plz choose the upper limit? ")) winner = [] for i in range(2): random_number = random.randint(lower_limit, upper_limit) print(f"\nIts player {i+1} turn:") guess_number = "" attempts = 0 while guess_number != random_number: attempts += 1 guess_number = int(input(f"Player {i+1}, Plz guess the number between {lower_limit} and {upper_limit}: ")) if guess_number < random_number: print("Plz choose higher number") elif guess_number > random_number: print("Plz choose lower number") else: print(f"Player {i+1}, You correctly