Chapter 11 — Expressions, Statements, Type Conversion & I/O

Easy-language Notes · Part 1 of 2 (expression vs statement · precedence · evaluation)
REVISION NOTES
Class XI · Computer Science (083)
Sheet 1 (Front)

1. Expression vs Statement

Two of the most-confused terms in programming. The difference is small but the exam asks it almost every year.

Expression

Any combination of operators and operands that evaluates to a value. Even a single literal or variable is an expression.

5 · x · x + 1 · (a * b) / 2 · "hi" + name · marks >= 33 · len(s) * 2 — all expressions, each one produces a value.

Statement

A complete instruction that Python can carry out. A statement may contain expressions but it does more than just produce a value — it changes the state of the program (assigns, prints, decides, loops, etc.).

x = 5 (assignment) · print(x + 1) (call) · if x > 0: (decision) · for i in range(5): (loop) — all statements.
An expression is a question that has an answer. A statement is a command that does something. 5 + 3 is the question; print(5 + 3) is the command that uses the answer.
PropertyExpressionStatement
Produces a value?YesNot by itself
Has side effects?Usually noUsually yes
Stand-alone?No (needs to be used)Yes (full instruction)
Examples2 + 3, n * 2x = 7, print(x)
An expression on its own line (like 2 + 3 in a script) doesn't show anything. The shell auto-prints it; in a file you must wrap it in print().

2. Precedence & Associativity

When several operators sit in one expression, Python decides the order using a fixed precedence table. Same-priority operators are decided by associativity (the direction Python reads them in).

Precedence — high (top) to low (bottom)

#LevelOperators
1Parentheses( )
2Exponent**
3Unary+x -x
4Multiply & divide* / // %
5Add & subtract+ -
6Comparisons== != > < >= <= in is
7Logical NOTnot
8Logical ANDand
9Logical ORor
10Assignment= += -= ... (always last)

Associativity

