0% found this document useful (0 votes)
4 views

Network Programming II Memo

The document is a memo for a Network Programming II exam, covering various topics in Python programming including boolean values, loops, functions, and methods. It includes true/false questions, code breakdowns, and explanations of concepts such as bubble sort and list operations. Additionally, it highlights common errors in code and provides examples of correct implementations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Network Programming II Memo

The document is a memo for a Network Programming II exam, covering various topics in Python programming including boolean values, loops, functions, and methods. It includes true/false questions, code breakdowns, and explanations of concepts such as bubble sort and list operations. Additionally, it highlights common errors in code and provides examples of correct implementations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

NETWORK PROGRAMMING II MEMO

QUESTION 1

1.1 TRUE

In Python, True is internally represented as 1, and False is represented as 0.

When comparing True > False, it translates to 1 > 0, which is True.

1.2 FALSE
x = 2 (assigns the value 2 to x).
The condition inside print():
x > 5 → 2 > 5 → False
x < 10 → 2 < 10 → True
The AND operator requires both conditions to be True for the whole expression to be True.

1.3 LARGE

a = 5 assigns the value 5 to the variable a.

The if condition checks whether a < 5. Since 5 is not less than 5, the condition is False, so "Small"
is not printed.

The next line (print("Large")) is outside the if statement, so it always executes.

1.4 Executes if a condition is true.

1.5 4

Why is the Output 4?

1. The loop starts with i = 1.

2. In each iteration, i increases by 1.

3. The loop stops when i becomes 4 because 4 <= 3 is False.

4. After exiting the loop, print(i) prints 4.

1.6 LOOP
1.7 8

When you shift the bits to the left, you are essentially multiplying the number by powers of 2 for
each position you shift. Each left shift operation multiplies the number by 2.

1.8 iteration through a specified sequence is required.

1.9 1

1.10 [3,7,4]

SECTION B

2.1 False – An if statement does not require a corresponding else part. An else component is
optional.

2.2 True – Methods are indeed tied to objects or classes and typically require an object or class
instance to execute, especially instance methods.

2.3 False – Parameters are variables defined within the function, not outside. Variables that exist
outside functions are called "global variables."

2.4 True – In Python, the index of the first element in a list is 0.

2.5 False – A built-in function is a function provided by Python (like print(), len(), etc.), not one
that is written by the user. Functions written by the user are considered custom functions.

SECTION C

3.1 List two differences between the while loop and the for loop in terms of their
implementation in Python.

Control Structure:
• The while loop runs as long as a specified condition remains True, making it more flexible
for situations where the number of iterations is not known in advance.
• The for loop is typically used to iterate over a sequence (such as a range or list), making it
suitable for situations where the number of iterations is known beforehand.
Loop Initialization and Termination:
• In a while loop, the loop variable and condition are manually controlled and updated
inside the loop.
• In a for loop, the loop variable is automatically initialized and updated by Python,
especially when iterating through a sequence or using the range() function.

b (i)

(ii)

i=1

while i < 8:

print(i)

i += 2

3.2

a. Differences between functions and methods:

o Functions are independent blocks of code that perform specific tasks and can be
applied to various data types. They are called using their name followed by
parentheses (e.g., function_name()).

o Methods are functions that are associated with an object or data type. They are
called using the object followed by a dot (e.g., object.method_name()).

OR
o Functions can operate on any data passed as arguments and are not tied to a
specific object.

o Methods are specific to the object they belong to and usually operate on the data
contained within that object (e.g., a list method like .append() operates on a list
object).

b. Two examples of functions used to process elements in a list

len(), sum(), min(), max(), sorted(), list(), index(), count(), clear() etc

c.

i. What is the purpose of the statement on line 2?

swapped = True

This initializes the swapped variable to True to enter the while loop. It's part of the logic for
Bubble Sort, which continues looping until no swaps are needed (i.e., the list is sorted).

ii. What is the purpose of the statement on line 8?

my_list.append(val)

is used to add the value val to the end of the list my_list.

iii. What is the purpose of the statement on line 14?

swapped = False

