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

List python programs algorithms

Uploaded by

laila godson
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

List python programs algorithms

Uploaded by

laila godson
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Palindrome

word = input("Enter a word: ")


is_palindrome = True
for i in range(len(word) // 2):
if word[i] != word[-i - 1]:
is_palindrome = False
Break
if is_palindrome:
print(word, "is a palindrome")
else:
print(word, "is not a palindrome")
Prime
n=int(input("Enter the value:"))
prime=0
for i in range(2,n):
if(n%i==0):
prime=1
break
if prime==0:
print("Prime number")
else:
print("not a prime number")
Write a python program to create a list of numbers in
the range 1 to 10. then delete all the even numbers
from the list and print the final list

•Create an empty list: Initialize an empty list to store the


numbers.
•Use a loop (e.g., for loop) to iterate from 1 to 10 and append each
number to the list.
•Iterate and remove even numbers:
•Iterate through the list using a loop.
•For each number, check if it's even (divisible by 2).
•If it's even, remove it from the list using the remove() method.
•Print the final list: Print the modified list to the console.
numbers = []
for i in range(1, 11):
numbers.append(i)
for num in numbers[:]: # Create a copy to avoid
index issues during removal
if num % 2 == 0:
numbers.remove(num)
print(numbers)
Write a python program to print index at which a
particular value exists. If the value exists at
multiple locations in the list, then print all the
indices. Also, count the number of times that value
is repeated in the list
1. Get a number from the user as input.
2. Initialize: Set a counter count and index i to 0.
3. Iterate: While i is less than the length of the
list:
4. Check if the current number in the list
matches the searched number.
5. If it matches, print its position and increase
the count.
6. Move to the next number in the list.
7. Repeat steps 3-5 until the end of the list.
8. Print the final count of matches.
Write a program that creates a list
of words by combining the words in
two individual lists.
1. Create an empty list to store the results.
2. Iterate over the first list: Take the first word from the first
list.
3. Iterate over the second list
4. Combine it with each word from the second list, adding
each combination to the result list.
5. Repeat step 4 for the second word from the first list.
6. Print the final list of combined words.
Write a program that forms a list of
first character of every word in
another list
• Create a new, empty list.
• Go through each word in the given list.
• Take the first letter of the current word.
• Add the first letter to the new list.
• Repeat steps 2-4 for all words in the list.
• Print the new list containing all the first letters.
Write a program to remove all duplicates from
a list, Reverse the list and print its values.
Part 1: Removing Duplicates
1.Iterate over the list:
•For each number num in the list:
•Iterate over the remaining part of the list:
•If the current number val is equal to num:
•Remove val from the list.
•Adjust the index i to avoid skipping elements.
Part 2: Appending Numbers
2.Iterate over the remaining part of the list:
•For each index j from i+1 to the end of the list:
•Append the value of j to the list.
Part 3: Reversing the List
3.Reverse the list:
•Reverse the order of the elements in the list.
Write a program that creates a list of 10
random integers. Then create two lists: Odd
List and Even List that has all odd and
even values in the list respectively
•Generate Random Numbers:
•Create an empty list num_list.
•Generate 10 random integers between 1 and 100.
•Append each random number to the num_list.
•Separate Even and Odd Numbers:
•Create two empty lists: even_list and odd_list.
•Iterate through each number in the num_list:
•If the number is even:
•Append it to the even_list.
•Else:
•Append it to the odd_list.
•Print the Lists:
•Print the original list, the list of even numbers, and the list of odd numbers.
• Write a program to create a list of first 20 odd numbers using the
shortcut method.
odd =[ 2i + 1 for i in range(20)]
print(odd)
• OUTPUT
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39]
Write a program that has a list of both positive and
negative numbers. Create another list using filter ()
that has only positive values.

•Create a function to check for positive numbers:


•Make a function that checks if a given number is positive.
•If the number is positive, the function returns the number.
•Make a list of numbers:
•Create a list with both positive and negative numbers.
•Filter out negative numbers:
•Use a special function to go through the list of numbers.
•For each number, check if it's positive using the function we created.
•Keep only the positive numbers in a new list.
•Print the positive numbers:
•Show the new list containing only the positive numbers.
Write a program to add two matrices (using nested
lists).
•Initialize Matrices:
•Create two matrices, X and Y, of the same dimensions.
•Create a result matrix result with the same dimensions as X and Y, filled with
zeros.
•Iterate Over Elements:
•Iterate over each row i of the matrices:
•Iterate over each column j of the matrices:
•Add the corresponding elements from X and Y at positions (i, j).
•Store the sum in the corresponding position of the result
matrix.
•Print the Result Matrix:
•Iterate over each row r of the result matrix.
•Print each row.
Write a program to find the median of a list of numbers.
•Input:
•Prompt the user to enter the number of elements n.
•Create an empty list List.
•For each number i from 0 to n-1:
•Prompt the user to enter a number.
•Append the entered number to the List.
•Sort the List:
•Sort the List in ascending order.
•Calculate the Median:
•Determine if n is even or odd.
•If n is odd:
•The median is the middle element of the sorted list.
•Calculate the index of the middle element as i // 2.
•Print the median.
•If n is even:
•The median is the average of the two middle elements.
•Calculate the indices of the middle elements as i // 2 and i + i // 2.
•Calculate the average of these two elements and print it as the median.
1.Input:
•Prompt the user to input the x and y coordinates 4.Print the Distance:
•Print the calculated distance.
of the starting point (x1, y1).
In simpler terms:
•Prompt the user to input the x and y coordinates
5.Get the starting and ending points:
of the ending point (x2, y2). •Ask the user for the x and y coordinates of the
2.Store Coordinates in Lists: starting point.
•Create two lists, p1 and p2, to store the coordinates. •Ask the user for the x and y coordinates of the
•Append the x and y coordinates of the starting point to p1. ending point.
•Append the x and y coordinates of the ending point to p2. 2.Calculate the distance:
3.Calculate Distance: •Find the difference between the x-coordinates.
•Use the distance formula: distance = sqrt((x2 - x1)^2 + (y2 - •Find the difference between the y-coordinates.
y1)^2). •Square both differences.
•Calculate the difference in x-coordinates: x2 - x1. •Add the squared differences.
•Calculate the difference in y-coordinates: y2 - y1. •Take the square root of the sum.
•Square the differences. 3.Show the result:
•Add the squared differences. •Print the calculated distance.
•Take the square root of the sum.
Write a program to calculate distance between
two points.
•Input:
•Prompt the user to input the x and y coordinates of the starting point (x1, y1).
•Prompt the user to input the x and y coordinates of the ending point (x2, y2).
•Store Coordinates in Lists:
•Create two lists, p1 and p2, to store the coordinates.
•Append the x and y coordinates of the starting point to p1.
•Append the x and y coordinates of the ending point to p2.
•Calculate Distance:
•Use the distance formula: distance = sqrt((x2 - x1)^2 + (y2 -
y1)^2).
•Calculate the difference in x-coordinates: x2 - x1.
•Calculate the difference in y-coordinates: y2 - y1.
•Square the differences.
•Add the squared differences.
•Take the square root of the sum.
•Print the Distance:
•Print the calculated distance.

You might also like