Learning Objectives – after this chapter you will be able to:
  • Describe Python and its main features.
  • Create and run a notebook in Google Colab using code and text cells.
  • Use print(), comments and correct indentation.
  • Recognise Python keywords and write valid identifiers.
What is Python?
  • Python is a high-level, general-purpose, interpreted programming language created by Guido van Rossum and first released in 1991. Python 3, the current version line, was released in 2008.
  • Its design emphasises readability: code blocks are defined by indentation instead of curly braces.
  • Python is dynamically typed (you do not declare variable types) and uses automatic memory management (garbage collection).
  • It supports several programming styles – procedural, object-oriented and functional.
  • It has a large standard library (“batteries included”) and a huge ecosystem of packages such as NumPy and pandas.
  • Python is used for web development, data analysis, machine learning, automation and scripting, scientific computing and education.
Google Colab
  • Google Colaboratory (Colab) is a free, browser-based Jupyter notebook environment hosted by Google. Python runs on Google’s cloud machines, so nothing needs to be installed on your computer.
  • Common libraries (NumPy, pandas, Matplotlib …) are pre-installed, notebooks are saved to Google Drive, and they can be shared like Google Docs.
  • A notebook (.ipynb file) is made of cells:
    • Code cell – contains Python code. Run it with the ▶ button or Shift + Enter; the output appears below the cell.
    • Text cell – contains formatted notes written in Markdown.
  • All cells share the same running program (the runtime or kernel): a variable created in one cell can be used in later cells. Runtime → Restart clears all variables.
Getting started
  1. Open colab.research.google.com and sign in with a Google account.
  2. Click New notebook.
  3. Type code in a code cell and press Shift + Enter.
  4. Rename the notebook (top-left) – it is saved automatically in My Drive → Colab Notebooks.
Your First Program: print()

print() displays values on the screen. It can print several values separated by commas; by default they are separated by a space and followed by a new line.

print("Hello, World!")
print("Python", 3, "is fun")
print("A", "B", "C", sep="-")
print("No new line", end=" ")
print("after this")
Output
Hello, World!
Python 3 is fun
A-B-C
No new line after this
  • sep sets the separator between values (default: a space).
  • end sets what is printed at the end (default: a new line \n).
Comments

Comments explain code to humans; Python ignores them. A single-line comment starts with #. A string placed as the first statement of a function, class or module is a docstring, which documents it.

# This is a comment - Python ignores this line
price = 50      # price of one item
quantity = 3
print(price * quantity)   # prints the total
Output
150
Indentation
  • Python uses indentation (spaces at the start of a line) to group statements into blocks – for example the body of an if, a loop or a function.
  • All statements in the same block must have the same indentation. PEP 8, Python’s style guide, recommends 4 spaces per level.
  • Wrong indentation causes an IndentationError.
marks = 72
if marks >= 40:
    print("Pass")          # inside the if block
    print("Well done")     # same block
print("Result declared")   # outside the block
Output
Pass
Well done
Result declared
Keywords and Identifiers
  • Keywords are reserved words with a special meaning, such as if, else, for, while, def, return, True, False and None. They cannot be used as names.
  • An identifier is a name given to a variable, function or class. Rules:
    • It may contain letters, digits and underscore (_).
    • It must not start with a digit.
    • It must not be a keyword and cannot contain spaces or symbols such as @ $ %.
    • Python is case-sensitive: Total and total are different names.
Checking keywords with the keyword module
import keyword
print(keyword.iskeyword("for"))
print(keyword.iskeyword("total"))
print("while" in keyword.kwlist)
Output
True
False
True
Valid identifiersInvalid identifiers
student_name, _count, mark1, TotalMarks1mark (starts with digit), student name (space), class (keyword), total$ (symbol)
Key Takeaways
  • Colab runs Python notebooks in the browser; code cells share one runtime.
  • print() displays output; sep and end control formatting.
  • Indentation defines blocks; identifiers cannot start with a digit or be keywords.
Video : Google Colab and Python Basics
Question / Answer (15 questions)

Answer : Python was created by Guido van Rossum and first released in 1991. Python 3, the current version line, was released in 2008.

