-
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, you can use arrays with the array
module when working with data of the same type.
While lists are more common, arrays are useful for performance in large datasets.
import array
import array
numbers = array.array("i", [1, 2, 3, 4])
print(numbers)
Note: "i"
stands for integer. Other type codes include:
Code | Type |
---|---|
'i' | Integer |
'f' | Float |
'd' | Double |
'u' | Unicode char |
print(numbers[0]) # 1
print(numbers[2]) # 3
for num in numbers:
print(num)
numbers.append(5) # Add at end
numbers.insert(1, 10) # Insert at index 1
numbers.remove(2) # Remove value
numbers.pop() # Remove last item
print(len(numbers)) # 5
numbers[0] = 100
Method | Description |
---|---|
append() |
Adds element to the end |
insert() |
Adds at specified index |
remove() |
Removes value |
pop() |
Removes last element |
reverse() |
Reverses the array |
extend() |
Adds elements from another array |
Practice creating arrays, adding/removing elements, and using array methods.
Q1. Write a Python program to create an array of integers with values 1 to 5 using the array
module.
Q2. Write a Python program to print the third element of the array.
Q3. Write a Python program to append the number 6 to the array using append()
.
Q4. Write a Python program to insert the number 10 at index 2 using insert()
.
Q5. Write a Python program to remove the number 2 from the array using remove()
.
Q6. Write a Python program to pop the last item from the array using pop()
.
Q7. Write a Python program to replace the first element of the array with 99.
Q8. Write a Python program to loop through the array and print each number.
Q9. Write a Python program to find the length of the array using len()
.
Q10. Write a Python program to use reverse()
to reverse the array order.
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