
Python Session 1
Abdul Sharif / January 15, 2025
Lecture 1: Python Fundamentals & Basic Programming Concepts
Welcome to your first lecture in learning Python! In this session, we'll cover the foundational programming concepts you need to get started with Python. We will break down key terms, explore how Python works as an interpreted language, and understand the basic building blocks like syntax, keywords, and indentation that form the core of writing Python programs.
1.1 What is a Program?
A program is a set of instructions written in a programming language that a computer follows to perform a specific task. These tasks can be as simple as printing a message on the screen or as complex as managing data in a database.
- Example: Think of a program like a recipe that tells a chef (the computer) what steps to follow to prepare a meal. Just like in cooking, the instructions must be clear and in the correct order, or else the meal (program) might not turn out as expected.
1.2 Algorithms, Syntax, and Semantics
- Algorithm: An algorithm is a step-by-step procedure to solve a problem. In
programming, algorithms are the logic or steps that our program will follow to
accomplish tasks.
- Example: An algorithm to find the largest number in a list might involve comparing each number with the current largest number and updating the largest number as needed.
- Syntax: Syntax refers to the rules that define how to write code. Every
programming language has its own syntax, which must be followed precisely.
- Example: In Python, ending a line with a colon (
:) indicates the start of a block of code, such as a loop or function. Failing to include the colon would result in a syntax error.
- Example: In Python, ending a line with a colon (
- Semantics: Semantics refers to the meaning behind the syntax—the logic and
functionality of the code. Even if the syntax is correct, if the semantics
(the meaning) is wrong, the program won’t behave as intended.
- Example: Using the wrong formula to calculate an area (e.g., multiplying the sides of a triangle instead of using ½ × base × height) would be a semantic error, even if the code runs without error messages.
1.3 Python: Interpreter vs. Compiler
- Python Interpreter: Python is an interpreted language, meaning that
Python code is executed line-by-line by an interpreter. The interpreter
reads the code, translates it into machine code, and executes it in real time.
- Interpreted Languages: These translate and execute code one instruction at a time (Python, JavaScript).
- Example: When you run a Python program, the interpreter reads each line of code, processes it, and moves to the next line. If an error occurs, the interpreter stops executing and displays an error message.
- Compiled Languages: Other languages, like C or Java, use a compiler,
which translates the entire program into machine code before executing it.
- Compiled Languages: These translate the entire program into machine code
in advance (C, Java).
- Compiled Languages: These translate the entire program into machine code
in advance (C, Java).
Key Difference: The interpreter runs code line-by-line (real-time), whereas a compiler translates the entire program before execution.
1.4 Python Programming Concepts: Lexis, Syntax, Semantics
-
Lexis: This is the most basic level. It consists of the individual building blocks or tokens in the code. Everything in a Python script, such as keywords, operators, identifiers, literals, and punctuation, is made up of lexis. These are the "words" of the language.
- For example, in the line
if score > 90:, the tokens include:if(keyword)score(identifier)>(operator)90(literal):(punctuation)
- For example, in the line
-
Syntax: As mentioned, syntax refers to the rules that dictate how to structure the code. Python’s syntax is known for being easy to read, and one of its main features is the use of indentation to define blocks of code.
-
Semantics: This is the logic that ensures the code works as expected. If a program has correct syntax but fails to perform its task, the semantic rules have not been followed.
1.5 Python Keywords, Instructions, and Comments
1.5.1 Python Keywords
Python has a set of reserved words, called keywords, that have special meanings and cannot be used as variable names. Examples include:
ifelseforwhiledefTrue,False
Example:
if True:
print("This will always print.")
1.5.2 Python Instructions
An instruction is a line of code that performs a specific task. Instructions can include:
-
Variable assignments: Assigning a value to a variable (e.g.,
x = 10) -
Function calls: Calling a built-in function like
print()Example:x = 10 print(x)
1.5.3 Comments in Python
Comments are notes that you include in your code for humans to read. They
are ignored by the Python interpreter. Comments are extremely useful for
documenting code and explaining complex logic. In Python, comments are written
using the # symbol.
Example:
# This is a comment
print("Hello, World!") # This will print a message
1.6 Python's Indentation Rules
Unlike many other programming languages that use curly braces {} or keywords
to define blocks of code, Python relies on indentation (spaces or tabs) to
group statements together.
- Indentation: A block of code is typically indented by four spaces. All the lines of code within a block must have the same indentation level.
Example:
if True:
print("This is indented") # This belongs to the 'if' block
print("This is outside the block") # This is outside the block
Why is this important? If you don’t indent your code correctly, Python will throw an IndentationError.
Learning Outcomes
By the end of this lecture, students should:
- Understand how Python works as an interpreted language.
- Know the key programming concepts like syntax, semantics, and algorithms.
- Be able to recognize Python’s keywords, understand basic instructions, and write comments in their code.
- Understand Python’s indentation rules and how they are used to structure code.
Exercises
-
Write a Python Program that Prints “Hello, World” and Includes Comments
- Task: Write a simple Python program that outputs “Hello, World!” and includes a comment explaining what the code does.
Example:
# This program prints Hello, World! print("Hello, World!") -
Explore Python's Built-In Keywords Using
help()- Task: Use Python's built-in
help()function to explore the list of Python keywords. This will help students understand what keywords are reserved in Python.
Instructions:
- Open the Python interpreter (or a Jupyter Notebook).
- Type
help("keywords")and press Enter. This will display all the keywords in Python.
Example:
help("keywords") - Task: Use Python's built-in
Summary
In this lecture, we’ve covered the basics of programming and Python’s role as an interpreted language. We discussed fundamental concepts like lexis, syntax, and semantics and learned how to write simple Python code using keywords, instructions, and comments. Python’s unique indentation rules make the code more readable, but they also require strict adherence to avoid errors.
In the next lecture, we’ll dive deeper into variables, data types, and how to work with Python’s core data structures.