0% found this document useful (0 votes)
2 views5 pages

Day1Code

The document outlines Python's 35 keywords and categorizes statements into expression-related, conditional, and compound types. It explains arithmetic and logical operators, along with examples of conditional statements and loops. Additionally, it presents a real-life example of student subject allocation based on merit and seat limits.

Uploaded by

tarifrifat56
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)
2 views5 pages

Day1Code

The document outlines Python's 35 keywords and categorizes statements into expression-related, conditional, and compound types. It explains arithmetic and logical operators, along with examples of conditional statements and loops. Additionally, it presents a real-life example of student subject allocation based on merit and seat limits.

Uploaded by

tarifrifat56
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

Keywords & Statements in Python

Keywords
Python has 35 keywords (reserved words) such as:

and, del, from, not, while, as, elif, global, or, with, assert, else, if,
pass, yield, break, except, import, raise, class, finally, in, return,
continue, for, try, def, is, lambda, nonlocal, False, None, True, async,
await

Statements in Python
A statement is a complete instruction in Python.

There are 3 main types of statements:

1. Expression-related Statement
2. Conditional Statement
3. Compound Statement

1 Expression-related Statements
(a) Arithmetic Operators

Arithmetic operators are used for mathematical expressions.

+ , - , * , / , ** , %

(b) Logical Operators (Comparison Operators)

Logical (comparison) operators are used to compare values.


Operator Meaning Example Output
== Equal To 5 == 5 True
!= Not Equal 5 != 3 True
> Greater Than 7 > 3 True
< Less Than 2 < 5 True
>= Greater Than Equal 5 >= 5 True
<= Less Than Equal 4 <= 6 True

Python Example:

x = 10

print(x == 10) # True


print(x != 5) # True
print(x > 5) # True
print(x < 20) # True
print(x >= 10) # True
print(x <= 15) # True

(c) Leap Year Example

A year is a leap year if:

 divisible by 4 and not divisible by 100


 OR divisible by 400

Python Example:

year = 2024

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):


print(year, "is a Leap Year")
else:
print(year, "is NOT a Leap Year")
2 Conditional Statements
(a) Selective Conditionals (if / elif / else)

Used for decision-making.

Example:

marks = 75

if marks >= 90:


print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")

(b) Repetitive Conditionals (Loops)

for loop (when we know how many times to repeat)

Example: Print the multiplication table of 5

for i in range(1, 11):


print(f"5 x {i} = {5*i}")

Output:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50

while loop (repeat until a condition is false)

Example: Countdown timer

count = 5
while count > 0:
print("Countdown:", count)
count -= 1
print("Blast Off ")

Output:

Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Blast Off

3 Real-Life Example – Student Allocation


We have 50 student records in a CSV file with the format:

id math eng phy gk total


1 22 1 17 7 47
2 24 25 2 9 60
3 6 10 4 10 30
4 1 3 29 8 41
5 3 4 0 3 10
6 0 12 12 3 27
7 4 10 15 5 34
8 3 2 17 0 22
9 26 29 23 9 87
10 29 30 5 3 67
… … … … … …

Task

We want to allocate subjects to students based on:

1. Merit Order → Students are first sorted by total marks (highest first).
2. Subject-Specific Conditions:
o MAT → If math >= 10 and a MAT seat is available.
o ENG → If eng >= 12 and an ENG seat is available.
o CSE → If phy >= 8 and math >= 8 and a CSE seat is available.
o MGT → If none of the above, and MGT seats are available.
o ARC → If MGT is full, and ARC seats are available.
o Otherwise → Student is Not Allocated.
3. Seat Limits: Each subject has only limited seats. (Here we assume 3 seats per subject
for demo.)
Python Code
import csv

# Seat limits (3 seats each for demo)


seats = {"MAT": 3, "ENG": 3, "CSE": 3, "MGT": 3, "ARC": 3}

# Read and sort students by total marks (highest first)


with open(r"D:\Consultancy\University\CoU\PythonEDA\SampleData\[Link]")
as f:
reader = [Link](f)
students = sorted(reader, key=lambda x: int(x["total"]), reverse=True)

# Print header
print(f"{'ID':<5}{'Math':<6}{'Eng':<6}{'Phy':<6}{'GK':<4}{'Total':<7}{'Subjec
t'}")

# Allocate subjects
for s in students:
math, eng, phy, gk, total = int(s["math"]), int(s["eng"]), int(s["phy"]),
int(s["gk"]), int(s["total"])
sub = "Not Allocated"

if math >= 10 and seats["MAT"] > 0:


sub = "MAT"
seats["MAT"] -= 1
elif eng >= 12 and seats["ENG"] > 0:
sub = "ENG"
seats["ENG"] -= 1
elif phy >= 8 and math >= 8 and seats["CSE"] > 0:
sub = "CSE"
seats["CSE"] -= 1
elif seats["MGT"] > 0:
sub = "MGT"
seats["MGT"] -= 1
elif seats["ARC"] > 0:
sub = "ARC"
seats["ARC"] -= 1

# Print student allocation


print(f"{s['id']:<5}{math:<6}{eng:<6}{phy:<6}{gk:<4}{total:<7}{sub}")

✅Summary

 We explored keywords and statements.


 Learned about arithmetic, logical operators, and loops with real examples.
 Finally, we solved a real-life problem of student subject allocation based on marks,
merit, and seat limits.

You might also like