At the start of each while loop iteration, this resets swapped to False. If no swapping occurs in
the inner for loop, the list is sorted and the while loop will terminate.
iv. What is the purpose of the statement on line 18?

my_list[i], my_list[i+1] = my_list[i+1], my_list[i]

This line performs the actual swap between adjacent elements if the element at index i is greater
than the one at i+1, ensuring ascending order.
Code Breakdown:

my_list = []

• Purpose: Initializes an empty list called my_list where the elements entered by the user
will be stored.

swapped = True

• Purpose: Initializes a boolean flag swapped to True. This flag will help control the while
loop to indicate whether any elements were swapped during the sorting process. It
ensures that the list keeps being processed until no further swaps are needed.

num = int(input("How many elements do you want to sort: "))

• Purpose: This line prompts the user to input the number of elements they wish to sort.
The input() function takes the user input as a string, and int() converts it to an integer. The
integer is then stored in the variable num, which tells the program how many elements to
expect and process.

print()

• Purpose: This line just prints an empty line for better readability in the output.
for i in range(num):

• Purpose: This begins a for loop that runs num times, based on the number of elements
the user wants to sort. For each iteration of the loop, the user will be asked to input a
value.

val = int(input("Enter element an element of the list: "))

• Purpose: This line prompts the user to enter a number (an element of the list) and
converts the input from a string to an integer using int(). The value is stored in the
variable val.

my_list.append(val)

• Purpose: The value entered by the user (val) is added to the end of the list my_list using
the append() method. This builds the list step by step with each user input.

print()

• Purpose: Prints an empty line for better readability in the output.

print("Original list:", my_list)

• Purpose: This line prints the original list after the user has entered all the elements. It
shows the list before any sorting occurs.
while swapped:

• Purpose: This begins a while loop that will keep running as long as swapped is True. The
loop performs the sorting process. Initially, swapped is True, so the loop will run at least
once.

swapped = False

• Purpose: Inside the while loop, this line sets swapped to False. The idea is to assume that
no elements are swapped at the start of each iteration. If any swap happens, the flag
swapped will be set to True, indicating the need for another pass through the list.

for i in range(len(my_list)-1):

• Purpose: This line starts a for loop that iterates over each pair of adjacent elements in the
list. range(len(my_list)-1) ensures that the loop stops one index before the last element,
preventing an index error when comparing my_list[i] with my_list[i+1].

if my_list[i] > my_list[i+1]:

• Purpose: This line compares each pair of adjacent elements. Specifically, it checks if the
current element (my_list[i]) is greater than the next element (my_list[i+1]). If this
condition is True, the two elements are in the wrong order (for ascending sorting), and
they need to be swapped.
swapped = True

• Purpose: If the condition my_list[i] > my_list[i+1] is True, this line sets swapped to True,
indicating that a swap has occurred. This ensures that the while loop will run again, as
there might still be unsorted elements in the list.

my_list[i], my_list[i+1] = my_list[i+1], my_list[i]

• Purpose: This line performs the swap. It exchanges the values of my_list[i] and
my_list[i+1]. The values are swapped in place, which is a key operation in the bubble sort
algorithm.

print("Sorted list:", my_list)

• Purpose: After the while loop finishes (when no swaps occur in a full pass), this line prints
the sorted list. By this point, the list has been arranged in ascending order (because the
comparison in line 18 checks for my_list[i] > my_list[i+1]).

3.3 a.

Three errors:

1. Line 1: n = input(...) returns a string. It should be converted to an integer using int().

2. Line 3: sum = 1 initializes sum incorrectly. It should be sum = 0.

3. Line 7: i = i + sum is incorrect. It should be i = i + 1 to correctly increment the loop counter.


b.

n = int(input("How many numbers are to be added : "))

i=0

sum = 0

while i < n:

number = int(input("Enter a number: "))

sum = sum + number

i=i+1

print("The sum of the numbers entered is: ", sum)

3.4

a . [3, 5, 1, 7, 9]

N.B append() method is used to add an element to the end of the list

b.

N.B CONTINUE statement python to skip the rest of the loop for the current iteration when I == 1
and print(i) is executed only when i is not 1.

c. 7

You might also like