Class 11 Computer Science – Unit 2 Detailed Notes
1. Problem Solving
Definition: Problem-solving is the process of designing, implementing, and testing
solutions to computational problems.
Problem-Solving Steps:
1. Understanding the problem
2. Analyzing requirements
3. Designing a solution (Algorithm, Flowchart, or Pseudocode)
4. Implementing the solution (Writing Code)
5. Testing and Debugging
6. Optimization and Documentation
2. Python Basics
Python is an interpreted, high-level, dynamically typed programming language.
Key Features:
o Easy to learn
o Supports multiple paradigms (Procedural, Object-Oriented, Functional)
o Extensive libraries
3. Python Data Types
Python has several built-in data types:
o Numeric Types: int, float, complex
o Sequence Types: list, tuple, range
o Text Type: str
o Mapping Type: dict
o Set Types: set, frozenset
o Boolean Type: bool
o Binary Types: bytes, bytearray, memoryview
x = 10 # int
y = 3.14 # float
z = "Hello" # str
lst = [1, 2, 3] # list
d = {"a": 1, "b": 2} # dictionary
4. Operators in Python
Arithmetic Operators: +, -, *, /, //, %, **
Comparison Operators: ==, !=, >, <, >=, <=
Logical Operators: and, or, not
Bitwise Operators: &, |, ^, ~, <<, >>
Assignment Operators: =, +=, -=, *=, /=, //=, %=, **=
x = 5
y = 2
print(x + y) # Addition
print(x ** y) # Exponentiation
Output:
7
25
5. Expressions in Python
An expression is a combination of variables, operators, and values that produce
a result.
result = (10 + 5) * 2
print(result)
Output:
30
6. Errors in Python
Syntax Error: Incorrect Python syntax (e.g., missing colon)
Runtime Error: Errors that occur during execution (e.g., division by zero)
Logical Error: The program runs but produces incorrect results
try:
print(10 / 0) # ZeroDivisionError
except ZeroDivisionError:
print("Cannot divide by zero!")
Output:
Cannot divide by zero!
7. Flow of Control
Python follows a sequential flow but allows control using:
o Conditional Statements (if, elif, else)
o Looping (for, while)
o Jump Statements (break, continue, pass)
8. Conditional Statements
Used for decision-making in Python.
x = 10
y = 20
if x > y:
print("x is greater")
elif x < y:
print("y is greater")
else:
print("x and y are equal")
Output:
y is greater
9. Looping Statements
For Loop
for i in range(5):
print(i)
Output:
0
1
2
3
4
While Loop
x = 5
while x > 0:
print(x)
x -= 1
Output:
5
4
3
2
1
10. Lists in Python (Mutable)
numbers = [3, 1, 4, 2]
[Link](5) # Adds 5 to the end of the list
[Link](1, 10) # Inserts 10 at index 1
[Link]() # Removes the last element
[Link](1) # Removes the first occurrence of 1
[Link]() # Sorts the list in ascending order
[Link]() # Reverses the list
print(numbers)
Output:
[10, 3, 2, 4]
11. Tuples in Python (Immutable)
tuple1 = (1, 2, 3, 4, 5)
print(tuple1[2]) # Accessing element at index 2
print([Link](3)) # Counts occurrences of 3
print([Link](4)) # Returns index of first occurrence of 4
Output:
3
1
3
12. Dictionary in Python (Mutable)
data = {"name": "Alice", "age": 25, "city": "New York"}
data["age"] = 26 # Modifies value of key 'age'
data["country"] = "USA" # Adds a new key-value pair
print([Link]()) # Returns all keys
print([Link]()) # Returns all values
print([Link]()) # Returns all key-value pairs as tuples
Output:
dict_keys(['name', 'age', 'city', 'country'])
dict_values(['Alice', 26, 'New York', 'USA'])
dict_items([('name', 'Alice'), ('age', 26), ('city', 'New York'),
('country', 'USA')])
13. Strings in Python (Immutable)
s = "hello world"
print([Link]()) # Converts to uppercase
print([Link]()) # Converts to lowercase
print([Link]("world", "Python")) # Replaces 'world' with 'Python'
print([Link]()) # Splits string into a list by whitespace
Output:
HELLO WORLD
hello world
hello Python
['hello', 'world']