-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
In Python, both lists and arrays can be used to store multiple items in a single variable. However, they serve slightly different purposes.
A list is a built-in, flexible data structure that can store any data type — integers, strings, floats, or even other lists.
my_list = [1, 2, 3, "hello", 4.5]
print(my_list)
✅ Output: [1, 2, 3, 'hello', 4.5]
Ordered
Mutable (you can change items)
Allows duplicates
Can store mixed types
print(my_list[0]) # First item
print(my_list[-1]) # Last item
my_list[1] = 99
print(my_list) # [1, 99, 3, 'hello', 4.5]
my_list.append("new") # Add at end
my_list.insert(2, "mid") # Add at index
my_list.remove("hello") # Remove by value
my_list.pop() # Remove last item
Python also has an array module, but it only stores items of the same type (like all integers).
import array
a = array.array('i', [1, 2, 3])
print(a)
✅ 'i'
stands for integer array.
Use arrays for performance and memory efficiency when dealing with large numeric datasets.
Type Code | C Type | Python Type |
---|---|---|
'i' |
int | int |
'f' |
float | float |
'u' |
Py_UNICODE | Unicode |
Feature | List | Array (array module) |
---|---|---|
Data Type | Mixed | Same type only |
Built-in | Yes | Need to import module |
Use Case | General-purpose | Numeric processing |
Performance | Slower | Faster for numbers |
Q1. Write a Python program to create a list of your favorite fruits and print it.
Q2. Write a Python program to replace the 2nd element in a list with a new value.
Q3. Write a Python program to append a new number to a list.
Q4. Write a Python program to use pop()
to remove the last element from a list.
Q5. Write a Python program to check if a value exists in a list using the in
keyword.
Q6. Write a Python program to create an integer array using array.array
.
Q7. Write a Python program to use a loop to print all values in an array.
Q8. Write a Python program to insert a value at index 1 in a list.
Q9. Write a Python program to use remove()
to delete a value from a list.
Q10. Write a Python program to count how many times an element appears in a list using count()
.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms