Skip to content

Summary

This summary brings together the main Python commands, operators, and programming ideas introduced in Session 1. It is intended as a compact reference that students can return to when completing later exercises.

Session 1 begins with the general idea of coding and the role of Python as a high-level, interpreted, general-purpose programming language. It then introduces Jupyter Notebooks as the working environment and moves into the first practical Python commands used to display output, store values, calculate results, and document code. The session closes by looking outward: the wider landscape of Python development tools, virtual environments, how to find documentation, and a first look at loops and functions.

Main Python Commands and Syntax

Command / Syntax Purpose
print() Displays output on the screen.
= Assigns a value to a variable.
# Adds a comment ignored by Python.
+ - * / Performs addition, subtraction, multiplication and division.
** Raises a number to a power.
% Returns the remainder after division.
// Performs floor division.
() Calls functions or controls the order of operations.
'text' or "text" Creates a string value.
for x in range(n): Repeats a block of code once for each value in a sequence.
while condition: Repeats a block of code while a condition remains true.
def name(): Defines a reusable, named block of code (a function).
return Sends a value back from a function to the code that called it.

Compact Python Examples

# Display output
print("Hello World")

# Store values in variables
x = 10
y = 20

# Use variables in calculations
total = x + y
print(total)

# Apply mathematical operators
price = 125
tax = 0.20
final_price = price * (1 + tax)
print(final_price)

# Use powers
radius = 5
area = 3.14159 * radius ** 2
print(area)

# Add comments to explain code
# This line calculates a simple portfolio return
portfolio_return = 0.08
print(portfolio_return)

# Repeat an action with a loop
for i in range(3):
    print("Loop iteration:", i)

# Organise code into a reusable function
def add_tax(price, tax_rate):
    return price * (1 + tax_rate)

print(add_tax(125, 0.20))

Concepts to Remember

  • Python code is written as source code and executed by the Python interpreter.
  • Jupyter Notebooks combine explanatory text, executable code, outputs and documentation.
  • Variables allow values to be stored, reused and updated.
  • Mathematical operators allow Python to perform numerical calculations.
  • Comments improve readability and help explain the purpose of the code.
  • Simple commands can be combined into short programs that solve practical tasks.
  • Different local and cloud environments can be used to write and run Python, though this course uses Jupyter Notebooks via Anaconda.
  • Virtual environments keep the packages used by different projects separate and reproducible.
  • Documentation, rather than memorisation, is the main way programmers learn how a command or function works.
  • Loops repeat a block of code automatically, avoiding the need to repeat instructions manually.
  • Functions group code into named, reusable blocks that can accept inputs and return outputs.

This concludes Session 1. You should now be ready to use basic Python commands in later sessions.

Reading and References

The following references and resources can be used to support independent study and further practice. They are not required before completing the session, but they provide useful background and extension material.

  • Downey, A. Think Python.
  • McKinney, W. Python for Data Analysis.
  • VanderPlas, J. Python Data Science Handbook.

Documentation and Practice Resources

  • Python documentation.
  • Jupyter Notebook documentation.
  • NumPy documentation.
  • pandas documentation.
  • Visual Studio Code Python extension documentation.
  • Jupyter Notebook documentation for working with text and code cells.
  • Introductory Python documentation on variables, expressions, operators and comments.
  • Anaconda and Conda documentation on managing environments and packages.
  • Python documentation on for and while loops.
  • Python documentation on defining and calling functions.

Suggested Practice

Rerun the notebook independently, modify the supplied examples, and try to reproduce the results without referring directly to the worked solutions. Programming is best learned through repeated experimentation, careful observation of outputs, and gradual correction of errors.