0% found this document useful (0 votes)
3 views27 pages

13jan26 - Control Structures Python

The document provides an overview of Python basics, including its history, data types, and operators. It covers control structures such as decision-making statements and loops, along with exercises for practical application. Additionally, it outlines a project for calculating student grades and eligibility for scholarships, and hints at upcoming topics on strings.

Uploaded by

programhacker012
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)
3 views27 pages

13jan26 - Control Structures Python

The document provides an overview of Python basics, including its history, data types, and operators. It covers control structures such as decision-making statements and loops, along with exercises for practical application. Additionally, it outlines a project for calculating student grades and eligibility for scholarships, and hints at upcoming topics on strings.

Uploaded by

programhacker012
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

Re-cap

Python Basics – Concepts


• High-level more close to human language,
interpreted executed line by line language
• Portable and easy to learn
• Used in web, data, automation

• Python was invented by Guido van Rossum in


1991 with the goal of making programming easy
and readable.
• Python is named after the comedy show “Monty
Python’s Flying Circus”, not the snake
Variables & Data Types – Concepts
• Variables store data
• int, float, str, bool
• Dynamic typing – at runtime
Operators – Concepts
• Arithmetic
– Used for mathematical calculations [+,-,/,*,%,**]
• Relational
– Used to compare values [<, >, =, ==, !=, >=, <=]
• Logical
– Used to combine conditions [and, or, not]
• Used in expressions
– combination of values and operators
• result = (10 + 5) * 2
• is_pass = marks >= 40
• eligible = age >= 18 and marks >= 40
Operators – Exercise 1
Simple Arithmetic
• Write a program to:
– Read two numbers
– Display their:
• Sum
• Difference
• Product
• Division
Operators – Exercise 2
Modulus Operator

Write a program to:


• Read a number
• Display the remainder when divided by 5
Operators – Exercise 3
Divisibility Test

Write a program to check whether a number is:


• Divisible by 3
• Divisible by 7
Operators – Exercise 4
Divisibility Test

Write a program to check whether a number is:


• Divisible by 3
• Divisible by 7
Operators – Exercise 5
Pass Eligibility

Write a program that:


• Reads marks and attendance
• Student passes if:
– marks ≥ 40 and
– attendance ≥ 75
Operators – Exercise 5
Pass Eligibility

Write a program that:


• Reads marks and attendance
• Student passes if:
– marks ≥ 40 and
– attendance ≥ 75
Operators – Exercise 6
Scholarship Eligibility

Write a program that:


• Reads marks and family income
• Scholarship is given if:
– marks ≥ 75 and
– income < 300000
Operators – Exercise 7
Write a program to check if a year is a leap year
(Hint: % and logical operators)
Operators – Questions

1. What does % operator do?


2. Difference between / and %?
3. What is the output of 10 > 5 and 3 > 7?
4. Which operator gives True/False?
Control Structures in Python
• Control structures control the flow of execution of a
program
– Decision-making statements: if, elif, else
• Used to implement conditions, repetition, and logic
in programs
– Looping statements: for loop, while loop
– Loop control statements: break, continue, pass
Control Structures in Python
• If, elif, else
– is used to make decisions based on conditions
• If – for single conditions
• If-elif-else – for multiple conditions (mutually exclusive)
Control Structures in Python
• For loop is used when no. of iterations is known in
advance
– Works with ranges, lists and strings
• While loop is used when no. of iterations is NOT
known in advance
– Works until the condition is true
Control Structures in Python
• Loop control statements – used to alter the normal
flow of a loop
– Break – to stop and exit the loop
– Continue – skip the current iteration and move to next
– Pass – dummy (place holder does nothing, code can be added later for
any use)
Exercise 1 [5 mins]
Decision Making (if / else)
• Write a program to check whether a number
entered by the user is positive or negative.
Exercise 2 [5 mins]
Multiple Conditions (elif)
Write a program to display grade based on
marks entered by the user.

➢ > 75 – Grade A
➢ Between 40 and 75 – Grade B
➢ < 40 – Grade C
Exercise 3 [5 mins]
for Loop
• Write a program to display numbers from 1 to
10 using a for loop.

• Write a program to print every third number


between 1 and 10
Exercise 4 [5 mins]
while Loop
Write a program to display numbers from 10 to
1 using a while loop.
Exercise 5 [5 mins]
break Statement
Write a program to stop printing numbers when
the number 5 is reached.
Exercise 6 [5 mins]
continue Statement
Write a program to print numbers from 1 to 10,
skipping even numbers.
Exercise 7 [5 mins]
pass Statement
Write a program demonstrating the use of pass
statement inside a loop.
Project 1 [10 minutes]
Write a Python program that:
• Accepts student name and age
• Accepts marks for 5 subjects
• Calculate:
– Total marks
– Average marks
• Determines:
– Pass or Fail
– Grade (Distinction / First Class / Pass / Fail)
• Check eligibility for scholarship based on:
– Average ≥ 75 and
– Age ≤ 25
Next session
STRINGS

• String indexing
• Slicing
• String methods
Strings – Examples
• Password checker
• Text formatting

You might also like