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

Python Program List

The document contains several Python programs demonstrating basic list operations, including swapping elements, counting even and odd numbers, filtering positive and negative numbers, counting them, removing multiple elements, and finding duplicates. Each program is presented with sample code and comments explaining its functionality. These examples serve as practical exercises for beginners learning Python list manipulation.

Uploaded by

Komal Babar
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)
2 views2 pages

Python Program List

The document contains several Python programs demonstrating basic list operations, including swapping elements, counting even and odd numbers, filtering positive and negative numbers, counting them, removing multiple elements, and finding duplicates. Each program is presented with sample code and comments explaining its functionality. These examples serve as practical exercises for beginners learning Python list manipulation.

Uploaded by

Komal Babar
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/ 2

1.

Python Program to Swap Two Elements in a List


a = [10, 20, 30, 40, 50]
a[0], a[4] = a[4], a[0]
print(a)
………………………………
temp = a[2]
a[2] = a[4]
a[4] = temp
print(a)

2. Python Program to Count Even and Odd Numbers in a List


a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even = 0
odd = 0

for num in a:

if num % 2 == 0:
even += 1
else:
odd += 1

# Printing the results


print("Even numbers:", even)
print("Odd numbers:", odd)

 to print positive numbers in a list


a = [-10, 15, 0, 20, -5, 30, -2]

for val in a:
if val > 0:
print(val)

 to print negative numbers in a list


a = [5, -3, 7, -1, 2, -9, 4]

for num in a:
if num < 0:
print(num)

 to count positive and negative numbers in a list

a = [10, -20, 30, -40, 50, -60, 0]


pos = 0
neg = 0
for n in a:
if n > 0:
pos += 1
elif n < 0:
neg += 1

print(pos)
print(neg)

 Remove multiple elements from a list

a = [10, 20, 30, 40, 50, 60, 70]

# Elements to remove
remove = [20, 40, 60]

# Remove elements using a simple for loop


res = []

for val in a:
if val not in remove:
res.append(val)

print(res)

Program to print duplicates from a list of


integers in Python
a = [1, 2, 3, 1, 2, 4, 5, 6, 5]

# Initialize an empty set to store seen elements


s = set()

# List to store duplicates


dup = []

for n in a:
if n in s:
dup.append(n)
else:
s.add(n)

print(dup)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy