Fixed Top Ad (Local Preview)800x90 • Slot 6608427872

Input, Output & Comments

1. Output using print()

    1.1 What is print()?
    1. print() is a built-in function used to display output on the screen. We can use print() to display: 📝 Text 🔢 Numbers 📦 Variables 🧮 Expressions Examples: print("Hello") # ➜ Hello print(5 + 2) # ➜ 7 name = "Rabindra" print(name) # ➜ Rabindra age = 30 print("I am", age, "years old") # ➜ I am 30 years old
    1.2 Printing Text
    1. Text must be written inside quotes. Example:print("Hello, World!") Output: Hello, World!
    1.3 Printing Numbers and Expressions
    1. Numbers and expressions can be printed directly. Example: print(25) print(5 + 2) print(10 * 3)Output: 25 7 30
    1.4 Printing Variables
    1. We can store a value in a variable and print it later. Example: name = "Rabindra" age = 30print(name) print(age)Output: Rabindra 30
    1.5 Printing Multiple Values
    1. We can print multiple values by separating them with commas. Example: name = "Rabindra" age = 30print("My name is", name) print("I am", age, "years old")Output: My name is Rabindra I am 30 years old 📌 When we use commas inside print(), Python automatically adds spaces between values.

2. f-strings

    2.1 What are f-strings?
    1. Imagine a form letter with blanks: Dear ___, your order of ___ will arrive on ___. If we have values stored in variables, f-strings help us fill those blanks automatically. Example name = "Ram" item = "book" date = "Monday"print(f"Dear {name}, your order of {item} will arrive on {date}.")Output: Dear Ram, your order of book will arrive on Monday. f-strings are used to create formatted output by placing variables or expressions directly inside a string. Syntax:f"Text {variable}" 📌 We must write f before the string. 📌 Variables or expressions are written inside curly braces { }.
f-string Analogy
f-string Analogy
    2.2 Why use f-strings?
    1. Without f-string: name = "Rabindra" age = 30print("My name is", name, "and I am", age, "years old.")Output: My name is Rabindra and I am 30 years old. Using f-string: print(f"My name is {name} and I am {age} years old.")Output: My name is Rabindra and I am 30 years old. ✅ f-strings make the output cleaner and easier to understand.
    2.3 Using Expressions inside f-strings
    1. We can also write expressions inside curly braces. Example: age = 30 print(f"Next year, I will be {age + 1} years old.")Output: Next year, I will be 31 years old.

3. Format Specifiers

    3.1 What are Format Specifiers?
    1. A format specifier is a short instruction that tells Python exactly how we want a value to look. It is similar to choosing formatting options in Word, such as alignment, spacing, or decimal places. Format specifiers are used inside f-strings to customize output. They help us control: 📏 Width 🎯 Decimal places ↔️ Alignment ✨ Padding characters Syntax:f"{variable:format_specifier}"
    3.2 🎯 Decimal Formatting
    1. We can control the number of digits after decimal point. Example: num = 3.14159 print(f"{num:.2f}")Output: 3.14 Here, .2f means display the number with 2 digits after decimal point.
    3.3 📏 Width Formatting
    1. Width means the minimum space given to display a value. Example: num = 123 print(f"{num:10}")Output: 123 Here, width 10 means Python uses 10 spaces to display the value.
    3.4 ↔️ Alignment
    1. We can align values using: < ➜ Left align > ➜ Right align ^ ➜ Center align Example: item = "Apple"print(f"{item:<10}") # Left aligned print(f"{item:>10}") # Right aligned print(f"{item:^10}") # Center aligned
Alignment in f-string
Alignment in f-string
    3.5 ✨ Fill Character
    1. We can fill empty spaces with a character. Example: num = 123print(f"{num:->10}") # ➜ -------123 print(f"{num:-<10}") # ➜ 123------- print(f"{num:#^11}") # ➜ ####123####
    3.6 Format Specifier Breakdown
    1. Example: print(f"{num:->10}") Here: - ➜ Fill character > ➜ Right alignment 10 ➜ Width Example: print(f"{num:.2f}") Here: .2 ➜ Two digits after decimal f ➜ Floating-point number
f-string Syntax Explanation
f-string Syntax Explanation
    3.7 Table-like Output Example
    1. item_1 = "Apple" price_1 = 3item_2 = "Banana" price_2 = 10item_3 = "Orange" price_3 = 200print(f"{'Item':<12}{'Price':>5}") print("-" * 17) print(f"{item_1:<12}{price_1:>5}") print(f"{item_2:<12}{price_2:>5}") print(f"{item_3:<12}{price_3:>5}")Output: Item Price ----------------- Apple 3 Banana 10 Orange 200
f-string Table Output Illustration
f-string Table Output Illustration
3.8 Format Specifier Summary
SpecifierDescription
FillCharacter used for padding the output (default is space). Has to be single character
AlignAlignment of the output: < for left, > for right, ^ for center. Default: > for numbers and < for strings
WidthMinimum width of the output
PrecisionNumber of digits after decimal point for floating-point numbers
Format Specifier Summary Table

