<strong>DAY 1</strong>
Python <strong>"not" operator</strong>
The not keyword in Python is a logical operator used to obtain the negation or opposite Boolean value of an operand.
The "not" is a unary operator, which means that it works with 1 operand and returns the inverted truth value for that operand. In simple terms, if the input is True, then the output is False and if the input is False, then the output is True.
This example shows various ways to use the not operator with different Boolean values and expressions.
print(not False) # Output True
print(not True)_ # Output False
print(not(True and False)) # Output True
print(not(True or False)) # Output False
print(not (5 > 7)) # Output # Output True
<strong>DAY 2</strong>
<strong>LIST</strong>
In Python, a list is a collection of items in a particular order. We can store all types of items in a list ranging from strings, integers, floats or boolean.
List in Python are Mutable. Hence, we can modify, replace or delete the items. List are ordered. It maintain the order of elements based on how they are added.
Accessing items in List can be done directly using their position (index), starting from 0.
For example;
Creating a Python list with different data types
a = [10, 20, "GfG", 40, True]
print(a)
Accessing elements using indexing
print(a[0]) # 10
print(a[1]) # 20
print(a[2]) # "GfG"
print(a[3]) # 40
print(a[4]) # True
Checking types of elements
print(type(a[2])) # str
print(type(a[4])) # bool
Explanation:
The list contains a mix of integers (10, 20, 40), a string ("GfG") and a boolean (True).
The list is printed and individual elements are accessed using their indexes (starting from 0).
type(a[2]) confirms "GfG" is a str.
type(a[4]) confirms True is a bool.
<strong>DAY 3</strong>
<strong>CREATING A LIST</strong>
Here are some common methods to create a list:
USING SQUARE BRACKETS
List of integers
a = [1, 2, 3, 4, 5]
print(a) # Output [1, 2, 3, 4, 5]
List of strings
b = ['apple', 'banana', 'cherry']
print(b) # Output ['apple', 'banana', 'cherry']
Mixed data types
c = [1, 'hello', 3.14, True]
print(c) # Output [1, 'hello', 3.14, True
USING list() CONSTRUCTOR
We can also create a list by passing an iterable (like a string, tuple or another list) to list() function.
From a tuple
a = list((1, 2, 3, 'apple', 4.5))
print(a) # Output [1, 2, 3, 'apple', 4.5]
ACCESSING LIST ELEMENTS
Elements in a list can be accessed using indexing. Python indexes start at 0, so a[0] will access the first element, while negative indexing allows us to access elements from the end of the list. Like index -1 represents the last elements of list.
a = [10, 20, 30, 40, 50]
# Access first element
print(a[0]) # Output 10
# Access last element
print(a[-1]) # Output 50
ADDING ELEMENTS INTO LIST
We can add elements to a list using the following methods:
append(): Adds an element at the end of the list.
extend(): Adds multiple elements to the end of the list.
insert(): Adds an element at a specific position.
# Initialize an empty list
a = []
# Adding 10 to end of list
a.append(10)
print("After append(10):", a)
Output After append(10): [10]
# Inserting 5 at index 0
a.insert(0, 5)
print("After insert(0, 5):", a)
Output After insert(0, 5): [5, 10]
# Adding multiple elements [15, 20, 25] at the end
a.extend([15, 20, 25])
print("After extend([15, 20, 25]):", a)
Output After extend([15, 20, 25]): [5, 10, 15, 20, 25]
UPDATING ELEMENTS INTO LIST
We can change the value of an element by accessing it using its index.
a = [10, 20, 30, 40, 50]
# Change the second element
a[1] = 25
print(a) # Output [10, 25, 30, 40, 50]
REMOVING ELEMENTS FROM LIST
We can remove elements from a list using:
<strong>remove()</strong>: Removes the first occurrence of an element.
<strong>pop()</strong>: Removes the element at a specific index or the last
element if no index is specified.
<strong>del statement</strong>: Deletes an element at a specified index.
a = [10, 20, 30, 40, 50]
# Removes the first occurrence of 30
a.remove(30)
print("After remove(30):", a)
Output After remove(30): [10, 20, 40, 50]
# Removes the element at index 1 (20)
popped_val = a.pop(1)
print("Popped element:", popped_val)
print("After pop(1):", a)
Popped element: 20
Output after After pop(1): [10, 40, 50]
# Deletes the first element (10)
del a[0]
print("After del a[0]:", a)
Output After del a[0]: [40, 50]
<strong>DAY 4</strong>
NESTED LISTS IN PYTHON
A nested list is a list within another list, which is useful for representing matrices or tables. We can access nested elements by chaining indexes.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Access element at row 2, column 3
print(matrix[1][2]) # Output 6