---
# System prepended metadata

title: COHORT III WEEK 5

---

<strong>DAY 1</strong>

<strong>INDEXING IN PYTHON</strong>

Indexing is the process of accessing an element in a sequence using its position in the sequence (its index).

In Python, indexing starts from 0, which means the first element in a sequence is at position 0, the second element is at position 1, and so on.

To access an element in a sequence, you can use square brackets [] with the index of the element you want to access.

For example name = 'Tom'

The variable, name stores a string data type 'Tom' 

To get their index;
name = 'T o m'
index =  0 1 2

This means that when we;
print(name[0])
print(name[1])
print(name[2])

The result will be;
T
o
m
    
Lets look at another example in the picture below.
                   
![Screenshot from 2025-07-08 16-01-46](https://hackmd.io/_uploads/S1qWXh9Hee.png)

RESULT

![Screenshot from 2025-07-08 16-02-07](https://hackmd.io/_uploads/ryvzQhqSxx.png)

<strong>SLICING IN PYTHON</strong>

Slicing is the process of accessing a sub-sequence of a sequence by specifying a starting and ending index. In Python, you perform slicing using the colon : operator.

    The syntax for slicing is as follows:
    
    sequence[start_index:end_index]
    
where start_index is the index of the first element in the sub-sequence and end_index is the index of the last element in the sub-sequence (excluding the element at the end_index). To slice a sequence, you can use square brackets [] with the start and end indices separated by a colon.

Example

![Screenshot from 2025-07-09 09-28-09](https://hackmd.io/_uploads/HyWNuiiHxe.png)

Result

![Screenshot from 2025-07-09 09-28-00](https://hackmd.io/_uploads/SyXUussrxl.png)

<strong>STIRNGS METHOD IN PYTHON</strong>

Python string methods is a collection of in-built Python functions that operates on strings.

Note: Every string method in Python does not change the original string instead returns a new string with the changed attributes. 

Python string is a sequence of Unicode characters that is enclosed in either single (' ') or double (" ") quotation marks.

<strong>CASING</strong>

lower(): Converts all uppercase characters in a string into lowercase.

    for example
    
    bio = "Programmer"
    
    print(bio.lower())
    
    OUTPUT: programmer

upper(): Converts all lowercase characters in a string into uppercase.

    for example
    
    bio = "Programmer"
    
    print(bio.upper())
    
    OUTPUT: PROGRAMMER
    
Example of both Upper and Lower can be seen in the 

![Scr

Result

![Screenshot from 2025-07-09 09-36-41](https://hackmd.io/_uploads/HJRP3osSxe.png)

<strong>DAY 2</strong>

capitalize(): Convert the first character of a string to uppercase.

    for example
    
    bio = "programmer"
    
    print(bio.capitalize())
    
    OUTPUT: Programmer
    
<strong>ESCAPE CHARACTERS</strong>

In Python, the backslash `\` serves as the escape character. It is used to introduce escape sequences, which are special character combinations that represent characters or actions that cannot be directly typed or have special meaning within a string.

Here are some common escape sequences in Python: 

\n:	Newline – Moves the cursor to the next line.
\\': Single Quote – Inserts a single quote inside a single-quoted string.
\\": Double Quote – Inserts a double quote inside a double-quoted string.
\\\\: Backslash – Inserts a literal backslash.
\t: Tab – Adds a horizontal tab.
\r: Carriage return, moving the cursor to the beginning of the current line.
\b: Represents a backspace.
\f: Represents a form feed.
\v: Represents a vertical tab.

EXAMPLES CAN BE SEEN BELOW

![Screenshot from 2025-07-11 20-02-50](https://hackmd.io/_uploads/Hk0bxJyUex.png)

RESULT

![Screenshot from 2025-07-11 20-03-19](https://hackmd.io/_uploads/By9zgk1Ugg.png)

    
<strong>DAY 3</strong>

<strong>'in'</strong> and <strong>'not in'</strong> Operators in Python

The 'in' and 'not in' operators in Python are membership operators used to check for the presence or absence of a value within a sequence or collection. They return a boolean value (True or False).

1. The 'in' Operator:
Purpose: Checks if a specified value is present within a sequence (like a string)
Syntax: value in sequence
Returns: True if value is found in sequence, otherwise False.

2. The 'not in' Operator:
Purpose: Checks if a specified value is not present within a sequence. It is the logical inverse of the in operator.
Syntax: value not in sequence
Returns: True if value is not found in sequence, otherwise False.


EXAMPLE

       message = "My name is John"
      
       print("name" in message)
      
       print("name" not in message)
       
        OUTPUT 
        True
        False

<strong>startswith() method</strong>

The <strong>startswith()</strong> method in Python is a built-in string method used to check if a string begins with a specified prefix. It returns True if the string starts with the given prefix, and False otherwise. This method is case-sensitive by default. 

EXAMPLE

      message = "My name is John"
   
      print(message.startswith("My"))
      
      print(message.startswith("name"))
      
      OUTPUT
      True
      False
      
<strong>split() method</strong>

The split() method in Python is a built-in string method used to divide a string into a list of substrings. It returns a new list containing the substrings, without modifying the original string. 

Split makes use of a separator which specifies the delimiter character or string at which the split should occur.

EXAMPLE

      message = "GhostNet#X44CR#98.654#TRC8821"
  
      newMessage = message.split("#")
   
      print(newMessage)

        OUTPUT
        ['GhostNet', 'X44CR', '98.654', 'TRC8821']

    NOTE: The seperator (delimiter) is #
    
<strong>DAY 4</strong>

<strong>HANDS ON ACTIVITIES</strong>

PROBLEM 1

Prompt a user to enter birth year. Use it to print their actual age.

E.g enter date of Birth: 2000

Output: You are 25 years old.

SOLUTION

![Screenshot from 2025-07-11 20-11-29](https://hackmd.io/_uploads/ry9gG1JUxg.png)

RESULT

![Screenshot from 2025-07-11 20-11-24](https://hackmd.io/_uploads/HylGf1k8lx.png)

PROBLEM 2

Create a variable account_balance and make your balance = 10000

Prompt a user to enter amount they want to spent (that is 10000 or below) and output their balance in 2 decimal places.

E.g Enter amount you want to spend: 3000

Output: Your Balance is remaining 7,000.00
    
SOLUTION

![Screenshot from 2025-07-11 20-19-36](https://hackmd.io/_uploads/rJ3xVykIxl.png)

OUTPUT

![Screenshot from 2025-07-11 20-20-10](https://hackmd.io/_uploads/r1aW41JLgl.png)


PROBLEM 3

Using any slicing method/concept you know, extract the word Python from the variable 

message = "rPyoagrhmotn id cool"

Output: Python

SOLUTION

![Screenshot from 2025-07-11 20-26-42](https://hackmd.io/_uploads/B1XFrJyUeg.png)

OUTPUT

![Screenshot from 2025-07-11 20-26-34](https://hackmd.io/_uploads/SkOoByyLgl.png)










                   


