Programming with Python for Paper 04 Computer Science 9618 A - LEVEL
Python List pop()
The pop() method removes the element at the specified position.
Remove the second element of the fruit list:
fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
print(fruits)
____________________________________________________
The pop() method removes the item at the given index from the list and returns
the removed item.
# create a list of prime numbers
prime_numbers = [2, 3, 5, 7]
# remove the element at index 2
removed_element = prime_numbers.pop(2)
print('Removed Element:', removed_element)
print('Updated List:', prime_numbers)
# create a list of prime numbers
prime_numbers = [2, 3, 5, 7]
# remove the element at index 2
removed_element = prime_numbers.pop(2)
print('Removed Element:', removed_element)
print('Updated List:', prime_numbers)
___________________________________________________________________________
Example 1: Pop item at the given index from the list
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']
# remove and return the 4th item
return_value = languages.pop(3)
print('Return Value:', return_value)
Programming with Python for Paper 04 Computer Science 9618 A - LEVEL
# Updated List
print('Updated List:', languages)
CLASSES EXAMPLE 01
class Employee:
def __init__(self,first,last,email):
self.first=first
self.last=last
self.email=first + '.'+ last +'@mrsaem.com'
emp1=Employee('SAEM','TARIQ','[email protected]')
emp2=Employee('ALI','MURTAZA','[email protected]')
print(emp1.email)
print(emp2.email)
________________________________________________________________
CLASSES EXAMPLE 02
class Human:
def __init__(self,n,o):
self.name=n
self.occupancy=o
def do_work(self):
if self.occupancy=="teacher":
print(self.name, " teaches Computer Science")
elif self.occupancy=="student":
print(self.name, "studies all the time")
def speaks(self):
print(self.name," says how are you")
saem = Human("Saem Tariq","teacher")
saem.do_work()
saem.speaks()
subhan=Human("Subhan Aziz", "student")
subhan.do_work()
subhan.speaks()
Programming with Python for Paper 04 Computer Science 9618 A - LEVEL
___________________________________________________________________________
_______________
Searching in Python
def search(list,n):
for i in range(len(list)):
if list[i]==n:
return True
return False
list=[3,5,6,7]
n=int(input("Enter the number"))
if search(list,n):
print("Found")
else:
print("Not Found")
__________________________________________________
Bubble Sort
# Bubble sort in python
# Sorts numbers in ascending order
str_input = input("Please enter numbers separated by spaces: ")
# split the string into numbers & create a list of numbers
numbers = [int(x) for x in str_input.split()]
count = len(numbers)
for outer in range(count - 1): # bubbles each number to the end
for i in range(count - outer - 1):
if numbers[i] > numbers[i + 1]:
# swap numbers!
numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i]
print(numbers)