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

CO2 Revision

Uploaded by

rajboss14344
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 views9 pages

CO2 Revision

Uploaded by

rajboss14344
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

1.

) Function Definition
A function is a reusable block of code that performs a specific task.
def show_item():
print("Item: Pen")
show_item()
2.) Return Values
A function can return a value back to the caller using return.
def total_value(price, qty):
return price * qty
print(total_value(20, 5))
3.) Parameters
Parameters are variables listed in the function definition that receive values.
def show_stock(item, qty): # parameters
print(item, qty)
4.) Arguments
Arguments are actual values passed to a function when calling it.
show_stock("Pen", 10) # arguments
5.) Scope (Local / Global)
Scope defines where a variable can be accessed.
 Local → inside function
 Global → outside function
stock = 50 # global
def sell(q):
local_value = q # local
print(local_value)
sell(5)
6.) Lambda Functions
A lambda function is a small anonymous function written in one line.
add_tax = lambda p: p * 1.18
print(add_tax(100))
7.) Lists — CRUD Operations
Lists store multiple values and support Create, Read, Update, Delete operations.
items = ["Pen", "Book"] # Create
print(items[0]) # Read
items[1] = "Notebook" # Update
[Link]("Pen") # Delete
8.) List Slicing
Extract part of a list using index range.
items = ["Pen","Book","Pencil"]
print(items[0:2])
9.) List Comprehension
Short way to create lists using a loop in one line.
prices = [10,20,30]
double = [p*2 for p in prices]
print(double)
10.) Tuples — Immutability
A tuple is like a list but cannot be changed after creation.
item = ("Pen", 10, 50)
# item[0] = "Book" # not allowed

11.) Tuple Packing / Unpacking


Packing = storing multiple values
Unpacking = extracting into variables
Example
item = ("Pen", 10, 50) # packing
name, price, qty = item # unpacking
12.) Sets — Unique Items
A set stores only unique values.
Example
items = {"Pen","Book","Pen"}
print(items)
13.) Set Operations
Sets support mathematical operations.
Example
a = {"Pen","Book"}
b = {"Book","Pencil"}

print(a | b) # union
print(a & b) # intersection
14.) Dictionaries — Key Value Pairs
Dictionary stores data as key : value pairs.
Example
item = {
"name": "Pen",
"price": 10,
"qty": 50
}
print(item["price"])
15.) Dictionary Methods
Example
[Link]()
[Link]()
[Link]("qty")
[Link]("price")
16.) JSON-like Structures
A list of dictionaries representing structured records (like datasets).
Example
inventory = [
{"name":"Pen","price":10},
{"name":"Book","price":40}
]
17.) Light Object Orientation (AI-friendly)
OOP represents data as objects instead of only variables.
Class = blueprint
Object = real item
18.) Class
A class is a blueprint for creating objects.
Example
class Item:
pass
19.) Object
An object is an instance of a class.
Example
x = Item()
20.) Attributes
Attributes are variables inside a class.
Example
class Item:
name = "Pen"
21.) Constructor (init)
Constructor initializes object values when created.
Example
class Item:
def __init__(self, name, price):
[Link] = name
[Link] = price
22.) Methods
Methods are functions defined inside a class.
Example
class Item:
def __init__(self, price, qty):
[Link] = price
[Link] = qty

def total(self):
return [Link] * [Link]
23.) Dataset Record as Object
Each inventory record can be stored as an object.
Example
class Record:
def __init__(self, name, price):
[Link] = name
[Link] = price

r = Record("Pen", 10)
print([Link])

Function Definition + Return Values


Example — Calculate Total Value
def total_value(price, quantity):
total = price * quantity
return total

result = total_value(20, 5)
print("Total value:", result)

Question
A shop sells an item at ₹35 with quantity 4. Write a function to return total price.
Solution
def total_value(price, qty):
return price * qty

print(total_value(35, 4))

Parameters vs Arguments
def add_stock(item, qty): # parameters
print(item, qty)

