Arrays in Python
Variables can be organized in a particular pattern called Arrays .
There are four basic types of arrays :
LIST :
#creating a list
>>> colors = ["red","green","blue",3]
#printing the contents of the list
>>> colors
['red', 'green', 'blue', 3]
# indexed access
>>> colors[0]
'red'
# 0 is the first element , -1 is the last !
>>> colors[-1]
3
# Starting from 0 print two elements
>>> colors[0:2]
['red', 'green']
>>> colors[-2]
'blue'
# Error !
>>> colors[-9]
Traceback (most recent call last): File "", line 1, in ? colors[-9] IndexError: list index out of range
# Appending an element to the list >>> colors.append('white')
>>> colors
['red', 'green', 'blue', 3, 'white']
# Most common mistake , while using len()
>>> colors.len()
Traceback (most recent call last): File "", line 1, in ? colors.len() AttributeError: 'list' object has no attribute 'len' # The correct way
>>> len(colors)
5
# Most common error while deleting
>>> del colors[len(colors)]
Traceback (most recent call last): File "", line 1, in ? del colors[len(colors)] IndexError: list assignment index out of range
# The correct way
>>> del colors[len(colors)-1]
>>> colors
['red', 'green', 'blue', 3]
# Inserting an element
>>> colors.insert(0,'black')
>>> colors
['black', 'red', 'green', 'blue', 3]
TUPLES :
Tuples are twins brother of List , they don't obey to you unlike his brother List , they are not flexible , you can't modify them once defined.
To define a tuple we use commas "," ; Parenthesis may also be used to make it more clear.
Examples :
>>> binary = '1','0'
# or
>>> binary = ('1','0')
>>> binary
# for both the output is
>>> ('1', '0')
Rather interesting , we can group them up to form meaning full tuple
>>> ego = 'i','me','myself',('bad','very bad','worst')
>>> ego
# o/p will be
('i', 'me', 'myself', ('bad', 'very bad', 'worst'))
DICTIONARIES :
Dictionaries cousins of lists, and they are mutable i.e once declared and defined can be modified.
Properties of dictionaries :
# creating
>>> places = {"Bangalore": "India", "NY" : "US","London":"UK"}
# displaying
>>> places
{'NY': 'US', 'Bangalore': 'India', 'London': 'UK'}
# retrieving
>>> places["Bangalore"]
'India'
# note it's case sensitive , therefore a KeyError
>>> places["bangalore"]
Traceback (most recent call last):
File "", line 1, in
KeyError: 'bangalore'
# resetting the value of the key
>>> places["NY"] = "India"
>>> places
{'NY': 'India', 'Bangalore': 'India', 'London': 'UK'}
SETS :
An unordered "list" , which does not allow duplicate values are know as sets .
Properties of sets :
Examples :
>>> luckyNum = set([(1,3,5,7),'lucky',(0,0,13),'unlucky'])
>>> luckyNum
set([(0, 0, 13), 'unlucky', (1, 3, 5, 7), 'lucky'])
This was just a toddler on Arrays , there are loads more to do with them :)
There are four basic types of arrays :
- List
- Tuples
- Dicitionary
- Sets
LIST :
- List is an ordered collection of variables .
- A list is created using square brackets [ ] .
- List is indexed , so you can accesses the objects/elements in the list using them.
- len() , append() , insert () , del , are the few common functions which are used to operate on the list to find its length , append new elements , insert elements and delete the whole list or specific index respectively .
#creating a list
>>> colors = ["red","green","blue",3]
#printing the contents of the list
>>> colors
['red', 'green', 'blue', 3]
# indexed access
>>> colors[0]
'red'
# 0 is the first element , -1 is the last !
>>> colors[-1]
3
# Starting from 0 print two elements
>>> colors[0:2]
['red', 'green']
>>> colors[-2]
'blue'
# Error !
>>> colors[-9]
Traceback (most recent call last): File "", line 1, in ? colors[-9] IndexError: list index out of range
# Appending an element to the list >>> colors.append('white')
>>> colors
['red', 'green', 'blue', 3, 'white']
# Most common mistake , while using len()
>>> colors.len()
Traceback (most recent call last): File "", line 1, in ? colors.len() AttributeError: 'list' object has no attribute 'len' # The correct way
>>> len(colors)
5
# Most common error while deleting
>>> del colors[len(colors)]
Traceback (most recent call last): File "", line 1, in ? del colors[len(colors)] IndexError: list assignment index out of range
# The correct way
>>> del colors[len(colors)-1]
>>> colors
['red', 'green', 'blue', 3]
# Inserting an element
>>> colors.insert(0,'black')
>>> colors
['black', 'red', 'green', 'blue', 3]
TUPLES :
Tuples are twins brother of List , they don't obey to you unlike his brother List , they are not flexible , you can't modify them once defined.
To define a tuple we use commas "," ; Parenthesis may also be used to make it more clear.
Examples :
>>> binary = '1','0'
# or
>>> binary = ('1','0')
>>> binary
# for both the output is
>>> ('1', '0')
Rather interesting , we can group them up to form meaning full tuple
>>> ego = 'i','me','myself',('bad','very bad','worst')
>>> ego
# o/p will be
('i', 'me', 'myself', ('bad', 'very bad', 'worst'))
DICTIONARIES :
Dictionaries cousins of lists, and they are mutable i.e once declared and defined can be modified.
Properties of dictionaries :
- Elements in a dictionary are not bound to numbers.
- {Key:value} pair makes up an element of a dictionary.
- Call the key , you get the value.
# creating
>>> places = {"Bangalore": "India", "NY" : "US","London":"UK"}
# displaying
>>> places
{'NY': 'US', 'Bangalore': 'India', 'London': 'UK'}
# retrieving
>>> places["Bangalore"]
'India'
# note it's case sensitive , therefore a KeyError
>>> places["bangalore"]
Traceback (most recent call last):
File "", line 1, in
KeyError: 'bangalore'
# resetting the value of the key
>>> places["NY"] = "India"
>>> places
{'NY': 'India', 'Bangalore': 'India', 'London': 'UK'}
SETS :
An unordered "list" , which does not allow duplicate values are know as sets .
Properties of sets :
- Elements of a set are neither bound to a number nor to a keys.
- Are faster for huge number of items than a list or tuple and sets provide fast data manipulations.
Examples :
>>> luckyNum = set([(1,3,5,7),'lucky',(0,0,13),'unlucky'])
>>> luckyNum
set([(0, 0, 13), 'unlucky', (1, 3, 5, 7), 'lucky'])
This was just a toddler on Arrays , there are loads more to do with them :)
Recent blog posts
- watir-webdriver web inspector
- gem list to gemfile
- Packing ruby2.0 on debian.
- Made it into The Guinness Book!
- to_h in ruby 2.0
- Filter elements by pattern jQuery.
- Better HTML password fields for mobile ?
- Grayscale image when user offline
- nth-child CSS pseudo-class Christmas colors
- EventEmitter in nodejs