Posts

Showing posts with the label Python Practice Program

Python Tutorial 39 --- "Class Methods in Python(OOPS 4)"

Python Tutorial 39 --- "Class Methods in Python(OOPS 4)" A s we have observed in the previous tutorials, we cannot change the value of a variable defined in the class from outside, using an object. Instead, if we try that, a new instance variable will be created for the class having the value we assigned. But no change will occur in the original value of the variable. We saw the use of self keyword in the previous tutorial. In this tutorial, we are going to know the working of a new keyword i.e., cls. Class methods take cls parameter that points to the class and not the object instance when the method is called. Syntax: class myClass : @classmethod def myfunc ( cls , arg1 , arg2 , . . . ) : . . . . Copy myfunc  defines the function that needs to be converted into a class method returns:  @classmethod returns a class method for function. Because the class method only has access to  cls  argument, it cannot modify object insta...

Python Tutorial 33 --- "Map, Filter & Reduce"

 Python Tutorial 33 --- "Map, Filter & Reduce" Figure 1:Anonymous/Lambda Functions In Python Why are lambdas relevant to map(), filter() and reduce()? These three methods expect a function object as the first argument. This function object can be a normal or predefined function. Most of the time, functions passed to map(), filter(), and reduce() are the ones we only use once, so there's often no point in defining a named function(def function). To avoid defining a new function, we write an anonymous function/lambda function that we will only use once and never again. map(): "A map function executes certain instructions or functionality provided to it on every item of an iterable." The iterable could be a list, tuple, set, etc. It is worth noting that the output in the case of the map is also an iterable i.e., a list. It is a built-in function, so no import statement required. SYNTAX: map ( function , iterable ) Copy A map function takes two parameters: Fir...

Python Tutorial 31 --- "If __name__==__main__ usage & necessity"

Python Tutorial 31 --- "If __name__==__main__ usage & necessity" Using  if __name__ == “__main__”  statement is one such concept that may be difficult to grasp for beginners, but once learned, it is very helpful and used quite often afterward. However, it may seem confusing at times. This article aims to provide an understanding of the behavior of the statement and further discusses how to use it. As discussed earlier, a module is just a file containing a python code with a .py extension and can be imported to other files. That's where the keyword  __name__  comes in. Let’s understand __name__ first before moving onto if __name__ == “__main__. What is __name__? “A __name__ is a built-in variable that returns us the name of the module being used.” In simple words, by using  __name__ , we can check whether our module is being imported or run directly. If we run it in the same module that it is created in, then it will print “main” onto the screen; otherwise, i...

Python Tutorial 24 --- "Using Python External & Built In Modules"

Python Tutorial 24 ---   "Using Python External & Built In Modules" DOC: In Python, a module can be defined as a file containing a runnable python code.  The extension used by modules in Python is .py.  Hence any simple file containing Python code can be considered a module if its extension is .py. The file and module names are the same; hence we can call a module only by name without using the extension. Along with this, a module can define functions, classes, and variables. There are two types of modules. Built-in External Built-in Modules:             Built-in modules are already installed in Python by default. We can import a module in Python by using the import statement along with the specific module name. We can also access the built-in module files and can read the Python code present in them. Newly integrated modules are added in Python witch each update.  Some important modules names are as follows ...

Python Exercise 5 --- "Astrologer's Stars"

 Python  Exercise 5 --- "Astrologer's Stars" In today’s exercise, what we have to do is, we have to print a pattern similar to that of  a  right-angle triangle , such as: * ** ** * ** ** Copy You have to follow certain instructions, which are as follows: You have to take an integer type variable, and the input of the variable will define the length of the triangle. You have to declare another Boolean variable. When the value of Boolean is 1 i.e. True, the pattern will be printed as shown above. But if the value of Boolean is 0 or false, then the triangle will be printed upside down . Solution: a = int ( input ( "Enter the number of lines you want to print \n " )) b = bool ( int ( input ( "Enter 0 or 1 \n " ))) def star (a , b): if b == True : c = 1 while c <= a: print (c * "*" ) c = c + 1 else : while a > 0 : print (a * "*" ) a = a - 1 star(a , ...

Python Practice Program 1

 Python Practice Program 1 Write a Python program which will keep adding a stream of numbers inputted by the user. The adding stops  as soon as the user presses q key on keyboard. Solution: """ Write a Python program which will keep adding a stream of numbers inputted by the user. The adding stops as soon as the user presses q key on keyboard. """ sum = 0 while True : userInput = input ( "Enter the item price or press q to quit: \n " ) if (userInput!= 'q' ): sum = sum + int (userInput) print ( f"Ordered total so far { sum } " ) else : print ( f"Your total bill amount is: { sum } \n " "Thanks for shopping with us" ) break