|
| 1 | +### **Input and Output in Python** |
| 2 | +Python provides simple built-in functions for input and output operations. Let's go over them in detail. |
| 3 | + |
| 4 | +--- |
| 5 | + |
| 6 | +### **1. Input in Python** |
| 7 | +The `input()` function is used to take input from the user. |
| 8 | + |
| 9 | +#### **Basic Usage** |
| 10 | +- The `input()` function reads the input as a string. |
| 11 | + ```python |
| 12 | + name = input("Enter your name: ") |
| 13 | + print("Hello, " + name + "!") |
| 14 | + ``` |
| 15 | + |
| 16 | +#### **Converting Input to Other Data Types** |
| 17 | +- Use type-casting functions like `int()`, `float()`, etc., to convert user input. |
| 18 | + ```python |
| 19 | + age = int(input("Enter your age: ")) |
| 20 | + print("You are", age, "years old!") |
| 21 | + ``` |
| 22 | + |
| 23 | +#### **Example: Adding Two Numbers** |
| 24 | +```python |
| 25 | +num1 = int(input("Enter first number: ")) |
| 26 | +num2 = int(input("Enter second number: ")) |
| 27 | +print("The sum is:", num1 + num2) |
| 28 | +``` |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +### **2. Output in Python** |
| 33 | +The `print()` function is used to display output to the user. |
| 34 | + |
| 35 | +#### **Basic Usage** |
| 36 | +```python |
| 37 | +print("Hello, World!") |
| 38 | +``` |
| 39 | + |
| 40 | +#### **Printing Multiple Values** |
| 41 | +- Separate multiple values with commas. Python automatically inserts a space between them. |
| 42 | + ```python |
| 43 | + name = "Alice" |
| 44 | + age = 25 |
| 45 | + print("Name:", name, "Age:", age) |
| 46 | + ``` |
| 47 | + |
| 48 | +#### **Using String Formatting** |
| 49 | +- **f-strings** (Python 3.6+): |
| 50 | + ```python |
| 51 | + name = "Alice" |
| 52 | + age = 25 |
| 53 | + print(f"My name is {name}, and I am {age} years old.") |
| 54 | + ``` |
| 55 | + |
| 56 | +- **`format()` Method**: |
| 57 | + ```python |
| 58 | + print("My name is {}, and I am {} years old.".format(name, age)) |
| 59 | + ``` |
| 60 | + |
| 61 | +- **Old-style Formatting (`%` Operator)**: |
| 62 | + ```python |
| 63 | + print("My name is %s, and I am %d years old." % (name, age)) |
| 64 | + ``` |
| 65 | + |
| 66 | +#### **Changing Separator and End Characters** |
| 67 | +- **`sep` Parameter**: Specifies the separator between values. |
| 68 | + ```python |
| 69 | + print("apple", "banana", "cherry", sep=", ") |
| 70 | + ``` |
| 71 | + |
| 72 | +- **`end` Parameter**: Changes the ending character (default is a newline `\n`). |
| 73 | + ```python |
| 74 | + print("Hello", end=" ") |
| 75 | + print("World!") |
| 76 | + ``` |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +### **3. Example Program: Simple Calculator** |
| 81 | +```python |
| 82 | +print("Simple Calculator") |
| 83 | +num1 = float(input("Enter the first number: ")) |
| 84 | +num2 = float(input("Enter the second number: ")) |
| 85 | + |
| 86 | +print("Choose an operation:") |
| 87 | +print("1. Add") |
| 88 | +print("2. Subtract") |
| 89 | +print("3. Multiply") |
| 90 | +print("4. Divide") |
| 91 | + |
| 92 | +choice = int(input("Enter your choice (1-4): ")) |
| 93 | + |
| 94 | +if choice == 1: |
| 95 | + print("Result:", num1 + num2) |
| 96 | +elif choice == 2: |
| 97 | + print("Result:", num1 - num2) |
| 98 | +elif choice == 3: |
| 99 | + print("Result:", num1 * num2) |
| 100 | +elif choice == 4: |
| 101 | + if num2 != 0: |
| 102 | + print("Result:", num1 / num2) |
| 103 | + else: |
| 104 | + print("Error: Division by zero is not allowed!") |
| 105 | +else: |
| 106 | + print("Invalid choice.") |
| 107 | +``` |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +### **4. Advanced Output Formatting** |
| 112 | +Python provides the `format()` function and formatted string literals for more control. |
| 113 | + |
| 114 | +#### **Controlling Decimal Places** |
| 115 | +- Using `:.nf` (where `n` is the number of decimal places): |
| 116 | + ```python |
| 117 | + pi = 3.14159265358979 |
| 118 | + print(f"Pi to 2 decimal places: {pi:.2f}") |
| 119 | + ``` |
| 120 | + |
| 121 | +#### **Padding and Alignment** |
| 122 | +- Left-align, right-align, and center-align using `<`, `>`, and `^`: |
| 123 | + ```python |
| 124 | + print(f"{'Name':<10}{'Age':>5}") |
| 125 | + print(f"{'Alice':<10}{25:>5}") |
| 126 | + ``` |
| 127 | + |
| 128 | +#### **Printing Percentages** |
| 129 | +- Use `:.n%` for percentages: |
| 130 | + ```python |
| 131 | + success_rate = 0.857 |
| 132 | + print(f"Success rate: {success_rate:.2%}") |
| 133 | + ``` |
| 134 | + |
| 135 | +--- |
0 commit comments