Posts

List comprehension in Python

List comprehensions in Python are a powerful and concise way to create new lists based on existing iterables (like lists, tuples, strings, etc.). They offer a more compact alternative to traditional for loops with append. Syntax: new_list = [ expression for item in iterable [if condition ]] expression:  This is what you want to do with each item in the iterable. It can be a simple variable representing the item, a calculation, a function call, or anything that evaluates to a value. item:  This represents the variable that takes on the value of each item in the iterable as you loop through it. Think of it as a temporary variable for each iteration. iterable:  This is the data source you're iterating over, like a list, tuple, string, or any object that can return its elements one at a time. condition (optional):  This is an optional  if  clause that acts as a filter. Only items that meet the condition will be included in the new list. Here are some examples to illustrate differen

Enumerate function in Python

  mlist = [ "abdur" , "banana" , "dog" , "gem" , "carbon" , "eyes" , "moon" ] index = 0 for item in mlist :     print ( index , item )     index += 1 # printing of item names along with indices can # be done with the enumerate function as below; for index , item in enumerate ( mlist ):     print ( index , item ) Also; another example l1 = [ "tomato" , "potato" , "laddoo" , "wresha" , "chawal" , "siwian" ] # return indexed list through ordinary method i = 1 for item in l1 :     print ( f " { i } . { item } " )     i += 1 # return indexed list through enumerate method for index , item in enumerate ( l1 , start = 1 ):     print ( f " { index } . { item } " )

You vs. Computer Guess Game (with error handling and Save-into-file high score capability)

Image
Error handling syntax try: except: import random rNum = random . randint ( 1 , 100 ) guesses = 0 while True :     try :         gn = int ( input ( "We have chosen a number between 1 and 100. Guess what? " ))         guesses += 1         if gn < rNum :                 print ( "Hint: Choose a higer number" )         if gn > rNum :                 print ( "Hint: Choose a lower number" )         if gn == rNum :                 print ( f "You guessed it right. Your number was { gn } " )                 print ( f "Our chosen number was { rNum } " )                 print ( f "You geussed it right in { guesses } guesses" )                 break     except :         print ( "invalid input. Plz enter only whole numbers!" ) with open ( "hi-score2.txt" , "r" ) as f :         hiscore = int ( f . read ()) if guesses < hiscore :     with open ( "hi-score2.txt" , "w"

Guessing Game - Between You and Computer

 Guess the number?  import random rNum = random . randint ( 1 , 100 ) while True :     gn = int ( input ( "We have chosen a number between 1 and 100. Guess what? " ))     if gn < rNum :         print ( "Choose higer number" )     if gn > rNum :         print ( "Choose lower number" )     if gn == rNum :         print ( f "You guessed it right. Your number was { gn } " )         print ( f "Our chosen number was { rNum } " )         break

Multi-level Inheritance in python

  class Animals :     animalCategory1 = "vertebrates"     animalCategory2 = "invertibrates" class Pets ( Animals ):     petCategory1 = "cats"     petCategory2 = "dogs" class Dog ( Pets ):     @ staticmethod     def bark ():                 print ( "The dog barks" ) a = Animals () b = Pets () c = Dog () print ( a . animalCategory2 ) print ( b . animalCategory2 ) print ( c . animalCategory2 ) print ( b . petCategory2 ) print ( c . petCategory2 ) a . animalCategory1 = "mammals" print ( a . animalCategory1 )

Creating a Class of Job Application form in Python

  class JobApplication :     def __init__ ( self , name , phone , address , experience , appliedPosition ):         self . name1 = name         self . phone1 = phone         self . address1 = address         self . experience1 = experience         self . appliedPosition1 = appliedPosition     def name ( self ):         print ( f "The name of the job applicant is { self . name1 } " )     def phone ( self ):         print ( f "The phone number of the job applicant is { self . phone1 } " )     def address ( self ):         print ( f "The residential address of the job applicant is { self . address1 } " )     def experience ( self ):         print ( f "The relevant experience of the job applicant is { self . experience1 } " )     def appliedPosition ( self ):         print ( f "The job applicant applied for the post of { self . appliedPosition1 } " ) abdur = JobApplication ( "Abdur" , "03369085972" , "P

A program that calculates the square, cube and square root of a given number

  💓 💓 💓 💓 Hello, dear ones! The following code can be used to calculate the square, cube and square root of a given number. class Calculator :         def __init__ ( self , num ):         self . num = num     def square ( self ):         print ( f "The square of { self . num } is { self . num ** 2 } " )         def cube ( self ):         print ( f "The cube of { self . num } is { self . num ** 3 } " )         def sqrt ( self ):         print ( f "The sqrt of { self . num } is { self . num ** 0.5 } " ) calc = Calculator ( 6 ) calc . square () calc . cube () calc . sqrt () Note: The above program will return the square, cube, and square root of a given number because all the following three functions are being called, i.e. calc.square() calc.cube() calc.sqrt() *If you want to find the square of a given number only, put the given number in the Calculator class and call the respective function. In this case it is; calc.square() *If you want to f