Python Revision Tour II Notes
Python Revision Tour II Notes
2.1 Introduction
Python is a high-level, interpreted programming language that supports multiple paradigms like
OOPs and procedural programming. It is simple and readable.
Important:
- Interpreted
- High-level
Important:
- Strings are immutable.
s = "hello"
s[0] = "H" # Error: TypeError
Important:
- Use loops to access characters.
for ch in "hello":
print(ch)
Important:
Python Revision Tour - II
- + joins, * repeats.
Important:
- Syntax: string[start:end]
s = "Python"
print(s[0:3]) # Pyt
Important:
- String manipulation made easy.
s = "Python"
print(s.upper()) # PYTHON
Important:
- Lists are mutable.
list1 = [1, 2, 3]
Important:
- Lists can be modified.
list1[0] = 5 # Works
# s[0] = "a" # Error for strings
Important:
- + merges, * repeats.
list1 = [1, 2]
list2 = [3, 4]
print(list1 + list2) # [1, 2, 3, 4]
Important:
- Lists are flexible.
list1 = [1, 2, 3]
list1.append(4)
Important:
- Avoids reference issues.
list1 = [1, 2]
list2 = list1.copy()
Python Revision Tour - II
Important:
- Useful for analyzing list data.
list1 = [1, 2, 3]
print(len(list1)) # 3
Important:
- Use parentheses ().
t = (1, 2, 3)
Important:
- Tuples are faster than lists.
t = (1, 2)
# t[0] = 5 # Error
Important:
- Tuples support operations.
Python Revision Tour - II
t = (1, 2)
print(t + (3, 4)) # (1, 2, 3, 4)
Important:
- Methods help analyze tuples.
t = (1, 2, 1)
print(t.count(1)) # 2
Important:
- Keys must be unique.
Important:
- Use keys to access values.
d = {"name": "John"}
print(d["name"]) # John
Important:
- No order guaranteed.
d = {"a": 1, "b": 2}
Important:
- Dictionaries are mutable.
d = {"a": 1}
d["a"] = 2
Important:
- Helps retrieve information.
d = {"a": 1}
print(d.keys()) # dict_keys(['a'])
Important:
- Simple but slow for large lists.
arr = [5, 1, 4]
for i in range(len(arr)):
for j in range(len(arr) - i - 1):
Python Revision Tour - II
Important:
- Efficient for small lists.
arr = [5, 2, 4]
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
print(arr) # [2, 4, 5]