Path: blob/master/Python Essentials/Python_basics 3.ipynb
3603 views
Python Basics -
In this notebook, we will continue learning Python fundamentals: conditions, loops, functions, and lists.
Conditional Statements
Python uses if
, elif
, and else
to make decisions based on conditions.
Case Study: Online Shopping Discount System
Scenario: An e-commerce platform wants to apply discounts based on the customer’s cart value and membership status.
Rules:
If the cart value is above ₹5000:
If the customer is a Premium Member, apply 20% discount.
If the customer is a Regular Member, apply 10% discount.
If the cart value is between ₹2000 and ₹5000, apply 5% discount for everyone.
Otherwise, no discount.
Loops
Python has two main types of loops:
for
loop (used to iterate over a sequence)while
loop (used to repeat as long as a condition is true)
Expanded & Interactive Case Study: Online Shopping Discount System
Scenario:
An e-commerce platform applies discounts based on cart value, membership, and promo codes.
Users can also buy multiple items, and the program calculates the total price per item and overall bill summary.
Functions
Functions are reusable blocks of code defined using the def
keyword.
1. Functions Without Variables
These functions don’t take any input and may or may not return something.
2. Functions With Variables (Parameters)
These allow reusability by passing arguments.
3. Functions With Default Parameters
Parameters can have default values.
4. Functions With Variable-Length Arguments
When the number of arguments is not fixed.
5. Lambda (Anonymous) Functions
Small, one-line functions.
Case Study: Simple Student Grading System
Create different functions to handle student grades System
Interactive Extension
Createa Grading Interface
Add all subject marks
Take avaearge of it
Classify learners as Excellent, Good, Average , Poor
Print their score sheet
Hands-on Quick Practice
Try solving the following problems:
Write a program that checks whether a number entered by the user is even or odd.
Use a loop to print the first 10 natural numbers.
Write a function that takes two numbers and returns their sum.
Write a Python program to take n items from the user and store them in a list. Then Print the list Print the number of items that start with the letter 'a'
Write a Python program to create a dictionary from user input. The user should enter n key-value pairs, where the value is an integer. Then:
Print the dictionary
Print the sum of all values in the dictionar