Lecture 1+2
Lecture 1+2
Lecture 1 :
What is Programming?
Programming is like giving instructions to a computer so it knows what to do. Imagine you
are telling a robot how to make a sandwich.
Programming is just like that, but instead of talking to a robot, we use special languages (like
Python or C++, etc.) to tell a computer what to do, like solving math problems, or perform a
task on the screen.
Introduction to Python
1
Python by Dr Sadaf Zeeshan
Python is useful for mechanical engineers because it helps with many important tasks, such
as:
1. Solving Math Problems – Engineers use Python to solve equations for things like
heat, movement, and forces.
2. Automating Repetitive Work – Python can do boring tasks quickly, like organizing
data or making reports.
3. Simulating Machines and Systems – Python helps test how machines and structures
will work before building them.
4. Using AI and Machine Learning – Engineers use Python to make smart machines,
like robots and self-driving cars.
5. Testing Strength and Airflow – Python helps study how strong materials are and
how air or water moves around objects.
6. Building Robots and Control Systems – Python is used to program machines like
robotic arms and drones.
By learning Python, mechanical engineers can work faster, solve problems better, and create
new technology!
Imagine you have a robot that has never made a jam sandwich before. It will only do exactly
what you tell it—nothing more, nothing less. Your task is to write a set of detailed
instructions for making a jam sandwich.
2
Python by Dr Sadaf Zeeshan
Instructions Example
Clear instructions matter: Computers (like the robot) need very specific, step-by-
step commands.
Order is important: If you try to spread jam before opening the jar, the process will
fail!
Precision is key: Saying "put jam on bread" is too vague—how much jam? Where
exactly?
In Python, writing a program follows the same logic. Each step must be carefully written so
the computer understands exactly what to do.
Now, imagine how we could write this as a Python script. Instead of handling jam, a program
might handle numbers, text, or data, but the principles remain the same.
3
Python by Dr Sadaf Zeeshan
Lecture 2 :
When writing a program, we follow structured steps, just like in a real-world task such as
making a jam sandwich. Let’s explore key programming concepts that help us structure our
code effectively.
1. Function
A function is a reusable block of code that performs a specific task. Think of it as a recipe:
you write the steps once and can use them whenever needed without rewriting everything.
Example in Python
def greet():
print("Hello! Welcome to Python programming.")
Here:
greet()
Real-Life Analogy
4
Python by Dr Sadaf Zeeshan
2. Sequence
Sequence means that instructions in a program run one after another, in the order they
appear.
Example in Python
print("Step 1: Take bread.")
print("Step 2: Spread jam.")
print("Step 3: Place the second slice on top.")
Real-Life Analogy
When following a recipe, you cannot bake a cake before mixing the ingredients—everything
must follow a logical sequence.
A condition allows the program to make decisions based on certain criteria. This is like
asking, “Do we have jam?” before applying it.
Example in Python
if jam_available:
print("Spread jam on the bread.")
else:
print("No jam available, making a plain sandwich.")
Here:
5
Python by Dr Sadaf Zeeshan
Real-Life Analogy
A traffic light:
Loops allow a program to repeat steps automatically instead of writing them multiple times.
Example in Python
for i in range(3):
print("Spreading jam on the bread.")
Real-Life Analogy
When washing clothes, you repeat the scrubbing motion multiple times instead of doing it
once.
5. Variables
Example in Python
bread_slices = 2
jam_amount = "2 tablespoons"
print("Using", bread_slices, "slices of bread and", jam_amount, "of jam.")
Here, bread_slices and jam_amount store values that we can use later.
6
Python by Dr Sadaf Zeeshan
Real-Life Analogy
A shopping list:
Summary
Definition Example
Concept
Let's take the example of making tea and apply the concepts of function, variable, sequence,
loop, and condition.
Scenario:
You want to make 3 cups of tea. You will follow the steps repeatedly for each cup. If
someone asks for extra sugar, you will add more.
Python Code:
7
Python by Dr Sadaf Zeeshan
Concepts Used:
This program simulates making tea, adjusting sugar for preferences, and repeating the process
for multiple cups.