4. User Input

    4.1 ⌨️ What is input()?
    1. input() is used to take input from the user. Example: name = input("Enter your name: ") print(f"Hello, {name}!") If user enters Rabindra, output will be: Hello, Rabindra!
    4.2 ⌨️ input() Always Returns String
    1. input() always returns data as a string. Example: num_1 = input("Enter first number: ") num_2 = input("Enter second number: ")print(num_1 + num_2) If user enters: 10 5 Output: 105 😕 Why did Python show 105 instead of 15? This happened because input() returns values as strings. So Python joined "10" and "5" as text.
input() Always Returns String
input() Always Returns String
    4.3 ⌨️ Type Conversion with input()
    1. To perform mathematical operations, we need to convert input into number. Example: num_1 = int(input("Enter first number: ")) num_2 = int(input("Enter second number: "))total = num_1 + num_2 print(f"Sum = {total}") If user enters: 10 5 Output: Sum = 15
    4.4 ⌨️ Taking Float Input
    1. If the input can contain decimal value, use float(). Example: price = float(input("Enter price: ")) print(f"Price = Rs. {price:.2f}")
    4.5 ⌨️ Input and Calculation Example
    1. length_in_cm = float(input("Enter length in cm: ")) length_in_m = length_in_cm / 100 print(f"Length = {length_in_m:.2f} m")

5. Comments

    5.1 What are Comments?
    1. Comments are notes written inside code. They are not executed by Python. Comments are used to: 📝 Explain code 📌 Improve readability 🧠 Help us remember logic
    5.2 Single-line Comments
    1. Single-line comments start with #. Example: # This program displays a greeting messagename = "Rabindra" print(f"Hello, {name}!")
    5.3 Inline Comments
    1. Comments can also be written beside code. Example: age = 30 # stores age of user print(age)
    5.4 Block Notes using Triple Quotes
    1. Python does not have a separate multi-line comment symbol like some other languages. However, triple quotes can be used to write multi-line notes when they are not assigned to a variable. Example: """ This program takes user information and displays it in a formatted way. """name = "Rabindra" print(name) 📌 Triple quotes actually create multi-line strings, but beginners often use them as block notes.
    5.5 Comment Shortcut in VS Code
    1. In VS Code, we can use: Ctrl + / to comment or uncomment selected lines.
Ad PlaceholderSlot: 7421026683

Practice QuestionsNot started

  1. 1. Output and f-strings

    Question 1 of 5

      Practice output and f-string formatting:
      1. Create a variable name and display: Hello, {name}!
      2. Create variables name and age. Display output in this format: My name is Python and I am 30 years old.
      3. Create variables item and price. Display output in this format: Ram bought a pen for Rs. 10.56.
      4. Create variable age. Display what the age will be next year using an f-string.
  2. 2. Format Specifiers

    Question 2 of 5

      Practice format specifier tasks:
      1. Create a variable num = 3.14159 and display it up to 2 decimal places.
      2. Create a variable price = 125.5 and display it in this format: Price: Rs. 125.50
      3. Create a variable num = 123 and display it with width 10.
      4. Create a variable num = 123 and display it in these formats: -------123 123------- ####123####
      5. Create item and price variables and display them in table-like format: Item Price ----------------- Apple 3 Banana 10 Orange 200
  3. 3. User Input

    Question 3 of 5

      Practice tasks using input():
      1. Ask user for his/her name and display: Hello, <name>!
      2. Ask user for birth year and current year. Calculate and display his/her age.
      3. Ask user for length in feet. Convert and display its value in meter. Formula: 1 ft = 0.3048 m
      4. Ask user for two numbers and display their: Sum, Difference, Product, Quotient. Note: Output should be in proper human-readable format, not just numbers.
      5. Ask user for length in cm. Convert it into meter and display the result up to 2 decimal places.
  4. 4. Input, Calculation, and Formatting

    Question 4 of 5

      Practice combined input, calculation, and formatting tasks:
      1. Ask user for name and price of a pen. Display output in this format: Ram bought a pen for Rs. 10.56.
      2. Ask user for current year, month, and day. Display it in this format: Today's Date: 2026-03-12
      3. Ask user for name and marks in 3 subjects. Calculate average percentage and display: Hari scored 85.5% in exam.
      4. Ask user for mass and length of a cube. Calculate and display its density. Formula: density = mass / volume, volume = length ** 3
      5. Ask user for a file name in format file.ext. Extract and display the file extension.
  5. 5. Comments

    Question 5 of 5

      Practice using comments in code:
      1. Write a program that displays your name and age. Add a comment explaining what the program does.
      2. Write a program that calculates area of a rectangle. Add comments explaining length, breadth, and area.
      3. Write a program that converts feet to meter. Add comments explaining the formula.
      4. Write a program with at least three lines of code and use Ctrl + / in VS Code to comment and uncomment them.
Ad PlaceholderSlot: 5413242224