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" → True10 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 e
    
  • While 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

Strings , Lists , Tuples & Dictionary begins with Position Zero in forward direction

Strings , Lists , Tuples & Dictionary begins with Position -1 in Reverse  direction

Royale in Forward Reading
R is Position Zero
e is Position Five

Python in Reverse Reading
R is Position -6
e is Position -1

Comparison Table




 πŸŽ― Mnemonic for Brackets

Think of the phrase:
“Quotes, Squares, Circles, Curly”

  • Quotes → Strings
    "Hello" → Characters are always spoken in quotes

  • Squares → Lists
    [1,2,3] → Lists are like boxes (square shelves) where items can be changed

  • Circles → Tuples
    (1,2,3) → Tuples are round plates — once served, you can’t change the food

  • Curly → 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

Popular posts from this blog

I PU Python 50 Marks Package - Theory - UPDATED

I PU Chapter 11 - Societal Impact