Two of the most-confused terms in programming. The difference is small but the exam asks it almost every year.
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.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.5 + 3 is the question; print(5 + 3) is the command that uses the answer.| Property | Expression | Statement |
|---|---|---|
| Produces a value? | Yes | Not by itself |
| Has side effects? | Usually no | Usually yes |
| Stand-alone? | No (needs to be used) | Yes (full instruction) |
| Examples | 2 + 3, n * 2 | x = 7, print(x) |
2 + 3 in a script) doesn't show anything. The shell auto-prints it; in a file you must wrap it in print().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).
| # | Level | Operators |
|---|---|---|
| 1 | Parentheses | ( ) |
| 2 | Exponent | ** |
| 3 | Unary | +x -x |
| 4 | Multiply & divide | * / // % |
| 5 | Add & subtract | + - |
| 6 | Comparisons | == != > < >= <= in is |
| 7 | Logical NOT | not |
| 8 | Logical AND | and |
| 9 | Logical OR | or |
| 10 | Assignment | = += -= ... (always last) |
Tells Python which side to start when two same-priority operators meet:
+ - * / // % == < > etc.).** and the assignment operators (=, +=…).* / goes left-to-right:
100 / 5 * 2 = (100 / 5) * 2 = 20 * 2 = 40
**:
2 ** 3 ** 2
= 2 ** (3 ** 2)
= 2 ** 9
= 512 # NOT 64(100 / 5) * 2 is clearer than 100 / 5 * 2 even though both compute the same answer.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.
** (right-to-left).+x, -x.* / // % (left-to-right), then + -.not, then and, then or.10 + 3 * 2 ** 2 - 4 = 10 + 3 * 4 - 4 # ** first = 10 + 12 - 4 # * next = 22 - 4 = 18
(10 + 3) * 2 ** 2 - 4 = 13 * 4 - 4 = 52 - 4 = 48
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
-3 ** 2 = -(3 ** 2) # ** is higher than unary - = -9 # NOT 9 (-3) ** 2 = 9 # brackets fix it
** before the unary minus. So -3 ** 2 equals -9, not 9. If you mean "minus three, squared", write (-3) ** 2.Python data types do not freely mix. Sometimes they must be converted from one type to another. There are two kinds.
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'>
"5" + 3 raises TypeError. You must convert manually.You ask Python to convert — using a built-in function whose name is the target type.
| Function | Converts 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
int("hello") raises ValueError; int("3.5") also fails — first do float("3.5") then int(...).round(x) for actual rounding (banker's rounding); use int(x) only when truncation toward zero is what you want.input()Reads one line of text from the user. Always returns a string, even if the user types digits.
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.
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.n = input("N: ") followed by n + 1 raises TypeError: can only concatenate str. Cast first: n = int(input(...)).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.print()Prints values to the screen. By default, separates arguments with a space and ends the line with a newline.
name = "Vivek" age = 16 print("Hi", name, "age", age) # Hi Vivek age 16Notice the spaces between items — that is the default separator.
sep and end arguments| Argument | Default | Use |
|---|---|---|
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.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}".% and .format() styles still work but are out of fashion in modern Python code.input() is always:
2 ** 3 ** 2:
print(int(7.9)):
sep in print() is:
-2 ** 4 evaluates to:
print() is:
float("3.14") + 1:
print(10 + 3 * 2 ** 2 - 4)
Output: print(100 / 5 * 2) print(100 / (5 * 2))Output:
print(type(3 + 2.0)) print(type(True + 1))Output:
print(int(3.7) + int("4"))Output:
print("a", "b", "c", sep="*") print("end", end="!") print("now")Output:
x = 5 print(f"x = {x}, x*2 = {x*2}")Output:
a, b = "3", "4" print(a + b) print(int(a) + int(b))Output:
pi = 3.14159 print(f"{pi:.2f}") print(round(pi, 3))Output:
Each snippet has exactly one error. Mark the line number and write the corrected line.
1 age = input("Age: ") 2 print(age + 10) 3 # Wanted: numeric add, got TypeErrorWrong line: Correction:
1 n = int("hello") 2 print(n)Wrong line: Correction:
1 name = "Riya" 2 m = 87 3 print("{name} scored {m}") 4 # Wanted: Riya scored 87. Got: {name} scored {m}Wrong line: Correction:
1 print("hi", sep="*", end)Wrong line: Correction:
1 marks = int(input("Marks: ")) 2 print("You scored " + marks) 3 # Wanted to print message, got TypeErrorWrong line: Correction:
1 x = 5 2 print(int("3.5")) 3 # Wanted to convert "3.5" to integer 3Wrong line: Correction:
input() with int() or float()? Show one line of code.
sep and end arguments in print(). Give one line for each.
2 + 3 * 4 ** 2 - 5 step by step using operator precedence.
Each program should read input from the user and produce a clear output. Use a separate sheet if you need more space.
{:.2f} formatting.
split() and map(int, ...).)
23 C = 73.4 F. Use the formula F = C * 9/5 + 32.
Bill: Rs. 245.50. Show 2 decimal places.
sep="-").