List and Tuple in Python
List and Tuple in Python
This articles teaches you the following topics:
- How to use lists
- How to use tuples
Introduction:
- Python provides different types of data structures as sequences.
- In a sequence, there are more than one values and each value has its own index.
- The first value will have an index 0, the second value will have index 1 and so on.
- These indices are used to access a particular value in the sequence.
- Python offers different types of sequences but we will discuss two of them, lists and tuples.
Lists
- Lists are the most important type of sequence being used in Python.
- It is a collection of same or different type of objects.
- These objects are separated by commas to distinguish from each other enclosed in square brackets.
Tuples
- Tuples are almost same as Lists but the main difference them is that objects defined in tuples cannot be changed while they can be changed in lists.
- A minor difference is that lists are enclosed in square brackets while tuples are enclosed parenthesis (round brackets).
Activity 1:
Accept 5 values from user and store them in a list. Display all the values (objects) of the list.
Solution:
- Create a new Python file from Python Shell and type the following
- Run the code by pressing F5.
You will get the following output.
Activity 2:
Repeat the above code by accepting 5 integer values from user. Store these values in a list and display the sum of given values.
Solution:
- Create a new Python file from Python Shell and type the following
- Run the code by pressing F5.
You will get the following output.
Activity 3:
Accept 5 integer values from user. Store these values in a list and display the list in ascending order.
Solution:
- Create a new Python file from Python Shell and type the following
- Run the code by pressing F5.
You will get the following output.
Activity 4:
Accept two lists from user and display their join.
Solution:
- Create a new Python file from Python Shell and type the following
- Run the code by pressing F5.
You will get the following output.
Activity 5:
Write a Python code to accept a list from user and find a required element in it.
Solution:
- Create a new Python file from Python Shell and type the following
- Run the code by pressing F5.
You will get the following output.
If we run the program again and enter 55 to find in the list then the output will be as below.
Activity 6:
Write a Python code to accept a tuple from user and display the maximum and minimum value of the tuple.
Solution:
- Create a new Python file from Python Shell and type the following
- Run the code by pressing F5.
You will get the following output.
Home Activities:
Activity 1:
Create two lists based on the user values. Merge both the lists and display in sorted order.
Activity 2:
Repeat the above activity to find the smallest and largest element of the list. (Suppose all the elements are integer values)
Activity 3:
Repeat the above activity to find the index of the smallest and largest element of the list. (Suppose all the elements are integer values)
Activity 4:
Accept a list of 10 integer values from user and display the list in descending order.
Related links
Microsoft word Installation of Python List and Tuple in python
Power Point Variable and statement String in python
Microsoft Excel If else statement in python functions in python
Microsoft Access loops in python Ubunto operating system
At Cui tutorial, courses, past papers and final year projects
#tutorial #cui #pastpaper #courses