Looping and Counting –
We are looping through a sequence (like a string or list) and counting how many times
something appears.
Example: Count characters in a string
Let’s say We want to count how many times the letter 'a' appears:
text = "banana"
count = 0
for char in text:
if char == 'a':
count += 1
print("Number of a's:", count)
Output:
Number of a's: 3
Example: Count items in a list
colors = ["red", "blue", "green", "red", "red"]
count = 0
for color in colors:
if color == "red":
count += 1
print("Number of reds:", count)
Alternative: Use .count() (if just counting directly)
text = "banana"
print([Link]("a")) # Output: 3
But a loop gives We more control if We're doing multiple things at once.
Loop + Count Example with Conditions
Count how many vowels are in a string:
text = "hello world"
vowels = "aeiou"
count = 0
for char in text:
if char in vowels:
count += 1
print("Vowel count:", count) # Output: 3
Summary
Task How to do it
Loop over string/list for item in sequence:
Count with condition if item == target: count += 1
Count directly [Link](target)
What Are List Values:
A list is a collection of values, called elements or items, stored in a single variable. List values can
be anything: strings, numbers, other lists, even mixed types.
Example:
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = ["hello", 42, True]
Accessing List Values (Indexing)
We can access list values using index numbers, starting at 0:
print(fruits[0]) # apple
print(fruits[2]) # cherry
We can also use negative indexes to start from the end:
print(fruits[-1]) # cherry (last item)
Modifying List Values
Lists are mutable, meaning We can change values directly:
fruits[1] = "blueberry"
print(fruits) # ['apple', 'blueberry', 'cherry']
Adding Values
[Link]("date") # Add to the end
[Link](1, "avocado") # Add at a specific index
Removing Values
[Link]("apple") # Remove by value
[Link]() # Remove last item
Looping Through List Values
for fruit in fruits:
print(fruit)
Check If a Value Is in the List
if "banana" in fruits:
print("Yes, it's there!")
Count, Sort, and More
numbers = [5, 2, 9, 1]
print(len(numbers)) # 4
print([Link](2)) # 1
[Link]() # [1, 2, 5, 9]
[Link]() # [9, 5, 2, 1]
Summary
Action Code Example
Access value my_list[0]
Modify value my_list[1] = "new"
Add value my_list.append("new")
Remove value my_list.remove("old")
Loop through for x in my_list:
Count values my_list.count("x")
Accessing Elements in Python:
We can access individual elements of sequences like lists or strings using indexing.
Basic Indexing
my_list = ["apple", "banana", "cherry"]
print(my_list[0]) # apple
print(my_list[1]) # banana
print(my_list[2]) # cherry
• Indexing starts at 0
• So my_list[0] is the first item
Negative Indexing
We can also use negative numbers to start counting from the end:
print(my_list[-1]) # cherry (last item)
print(my_list[-2]) # banana
Accessing Characters in a String
Strings work just like lists of characters:
word = "Python"
print(word[0]) # P
print(word[-1]) # n
Out of Range Error
Be careful! If We try to access an index that doesn’t exist, Python will raise an error:
print(my_list[3]) # ❌ IndexError: list index out of range
Accessing in Loops
To go through each element, use a loop:
for item in my_list:
print(item)
Or, to access by index:
for i in range(len(my_list)):
print(f"Index {i}: {my_list[i]}")