5 questions ยท List, Tuple, Set, Dict, OOP, File Handling ยท Difficulty 2/5
open("file.txt", "w") โ write mode (creates file)open("file.txt", "a") โ append mode (adds to existing)open("file.txt", "r") โ read modewith open(...) as f: โ it auto-closes the file!
Lunch,120
Use "w" mode. Write item and amount separated by comma.
expenses.txt, load it back, and print the summaryf.write(f"{e['item']},{e['amount']}\n") for each expense.line.strip().split(",") gives you ["Lunch", "120"]
(title, genre, rating). Tuples are used because movie data shouldn't change once recorded. You need to filter, group, and analyze them.
{"Sci-Fi": [...], "Drama": [...]}
Each value is a list of movie titles in that genre
{"Sci-Fi": ("Inception", 8.8), ...}
dict.setdefault(genre, []).append(title) โ this creates the key if it doesn't exist and appends to the list!
python_class - java_class - ml_class
Library has a list of Book objects โ that's composition.is_borrowed = False"Atomic Habits by James Clear (320 pg)"checkout, loop through self.books to find the book by title. Use book.is_borrowed to check status and set it to True when borrowed.
word_report.txt โ one word per line: python: 3freq[word] = freq.get(word, 0) + 1 โ this adds 1 to existing count or starts at 0 if word is new. No need to check if key exists!
5 questions ยท ArrayList, HashMap, HashSet, OOP, File I/O ยท Difficulty 2/5
{"Rahul": 72}HashMap<String, Integer> grades = new HashMap<>();grades.put("Rahul", 72);grades.get("Rahul");for (Map.Entry<String,Integer> e : grades.entrySet())
for (Map.Entry<String,Integer> entry : grades.entrySet()) { entry.getKey(); entry.getValue(); }
A & B โ Java: setA.retainAll(setB) (modifies setA!)A - B โ Java: setA.removeAll(setB) (modifies setA!)A | B โ Java: setA.addAll(setB) (modifies setA!)new HashSet<>(setA)
HashSet<String> common = new HashSet<>(rahulFriends); then common.retainAll(priyaFriends);. If you don't copy, you'll destroy the original set!
class SavingsAccount(BankAccount):class SavingsAccount extends BankAccountsuper().__init__(name, balance)super(name, balance);raise Exception("msg")throw new Exception("msg");
this.name = namethis.balance += amt to add to the balance. The keyword this refers to the current object โ same as Python's self!
scores = [] โ scores.append(x)ArrayList<Player> scores = new ArrayList<>(); โ scores.add(x);for p in scores:for (Player p : scores)
Player highest = team.get(0); then loop โ if p.runs > highest.runs, update highest = p;
with open("f.txt","w") as f: f.write(text)FileWriter fw = new FileWriter("f.txt"); fw.write(text); fw.close();with open("f.txt","r") as f: lines = f.readlines()BufferedReader br = new BufferedReader(new FileReader("f.txt"));String line = br.readLine(); in a loop until null
Rahul,72 on a new line using FileWriterFileWriter fw = new FileWriter(filename); fw.write(name + "," + marks + "\n"); fw.close();String line; while ((line = br.readLine()) != null) { String[] parts = line.split(","); }