Class 12 CS (Code 083) - Advanced Python
Function ek code ka block hota hai jo specific kaam karta hai. Isse code reusable banta hai.
Key concepts: def keyword, arguments, return
statement.
# Function definition
def greet(name):
return "Hello " + name
# Function calling
msg = greet("Rahul")
print(msg) # Output: Hello Rahul
Python se hum text files (.txt), binary files (.dat) aur CSV files ko read/write kar sakte hain.
Useful for storing data permanently.
Modes: 'r' (read), 'w' (write), 'a' (append).
# Writing to a text file
f = open("data.txt", "w")
f.write("Confidential Data")
f.close()
# Reading a text file
f = open("data.txt", "r")
print(f.read())
f.close()
Stack follows LIFO (Last In First Out) principle. Jaise plates ka stack - jo last mein
rakhi wo pehle uthegi.
Operations: Push (Add), Pop (Remove).
stack = []
stack.append(10) # PUSH
stack.append(20)
print(stack.pop()) # POP -> Output: 20
Q1. Which keyword is used to define a function?
Q2. Which mode is used to append data to a file?