0% found this document useful (0 votes)
7 views34 pages

lec1 intro to algorithm and python

The document introduces algorithms and their characteristics, emphasizing their importance in problem-solving and various fields. It covers writing algorithms in pseudocode, basic Python programming, and practical examples such as grocery shopping and meal preparation. Additionally, it discusses the application of algorithms in agriculture and precision farming, highlighting the benefits of learning algorithms and Python for efficient task execution.

Uploaded by

ahmadfatih07ao
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)
7 views34 pages

lec1 intro to algorithm and python

The document introduces algorithms and their characteristics, emphasizing their importance in problem-solving and various fields. It covers writing algorithms in pseudocode, basic Python programming, and practical examples such as grocery shopping and meal preparation. Additionally, it discusses the application of algorithms in agriculture and precision farming, highlighting the benefits of learning algorithms and Python for efficient task execution.

Uploaded by

ahmadfatih07ao
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/ 34

Introduction to

Algorithms and Python


Agenda
1. What is an Algorithm?
2. Characteristics of Algorithms
3. Writing Algorithms in Pseudocode
4. Introduction to Python
5. Writing Basic Python Code
6. Exercises: Writing Algorithms and Simple
Python Code
7. Summary and Q&A
What is an Algorithm?
• A step-by-step process to solve a problem.
• Used to structure and solve tasks efficiently.
• Examples:
- Following a recipe to cook a meal
- Calculating a total bill at a restaurant
- Planning a travel route
Why Study Algorithms?

• Efficiency in problem-solving
• Basis of computer science
• Applications in various fields (e.g.,
healthcare, transportation, finance)
Characteristics of Algorithms
1. Input: Accepts input values.
2. Output: Produces output values.
3. Definiteness: Steps are clear and precise.
4. Finiteness: Terminates after a finite number of
steps.
5. Effectiveness: Each step is simple and
practical.
Steps to Write an Algorithm

1. Define the problem.


2. Identify the inputs and outputs.
3. Outline the process step-by-step.
4. Validate with test cases.
Writing Algorithms in Pseudocode
• Pseudocode is an easy way to describe an
algorithm.
• Example: Calculate the sum of two numbers:

1. Start
2. Input two numbers, A and B
3. Calculate the sum: SUM = A + B
4. Output the result
5. End
What Makes a Good Algorithm?
• Clear: Steps are easy to follow and
understand.
• Efficient: Solves the problem using minimal
time and resources.
• Reliable: Produces correct and consistent
results.
• Flexible: Can adapt to different inputs or
scenarios.
Daily Life Example: Preparing a Meal
1. Choose a recipe.
2. Gather ingredients.
3. Follow cooking steps (e.g., chop, fry, mix).
4. Serve and enjoy.
5. Clean up after the meal.
This is a step-by-step algorithm for meal
preparation.
Daily Life Example: Doing Laundry
1. Gather dirty clothes.
2. Separate clothes by color and fabric type.
3. Load clothes into the washing machine.
4. Add detergent and start the cycle.
5. Dry clothes and fold them.
This algorithm ensures an organized approach to
laundry.
Benefits of Learning Algorithms
• Improves logical thinking.
• Helps in efficient problem solving.
• Forms the foundation for learning
programming.
• Prepares you for real-world tasks and
challenges.
Task: Grocery Shopping Algorithm

Discuss with your friend


Write algorithm to plan a trip
Task: Grocery Shopping Algorithm
Write an algorithm for a grocery shopping task:
1. Start
2. List items to buy
3. Visit the store and locate items
4. Add items to the cart
5. Pay at the counter
6. Return home
7. End
Task: Planning a Trip Algorithm
Discuss with your friend
Write algorithm to plan a trip
Task: Planning a Trip Algorithm
1. Decide the destination.
2. Set a budget.
3. Book transportation and accommodation.
4. Pack essentials.
5. Follow the itinerary during the trip.
6. Return and unpack.
Post-Harvest Processing
► Algorithms implemented in computer program to sort and
grade crops based on size, weight, and quality.
► Example:
► A fruit sorting machine uses image recognition algorithms to
classify fruits.
► Steps:
1. Capture images of fruits.
2. Process images to extract features (size, color, defects).
3. Classify and sort fruits accordingly.
IoT and Algorithms in Agroindustry

