Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views2 pages

8&9 Programs

The document contains two Python programs: one for printing prime numbers less than 20 and another for creating, appending, and removing elements from a list. The prime number program checks each number for divisibility and prints the primes, while the list program demonstrates list operations such as appending, extending, and removing elements. Example outputs for both programs are provided.

Uploaded by

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

8&9 Programs

The document contains two Python programs: one for printing prime numbers less than 20 and another for creating, appending, and removing elements from a list. The prime number program checks each number for divisibility and prints the primes, while the list program demonstrates list operations such as appending, extending, and removing elements. Example outputs for both programs are provided.

Uploaded by

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

8.

Write a python program to print prim numbers less than 20

for num in range(2, 20): # Iterate through numbers from 2 to 19


is_prime = True # Assume the number is prime
for i in range(2, num): # Check divisibility
if num % i == 0:
is_prime = False # Not a prime number
break
if is_prime:
print(num, end=" ")

Explanation:
1. We loop through numbers from 2 to 19.
2. For each number, we check if it's divisible by any number from 2 to (num - 1).
3. If it's not divisible, it's a prime number, and we print it.

output: 2 3 5 7 11 13 17 19
9. Write a python program to create, append and remove lists in python

# 1. Creating a List
my_list = [10, 20, 30, 40]
print("Initial List:", my_list)
# 2. Appending elements to the list
my_list.append(50) # Adds 50 to the end of the list
print("After Append:", my_list)
# 3. Appending multiple elements
my_list.extend([60, 70, 80]) # Adds multiple elements at once
print("After Extending:", my_list)
# 4. Removing elements from the list
my_list.remove(30) # Removes the first occurrence of 30
print("After Removing 30:", my_list)
# 5. Removing the last element using pop()
last_element = my_list.pop() # Removes and returns the last element
print("After Pop:", my_list)
print("Popped Element:", last_element)
# 6. Removing an element at a specific index
del my_list[1] # Removes the element at index 1
print("After Deleting Index 1:", my_list)

Explanation:
1. Creating a List → my_list = [10, 20, 30, 40]
2. Appending Elements → append(50) adds an element to the list.
3. Appending Multiple Elements → extend([60, 70, 80]) adds multiple values.
4. Removing a Specific Element → remove(30) deletes the first occurrence of 30.
5. Removing Last Element → pop() removes the last element and returns it.
6. Removing an Element by Index → del my_list[1] deletes the element at index 1.

Output:
Initial List: [10, 20, 30, 40]
After Append: [10, 20, 30, 40, 50]
After Extending: [10, 20, 30, 40, 50, 60, 70, 80]
After Removing 30: [10, 20, 40, 50, 60, 70, 80]
After Pop: [10, 20, 40, 50, 60, 70]
Popped Element: 80
After Deleting Index 1: [10, 40, 50, 60, 70]

You might also like