Repository navigation
Expand file tree
/
Copy path063-Clear-List.py
More file actions
70 lines (56 loc) · 2.35 KB
/
Copy path063-Clear-List.py
File metadata and controls
70 lines (56 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
===============================================================================
File Name : 63-Clear-List.py
Description : Clear a List in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The clear() method is used to remove all elements from a list. After calling
this method, the list becomes empty, but the list object still exists.
Syntax:
list_name.clear()
Example:
numbers.clear()
"""
# -----------------------------------------------------------------------------
# Creating a list of integers.
# -----------------------------------------------------------------------------
numbers = [10, 20, 30, 40, 50]
# -----------------------------------------------------------------------------
# Displaying the original list.
# -----------------------------------------------------------------------------
print("Original List :", numbers)
# Output:
# Original List : [10, 20, 30, 40, 50]
print()
# -----------------------------------------------------------------------------
# Displaying the number of elements before clearing the list.
# -----------------------------------------------------------------------------
print("Elements Before Clear :", len(numbers))
# Output:
# Elements Before Clear : 5
print()
# -----------------------------------------------------------------------------
# Removing all elements from the list.
# -----------------------------------------------------------------------------
numbers.clear()
# -----------------------------------------------------------------------------
# Displaying the list after clearing.
# -----------------------------------------------------------------------------
print("List After clear() :", numbers)
# Output:
# List After clear() : []
print()
# -----------------------------------------------------------------------------
# Displaying the number of elements after clearing the list.
# -----------------------------------------------------------------------------
print("Elements After Clear :", len(numbers))
# Output:
# Elements After Clear : 0
print()
# -----------------------------------------------------------------------------
# Displaying the type of the list.
# -----------------------------------------------------------------------------
print("Type of List :", type(numbers))
# Output:
# Type of List : <class 'list'>