Tells Python which side to start when two same-priority operators meet:

  • Left-to-right for nearly all operators (+ - * / // % == < > etc.).
  • Right-to-left for ** and the assignment operators (=, +=…).
Same-level * / goes left-to-right:
100 / 5 * 2
= (100 / 5) * 2
= 20 * 2
= 40
Right-to-left for **:
2 ** 3 ** 2
= 2 ** (3 ** 2)
= 2 ** 9
= 512        # NOT 64
Rule of thumb — in exam answers, show the brackets at every step so the marker can see your reasoning. (100 / 5) * 2 is clearer than 100 / 5 * 2 even though both compute the same answer.

3. Evaluating an Expression Step by Step

Most CBSE "find the output" questions are solved by applying the precedence table one operator at a time. The trick is to be patient and never skip a step.

The 5-step method

  1. Solve everything inside parentheses first.
  2. Apply ** (right-to-left).
  3. Apply unary +x, -x.
  4. Apply * / // % (left-to-right), then + -.
  5. Compare, then not, then and, then or.
Mixed arithmetic:
10 + 3 * 2 ** 2 - 4
= 10 + 3 * 4 - 4    # ** first
= 10 + 12 - 4       # * next
= 22 - 4
= 18
Brackets change everything:
(10 + 3) * 2 ** 2 - 4
= 13 * 4 - 4
= 52 - 4
= 48
Mixing comparisons and logic:
x, y = 5, 8
x > 3 and y < 10 or x == 0
= True and True or False     # comparisons first
= (True and True) or False     # and before or
= True or False
= True
Unary minus + exponent surprise:
-3 ** 2
= -(3 ** 2)         # ** is higher than unary -
= -9                # NOT 9

(-3) ** 2 = 9       # brackets fix it
Python applies ** before the unary minus. So -3 ** 2 equals -9, not 9. If you mean "minus three, squared", write (-3) ** 2.

4. Type Conversion

Python data types do not freely mix. Sometimes they must be converted from one type to another. There are two kinds.

4.1 Implicit Conversion (Type Coercion)

Python does this automatically when an operation mixes a "smaller" type with a "larger" one. The rule: smaller is promoted to larger so no precision is lost.

Promotion order  →  bool → int → float → complex

print(True + 5)        # 6   (bool -> int)
print(3 + 2.5)         # 5.5 (int -> float)
print(2.0 + 3j)        # (2+3j) (float -> complex)
print(type(2 + 1.0))   # <class 'float'>
Python never implicitly converts a string to a number, or a number to a string. "5" + 3 raises TypeError. You must convert manually.

4.2 Explicit Conversion (Type Casting)

You ask Python to convert — using a built-in function whose name is the target type.

FunctionConverts to
int(x)integer (truncates a float)
float(x)float
str(x)string
bool(x)True/False
list(x)list (from any iterable)
tuple(x)tuple (from any iterable)
print(int(7.9))       # 7    (truncates, not rounds)
print(float("3.14"))  # 3.14
print(str(100))       # '100'  (now a string)
print(int("42") + 8)  # 50
print(bool(0), bool(""))   # False False
A string can be converted only if its content looks like the target type. int("hello") raises ValueError; int("3.5") also fails — first do float("3.5") then int(...).
Use round(x) for actual rounding (banker's rounding); use int(x) only when truncation toward zero is what you want.

Chapter 11 — Expressions, Statements, Type Conversion & I/O

Easy-language Notes · Part 2 of 2 (type conversion · input · print · f-strings)
REVISION NOTES
Class XI · Computer Science (083)
Sheet 1 (Back)

5. Input from the Console — input()

Reads one line of text from the user. Always returns a string, even if the user types digits.

Basic syntax

name = input("Enter your name: ")
print("Hi", name)

The text in quotes is the prompt. It is shown to the user but is not part of the input.

Reading numbers — convert!

Because input() always returns a string, you must wrap it with int() or float() when you need a number.

age = int(input("Age: "))
price = float(input("Price: "))
print(age + 10, price * 2)
If the user types non-numeric text, int()/float() raises ValueError.
Forgetting to convert is the most common bug for beginners. n = input("N: ") followed by n + 1 raises TypeError: can only concatenate str. Cast first: n = int(input(...)).

Reading several values on one line

Use split() to break the line by spaces, then convert each part:

a, b = input("Two nums: ").split()
a, b = int(a), int(b)
print(a + b)
Or use map: a, b = map(int, input().split()) — same effect, shorter.

6. Output to the Console — print()

Prints values to the screen. By default, separates arguments with a space and ends the line with a newline.

Multiple arguments

name = "Vivek"
age  = 16
print("Hi", name, "age", age)
# Hi Vivek age 16
Notice the spaces between items — that is the default separator.

The sep and end arguments

ArgumentDefaultUse
sep' ' (one space)String placed between arguments
end'\n' (newline)String placed after all arguments
print("a", "b", "c", sep="-")
# a-b-c

print("loading", end="...")
print("done")
# loading...done
end="" stops print from going to a new line — useful when several prints should appear on one line.

Formatted Output — f-strings

Put an f right before the opening quote and use {var} placeholders inside.

name, m = "Riya", 87
print(f"{name} scored {m}/100")
# Riya scored 87/100

pi = 3.14159
print(f"pi = {pi:.2f}")   # pi = 3.14
:.2f formats the float to 2 decimal places. f-strings can also call expressions: f"{a+b}".
f-strings (Python 3.6+) are the cleanest way to mix variables with text. The older % and .format() styles still work but are out of fashion in modern Python code.

Chapter 11 Worksheet — Expressions, Statements, Type Conversion & I/O

Total: 50 marks  |  Time: 75 min
Name: Class/Sec: Roll No.: Date:

Section A — Multiple Choice (1 × 10 = 10)

  1. Which of these is an expression?
    (a) x = 5(b) print(x)(c) x + 1(d) if x:
  2. The data type returned by input() is always:
    (a) int(b) float(c) str(d) Depends on input
  3. Result of 2 ** 3 ** 2:
    (a) 64(b) 512(c) 6(d) Error
  4. Output of print(int(7.9)):
    (a) 7(b) 8(c) 7.9(d) "7"
  5. Which is an implicit type conversion?
    (a) int("5")(b) float(3)(c) 3 + 2.5(d) str(7)
  6. The default sep in print() is:
    (a) "" (empty)(b) " " (space)(c) "," (comma)(d) "\n" (newline)
  7. -2 ** 4 evaluates to:
    (a) 16(b) -16(c) 8(d) -8
  8. Which is a statement, not an expression?
    (a) 2 + 3(b) n * 2(c) x = 7(d) len(s)
  9. The character placed after the last argument by default in print() is:
    (a) space(b) tab(c) newline(d) nothing
  10. Output of float("3.14") + 1:
    (a) "3.141"(b) 4.14(c) 4(d) Error

Section B — Find the Output (1 × 8 = 8)

  1. print(10 + 3 * 2 ** 2 - 4)
    Output:
  2. print(100 / 5 * 2)
    print(100 / (5 * 2))
    Output:
  3. print(type(3 + 2.0))
    print(type(True + 1))
    Output:
  4. print(int(3.7) + int("4"))
    Output:
  5. print("a", "b", "c", sep="*")
    print("end", end="!")
    print("now")
    Output:
  6. x = 5
    print(f"x = {x}, x*2 = {x*2}")
    Output:
  7. a, b = "3", "4"
    print(a + b)
    print(int(a) + int(b))
    Output:
  8. pi = 3.14159
    print(f"{pi:.2f}")
    print(round(pi, 3))
    Output:

Section C — Find the Error (1 × 6 = 6)

Each snippet has exactly one error. Mark the line number and write the corrected line.

  1. 1  age = input("Age: ")
    2  print(age + 10)
    3  # Wanted: numeric add, got TypeError
    Wrong line:   Correction:
  2. 1  n = int("hello")
    2  print(n)
    Wrong line:   Correction:
  3. 1  name = "Riya"
    2  m = 87
    3  print("{name} scored {m}")
    4  # Wanted: Riya scored 87. Got: {name} scored {m}
    Wrong line:   Correction:
  4. 1  print("hi", sep="*", end)
    Wrong line:   Correction:
  5. 1  marks = int(input("Marks: "))
    2  print("You scored " + marks)
    3  # Wanted to print message, got TypeError
    Wrong line:   Correction:
  6. 1  x = 5
    2  print(int("3.5"))
    3  # Wanted to convert "3.5" to integer 3
    Wrong line:   Correction:

Section D — Short Answer (2 × 5 = 10)

  1. Differentiate between an expression and a statement with one example each.
  2. What is the difference between implicit and explicit type conversion? Give one example of each.
  3. Why must we usually wrap input() with int() or float()? Show one line of code.
  4. Explain the role of sep and end arguments in print(). Give one line for each.
  5. Evaluate 2 + 3 * 4 ** 2 - 5 step by step using operator precedence.

Chapter 11 Worksheet — (continued)

Sheet 2 (Back)

Section E — Code Tasks (16 marks)

Each program should read input from the user and produce a clear output. Use a separate sheet if you need more space.

  1. (2) Write a Python program that reads the user's name and age on two lines and prints a greeting like "Hello Riya, you are 16 years old." using an f-string.
  2. (3) Read the length and breadth of a rectangle as decimals and print the area with exactly 2 decimal places. Use {:.2f} formatting.
  3. (3) Read three numbers on a single line separated by spaces. Print their sum and average. (Hint: use split() and map(int, ...).)
  4. (2) Read a temperature in Celsius and print its Fahrenheit equivalent on the same line as the input prompt: 23 C = 73.4 F. Use the formula F = C * 9/5 + 32.
  5. (3) Read a price (float) and a quantity (int). Compute the total bill and print it formatted as Bill: Rs. 245.50. Show 2 decimal places.
  6. (3) Read a string from the user, then print: (a) its length, (b) its uppercase version, and (c) the same string repeated 3 times separated by hyphens (use sep="-").