Input, Composition and Modulus Operator
Created by: Prof. Pratiksha Rajnoor
1. Input
Definition:
In Python, the input() function is used to take data (information) from the user.
Syntax:
variable_name = input("Enter something: ")
Explanation:
The text inside quotes is shown on the screen (a prompt).
The user types something and presses Enter.
The input is always read as a string (text) by default.
Example:
name = input("Enter your name: ")
print("Hello,", name)
If you need a number as input:
age = int(input("Enter your age: "))
print("You will be", age + 1, "next year.")
---
2. Composition
Definition:
Composition means combining multiple operations or functions together to perform a task
in a single expression.
In simple terms: You can use the result of one function or operation as the input to another.
Example:
name = input("Enter your name: ")
print("Your name in uppercase is:", [Link]())
Here, the input() function’s result is used directly inside print(). That’s composition —
combining input() and print() in one statement.
Another example:
print(int(input("Enter a number: ")) + 5)
Explanation:
1. input() takes user input.
2. int() converts it to a number.
3. print() displays the final result — all in one line.
---
3. The Modulus Operator (%)
Definition:
The modulus operator % gives the remainder after dividing one number by another.
Syntax:
result = a % b
Example:
print(10 % 3) # Output: 1
print(8 % 2) # Output: 0
Common Uses:
To check if a number is even or odd:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
To find cycles or repeat patterns (like every 7th day, every 3rd element, etc.)