Python Syntax — Python Technical Interview Questions & Answers
Python Syntax
Python Syntax
- Syntax is the rulebook. If you break it, Python refuses to run. Indentation defines blocks — usually 4 spaces. if, for, def, class lines end with a colon, then the body sits under them. No semicolon needed at the end of a normal line.
- Python is case sensitive: Name and name are different. Mix tabs and spaces and you get IndentationError. # starts a comment. Freshers coming from Java keep adding braces; Python does not want them for blocks.
- Exam line: indentation is not style. It is syntax. Say that, then show one if with a colon and two indented prints.
- Python Syntax — output: Adult. The print sits inside if because it is indented.
if marks >= 40:
print("Pass") ← indent = block
else:
print("Fail")Technical Interview Tip: IndentationError — explain why it is syntax.
Code Examples for Python Syntax
Example
# Syntax
age = 18
if age >= 18:
print("Adult")
# print("Adult") # wrong indent would crash
Python Syntax — output: Adult. The print sits inside if because it is indented.
How to Explain Python Syntax to an Interviewer
Explain Syntax as if you are teaching a junior — definition, then one tiny script.
Indentation defines blocks. Colon then indent.
What does the example print, and why?
Python Syntax — output: Adult
What mistake do freshers make with Syntax?
Indentation defines blocks.