- 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 (
.ipynbfile) 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
- Open
colab.research.google.comand sign in with a Google account. - Click New notebook.
- Type code in a code cell and press Shift + Enter.
- 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")Hello, World! Python 3 is fun A-B-C No new line after this
sepsets the separator between values (default: a space).endsets 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
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 blockPass Well done Result declared
Keywords and Identifiers
- Keywords are reserved words with a special meaning, such as
if,else,for,while,def,return,True,FalseandNone. 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:
Totalandtotalare different names.
- It may contain letters, digits and underscore (
import keyword
print(keyword.iskeyword("for"))
print(keyword.iskeyword("total"))
print("while" in keyword.kwlist)True False True
| Valid identifiers | Invalid identifiers |
|---|---|
student_name, _count, mark1, TotalMarks | 1mark (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;sepandendcontrol formatting.- Indentation defines blocks; identifiers cannot start with a digit or be keywords.
Video : Google Colab and Python Basics
Question / Answer (15 questions)
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.
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.
- Python Software Foundation. (n.d.). Using the Python Interpreter. In The Python Tutorial. https://docs.python.org/3/tutorial/interpreter.html
- Python Software Foundation. (n.d.). An Informal Introduction to Python. In The Python Tutorial. https://docs.python.org/3/tutorial/introduction.html
- Python Software Foundation. (n.d.). Lexical Analysis. In The Python Language Reference. https://docs.python.org/3/reference/lexical_analysis.html
- Python Software Foundation. (n.d.). keyword — Testing for Python keywords. The Python Standard Library. https://docs.python.org/3/library/keyword.html
- van Rossum, G., Warsaw, B., & Coghlan, N. (2001). PEP 8 – Style Guide for Python Code. Python Enhancement Proposals. https://peps.python.org/pep-0008/
- Goodger, D., & van Rossum, G. (2001). PEP 257 – Docstring Conventions. Python Enhancement Proposals. https://peps.python.org/pep-0257/
- Google. (n.d.). Google Colaboratory – Frequently Asked Questions. https://research.google.com/colaboratory/faq.html
- Project Jupyter. (n.d.). Jupyter Notebook Documentation. https://jupyter-notebook.readthedocs.io/
- Python Software Foundation. (n.d.). General Python FAQ. Python 3 Documentation. https://docs.python.org/3/faq/general.html