Python List Programs with Output

 

                                                                    Introduction  

Python List is one of the most important data structures in Python. Lists are used to store multiple values in a single variable. This article explains important Python list programs with output for exams.


                                                           What is a List in Python?  

A list is an ordered and mutable collection of elements. Lists can store different data types like integers, strings, and floats.


Example of a List  

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


Program 1: Find the length of a list  

my_list = [10, 20, 30, 40]  

print(len(my_list))  


Output:  

4


Program 2: Add an element to a list  

my_list = [1, 2, 3]  

my_list.append(4)  

print(my_list)  


Output:  

[1, 2, 3, 4]


Program 3: Remove an element from a list  

my_list = [5, 10, 15, 20]  

my_list.remove(10)  

print(my_list)  


Output:  

[5, 15, 20]


Program 4: Find the largest element in a list  

numbers = [12, 45, 7, 89, 23]  

print(max(numbers))  


Output:  

89


Program 5: Reverse a list  

numbers = [1, 2, 3, 4]  

numbers.reverse()  

print(numbers)  


Output:  

[4, 3, 2, 1]


                                                  Advantages of Python Lists  

- Easy to use  

- Can store multiple data types  

- Dynamic in size  


                                                        Conclusion  

Python lists are widely used in programming. Understanding list programs is very helpful for exams and practical coding.


Comments

Popular posts from this blog

What is DNS? Simple Explanation for Engineering Students

Dijkstra’s Algorithm – Step by Step Explanation with Example

Difference Between Stack and Queue