Practice List Programs
##Making a list:
colors = ['Red', 'Blue', 'Green', 'Black', 'White']
##Accessing elements:
# Getting the first element
first_col = colors[0]
# Getting the second element
second_col = colors[1]
# Getting the last element
newest_col = colors[-1]
##
# Changing an element
colors[0] = 'Yellow'
colors[-2] = 'Red'
##Adding elements:
# Adding an element to the end of the list
colors.append('Orange')
# Starting with an empty list
colors = []
colors.append('Red')
colors.append('Blue')
colors.append('Green')
# Inserting elements at a particular position
colors.insert(0, 'Violet')
colors.insert(2, 'Purple')
##Removing elements:
# Deleting an element by its position
del colors[-1]
# Removing an item by its value
colors.remove('Green')
##Popping elements:
# Pop the last item from a list
most_recent_col = colors.pop()
print(most_recent_col)
# Pop the first item in a list
first_col = colors.pop(0)
print(first_col)
##List length:
# Find the length of a list
num_colors = len(colors)
print("We have " + str(num_colors) + " colors.")
##Sorting a list:
# Sorting a list permanently
colors.sort()
# Sorting a list permanently in reverse alphabetical order
colors.sort(reverse=True)
# Sorting a list temporarily
print(sorted(colors))
print(sorted(colors, reverse=True))
# Reversing the order of a list
colors.reverse()
##Looping through a list:
# Printing all items in a list
for col in colors:
print(col)
# Printing a message for each item, and a separate message afterwards
for col in colors:
print("Welcome, " + col + "!")
print("Welcome, we're glad to see you all!")
##The range() function:
# Printing the numbers 0 to 2000
for num in range(2001):
print(num)
# Printing the numbers 1 to 2000
for num in range(1, 2001):
print(num)
# Making a list of numbers from 1 to a million
nums = list(range(1, 1000001))
##Simple statistics:
# Finding the minimum value in a list
nums = [23, 22, 44, 17, 77, 55, 1, 65, 82, 2]
num_min = min(nums)
# Finding the maximum value
nums = [23, 22, 44, 17, 77, 55, 1, 65, 82, 2]
num_max = max(nums)
# Finding the sum of all numbers
nums = [23, 22, 44, 17, 77, 55, 1, 65, 82, 2]
total_num = sum(nums)
##Slicing a list:
# Getting the first three items
colors = ['Red', 'Blue', 'Green', 'Black', 'White']
first_three = colors [:3]
# Getting the middle three items
middle_three = colors[1:4]
# Getting the last three items
last_three = colors[-3:]
##Copying a list:
# Making a copy of a list
colors = ['Red', 'Blue', 'Green', 'Black', 'White']
copy_of_colors = colors[:]
##List of Comprehensions:
# Using a loop to generate a list of square numbers
squr = []
for x in range(1, 11):
sq = x**2
squr.append(sq)
# Using a comprehension to generate a list of square numbers
squr = [x**2 for x in range(1, 11)]
# Using a loop to convert a list of names to upper case
colors = ['Red', 'Blue', 'Green', 'Black', 'White']
upper_cols = []
for cols in colors:
upper_cols.append(cols.upper())
# Using a comprehension to convert a list of names to upper case
colors = ['Red', 'Blue', 'Green', 'Black', 'White']
upper_cols = [cols.upper() for cols in colors]
ASSIGNMENTS
1. Write a Python program to sum all the items in a list.
refer solution here
2. Write a Python program to get the smallest number from a list.
refer solution here
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. Sample List : ['abc', 'xyx', '4554', 'abc'] Expected Result : 2
refer solution here
4. Write a Python program to get the powerset of a given iterable. Sample Output: Original list elements: [1, 2] Powerset of the said list: [(), (1,), (2,), (1, 2)]
refer solution here
Original list elements: [1, 2, 3, 4] Powerset of the said list: [(), (1,), (2,), (3,), (4,), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (1, 2, 3, 4)]
refer solution here
5. Write a Python program to check if all the elements of a list are included in another given list. List1 = [2,4,6,8,10] List2 = [4,2,10] Output: True
refer solution here
6. Write a Python program to initialize a list containing the numbers in the specified range where start and end are inclusive and the ratio between two terms is step. Returns an error if step equals 1. Sample Output: [1, 2, 4, 8, 16, 32, 64, 128, 256] [3, 6, 12, 24, 48, 96, 192] [1, 4, 16, 64, 256]
refer solution here
7. Program to input 5 subjects marks for 3 students and find the highest marks in each subject and also overall total marks
refer solution here