Show List

Python Arrays, Lists, and Dictionaries

Arrays, Lists, and Dictionaries are all data structures used to store collections of items in Python. Here's a brief explanation of each, along with examples.

Arrays: An array is a collection of items stored at contiguous memory locations. The items can be of any data type, including numbers, strings, and other objects. In Python, arrays can be created using the "array" module.

Here's an example:

php
Copy code
import array a = array.array("i", [1, 2, 3, 4, 5]) print("Array: ", a)

Lists: Lists are similar to arrays, but they are more flexible and can store items of different data types. Lists are declared with square brackets [] in Python.

Here's an example:

less
Copy code
a = [1, 2, 3, 4, 5] print("List: ", a)

Dictionaries: A dictionary is a data structure that stores a collection of key-value pairs. Each key is unique and is used to access the corresponding value. Dictionaries are declared with curly braces {} in Python.

Here's an example:

python
Copy code
a = {"name": "John", "age": 32, "city": "New York"} print("Dictionary: ", a)

In the example, the keys are "name", "age", and "city", and the corresponding values are "John", 32, and "New York". To access a value, you can use the square bracket notation and provide the key:

python
Copy code
print("Name: ", a["name"])

This will print "Name: John".


    Leave a Comment


  • captcha text