Why Python?
- Python is easy to learn, resourceful, well-supported, and free.
- Some firms require workers to learn programming languages like Python.
Lists in Python
- Lists are ordered sequences that can store items of different data types.
- For array-like algorithms, data within lists should be of the same data type.
Basic Operations of Python Lists
- Creation:
list1 = ["David", "Lily", "Ben"]list2 = [34, 23, 90, 56, 100]
- Initialization (assigning default values):
list3 = [0]*10list4 = [""]*5
- Updating Elements:
list2[0] = 28list2[1] = list2[2] - 5
- Output:
print(list1[1]) # Output: Lily- Looping through a list:
python
list2 = [34, 23, 90, 56, 100]
for i in range(0, 5):
print(list2[i])
- Copying a list:
list2 = [34, 23, 90, 56, 100]
new_list = [0]*5
for i in range(0, 5):
new_list[i] = list2[i]
List Operations
time = [15.1, 14.9, 15.6, 16.1, 16.8, 17.0, 16.9, 16.5]
total = 0
for i in range(0, 8):
total = total + time[i]
print(total) # Output: 128.9
time = [15.1, 14.9, 15.6, 16.1, 16.8, 17.0, 16.9, 16.5]
total = 0
for i in range(0, 8):
total = total + time[i]
avg = total/8
print(avg) # Output: 16.1125
- len() function: Returns the number of items in a list.
Searching for Items in a List
target = int(input())
found = False
N = len(the_list)
for i in range(0, N):
if target == the_list[i]:
found = True
print(found)
- Using a
while loop to find the first index:
target = int(input())
found = False
index = -1
i = 0
N = len(the_list)
while i < N and found == False:
if target == the_list[i]:
found = True
index = i
i = i + 1
print(index)
- Counting items greater than a value:
bound = int(input())
count = 0
N = len(the_list)
for k in range(0, N):
if the_list[k] > bound:
count = count + 1
print(count)
- Finding the largest value:
largest = the_list[0]
N = len(the_list)
for i in range(1, N):
if the_list[i] > largest:
largest = the_list[i]
print(largest)
- Checking if a list is sorted in ascending order:
sorted_list = True
N = len(the_list)
for k in range(0, N-1):
if the_list[k] > the_list[k+1]:
sorted_list = False
print(sorted_list)
List Manipulation
- Deleting an item at index P. Number of elements is reduced by one.
N = len(the_list)
for k in range(P, N-1):
the_list[k] = the_list[k+1]
N = N - 1
- Adding a new item
new_data at index P.
the_list = the_list + [0]
N = len(the_list)
for k in range(N-1, P, -1):
the_list[k] = the_list[k-1]
the_list[P] = new_data
- Moving the item at index P to the first position:
temp = the_list[P]
for k in range(P, 0, -1):
the_list[k] = the_list[k-1]
the_list[0] = temp
Integrated Application of Python Strings
- Strings in Python can be modified. Values cannot be directly assigned to items in a string.
- Finding Length: Use the len() function.
- Finding a Character:
- Using a
for loop:
python
the_string = "Work Hard!"
target = "a"
found = False
N = len(the_string)
for i in range (0, N):
if target == the_string[i]:
found = True
print(found)
- Using a
while loop.
- Extracting Characters: Create substrings by extracting parts of strings.
Determining the Type of a Character
- Use ASCII values for comparisons.
- Features of ASCII:
- Capital and small letters are in alphabetical order.
- Numbers are in ascending order.