---
# System prepended metadata

title: 'Week 7: Comparison Operators, Logical Operators and list in Python'

---

## Week 7: Comparison Operators, Logical Operators and List(Data type) In Python

  This past week, we started with comparison operators and ended with list as a datatype in python.  

### Comparison Operator
  Comparison operators are used to compare two values and evaluate whether a statement is true or false. The result of comparison is always boolean which is either true or false.

Example of comparison operators are:

== (equal to):
  This checks whether two values are exactly the same. 
Example:
```python
5 == 5   # True, because both values are equal
5 == 3   # False, because 5 is not equal to 3
```

!= (not equal to):
Confirms that two values are not equal.
Example:
```python
7 != 3   # True, since 7 is different from 3
4 != 4   # False, since both values are the same
```

> (greater than):
Returns True if the value on the left is larger.
Example:
```python
10 > 4   # True, 10 is greater than 4
3 > 8    # False, 3 is smaller than 8
```

>= (greater than or equal to):
Returns True if the left value is either greater than or equal to the right.
Example:
```python
6 >= 6   # True, because they are equal
9 >= 5   # True, because 9 is greater
2 >= 7   # False, 2 is less than 7
```

< (less than):
Returns True if the value on the left is smaller.
Example:
```python
2 < 5    # True
10 < 4   # False
```

<= (less than or equal to):
Returns True if the left value is either smaller than or equal to the right.
Example:
```python
3 <= 3   # True
1 <= 8   # True
9 <= 6   # False
```
🔗 Logical Operators in Python

Logical operators help you combine multiple conditions in your code. They allow your program to make more complex decisions based on multiple expressions.

1. and Operator
This operator returns True only if both conditions are true. If either is false, the result is False.

Example:

#### Check if user is over 18 AND born in the year 2000
```python
age = 25
year = 2000
print(age > 18 and year == 2000) # Outputs True
```
In this case, both conditions were True, so it prints out the boolean value true

2. or Operator
This operator returns True if at least one of the conditions is true.

Example:

#### Check if user is over 18 OR born in the year 2000
```python=
age = 25
year = 2000
print(age > 18 or year != 2000 ) #Output is True
```

3. not Operator
The not operator flips a boolean value. It changes True to False, and False to True.

Example:
#### If the user is NOT doesn't meet requirements
```python=
age = 25
year = 2000
print(not(age > 18 and year == 2000)) #Outputs False
```
### Working with Lists in Python

A list in Python is like a container that holds multiple values in a single variable. Each value in the list can be accessed by its index (position), starting from 0.

Creating Lists:
```python
fruits = ["apple", "banana", "mango"]
numbers = [1, 3, 5, 7]
empty_list = []
```

#### Accessing and Modifying List Items using Indexing
To access or change a value in a list, use the index:

```python
fruits = ["apple", "banana", "mango"]
print(fruits[1])     # Outputs: banana

fruits[2] = "orange"
print(fruits)        # Outputs: ['apple', 'banana', 'orange']
```
#### Slicing Lists
Slicing means taking a part of a list using a range of indexes.

```python
numbers = [4, 5, 12, 4, 9]

print(numbers[:3])     # Outputs: [4, 5, 12] — up to index 3, not including it
print(numbers[2:4])    # Outputs: [12, 4] — from index 2 to index 3
print(numbers[-4:-2])  # Outputs: [5, 12] — slicing from the end
```
#### List Methods
Python offers several built-in methods to work with lists.

1. append()

Adds a new item to the end of the list.

```python
colors = ["red", "green"]
colors.append("blue")
print(colors)   # Outputs: ['red', 'green', 'blue']
```

2. remove()

Removes the first occurrence of a specified item.

```python
nums = [3, 7, 7, 2]
nums.remove(7)
print(nums)     # Outputs: [3, 7, 2]
```

3. insert()

Inserts an item at a specific position.

```python
nums = [1, 2, 4]
nums.insert(2, 3)
print(nums)     # Outputs: [1, 2, 3, 4]
```

4. index()

Finds the position of an item in the list.

```python
animals = ["cat", "dog", "bird"]
print(animals.index("dog"))   # Outputs: 1
```

## CONCLUSION
  It was an insight to see the things we could do with both the comparison operators and the logical operators and wrapping the week up with a datatype that takes in more than one value and any datatype was a fun thing to do. Looking forward to what the new week holds. Thanks for reading. See you soon!