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

python programming

The document provides various Python code snippets demonstrating basic data structures and algorithms, including bubble sort for sorting numbers, removing duplicates from a list, and operations on lists, tuples, and dictionaries. It also includes examples of counting even and odd numbers, printing them, and using sets to eliminate duplicates. Additionally, it highlights the differences between lists and sets regarding duplicates and ordering.

Uploaded by

Pandu Valla
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python programming

The document provides various Python code snippets demonstrating basic data structures and algorithms, including bubble sort for sorting numbers, removing duplicates from a list, and operations on lists, tuples, and dictionaries. It also includes examples of counting even and odd numbers, printing them, and using sets to eliminate duplicates. Additionally, it highlights the differences between lists and sets regarding duplicates and ordering.

Uploaded by

Pandu Valla
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.Sort numbers means ascending order .

if you want descending order just change arr[j < arr[j+1]


that’s it.

def bubble_sort(arr):

n = len(arr)

for i in range(n):

for j in range(n -i -1):

if arr[j] > arr[j+1]:

arr[j],arr[j+1] = arr[j+1],arr[j]

return arr

numbers = [2,4,1,7,8,3]

print("the sorted numbers:", bubble_sort(numbers))

2.remove duplicate numbers:

def duplicate_num(arr):

unique_list = []

for num in arr:

if num not in unique_list:

unique_list.append(num)

return unique_list

numbers = [1,1,2,2,3,4,5,5,5,6]

print("the correct numbers after remove",duplicate_num(numbers))

3.list

my_list = [10, 20, 30, 40]

my_list.append(50) # Adds 50 to the list

my_list.remove(20) # Removes 20
print(my_list) # Output: [10, 30, 40, 50]

4.tuple

my_tuple = (10, 20, 30, 40)

print(my_tuple[1]) # Output: 20

5.dictionary

my_dict = {"name": "John", "age": 25, "city": "New York"}

print(my_dict)

{'name': 'John', 'age': 25, 'city': 'New York'}

print(my_dict["name"]) # Output: John

print(my_dict.get("age")) # Output: 25

practice:

list = [10,20,30,40]

list.append(50)

print(list)

tuple = (10,20,30)

print(tuple[1])

dict = {"name" : "pandu", "class" : "b.tech","roll-no" : "26"}

print(dict['name'])

6.even and odd count

numbers = [1,2,3,4,5,6,7,8]

even_count = 0

odd_count = 0

for num in numbers:

if num % 2 == 0 :

even_count += 1
else

odd_count += 1

print("even numbers",even_count)

print("odd numbers",odd_count)

7.print even and odd

numbers = [1,2,3,4,5,6,7,8]

even_numbers = []

odd_numbers = []

for num in numbers:

if num % 2 == 0 :

even_numbers.append(num)

else:

odd_numbers.append(num)

print("even numbers",even_numbers)

print("odd numbers",odd_numbers)

8.set (remove duplicate direct)

numbers = set([1, 2, 3, 4, 5, 5, 6, 6])

print(numbers)

Feature list.append() set.add()

Duplicates Allowed ❌ Not Allowed

Ordering Ordered ❌ Unordered

Method append(item) add(item)


numbers = [1, 2, 3, 4, 5]

skip = 3 # Number to skip

for num in numbers:

if num == skip:

continue # Skip this number

print(num,end = " ")

You might also like