Answer : Easy-to-read syntax using indentation, interpreted execution, dynamic typing, automatic memory management (garbage collection), support for multiple paradigms (procedural, object-oriented, functional), and a large standard library with many third-party packages.

Answer : A variable’s type is decided at run time from the value assigned to it; you do not declare types in advance. The same name can later refer to a value of a different type.

Answer : Google Colab is a free, cloud-hosted Jupyter notebook environment. It requires no installation, includes popular libraries such as NumPy and pandas, saves notebooks to Google Drive and makes sharing easy.

Answer : A code cell contains Python code that can be executed, and its output is shown below it. A text cell contains formatted notes written in Markdown and is not executed as code.

Answer : The runtime is the running Python process that executes the notebook’s code cells and keeps their variables in memory. Restarting it clears all variables and imports, so cells must be run again.

Answer : sep is the string inserted between multiple values (default is a single space). end is the string printed after the last value (default is a newline). For example print("A","B",sep="-") prints A-B.

Answer : A single-line comment begins with #, and everything after # on that line is ignored. Docstrings – string literals placed as the first statement of a module, function or class – are used for documentation.

Answer : Python uses indentation to define blocks of code, such as the body of if statements, loops and functions. Statements in the same block must be indented equally; incorrect indentation causes an IndentationError or changes the program’s meaning.

Answer : PEP 8, the official style guide for Python code, recommends 4 spaces per indentation level.

Answer : Keywords are reserved words with special meaning in Python that cannot be used as identifiers. Examples: if, else, for, def and return (also True, False, None, while, import).

Answer : An identifier may contain letters, digits and underscores; it cannot start with a digit; it cannot be a keyword; it cannot contain spaces or special symbols; and names are case-sensitive.

Answer : Yes. Names that differ only in letter case are different identifiers. For example, Total = 10 and total = 20 create two separate variables.

Answer : Import the keyword module and call keyword.iskeyword("word"), which returns True for keywords. keyword.kwlist gives the full list of keywords.

Answer : It displays: Python 3 is fun – the three values are separated by the default separator, a single space.
MCQ Test : Check Your Understanding (5 questions)

Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.

Q1. Python was created by:

Show Answer

Correct Answer : B. Guido van Rossum
Guido van Rossum created Python, first released in 1991.

Q2. What is the output of print("A", "B", sep="-")?

Show Answer

Correct Answer : C. A-B
sep="-" places a hyphen between the values.

Q3. Which of these is a VALID identifier?

Show Answer

Correct Answer : A. _total1
Identifiers may start with an underscore; they cannot start with a digit, contain spaces or be keywords.

Q4. Which keyboard shortcut runs the current cell in Google Colab?

Show Answer

Correct Answer : D. Shift + Enter
Shift + Enter runs the cell and moves to the next one.

Q5. Which symbol begins a single-line comment in Python?

Show Answer

Correct Answer : B. #
Python comments start with #.

Bibliography/References

The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.

  1. Python Software Foundation. (n.d.). Using the Python Interpreter. In The Python Tutorial. https://docs.python.org/3/tutorial/interpreter.html
  2. Python Software Foundation. (n.d.). An Informal Introduction to Python. In The Python Tutorial. https://docs.python.org/3/tutorial/introduction.html
  3. Python Software Foundation. (n.d.). Lexical Analysis. In The Python Language Reference. https://docs.python.org/3/reference/lexical_analysis.html
  4. Python Software Foundation. (n.d.). keyword — Testing for Python keywords. The Python Standard Library. https://docs.python.org/3/library/keyword.html
  5. van Rossum, G., Warsaw, B., & Coghlan, N. (2001). PEP 8 – Style Guide for Python Code. Python Enhancement Proposals. https://peps.python.org/pep-0008/
  6. Goodger, D., & van Rossum, G. (2001). PEP 257 – Docstring Conventions. Python Enhancement Proposals. https://peps.python.org/pep-0257/
  7. Google. (n.d.). Google Colaboratory – Frequently Asked Questions. https://research.google.com/colaboratory/faq.html
  8. Project Jupyter. (n.d.). Jupyter Notebook Documentation. https://jupyter-notebook.readthedocs.io/
  9. Python Software Foundation. (n.d.). General Python FAQ. Python 3 Documentation. https://docs.python.org/3/faq/general.html