[go: up one dir, main page]

0% found this document useful (0 votes)
5 views8 pages

Lecture 1+2

Python lecture

Uploaded by

Zain Ali Rizvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Lecture 1+2

Python lecture

Uploaded by

Zain Ali Rizvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Python by Dr Sadaf Zeeshan

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.

Why learn Python?

 Introduction to Python

Python is a high-level, general-purpose programming language known for its simplicity,


readability, and versatility. It is widely used in web development, data science, artificial
intelligence (AI), machine learning (ML), automation, and scientific computing.

 Why Learn Python?


o Easy to read and write (similar to English).
o Versatile (used in various fields like AI, robotics, and finance).
o Extensive libraries (pre-built tools for different tasks).
o Beginner-friendly (ideal for new programmers).

1
Python by Dr Sadaf Zeeshan

 Why Python in Mechanical Engineering?

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!

Activity: Writing Instructions for a Jam Sandwich

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

1. Take two slices of bread from the packet.


2. Place them flat on a plate.
3. Pick up a butter knife.
4. Open the jar of jam by twisting the lid counterclockwise.
5. Insert the knife into the jam jar and scoop out some jam.
6. Spread the jam evenly on one side of one bread slice.
7. Place the second slice of bread on top of the jam-covered slice.
8. Press the sandwich gently.
9. Cut the sandwich in half if desired.
10. Enjoy your jam sandwich!

What This Teaches About Programming

 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 :

Fundamental Programming Concepts

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:

 def greet(): defines a function named greet.


 Inside the function, print("Hello! Welcome to Python programming.") is the task it
performs.
 We can call (execute) this function by writing:

greet()

Real-Life Analogy

A function is like a coffee machine:

 You press a button (call the function).


 It follows predefined steps to make coffee.
 You don’t need to know how it works inside, just how to use it.

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.")

Here, the steps execute one by one from top to bottom.

Real-Life Analogy

When following a recipe, you cannot bake a cake before mixing the ingredients—everything
must follow a logical sequence.

3. Condition (If-Else Statements)

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

jam_available = True # We have jam

if jam_available:
print("Spread jam on the bread.")
else:
print("No jam available, making a plain sandwich.")

Here:

 If jam_available is True, we spread the jam.


 If it’s False, we make a plain sandwich instead.

5
Python by Dr Sadaf Zeeshan

Real-Life Analogy

A traffic light:

 If the light is green, go.


 If it’s red, stop.
 If it’s yellow, slow down.

4. Loop (Repeating Actions)

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.")

This repeats the action three times.

Real-Life Analogy

When washing clothes, you repeat the scrubbing motion multiple times instead of doing it
once.

5. Variables

A variable stores information that can change during a program’s execution.

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:

 If you buy more items, the total changes.


 Similarly, a variable holds different values as needed.

Summary

Definition Example
Concept

Function A reusable block of code def greet(): print("Hello!")

Sequence Steps run in order print("Step 1") → print("Step 2")

Condition Decision-making (if-else) if jam: spread it

Loop Repeats actions for i in range(3): print("Spread jam")

Variable Stores data values jam_amount = "2 tbsp"

Now apply above concept in an example

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

def make_tea(cups): # Function to make tea


for cup in range(1, cups + 1): # Loop to make multiple cups
tea = "Tea"
sugar = 1 # Variable for sugar level

print(f"Making cup {cup}...")

if cup == 2: # Condition: Someone wants extra sugar in the second cup


sugar = 2
print("Adding extra sugar to this cup.")

print(f"Cup {cup} ready with {sugar} spoon(s) of sugar!\n")

# Calling the function


make_tea(3)

Concepts Used:

1. Function (make_tea(cups)) – Groups the tea-making process.


2. Variables (tea, sugar) – Stores the tea and sugar amount.
3. Sequence (steps inside the function) – Follows the order of making tea.
4. Loop (for cup in range(1, cups + 1)) – Repeats for the required number of cups.
5. Condition (if cup == 2) – Checks if someone wants extra sugar.

This program simulates making tea, adjusting sugar for preferences, and repeating the process
for multiple cups.

You might also like