► Internet of Things (IoT) devices collect real-time data (e.g.,


soil moisture, temperature).
► Algorithms analyze this data and provide actionable insights:
► Optimize water usage through smart irrigation systems.
► Predict pest infestations using pattern recognition.
Case Study: Smart Irrigation
► Sensors monitor soil moisture levels.
► Algorithms calculate the optimal amount of water required.
► Automatic irrigation systems adjust water supply based on
real-time conditions.
► Benefits:
► Saves water.
► Improves crop health.
Case Study: Yield Prediction

► Historical data (e.g., weather, soil conditions, crop type) is fed into
machine learning algorithms.
► Algorithms predict:
► Crop yields for the upcoming season.
► Factors affecting yield (e.g., droughts, pests).
► Applications:
► Helps farmers plan resources efficiently.
► Reduces financial risks.
Example Algorithm: Scheduling
► Problem: Fertilizer application scheduling.
► Solution:
1. Collect weather forecasts.
2. Analyze soil nutrient levels.
3. Determine the best time for fertilizer application.
► Algorithm ensures:
► Optimal growth conditions.
► Reduced waste and environmental impact.
Algorithms in Precision Farming
► Use satellite imagery to monitor crop health.
► Algorithms identify areas with water stress or nutrient
deficiency.
► Recommendations are provided to farmers to address specific
areas.
► Example: Variable rate application of fertilizers.
Introduction to Python
• Python is a beginner-friendly programming
language.
• It uses simple syntax and is easy to learn.
• Python is widely used in data analysis,
automation, and app development.
ASPECTS OF LANGUAGES
primitive constructs
◦ English: words
◦ programming language: numbers, strings, simple
operators

Word Cloud copyright Michael Twardos, All Right Reserved. This content is excluded from our Word Cloud copyright unknown, All Right Reserved.
Creative Commons license. For more information, see https://ocw.mit.edu/help/faq-fair-use/. This content is excluded from our Creative
Commons license. For more information, see
https://ocw.mit.edu/help/faq-fair-use/.
ASPECTS OF LANGUAGES

syntax
◦ English: "cat dog boy" → not syntactically valid
"cat fights dog" → syntactically valid

◦ programming language: "hi"5 → not syntactically valid


3.2*5 → syntactically valid
ASPECTS OF LANGUAGES

static semantics is which syntactically


valid strings have meaning
◦ English: "I are hungry" → syntactically valid
• but static semantic error

◦ programming language: 3.2*5 → syntactically valid


• 3+"hi" → static semantic error
ASPECTS OF LANGUAGES
semantics is the meaning associated with a
syntactically correct string of symbols with no static
semantic errors
◦ English: can have many meanings "Flying planes
can be dangerous"
◦ programming languages: have only one meaning but may
not be what programmer intended
WHERE THINGS GO WRONG
▪ syntactic errors
◦ common and easily caught
▪ static semantic errors
◦ some languages check for these before running program
◦ can cause unpredictable behavior
▪ no semantic errors but different meaning than what
programmer intended
◦ program crashes, stops running
◦ program runs forever
◦ program gives an answer but different than expected
Exercise: Writing an Algorithm
Write an algorithm to calculate the area of a
rectangle.
1. Start
2. Input the length and width of the rectangle
3. Calculate the area: AREA = LENGTH × WIDTH
4. Output the result
5. End
Exercise: Writing Simple Python Code
Write a Python program to calculate the area of
a rectangle:

length = 8
width = 4
area = length * width
print('Area of the rectangle:', area)
Recap
• Algorithms help solve problems systematically.
• Pseudocode is a simple way to write
algorithms.
• Python is a beginner-friendly language to
implement algorithms.
• Practice is key to mastering these concepts.
Tips for Beginners
• Start small: Begin with simple problems.
• Break tasks into smaller steps.
• Practice writing pseudocode before coding.
• Don’t hesitate to ask for help or use online
resources.
Exercise: Writing an Algorithm
Write an algorithm to find the average of three
numbers.
Steps:
1. Start
2. Input three numbers A, B, and C
3. Calculate Average = (A + B + C) / 3
4. Output Average
5. End
Exercise: Writing Python Code
Write a Python program to find the average of three
numbers:

num1 = 12
num2 = 15
num3 = 18
average = (num1 + num2 + num3) / 3
print('Average:', average)
Thank You!

Questions and Discussion

You might also like