# Goal: Master strings, numbers, and user input
# Topics:
# • String formatting (f-strings)
# • Math operations (+, -, *, /, //, %, **)
# • input() function
# • Type conversion (int(), float(), str())
# ── 1. String Formatting (f-strings) ─────────────────────────────────────────
name = "Alice"
age = 30
height = 5.6
print(f"Name: {name}") # Name: Alice
print(f"Age: {age}, Height: {height:.1f} ft") # Age: 30, Height: 5.6 ft
print(f"In 5 years, {name} will be {age + 5}.") # In 5 years, Alice will be 35.
# Padding and alignment
print(f"{'Left':<10}|{'Center':^10}|{'Right':>10}") # Left | Center | Right
# ── 2. Math Operations ────────────────────────────────────────────────────────
a, b = 10, 3
print(f"\n--- Math Operations (a={a}, b={b}) ---") # --- Math Operations (a=10, b=3) ---
print(f"Addition : {a} + {b} = {a + b}") # Addition : 10 + 3 = 13
print(f"Subtraction : {a} - {b} = {a - b}") # Subtraction : 10 - 3 = 7
print(f"Multiplication : {a} * {b} = {a * b}") # Multiplication : 10 * 3 = 30
print(f"Division : {a} / {b} = {a / b:.2f}") # Division : 10 / 3 = 3.33
print(f"Floor Division : {a} // {b} = {a // b}") # Floor Division : 10 // 3 = 3
print(f"Modulus : {a} % {b} = {a % b}") # Modulus : 10 % 3 = 1
print(f"Exponentiation : {a} ** {b} = {a ** b}") # Exponentiation : 10 ** 3 = 1000
# ── 3. input() Function ───────────────────────────────────────────────────────
print("\n--- User Input Demo ---") # --- User Input Demo ---
user_name = input("Enter your name: ") # Enter your name: (waits for input)
print(f"Hello, {user_name}!") # Hello, <user_name>!
# ── 4. Type Conversion ────────────────────────────────────────────────────────
print("\n--- Type Conversion ---") # --- Type Conversion ---
num_str = input("Enter a number: ") # Enter a number: (waits for input, e.g. 42)
num_int = int(num_str)
num_float = float(num_str)
print(f"As string : '{num_str}' → type: {type(num_str).__name__}") # As string : '42' → type: str
print(f"As int : {num_int} → type: {type(num_int).__name__}") # As int : 42 → type: int
print(f"As float : {num_float} → type: {type(num_float).__name__}") # As float : 42.0 → type: float
# Convert back to string
back_to_str = str(num_int)
print(f"Back to str: '{back_to_str}' → type: {type(back_to_str).__name__}") # Back to str: '42' → type: str
# ── 5. Mini Project: Simple Calculator ───────────────────────────────────────
print("\n--- Simple Calculator ---") # --- Simple Calculator ---
x = float(input("Enter first number: ")) # Enter first number: (e.g. 9)
y = float(input("Enter second number: ")) # Enter second number: (e.g. 4)
print(f"\nResults for {x} and {y}:") # Results for 9.0 and 4.0:
print(f" Sum : {x + y}") # Sum : 13.0
print(f" Difference : {x - y}") # Difference : 5.0
print(f" Product : {x * y}") # Product : 36.0
if y != 0:
print(f" Quotient : {x / y:.4f}") # Quotient : 2.2500
print(f" Remainder : {x % y}") # Remainder : 1.0
else:
print(" Division by zero is not allowed.") # Division by zero is not allowed.
print(f" Power : {x} ** {y} = {x ** y}") # Power : 9.0 ** 4.0 = 6561.0
No comments:
Post a Comment
Please comment below to feedback or ask questions.