🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Arrays in Python are a collection of items stored at contiguous memory locations. They are used to store multiple values of the same type, making it easier to manage large sets of data. In Python, arrays are provided by the array module.
Table of Contents
- Define Array and Key Points
- Create Array
- Access Array Items
- Change Array Items
- Add Array Items
- Remove Array Items
- Loop Arrays
- Array Methods
- Conclusion
Key Points
An array is a collection of items of the same type.
Key points about arrays:
- Ordered: Items have a defined order.
- Mutable: Items can be changed.
- Indexed: Each item is accessible using its index.
- Homogeneous: All items are of the same type.
1. Create Array
To create an array, you need to import the array module. The array module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers.
Example
import array
# Creating an array of integers
int_array = array.array('i', [1, 2, 3, 4, 5])
print(int_array) # Output: array('i', [1, 2, 3, 4, 5])
# Creating an array of floating point numbers
float_array = array.array('d', [1.1, 2.2, 3.3, 4.4])
print(float_array) # Output: array('d', [1.1, 2.2, 3.3, 4.4])
2. Access Array Items
Array items can be accessed using their index. Indexing starts at 0.
Example
# Accessing array items
print(int_array[0]) # Output: 1
print(int_array[3]) # Output: 4
# Negative indexing
print(int_array[-1]) # Output: 5
print(int_array[-2]) # Output: 4
3. Change Array Items
Array items can be changed by assigning a new value to a specific index.
Example
# Changing array items
int_array[1] = 20
print(int_array) # Output: array('i', [1, 20, 3, 4, 5])
4. Add Array Items
New items can be added to an array using methods like append(), insert(), and extend().
Example
# Using append() to add a single item
int_array.append(6)
print(int_array) # Output: array('i', [1, 20, 3, 4, 5, 6])
# Using insert() to add an item at a specific position
int_array.insert(1, 10)
print(int_array) # Output: array('i', [1, 10, 20, 3, 4, 5, 6])
# Using extend() to add multiple items
int_array.extend([7, 8, 9])
print(int_array) # Output: array('i', [1, 10, 20, 3, 4, 5, 6, 7, 8, 9])
5. Remove Array Items
Items can be removed from an array using methods like remove(), pop(), and del.
Example
# Using remove() to remove a specific item
int_array.remove(10)
print(int_array) # Output: array('i', [1, 20, 3, 4, 5, 6, 7, 8, 9])
# Using pop() to remove an item at a specific index
popped_item = int_array.pop(1)
print(popped_item) # Output: 20
print(int_array) # Output: array('i', [1, 3, 4, 5, 6, 7, 8, 9])
# Using del to remove an item at a specific index
del int_array[2]
print(int_array) # Output: array('i', [1, 3, 5, 6, 7, 8, 9])
6. Loop Arrays
You can loop through the items in an array using a for loop.
Example
# Looping through an array
for item in int_array:
print(item)
# Output:
# 1
# 3
# 5
# 6
# 7
# 8
# 9
7. Array Methods
Python arrays have several built-in methods, including append(), extend(), insert(), remove(), pop(), index(), reverse(), and buffer_info().
Example
# Using various array methods
example_array = array.array('i', [1, 2, 3, 4, 5])
# append() method
example_array.append(6)
print(example_array) # Output: array('i', [1, 2, 3, 4, 5, 6])
# extend() method
example_array.extend([7, 8, 9])
print(example_array) # Output: array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9])
# insert() method
example_array.insert(0, 0)
print(example_array) # Output: array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# remove() method
example_array.remove(5)
print(example_array) # Output: array('i', [0, 1, 2, 3, 4, 6, 7, 8, 9])
# pop() method
popped_item = example_array.pop()
print(popped_item) # Output: 9
print(example_array) # Output: array('i', [0, 1, 2, 3, 4, 6, 7, 8])
# index() method
index = example_array.index(4)
print(index) # Output: 4
# reverse() method
example_array.reverse()
print(example_array) # Output: array('i', [8, 7, 6, 4, 3, 2, 1, 0])
# buffer_info() method
buffer_info = example_array.buffer_info()
print(buffer_info) # Output: (address, length)
Conclusion
Python arrays are useful for storing collections of items of the same type. Understanding how to create, access, modify, and perform operations on arrays is essential for effective programming. This tutorial covered various aspects of arrays, including creating, accessing, changing, and removing items, as well as advanced topics like looping and using built-in methods. By mastering these concepts, you can efficiently work with arrays in your Python programs.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment