Posts

Showing posts from August, 2020

Python Exercise 4 --- "Number Guessing Game"

  Python Exercise 4: "Number Guessing Game" Q: You have to build a  "Number Guessing Game,"  in which a winning number is set to some integer value. The Program should take input from the user, and if the entered number is less than the winning number, a message should display that the number is smaller and vice versa. Instructions: 1. You are free to use anything we've studied till now. 2. The number of guesses should be limited, i.e (5 or 9). 3. Print the number of guesses left. 4. Print the number of guesses he took to win the game. 5. “Game Over” message should display if the number of guesses becomes equal to 0. You are advised to participate in solving this problem. This task helps you become a good problem solver and helps you accept the challenge and acquire new skills. Solution: print ( "Hii! Welcome, \n " , "This a number guessing game, " "made by Saswata \n " , "Let's Start \n " ) n

Exercise 3 --- "Calculator"

 Exercise 3 ---  "Calculator" Q:  Design a calculator which will correctly solve all the problems: Solution: # Exercise 3 - Calculator """Design a calculator which will correctly solve all the problems:""" # Solution : # Input Part Calculator_On = True print ( 'Welcome, Your Calculator is ready to serve you !' ) while (Calculator_On == True ): x = int ( input ( 'Please enter First number : \n ' )) y = int ( input ( 'Please enter Second number : \n ' )) operator = input ( 'Please Select Operator from these {*,/,+,-} : ' ) # Output Part if (operator == '*' ): print ( 'Output : ' , x * y) elif (operator == '/' ): print ( 'Output : ' , x / y) elif (operator == '+' ): print ( 'Output : ' , x + y) elif (operator == '-' ): print ( 'Output : ' , x - y) print ( ' Do you want to

Python Tutorial 17 --- "Open(), Read() & Readline() For Reading File"

 Python Tutorial 17 ---  "Open(), Read() & Readline() For Reading File" This PROGRAM or TUTORIAL is about "Open(), Read() & Readline() For Reading File" DOC: As we now have an idea of what files(text or binary) are and their access modes, we are now ready to dive into the discussion of file handling methods. When we want to read or write a file (say on our hard drive), we must first open the file. When we open a file, we are asking the operating system to find the file by name, making sure the file exists. How to open a file ? Python has a built-in open() function to open a file. The syntax of the function is: open ( "filename" , "mode" ) Copy To open a file, we must specify two things, Name of the file and its extension Access mode where we can specify in which mode file has to be opened, it could either be read (r), write (w) or append(a), etc. For more information regarding access modes, refer to the previous tutorial. For Example,  o

Python Tutorial 16 --- "File IO Basics"

 Python Tutorial 16 --- "File IO Basics" This PROGRAM or TUTORIAL is about  "File IO Basics" DOC You must have noticed that till now we have been learning one new concept per tutorial. For some important concepts like loops we had to allocate two tutorials so we can grasp the concept of both loops (for and while) separately. But now in the case of the file, we have allocated the next five tutorials (excluding the exercise and their solutions). So, from this, you can take a hint that how important file handling is in programming. In this tutorial we are not getting into files in detail, instead, we are discussing the basics of the file and its modes in a theoretical manner. In computer terms, “a file is a resource for saving data and information in computer hardware”. A file is stored in the form of bytes in hardware. A file is opened in the RAM, but it is stored in the hardware because the hardware is non-volatile i.e. it stores its data permanently. On the other ha

Python Tutorial 15 --- “Try Except Exception Handling In Python”

 Python Tutorial 15 ---  “Try Except Exception Handling In Python” This PROGRAM or TUTORIAL is about “Try Except Exception Handling In Python” DOC Before discussing exceptional handling, let us discuss, what an exception is actually. “Exception can be said as an error, that causes a program to crash. Unlike syntax error, it is syntactically correct and occurs mostly due to our negligence” For example, assigning a string value to an int data type variable or dividing a number by zero or also when a name of a variable or a function is not found, an exception occurs. Python has built-in support for dealing with many sorts of exceptions automatically, but we can also define our own.    The solution to exception related problems is simple. We just have to alter the normal flow of the program by a bit of code known as an exception handler. This code will save the state of the program up to the point where the exception occurred and will continue the normal flow from the code written

Python Tutorial 14 --- “Functions and Docstrings”

  Python Tutorial 14 --- “Functions and Docstrings” This PROGRAM or TUTORIAL is about Python’s “Functions and Docstrings” DOC “Functions in Python can be defined as lines of codes that are built to create a specific task and can be used again and again in a program when called.” There are two types of functions in the Python language: Built-in functions User-defined functions We have used a lot of built-in functions in our code till now, these functions include print(), or sum(), etc. So, we have a good idea about how to call a function. Built-in functions are already present in our python program, and we just have to call them whenever we need them to execute. Being familiar with built-in functions we will now look into User-defined function mostly in this tutorial. We must know how to define a function in Python in order to create one ourselves. We have to use the def keyword in order to define a function accompanied by the function's name with a pair of pare

Python Tutorial 13 --- "Short Hand If Else Notation In Python"

 Python Tutorial 13 --- "Short Hand If Else Notation In Python" This PROGRAM or TUTORIAL about Python's  "Short Hand If Else Notation In Python" DOC Before going through the if-else short-hand statement, we must be familiar with the basic definition, so we know what the short-hand statement actually is. In basic English, shorthand can be said as “a basic way of writing using abbreviations or symbols”. For example, we do not use the full name, Kentucky Fried Chicken, instead we use its abbreviation that is KFC. Writing in such a compact manner is known as shorthand. Now let us move towards the definition of shorthand with respect to the Python language. “An executable statement, written in so compact manner that it comprises of only a single line of code is known as shorthand statement.” Python supports many sorts of shorthand statements and with the help of these statements, we can write our code in a more compact sort or form using less space. We learned the or

Python Tutorial 12 --- "Operators In Python "

 Python Tutorial 12 ---  "Operators In Python " This PROGRAM or TUTORIAL about Python's  "Break and Continue Statement In Python" DOC “Operators in Python can be defined as symbols that assist us to perform certain operations. The operations could be between variable and variable, variable and value, value and value” Operators that Python Language supports are: Arithmetic Operators Assignment Operators Comparison Operators Logical Operators Identity Operators Membership Operators Bitwise Operators We must remember most of them from our basic mathematics that we studied in school. May be except for the last one, that might be new for some of us. To understand bitwise fully, we must have the basic knowledge of the conversion of decimal into binary. For example, the binary for the first five number is: 0001 0010 0011 0100 0101 Arithmetic Operators:             Basic mathematical operations such as addition, multiplication, subtraction, division, etc. are performed

Python Exercise 2 - "Faulty Calculator"

 Python Exercise 2 -  "Faulty Calculator" Q:     Design a calculator which will correctly solve all the problems except the following ones:    45 * 3 = 555, 56+9 = 77, 56/6 = 4   Your program should take operator & the two numbers as input from the user and then return the result. Solution: # Input Part Calculator_On = True print ( 'Welcome, Your Calculator is ready to serve you !' ) while (Calculator_On == True ): x = int ( input ( 'Please enter First number : ' )) y = int ( input ( 'Please enter Second number : ' )) operator = input ( 'Please Select Operator from these {*,/,+,-} : ' ) # Output Part if (operator == '*' ): if (x == 45 and y == 3 ): print ( 'Output : ' , 555 ) else : print ( 'Output : ' , x * y) elif (operator == '/' ): if (x == 56 and y == 6 ): print ( 'Output : ' , 4 ) else : prin