Oop2 Midterm Module 3
Oop2 Midterm Module 3
Discussions
Introduction
To be useful, a program usually needs to communicate with the outside world by obtaining input data from the user
and displaying result data back to the user. This module will introduce you to Python input and output.
Input may come directly from the user via the keyboard, or from some external source like a file or database. Output
can be displayed directly to the console or IDE, to the screen via a Graphical User Interface (GUI), or again to an external
source.
input() pauses program execution to allow the user to type in a line of input from the keyboard. Once the user
presses the Enter key, all characters typed are read and returned as a string.
# input
input1 = input(“Enter a value: “)
# output print(input1)
Type Casting
We can also type cast this input to integer, float or string by specifying the input() function inside the type.
1. Typecasting the input to Integer: There might be conditions when you might require integer input from
user/Console, the following code takes two input(integer/float) from console and typecasts them to integer then
prints the sum.
# input
num1 = int(input(“Number 1: ”)) num2 =
int(input(“Number 2: ”))
# output print(string)
Example:
# taking two inputs at a time
x, y = input("Enter a two value: ").split()
print("Number of boys: ", x) print("Number of girls:
", y)
print()
Python Functions
A function is a block of code that contains one or more Python statements and used for performing a specific task.
Why use function in Python?
1. Code re-usability: Lets say we are writing an application in Python where we need to perform a specific
task in several places of our code, assume that we need to write 10 lines of code to do that specific task. It
would be better to write those 10 lines of code in a function and just call the function wherever needed,
because writing those 10 lines every time you perform that task is tedious, it would make your code
lengthy, less-readable and increase the chances of human errors.
2. Improves Readability: By using functions for frequent tasks you make your code structured and
readable. It would be easier for anyone to look at the code and be able to understand the flow and
purpose of the code.
3. Avoid redundancy: When you no longer repeat the same lines of code throughout the code and use
functions in places of those, you actually avoiding the redundancy that you may have created by not
using functions.
Types of functions
There are two types of functions in Python:
1. Built-in functions: These functions are predefined in Python and we need not to declare these functions
before calling them. We can freely invoke them as and when needed.
2. User defined functions: The functions which we create in our code are user-defined functions. The
add() function that we have created in above examples is a user-defined function.
Python Recursion
A function is said to be a recursive if it calls itself. For example, lets say we have a function abc() and in the body of
abc() there is a call to the abc().
Python example of Recursion
In this example we are defining a user-defined function factorial(). This function finds the factorial of a number
by calling itself repeatedly until the base case is reached.
def factorial(num):
"""This function calls itself to find the
factorial of a number"""
if num == 1:
return 1 else:
return (num * factorial(num - 1)) num = 5
print("Factorial of", num, "is: ", factorial(num))
We use recursion to break a big problem in small problems and those small problems into further smaller
problems and so on. At the end the solutions of all the smaller subproblems are collectively helps in finding the
solution of the big main problem.
Advantages of recursion
Recursion makes our program:
1. Easier to write.
2. Readable – Code is easier to read and understand.
3. Reduce the lines of code – It takes less lines of code to solve a problem using recursion.
Disadvantages of recursion
1. Not all problems can be solved using recursion.
2. If you don’t define the base case then the code would run indefinitely.
3. Debugging is difficult in recursive functions as the function is calling itself in a loop and it is hard to
understand which call is causing the issue.
4. Memory overhead – Call to the recursive function is not memory efficient.
Module Recap
Input may come directly from the user via the keyboard, or from some external source like a file or database
Output can be displayed directly to the console or IDE, to the screen via a Graphical User Interface (GUI), or
again to an external source.
Programs often need to obtain data from the user, usually by way of input from the keyboard. The simplest
way to accomplish this in Python is with input().
All kind of input can be converted to string type whether they are float or integer.
Developer often wants a user to enter multiple values or inputs in one line
A function is a block of code that contains one or more Python statements and used for performing a specific
task.
By using functions for frequent tasks you make your code structured and readable.
A function is said to be a recursive if it calls itself.
When working with recursion, we should define a base case for which we already know the answer.