🎓 Velnov

🐍 Python · Topic 1 of 20

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.

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.

print("Hello, World!")
print(2 + 3)
print("2 + 3")
OutputHello, World!
5
2 + 3

Remember: Quotes mark the start and end of text (a string). They are not printed.

3. print() options: sep and end

print("a", "b", "c")
print("a", "b", "c", sep="-")
print("Hello", end=" ")
print("World")
Outputa b c
a-b-c
Hello World

4. Escape sequences

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

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

MisconceptionCorrect 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

  1. Who created Python?

    Show answer

    Guido van Rossum (first released in 1991).

  2. What type does input() always return?

    Show answer

    A string (str).

  3. Which symbol starts a comment?

    Show answer

    #

  4. What does print("a", "b", sep="-") show?

    Show answer

    a-b

  5. What is printed by print("2 + 3")?

    Show answer

    2 + 3 (it is text, so it is not calculated).

  6. Which error comes from inconsistent spacing at the start of lines?

    Show answer

    IndentationError.

  7. How do you run a file called app.py?

    Show answer

    python app.py

  8. 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

Jump to: Easy · Medium · Hard

Easy questions (18)

Q1. easy

Who created the Python programming language?

  1. A James Gosling
  2. B Guido van Rossum
  3. C Dennis Ritchie
  4. D Bjarne Stroustrup

Answer: B — Guido van Rossum

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:

  1. A A compiled, low-level language
  2. B An interpreted, high-level language
  3. C A markup language
  4. 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?

  1. A .pt
  2. B .pyt
  3. C .py
  4. 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!
  1. A Hello, World!
  2. B "Hello, World!"
  3. C print(Hello, World!)
  4. 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
  1. A //
  2. B #
  3. C /*
  4. 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")
  1. A Triple quotes """ ... """
  2. B /* ... */
  3. C <!-- ... -->
  4. 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
  1. A Uppercase letters are not allowed
  2. B name, Name and NAME are three different variables
  3. C Strings must be lowercase
  4. 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
  1. A Curly braces { }
  2. B The keyword 'end'
  3. C Indentation (spaces at the start of the line)
  4. 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!
  1. A SyntaxError only
  2. B IndentationError
  3. C SpacingError
  4. 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?

  1. A run app.py
  2. B python app.py
  3. C execute app.py
  4. 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
  1. A 5 then 5
  2. B 5 then 2 + 3
  3. C 2 + 3 then 5
  4. 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
  1. A A literal backslash-n
  2. B A new line
  3. C A tab
  4. 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
  1. A \n
  2. B \s
  3. C \t
  4. 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")

  1. A ABC
  2. B A B C
  3. C A,B,C
  4. 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)
  1. A name = input("Your name: ")
  2. B input = name("Your name: ")
  3. C name = read("Your name: ")
  4. 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
  1. A Yes, always
  2. B No - a new line ends a statement
  3. C Only inside functions
  4. 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?

  1. A python --version
  2. B python --v
  3. C version python
  4. 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?

  1. A VS Code
  2. B PyCharm
  3. C IDLE (comes with Python)
  4. 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
  1. A int
  2. B float
  3. C str
  4. 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'
  1. A A tool that converts Python to C
  2. B A prompt where you type code and see results immediately
  3. C A graphical app builder
  4. 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
  1. A print('She said "hi"')
  2. B print("She said "hi"")
  3. C print(She said "hi")
  4. 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
  1. A Prints a-b-c (puts - between the values)
  2. B Prints abc- (adds - at the end)
  3. C Separates the output onto 3 lines
  4. 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)
  1. A Ends the program
  2. B Prevents print from moving to a new line afterwards
  3. C Deletes the last character
  4. 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!")
  1. A n becomes 0
  2. B n becomes "hello"
  3. C ValueError - invalid literal for int()
  4. 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?

total = (1 + 2 + 3 +
         4 + 5)        # preferred

total = 1 + 2 + 3 + \
        4 + 5          # backslash style
  1. A With a backslash \ at the end of the line, or inside brackets ( )
  2. B With the keyword 'continue'
  3. C With a semicolon
  4. 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 ...
  1. A Prints the word 'help'
  2. B Shows the documentation for the print function
  3. C Opens a web browser
  4. 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

  1. A An error - 'this' doesn't exist
  2. B The Zen of Python - a poem of design principles
  3. C The Python version number
  4. 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()
  1. A Top to bottom, line by line
  2. B It starts from a main() function like C
  3. C Alphabetical order of variables
  4. 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
  1. A They are the same thing
  2. B An expression produces a value; a statement performs an action
  3. C Expressions are only used in loops
  4. 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?

  1. A Machine code for your CPU
  2. B Bytecode, executed by the Python Virtual Machine
  3. C JavaScript
  4. 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.

PVM__pycache__CPython

Practice “Introduction & Setup” as a quiz