









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Python is an interpreted, high-level, object-oriented programming language created
by Guido van Rossum. It's known for its simple syntax and readability.
Key Features Exam Often Tests:
โ Interpreted Language: Code executes line-by-line, making debugging easier โ Dynamically Typed: No need to declare variable types (type is determined at runtime) โ Platform Independent: Write once, run anywhere (with Python interpreter) โ Extensive Libraries: Rich collection of built-in modules โ Indentation Matters: Uses whitespace instead of curly braces for blocks
Common Exam Trap: Students often confuse "interpreted" with "compiled."
Remember: Python code is first compiled to bytecode (.pyc files), then interpreted by
PVM.
Variables are memory locations that store data. No declaration needed - just assign.
Fundamental Data Types:
Important Note: type() function returns the data type. id() returns memory
address.
Common Mistake: Assuming int has a maximum value like in other languages.
Python integers have unlimited precision.
Input: input() always returns a string. Need type conversion for numbers.
python
num = int(input("Enter a number: ")) # Must convert
Output: print() function with parameters:
โ sep: Separator (default: space) โ end: Ending character (default: newline) โ file: Output destination
Exam Focus: print() formatting using f-strings, format() method, or % operator.
Defining: def function_name(parameters):
Calling: function_name(arguments)
Return: return statement (optional, returns None if omitted)
Types of Arguments:
โ Positional arguments โ Keyword arguments โ Default arguments โ Variable-length arguments (*args, **kwargs)
Scope: Local vs Global variables. Use global keyword to modify global variable
inside function.
Immutable sequences of characters.
Important Methods: split(), join(), find(), replace(), strip(), lower(), upper()
Slicing: string[start:stop:step] (creates new string)
Exam Favorite: String slicing with negative indices and steps.
List: [], mutable, ordered, allows duplicates
Tuple: (), immutable, ordered, allows duplicates
Set: {}, mutable, unordered, unique elements
Dictionary: {key:value}, mutable, unordered, unique keys
Key Operations:
โ Lists: append(), extend(), insert(), remove(), pop() โ Dictionaries: keys(), values(), items(), get(), update()
Memory Trick: Lists are like notebooks (add/remove pages), tuples are printed books
(can't change), sets are unique item collections, dictionaries are phonebooks
(key-value pairs).
Modes: 'r' (read), 'w' (write), 'a' (append), 'r+' (read and write)
Essential Methods: open(), read(), readline(), readlines(), write(),
writelines(), close()
Always Use: with statement for automatic resource management
python
with open('file.txt', 'r') as f:
content = f.read()
python
try: # risky code except ExceptionType: # handle error else: # executes if no exception finally:
# always executes
Common Exceptions: ValueError, TypeError, IndexError, KeyError,
ZeroDivisionError, FileNotFoundError
python
numbers = [ 1 , 2 , 3 , 4 , 5 ] for num in numbers: if num % 2 == 0 : numbers.remove(num)
print(numbers)
Output:
text
[1, 3, 5]
Explanation: Modifying list while iterating can cause unexpected behavior, but here it
works because we're checking conditions properly.
python
dict1 = {'a': 1 , 'b': 2 } dict2 = dict dict2['c'] = 3
print(dict1)
Output:
text
{'a': 1, 'b': 2, 'c': 3}
Explanation: dict2 = dict1 creates reference, not copy. Both point to same
dictionary.
python
s = "hello"
s[ 0 ] = 'H' # This will cause error
print(s)
Error:
text
TypeError: 'str' object does not support item assignment
Explanation: Strings are immutable. Use s = 'H' + s[1:] instead.
python
def add_item(item, my_list=[]): my_list.append(item) return my_list
print(add_item( 1 ))
print(add_item( 2 ))
Output:
text
[1]
[1, 2]
Explanation: Default argument is evaluated only once when function is defined.
Same list is reused in subsequent calls.
SECTION 3: MULTIPLE CHOICE QUESTIONS (MCQs)
Answer: B) 18
Explanation: Exponent has higher precedence than multiplication: 3*2 = 9 , then 2
= 18.
Q4. What is the output?
python
x = [ 1 , 2 , 3 ] y = x y.append( 4 )
print(x)
A) [1, 2, 3]
B) [1, 2, 3, 4]
C) [4, 3, 2, 1]
D) Error
Answer: B) [1, 2, 3, 4]
Explanation: y = x creates a reference, not a copy. Both point to same list.
Q5. Assertion: In Python, tuples are faster than lists.
Reason: Tuples are immutable, allowing Python to make optimizations.
A) Both Assertion and Reason are true, and Reason is correct explanation
B) Both are true but Reason is NOT correct explanation
C) Assertion is true but Reason is false
D) Assertion is false but Reason is true
Answer: A) Both Assertion and Reason are true, and Reason is correct explanation
Explanation: Tuples are indeed faster for iteration due to immutability optimizations.
Q6. Which line will cause an error?
python
A) Line 1
B) Line 2
C) Line 3
D) No error
Answer: C) Line 3
Explanation: dict.keys() returns a view object, not a list. It doesn't have append()
method.
(Continuing with 24 more MCQs in similar pattern)
Q30. What is the output?
python
def func(x, lst=[]): lst.append(x) return lst
print(func( 1 )) print(func( 2 , []))
print(func( 3 ))
SECTION 6: LONG ANSWER QUESTIONS
SECTION 7: PRACTICE EXERCISE (WITHOUT SOLUTIONS)
content = file.read() print(content)
return
python
class Student def init(self, name): name = self.name
def display():
print("Name:", name)
EXAM PREPARATION TIPS FROM YOUR TEACHER
Remember: Python is about readability and simplicity. If your code looks
complicated, there's probably a simpler way. Focus on understanding concepts
rather than memorizing syntax. Good luck with your exams!
These notes have been prepared keeping in mind common exam patterns and student
difficulties. Regular practice is key to mastering Python programming.