0% found this document useful (0 votes)
4 views9 pages

OOP Reviewer 1

The document contains multiple code snippets demonstrating the use of while loops, list operations, and methods in Python. Each code block initializes variables, performs iterations, and includes comments explaining the functionality of the code. Key examples include printing messages in loops, validating user input for age, and manipulating lists with methods like sort, count, and insert.

Uploaded by

Angela Tangco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

OOP Reviewer 1

The document contains multiple code snippets demonstrating the use of while loops, list operations, and methods in Python. Each code block initializes variables, performs iterations, and includes comments explaining the functionality of the code. Key examples include printing messages in loops, validating user input for age, and manipulating lists with methods like sort, count, and insert.

Uploaded by

Angela Tangco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Code 1: i = 1 #initial value

 This initializes a variable i with a value of 1.

while i <= 5: #condition, w/ ending value


 This is a while loop. The loop will continue to run as long as
the condition i <= 5 is true. In other words, it will keep
running until i becomes less than equal 5.

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.

while i <= 10:


 This is a while loop. The loop will continue running as long
as the condition i <= 10 is true. In other words, it will
keep running until it exceeds 10.
Output:
print(i)
 Inside the loop, the statement print(i) prints the
current value of i during each iteration of the loop.

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).

while age < 0 or age > 110:


 This is a while loop that checks if the condition age < 0
or age > 110 is true. The loop will keep running as
long as the entered age is either negative (< 0) or greater
Output: than 110 (> 110).

age = int(input("Enter age: "))


 Inside the loop, the program asks the user to enter their
age using input(). The input is then converted into an
integer using int(), and the value is stored in the
variable age.
 The loop will repeat, prompting the user to input their age
again, until they provide a valid age (a number between 0
and 110).

Code 5:

Output:

Code 6:

Output:
Code 7:

Output:

Code 8:

Output:

WITH EXTRA PRINT


STATEMENT
Includes print(len(students)), displaying

Code 9:

Output:
Code 10:

Output:

Code 11:

Output:

Code 12:

Output:

Code 13:
Output:

Code 14:

Output:

Code 15:

students = ["Diego", "Bruno", "James", "Abner"]


Output:  This creates a list of students: ["Diego", "Bruno", "James", "Abner"]

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:

students = ["Diego", "Bruno", "James", "Diego", "Abner"]


 The students list contains the names: ["Diego",
Output: "Bruno", "James", "Diego", "Abner"]. Notice that
"Diego" appears twice in the list.

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:

Code 20: fruits = ["apple", "banana", "cherry"]


 The fruits list contains three elements: ["apple",
"banana", "cherry"].

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:

CHANGING VALUE LIST


Output:

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:

ADDING VALUE LIST USING APPEND

numList.append(99)
 The .append(99) method adds the value 99 to the end of
Output: the numList

Code 24:

COMBINING LIST USING EXTEND


male1.extend(male2)
 The .extend() method adds all elements from male2 to
the end of male1.
Output:  After this operation, male1 becomes
["Bien", "John", "Jays", "JR"]

Code 25:
Output:

Code 26:

Output:

Code 26:

Output:
NO EXTRA PRINT
STATEMENT
No extra print(len(fruits))

You might also like