add_stock("Pen", 10) # arguments


Scope — Local & Global
stock = 100 # global

def sell(qty):
global stock
stock -= qty
print("Remaining:", stock)

sell(20)

Lambda Functions (Simple Transformations)


Example — Add GST to prices
add_gst = lambda price: price * 1.18
print(add_gst(100))
With list
prices = [10, 20, 30]
new_prices = list(map(lambda x: x * 1.18, prices))
print(new_prices)

Increase all item prices by 10%.


Solution
prices = [50, 60, 70]
updated = list(map(lambda x: x * 1.10, prices))
print(updated)
Lists — CRUD Operations
Create
items = ["Pen", "Book"]
Read
print(items[0])
Update
items[1] = "Notebook"
Delete
[Link]("Pen")

List Slicing
items = ["Pen","Book","Pencil","Eraser"]
print(items[1:3])

List Comprehension
prices = [10, 20, 30]
double_prices = [p*2 for p in prices]
print(double_prices)

Question
Keep only items costing more than 20.
Solution
prices = [10, 25, 30, 15]
filtered = [p for p in prices if p > 20]
print(filtered)

Tuples — Immutability & Packing


item = ("Pen", 10, 50) # packed
name, price, qty = item # unpacking

print(name, price, qty)


# Cannot modify:
# item[0] = "Book" → error

Question
Store item record safely so it cannot change.
Solution → Use tuple

Sets — Unique Items


items = {"Pen", "Book", "Pen"}
print(items) # duplicates removed
Set Operations
a = {"Pen","Book"}
b = {"Book","Pencil"}

print(a | b) # union
print(a & b) # intersection
print(a - b) # difference

Dictionaries — Key Value Inventory


item = {
"name": "Pen",
"price": 10,
"qty": 50
}
Dictionary Methods
print([Link]())
print([Link]())
print([Link]("price"))

item["qty"] = 40
[Link]("price")

Question
Increase quantity by 10.
Solution
item["qty"] += 10

JSON-like Structures (Dataset Style)


inventory = [
{"name": "Pen", "price": 10, "qty": 50},
{"name": "Book", "price": 40, "qty": 20}
]

print(inventory[0]["name"])

Question
Find total stock value.
Solution
total = 0
for item in inventory:
total += item["price"] * item["qty"]

print(total)

Light Object Orientation (Explanation)


A dictionary = record
A class = blueprint of record
An object = one real record

Class, Object, Attributes


class Item:
name = ""
price = 0
qty = 0

x = Item()
[Link] = "Pen"
[Link] = 10
[Link] = 50

Constructor (init)
class Item:
def __init__(self, name, price, qty):
[Link] = name
[Link] = price
[Link] = qty

item1 = Item("Pen", 10, 50)

Methods
class Item:
def __init__(self, name, price, qty):
[Link] = name
[Link] = price
[Link] = qty

def total_value(self):
return [Link] * [Link]

item = Item("Book", 40, 5)


print(item.total_value())

Dataset Record as Object


class InventoryRecord:
def __init__(self, name, price, qty):
[Link] = name
[Link] = price
[Link] = qty

def show(self):
print(f"{[Link]} | {[Link]} | {[Link]}")

records = [
InventoryRecord("Pen",10,50),
InventoryRecord("Book",40,20)
]

for r in records:
[Link]()

Question 1
Increase all prices by 5% using lambda.
prices = [100,200,300]
new = list(map(lambda x: x*1.05, prices))
print(new)

Question 2
Remove duplicate items from list.
items = ["Pen","Book","Pen"]
unique = list(set(items))
print(unique)

Question 3
Convert inventory dictionary to object.
data = {"name":"Pen","price":10,"qty":5}
obj = InventoryRecord(**data)
[Link]()

Question 4
Return only high-stock items using list comprehension.
inv = [{"qty":5},{"qty":20},{"qty":3}]
high = [i for i in inv if i["qty"] > 10]
print(high)

You might also like