Chapter 8 Strings . Chapter 9 Lists . Chapter 10 Tuples & Dictionary
π€ Strings
- Definition: Sequence of characters enclosed in quotes.
- Example:
str1 = "Hello"→"Hello"
π Lists
- Definition: Ordered, mutable collection enclosed in square brackets.
- Example:
list1 = [10, 20, 30]→[10, 20, 30]
π Tuples
- Definition: Ordered, immutable collection enclosed in parentheses.
- Example:
tuple1 = (1, 2, 3)→(1, 2, 3)
π Dictionaries
- Definition: Unordered collection of key–value pairs enclosed in curly braces.
- Example:
dict1 = {'Math': 90, 'Sci': 85}→{'Math': 90, 'Sci': 85}
⚙️ Key Properties
Strings: Indexed, immutable.
Example →"python"[0]→'p'Lists: Indexed, mutable.
Example →list1[2] = 35→[10, 20, 35]Tuples: Indexed, immutable (but can hold mutable objects).
Example →(7, 8, [55, 66])[2][1] = 100→(7, 8, [55, 100])Dictionaries: Keys unique, values mutable.
Example →dict1['Math'] = 95→{'Math': 95, 'Sci': 85}
➕ Operators
- Concatenation (+):
"hi"+"bye"→"hibye";[1]+[2]→[1,2];(1,)+(2,)→(1,2) - Repetition ():*
"ha"*3→"hahaha";[3]*2→[3,3];(4,)*3→(4,4,4) - Membership (in / not in):
"a" in "cat"→True;10 in [2,4,6]→False
✂️ Slicing
- Strings:
"computer"[1:5]→"ompu" - Lists:
[2,4,6,8,10][2:4]→[6,8] - Tuples:
(10,20,30,40)[1:3]→(20,30)
πTraversal
For loop:
for ch in "code": print(ch) # c o d eWhile loop:
i=0; s="hi" while i<len(s): print(s[i]); i+=1 # h i
π ️ Common Methods
Strings
len("hi")→2"hello".upper()→"HELLO""HELLO".lower()→"hello""hello world".split()→['hello','world']
Lists
list1.append(5)→[10,20,5]list1.remove(20)→[10,5]list1.sort()→[5,10]
Tuples
(10,20,10).count(10)→2(10,20,30).index(20)→1
Dictionaries
dict1.keys()→['Math','Sci']dict1.values()→[90,85]dict1.get('Math')→90
NOTE
R is Position Zero
R is Position -6
Comparison Table
Think of the phrase:
“Quotes, Squares, Circles, Curly”
Quotes → Strings
"Hello"→ Characters are always spoken in quotesSquares → Lists
[1,2,3]→ Lists are like boxes (square shelves) where items can be changedCircles → Tuples
(1,2,3)→ Tuples are round plates — once served, you can’t change the foodCurly → Dictionaries
{'key': 'value'}→ Curly braces look like locks protecting key–value pairs
π§ Quick Memory Trick
“Quotes talk, Squares store, Circles stay, Curly connect.”
- Quotes talk → Strings are words/characters.
- Squares store → Lists store items flexibly.
- Circles stay → Tuples stay fixed.
- Curly connect → Dictionaries connect keys to values.
Comments
Post a Comment