Introduction & Setup — Python Questions with Answers
Start with the study notes on Introduction & Setup (definitions, examples and a quick revision), then work through 30 practice questions, each with the correct answer and a clear explanation of why it is right. When you are ready, take this topic as an interactive quiz to test yourself.
18 easy · 10 medium · 2 hard · Part of Python: Master Python from basics to advanced - 20 topics.
Topic overview
Introduction to Python – your first programs
1. What is Python?
Python is a high-level, interpreted programming language created by Guido van Rossum and first released in 1991. It is famous for clear, readable code.
Python files end in .py, e.g. hello.py.
You run a file from the terminal with python hello.py.
python --version shows which version is installed.
Behind the scenes, Python compiles your code to bytecode and runs it on the Python Virtual Machine. This happens automatically.
Example: Python is used for websites, data science, machine learning, automation scripts and teaching programming.
2. Your first program: print()
print() shows values on the screen. Text must be inside quotes; numbers and calculations need no quotes.
An escape sequence is a backslash code inside a string: \n means a new line and \t means a tab.
print("Hello\nWorld")
print("Name\tAge")
print('She said "hi"')
OutputHello
World
Name Age
She said "hi"
Remember: To print double quotes, wrap the string in single quotes (or the other way round).
5. Comments and docstrings
A comment starts with #. Python ignores everything after it on that line. A docstring is a triple-quoted string right under a function, used as its built-in help.
# This line is a comment
print("Hi") # a comment can follow code
def greet():
"""Print a friendly greeting."""
print("Hello")
greet()
OutputHi
Hello
6. Indentation defines blocks
Python uses indentation (spaces at the start of a line) to group code. Lines indented under an if, loop or function belong to it. Other languages use { } for this.
score = 70
if score > 50:
print("Pass") # inside the if
print("Done") # outside the if
OutputPass
Done
Remember: Inconsistent spacing gives an IndentationError. Use 4 spaces per level.
7. Getting input from the user
input() shows a prompt, waits for the user to type, and always returns a string. Convert it with int() or float() to do maths.
name = input("Your name: ")
age = int(input("Your age: ")) # convert text to a number
print("Hello", name, "- next year you will be", age + 1)
Remember: If the user types hello, int() fails with a ValueError. You will learn to handle that in Error Handling.
8. Ways to run Python
Script: write a .py file and run python file.py. Lines run from top to bottom.
REPL (interactive shell): type python and press Enter. You get a >>> prompt where each line runs immediately; great for experimenting.
Editors: IDLE comes with Python; VS Code and PyCharm are popular editors.
Examples:
help(print) shows the documentation for print.
import this prints the Zen of Python, e.g. “Readability counts.”
Remember: A statement ends at the end of the line, so no semicolons are needed. Python is case-sensitive: print works, but Print is a NameError.
9. Common misconceptions
Misconception
Correct understanding
input() returns a number when the user types digits.
input() always returns a string; convert it with int() or float().
Indentation is just for neatness.
In Python, indentation is the structure; wrong spacing is an error.
Every line must end with a semicolon.
A new line ends a statement; semicolons are allowed but considered bad style.
Print and print are the same.
Python is case-sensitive; only print exists.
Python is too slow to be compiled at all.
Python compiles to bytecode automatically and runs it on the Python Virtual Machine.
10. Quick revision
Interpreter
Runs Python code line by line.
print()
Shows values on the screen.
input()
Reads what the user types, as a string.
Comment
Text after # that Python ignores.
Docstring
Triple-quoted help text under a function.
Indentation
Spaces at the start of a line that group code into blocks.
REPL
Interactive shell where each line runs immediately.
\n
Escape sequence for a new line.
11. Test your understanding
Who created Python?
Show answer
Guido van Rossum (first released in 1991).
What type does input() always return?
Show answer
A string (str).
Which symbol starts a comment?
Show answer
#
What does print("a", "b", sep="-") show?
Show answer
a-b
What is printed by print("2 + 3")?
Show answer
2 + 3 (it is text, so it is not calculated).
Which error comes from inconsistent spacing at the start of lines?
Show answer
IndentationError.
How do you run a file called app.py?
Show answer
python app.py
What is the REPL?
Show answer
The interactive shell, where you type code and see results immediately.
Key takeaway: Python reads like plain English: print() to show, input() to ask (always text), # for comments, and indentation to group code.
Notes written by Velnov. Every code example with an output was run in Python 3 to check that the output shown is exactly what Python prints.
Key concepts in this topic
History of PythonInterpreterCompilerRunning scriptsprint()StringsCommentsDocstringshelp()Type conversionint()IdentifiersNameErrorCode blocksIndentationErrorIndentationSyntaxErrorCommand lineIDLEJupyterExpressionsEscape sequences\tQuotes
Python was created by Guido van Rossum and first released in 1991. James Gosling created Java, Dennis Ritchie created C, and Bjarne Stroustrup created C++.
Python is named after the comedy group 'Monty Python', not the snake.
History of Python
Q2.easy
Python is best described as:
A A compiled, low-level language
B An interpreted, high-level language
C A markup language
D A database language
Answer: B — An interpreted, high-level language
Python is a high-level, interpreted language - your code is executed line by line by the Python interpreter instead of being compiled to machine code first. This makes it easy to write and test quickly.
Technically Python compiles to bytecode first, but you never have to manage that step.
InterpreterCompiler
Q3.easy
What file extension do Python files use?
A .pt
B .pyt
C .py
D .python
Answer: C — .py
Python source files end in .py, for example: hello.py. You run them with: python hello.py
Running scripts
Q4.easy
What is the output of:
print("Hello, World!")
print("Hello, World!") # Hello, World!
A Hello, World!
B "Hello, World!"
C print(Hello, World!)
D Error
Answer: A — Hello, World!
print() displays the text inside the quotes. The quotes themselves are not printed - they only mark where the text (string) starts and ends.
print()Strings
Q5.easy
Which symbol starts a single-line comment in Python?
# This is a comment
print("Hi") # comments can follow code too
A //
B #
C /*
D --
Answer: B — #
Everything after # on a line is a comment - Python ignores it. // is used in C/Java/JavaScript, /* */ in C-style languages, and -- in SQL.
CommentsDocstrings
Q6.easy
How do you typically write a multi-line comment / documentation string in Python?
def greet():
"""This function prints a greeting."""
print("Hello")
A Triple quotes """ ... """
B /* ... */
C <!-- ... -->
D ## ... ##
Answer: A — Triple quotes """ ... """
Triple-quoted strings ("""like this""") span multiple lines. Placed right after a function or class definition they become the docstring - Python's built-in documentation.
For plain multi-line notes you can also just use # on each line.
Docstringshelp()
Q8.easy
Python is case-sensitive. What does that mean?
name = "Ana"
# print(Name) # NameError: Name is not defined
A Uppercase letters are not allowed
B name, Name and NAME are three different variables
C Strings must be lowercase
D Keywords must be uppercase
Answer: B — name, Name and NAME are three different variables
Case-sensitive means capitalization matters everywhere: name, Name and NAME are treated as completely different identifiers, and Print is not the same as print.
IdentifiersNameError
Q9.easy
How does Python know which lines belong inside an if statement or a loop?
if score > 50:
print("Pass") # inside the if
print("Done") # outside the if
A Curly braces { }
B The keyword 'end'
C Indentation (spaces at the start of the line)
D Semicolons
Answer: C — Indentation (spaces at the start of the line)
Python uses indentation to define code blocks - lines indented under an if/for/def belong to it. Most languages use { } instead; in Python the indentation IS the structure.
The standard is 4 spaces per level. Never mix tabs and spaces.
Code blocksIndentationError
Q10.easy
What error do you get when the spacing at the start of lines is inconsistent?
if True:
print("a")
print("b") # IndentationError!
A SyntaxError only
B IndentationError
C SpacingError
D TypeError
Answer: B — IndentationError
Python raises IndentationError when a block's indentation is wrong or inconsistent - for example a line indented with 3 spaces inside a 4-space block.
IndentationSyntaxError
Q11.easy
Which command runs a Python file called app.py from the terminal?
A run app.py
B python app.py
C execute app.py
D app.py start
Answer: B — python app.py
You run a script by passing its filename to the interpreter: python app.py (or python3 app.py on some systems).
On Windows you can also use: py app.py
InterpreterCommand line
Q13.easy
What is the output of:
print(2 + 3)
print("2 + 3")
print(2 + 3) # 5
print("2 + 3") # 2 + 3
A 5 then 5
B 5 then 2 + 3
C 2 + 3 then 5
D 2 + 3 then 2 + 3
Answer: B — 5 then 2 + 3
Without quotes, 2 + 3 is a math expression that evaluates to 5. With quotes, "2 + 3" is just text and is printed exactly as written.
ExpressionsStrings
Q14.easy
What does \n mean inside a string?
print("Hello\nWorld")
# Hello
# World
A A literal backslash-n
B A new line
C A tab
D Nothing - it's an error
Answer: B — A new line
\n is an escape sequence meaning 'new line'. When printed, the text after it appears on the next line.
Other escapes: \t = tab, \\ = one backslash, \" = a quote inside a string.
Escape sequences\t
Q15.easy
Which escape sequence inserts a TAB space inside a string?
print("Name\tAge")
print("Ana\t21")
# Name Age
# Ana 21
A \n
B \s
C \t
D \b
Answer: C — \t
\t inserts a horizontal tab - useful for aligning columns of text. \n is a newline; \s is not a Python escape sequence.
Escape sequences
Q19.easy
What is the output of:
print("A", "B", "C")
A ABC
B A B C
C A,B,C
D ("A", "B", "C")
Answer: B — A B C
print() with multiple values separated by commas prints them with a single space between each (the default sep).
print()sep parameter
Q21.easy
Which line correctly asks the user for their name and stores it?
name = input("Your name: ")
print("Hello,", name)
A name = input("Your name: ")
B input = name("Your name: ")
C name = read("Your name: ")
D get name "Your name: "
Answer: A — name = input("Your name: ")
input("prompt") shows the prompt text, waits for the user to type, and returns what they typed as a string - which we store in the variable name.
input()Variables
Q22.easy
Are semicolons required at the end of Python statements?
x = 1
y = 2 # normal
x = 1; y = 2 # legal but unpythonic
A Yes, always
B No - a new line ends a statement
C Only inside functions
D Only in Python 2
Answer: B — No - a new line ends a statement
Unlike C/Java/JavaScript, Python statements end at the end of the line. Semicolons are allowed (to squeeze two statements on one line) but are considered bad style.
StatementsStyle (PEP 8)
Q26.easy
Which command shows which Python version is installed?
A python --version
B python --v
C version python
D python ?version
Answer: A — python --version
python --version (or python -V) prints the installed version, e.g. Python 3.12.1.
Python 3 is the modern version; Python 2 support ended in 2020.
Python 2 vs 3
Q30.easy
Which of these is a popular editor / IDE for writing Python?
A VS Code
B PyCharm
C IDLE (comes with Python)
D All of the above
Answer: D — All of the above
All three are widely used: IDLE ships with Python itself, VS Code is a free general-purpose editor with a great Python extension, and PyCharm is a dedicated Python IDE.
Development environment
Medium questions (10)
Q7.medium
What type of value does input() always return?
age = input("Age: ") # user types 25
print(type(age)) # <class 'str'>
age = int(age) # now it's a number
A int
B float
C str
D It depends on what the user types
Answer: C — str
input() ALWAYS returns a string, even if the user types 25. To use it as a number you must convert it: int(input()) or float(input()).
Forgetting this conversion is one of the most common beginner bugs.
Type conversionint()
Q12.medium
What is the Python REPL / interactive shell?
>>> 2 + 3
5
>>> "py" * 3
'pypypy'
A A tool that converts Python to C
B A prompt where you type code and see results immediately
C A graphical app builder
D Python's package installer
Answer: B — A prompt where you type code and see results immediately
Typing just 'python' in the terminal opens the interactive shell (REPL = Read, Evaluate, Print, Loop). You type one line at a time and instantly see the result - perfect for experimenting.
IDLEJupyter
Q16.medium
How can you print: She said "hi" (with the double quotes)?
print('She said "hi"') # works
print("She said \"hi\"") # also works
A print('She said "hi"')
B print("She said "hi"")
C print(She said "hi")
D It is impossible to print quotes
Answer: A — print('She said "hi"')
Wrap the string in SINGLE quotes so the double quotes inside are just normal characters. Alternatively, escape them: print("She said \"hi\"").
QuotesEscape sequences
Q17.medium
What does the sep parameter do in print("a", "b", "c", sep="-")?
print("a", "b", "c") # a b c
print("a", "b", "c", sep="-") # a-b-c
A Prints a-b-c (puts - between the values)
B Prints abc- (adds - at the end)
C Separates the output onto 3 lines
D Causes an error
Answer: A — Prints a-b-c (puts - between the values)
sep sets the text placed BETWEEN multiple values (default is a space). So the output is a-b-c.
print()end parameter
Q18.medium
What does the end parameter do in print("Hello", end="")?
print("Hello", end=" ")
print("World")
# Hello World (one line)
A Ends the program
B Prevents print from moving to a new line afterwards
C Deletes the last character
D Prints Hello twice
Answer: B — Prevents print from moving to a new line afterwards
By default print() ends with a newline (\n). Setting end="" (empty) means the NEXT print continues on the same line.
print()sep parameter
Q20.medium
A user runs: n = int(input()) and types: hello
What happens?
try:
n = int(input("Number: "))
except ValueError:
print("That's not a number!")
A n becomes 0
B n becomes "hello"
C ValueError - invalid literal for int()
D The program waits for a number
Answer: C — ValueError - invalid literal for int()
int("hello") cannot convert letters to a number, so Python raises ValueError. Real programs wrap this in try/except to handle bad input politely.
ValueErrortry/exceptinput()
Q23.medium
How can a long statement be continued onto the next line?
A With a backslash \ at the end of the line, or inside brackets ( )
B With the keyword 'continue'
C With a semicolon
D It's impossible - statements must fit one line
Answer: A — With a backslash \ at the end of the line, or inside brackets ( )
A trailing backslash joins physical lines, and anything inside (), [] or {} can span lines freely - the bracket style is preferred.
Line continuationStyle (PEP 8)
Q24.medium
What does help(print) do in the Python shell?
>>> help(print)
# Help on built-in function print ...
A Prints the word 'help'
B Shows the documentation for the print function
C Opens a web browser
D Causes an error
Answer: B — Shows the documentation for the print function
help(anything) shows the built-in documentation (docstring) for that object - functions, modules, classes. It's Python's built-in manual.
Docstringsdir()
Q25.medium
What appears when you run: import this
A An error - 'this' doesn't exist
B The Zen of Python - a poem of design principles
C The Python version number
D A list of all keywords
Answer: B — The Zen of Python - a poem of design principles
import this prints 'The Zen of Python' - 19 guiding principles like 'Simple is better than complex' and 'Readability counts'. It's a famous Easter egg.
Its ideas explain why Python code favors clarity over cleverness.
PEP 20Pythonic style
Q27.medium
In what order does Python execute the lines of a script?
print("first")
def greet():
print("third - only when called")
print("second")
greet()
A Top to bottom, line by line
B It starts from a main() function like C
C Alphabetical order of variables
D Random order
Answer: A — Top to bottom, line by line
Python simply runs the file from the first line to the last. Functions are only DEFINED when read - their bodies run later, when called. There is no required main() function.
def__main__
Hard questions (2)
Q28.hard
What is the difference between an expression and a statement?
y = 2 + 3 # '2 + 3' is an expression,
# 'y = 2 + 3' is a statement
A They are the same thing
B An expression produces a value; a statement performs an action
C Expressions are only used in loops
D Statements always return values
Answer: B — An expression produces a value; a statement performs an action
An expression evaluates to a value (2+3, len("hi"), x > 5) and can be used inside other code. A statement is a complete instruction (if, for, x = 5, return) that DOES something but isn't itself a value.
EvaluationAssignment
Q29.hard
Python code is actually compiled before running. Into what?
A Machine code for your CPU
B Bytecode, executed by the Python Virtual Machine
C JavaScript
D Assembly language
Answer: B — Bytecode, executed by the Python Virtual Machine
Python source is compiled to bytecode (.pyc files in __pycache__), which the Python Virtual Machine (PVM) then interprets. This is automatic and invisible - which is why Python is still called an interpreted language.
The __pycache__ folder you see in projects holds this cached bytecode.