OOP Reviewer 1
OOP Reviewer 1
print("Hello world!")
Output: Inside the loop, the statement print("Hello
world!") will print the message "Hello world!"
each time the loop runs.
i += 1 #increment; add 1 to i
After printing the message, i += 1 increments the value
of i by 1. This ensures that the loop progresses and
eventually reaches the condition where i <= 5, which
will stop the loop.
Code 2: i=2
This initializes a variable i with the value 2. This is where
the loop will start.
i += 2
This increments the value of i by 2 after printing it. It
ensures that each time the loop runs, i increases by 2,
which will help the loop print even numbers starting from
2.
Code 3: i=5
This initializes the variable i with the value 5. The loop will
start with this value.
while i >= 0:
This is a while loop. The loop will continue running as long
as the condition i >= 0 is true. The loop will keep
Output: running until i becomes less than 0.
print(i)
Inside the loop, print(i) prints the current value of i in
each iteration of the loop.
i -= 1
This decreases the value of i by 1 after printing it. So each
time the loop runs, i will be reduced by 1.
age = -1
This initializes the age variable with a value of -1, which is
outside the valid range (0 to 110). This ensures that the
while loop will run at least once because the initial value
Code 4: of age does not meet the valid condition (age < 0 or
age > 110).
Code 5:
Output:
Code 6:
Output:
Code 7:
Output:
Code 8:
Output:
Code 9:
Output:
Code 10:
Output:
Code 11:
Output:
Code 12:
Output:
Code 13:
Output:
Code 14:
Output:
Code 15:
students.sort()
The sort() method sorts the list in ascending order (alphabetically, in this case).
After sorting, the list becomes: ['Abner', 'Bruno', 'Diego', 'James']
students.sort(reverse=True)
The sort(reverse=True) method sorts the list in descending order
(reverse alphabetical order).
After this step, the list becomes: ['James', 'Diego', 'Bruno', 'Abner']
print(students[0])
This prints the first element of the sorted list, which is "James"
(since the list is now sorted in reverse alphabetical order).
Code 16:
Output:
Code 17:
Output:
Code 18:
print(students.count("Diego"))
The count() method counts how many times the element
"Diego" appears in the list.
In the list, "Diego" appears 2 times, so this will print 2.
print(students.index("Diego"))
The index() method returns the index of the first occurrence
of the element "Diego" in the list.
Since "Diego" first appears at index 0, this will print 0.
Code 19:
Output:
print(fruits[-1])
Output:
Here, fruits[-1] refers to the last element of the list. In
Python, negative indexing starts from the end of the list, where
-1 is the index of the last element.
So, fruits[-1] accesses "cherry" (the last element in the
list).
Cherry
This prints "cherry", which is the last element of the
fruits list.
Code 21:
Code 22:
ADDING VALUE LIST USING INSERT
male.insert(1, "Jake")
The .insert(index, element) method inserts
Output: an element at a specific index in the list.
male.insert(1, "Jake") inserts "Jake" at
index 1.
Since "Mike" was originally at index 1, it
gets shifted to index 2
Code 23:
numList.append(99)
The .append(99) method adds the value 99 to the end of
Output: the numList
Code 24:
Code 25:
Output:
Code 26:
Output:
Code 26:
Output:
NO EXTRA PRINT
STATEMENT
No extra print(len(fruits))