Additional practice for lists and loops

  1. Write a Python program to sum all the items in a list.

  2. Write a Python program to get the largest number from a list.

  3. Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings.

  4. Write a Python program to remove duplicates from a list.

  5. Write a Python function that takes two lists and returns True if they have at least one common member.

  6. Write a Python program to print a specified list after removing the 0th, 4th and 5th elements. Sample List : ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow'] Expected Output : ['Green', 'White', 'Black']

  7. Write a Python program to print the numbers of a specified list after removing even numbers from it.

  8. Write a Python program to shuffle and print a specified list. Very useful for things like card games. Imagine if this was your list

cards = ['Ace of clubs', '2 of clubs', '3 of clubs', '4 of clubs', '5 of clubs', '6 of clubs', '7 of clubs', '8 of clubs', '9 of clubs', '10 of clubs', 'Jack of clubs', 'Queen of clubs', 'King of clubs', 'Ace of diamonds', '2 of diamonds', '3 of diamonds', '4 of diamonds', '5 of diamonds', '6 of diamonds', '7 of diamonds', '8 of diamonds', '9 of diamonds', '10 of diamonds', 'Jack of diamonds', 'Queen of diamonds', 'King of diamonds', 'Ace of hearts', '2 of hearts', '3 of hearts', '4 of hearts', '5 of hearts', '6 of hearts', '7 of hearts', '8 of hearts', '9 of hearts', '10 of hearts', 'Jack of hearts', 'Queen of hearts', 'King of hearts', 'Ace of spades', '2 of spades', '3 of spades', '4 of spades', '5 of spades', '6 of spades', '7 of spades', '8 of spades', '9 of spades', '10 of spades', 'Jack of spades', 'Queen of spades', 'King of spades']
  1. Write a Python program to generate and print a list of first and last 5 elements where the values are square of numbers between 1 and 30 (both included).

  2. Write a Python program to generate all permutations of a list in Python. Go to the editor Click me to see the sample solution

  3. Write a Python program to get the difference between the two lists.

  4. Write a Python program to append a list to the second list.

  5. Write a Python program to select an item randomly from a list, which is then removed from the original list so it can’t be re-drawn (just like a deck of cards scenario)

  6. Write a Python program to get the frequency of the elements in a list.

  7. Write a Python program to check whether a list contains a sublist. Go to the editor Click me to see the sample solution

  8. Write a Python program to generate all sublists of a list. Go to the editor Click me to see the sample solution

  9. Write a Python program for computing primes upto 1000.

Hint: You will probably want a for-loop inside another for-loop.

Note: One possible strategy is known In mathematics as the Sieve of Eratosthenes which is illustrated below.

Sieve Of Eratosthenes Animation

  1. Write a Python program to find common items from two lists.

  2. Write a Python program to implement Ceasars Cipher which was famously one of the first methods of sending cryptic messages. It involves using a simple letter substitution. The example below moves each letter by 3 positions. (Remember to ensure that xyz wrap around to abc)

message in:  "attack the castle"
message out: "dwwdfn wkh fdvwoh"
  1. Building on the deck of cards scenarios again. Can you start with a list of a deck of cards, randomise it, and then draw 5 cards for each of 5 players, removing each card from the main deck as you do. So you will start with one “master” list and end up with the leftovers in the master list, and 5 lists of 5 cards each.

Sources

Questions sourced from: https://www.w3resource.com/python-exercises/list/ (note: please don’t just steal the solutions from this site. Most of their solutions use techniques I haven’t taught you yet anyway, so would (a) confuse you and (b) be a give away you didn’t do it yourself and (c) defeat the point of learning by solving. Instead, I suggest you work with your neighbours and try to figure out the solutions together!)

Sieve Of Eratosthenes Animation By SKopp at German Wikipedia - Own work, Original image at Image:Animation_Sieve_of_Eratosth.gif, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=2810935