Input and Output Operations in Python
Input and output (I/O) operations allow a Python program to communicate with the outside world. Without I/O, programs would be isolated and impractical.
Taking Input in Python
Python uses the input() function to receive data from users.
The input is always returned as a string.
name = input("Enter your name: ")
print(name)
Even numeric input is read as text. This design keeps input handling simple and predictable.
Converting Input to Other Data Types
To perform calculations, input often needs to be converted.
age = int(input("Enter your age: "))
Failing to convert input correctly can lead to runtime errors.
Output Operations in Python
Python primarily uses the print() function to display output.
It can print text, variables, expressions, and combinations of them.
print("Hello, Python")
print("Sum:", 10 + 5)
Formatted Output
Formatted output improves readability and professionalism.
price = 99.5
print(f"The price is {price}")
What Are Operators in Python?
Operators are symbols that perform operations on values. They tell Python what kind of computation to perform.
Main Types of Python Operators
Arithmetic Operators
+ - * / // % **
a = 10 b = 3 print(a + b) print(a ** b)
Comparison Operators
== != > < >= <=
print(10 > 5)
Logical Operators
and or not
age = 20 print(age > 18 and age < 60)
Assignment Operators
= += -= *= /=
x = 5 x += 2
Membership Operators
in not in
print("a" in "python")
Identity Operators
is is not
x = None print(x is None)
What Is an Expression?
An expression is a combination of values, variables, and operators that Python evaluates to produce a result.
total = (price * quantity) - discount
Expressions can be simple or complex, but they always resolve to a single value.
Operator Precedence in Python
Operator precedence determines the order in which parts of an expression are evaluated.
Python follows a strict precedence hierarchy, similar to mathematical rules.
Example Without Parentheses
result = 10 + 5 * 2 print(result)
Multiplication happens before addition, so the result is 20, not 30.
Using Parentheses to Control Order
result = (10 + 5) * 2
Parentheses always override precedence.
Operator Precedence Table (Simplified)
| Priority | Operators |
|---|---|
| Highest | () |
| ** | |
| *, /, //, % | |
| +, - | |
| <, >, ==, != | |
| Lowest | and, or, not |
Associativity in Python
Associativity determines the direction in which operators of the same precedence are evaluated.
Left-to-Right Associativity
10 - 5 - 2
Evaluated as:
(10 - 5) - 2
Right-to-Left Associativity
The exponent operator is right-associative.
2 ** 3 ** 2
Evaluated as:
2 ** (3 ** 2)
Common Mistakes Beginners Make
- Forgetting input is always a string
- Ignoring operator precedence
- Overusing complex expressions
- Confusing
==with=
Performance and Readability Considerations
While Python evaluates expressions efficiently, clarity should always come first.
- Use parentheses for readability
- Break complex expressions into steps
- Avoid deeply nested logic
Real-World Example
A checkout system takes user input for price and quantity, calculates total cost using arithmetic expressions, checks eligibility using comparison and logical operators, and outputs a formatted bill. Every step relies on correct use of I/O, operators, and precedence rules.
Summary
Input and output operations allow Python programs to interact with users. Operators define how values are combined or compared, and expressions bring logic to life. Operator precedence and associativity control how expressions are evaluated, making them critical for correctness. Mastering these fundamentals ensures your programs behave predictably, remain readable, and scale confidently into more complex logic.