Programming in Python : Dictionaries

Dictionaries are another data type in python. It can be think as a unordered collection of keys associated with values. The keys can be numbers, strings, or tuples. An empty dictionary is denoted by ‘{ }’. We can view all keys by simply calling keys(). The dict() constructor is using to build a dictionary.

>>> dic = {‘one’:1,’two’:2,’three’:3,’four’:4,’five’:5}

>>> dic
{‘four’: 4, ‘three’: 3, ‘five’: 5, ‘two’: 2, ‘one’: 1}

>>> dic[‘six’] = 6

>>> dic
{‘six’: 6, ‘three’: 3, ‘two’: 2, ‘four’: 4, ‘five’: 5, ‘one’: 1}

>>> del dic[‘five’]

>>> dic
{‘six’: 6, ‘three’: 3, ‘two’: 2, ‘four’: 4, ‘one’: 1}

>>> dic[‘seven’] = 7

>>> dic
{‘seven’: 7, ‘six’: 6, ‘three’: 3, ‘two’: 2, ‘four’: 4, ‘one’: 1}

>>> dic.key()

Traceback (most recent call last):
File “”, line 1, in
AttributeError: ‘dict’ object has no attribute ‘key’

>>> dic.keys()
[‘seven’, ‘six’, ‘three’, ‘two’, ‘four’, ‘one’]

>>> dic[‘seven’]
7
>>> dic[‘one’]
1

>>> ‘one’ in dic
True

>>> ‘three’ in dic
True

>>> ‘kjkk’ in dic
False

>>> del dic[‘one’]

>>> dic
{‘seven’: 7, ‘six’: 6, ‘three’: 3, ‘two’: 2, ‘four’: 4}

>>> dic.keys()
[‘seven’, ‘six’, ‘three’, ‘two’, ‘four’]

>>> dic.values()
[7, 6, 3, 2, 4]

>>> a=[(‘aass’,23),(‘iiii’,56),(‘dftj’,38)]

>>> a
[(‘aass’, 23), (‘iiii’, 56), (‘dftj’, 38)]

>>> dict(a)
{‘aass’: 23, ‘iiii’: 56, ‘dftj’: 38}

>>> b = dict(a)

>>> b
{‘aass’: 23, ‘iiii’: 56, ‘dftj’: 38}

>>> dict(jhjkhk = 433,jkhjkhk = 3434,iuijmkm = 344343)
{‘jkhjkhk’: 3434, ‘jhjkhk’: 433, ‘iuijmkm’: 344343}

>>> c = dict(jhjkhk = 433,jkhjkhk = 3434,iuijmkm = 344343)

>>> c
{‘jkhjkhk’: 3434, ‘jhjkhk’: 433, ‘iuijmkm’: 344343}

Advertisements

Programming in Python : Sets

Set is a data type in python. It is an unordered set of elements without duplicates. We can test an element is a member of the set. Set operations can operate on unique letters as given below.

>>> list1 = [‘apple’,’mango’,’pineapple’,’mango’,’apple’,’orange’]

>>> fruit = set(list1)

>>> fruit
set([‘orange’,’mango’,’apple’,’pineapple’])

>>> ‘mango’ in fruit
True

>>> ‘grape’ in fruit
False

>>> a = (‘abhilash’)

>>> a
‘abhilash’

>>> b = (‘abhijith’)

>>> b
‘abhijith’

>>> set(a)
set([‘a’, ‘b’, ‘i’, ‘h’, ‘l’, ‘s’])

>>> set(b)
set([‘a’, ‘b’, ‘i’, ‘h’, ‘j’, ‘t’])

>>> c = set(a)

>>> d = set(b)

>>> c
set([‘a’, ‘b’, ‘i’, ‘h’, ‘l’, ‘s’])

>>> d
set([‘a’, ‘b’, ‘i’, ‘h’, ‘j’, ‘t’])

>>> c-d
set([‘s’, ‘l’])

>>> d-c
set([‘j’, ‘t’])

>>> c |d
set([‘a’, ‘b’, ‘i’, ‘h’, ‘j’, ‘l’, ‘s’, ‘t’])

>>> c & d
set([‘a’, ‘i’, ‘b’, ‘h’])

>>> c ^ d
set([‘s’, ‘t’, ‘j’, ‘l’])

Programming in python: Tuples and sequences

Tuples consists of elements that are enclosed in parenthesis. The empty tuples is represented by ‘()’. The single element tuple is ending with a comma.

>>> t = (‘one’,’two’,’three’)

>>> t
(‘one’,’two’,’three’)

>>> e = ()

>>> e
()

>>> len(e)
0

>>> single = (‘one’,)

>>> single
(‘one’,)

>>> len(single)
1

The statement t = 1223,5676,’one’,’two’ is an example of tuple packing. The elements are packed together in a tuple. The sequence unpacking is also possible. ex: a,b,c,d = t

>>> t = 1223,5676,’one’,’two’

>>> t
(1223,5676,’one’,’two’)

>>> a,b,c,d = t

>>> a
1223

>>> d
‘two’

Programming in Python : Lists

The list data type in python allows more operations compared to the other data types. The items in the lists are enclosed by brackets ‘[]’. It may contain numbers, srtings, tuples or lists (nesting of lists).

>>> list1 = [1,2,3,’how’,’are’,’you’]

>>> list1
[1, 2, 3, ‘how’, ‘are’, ‘you’]

>>> list1[3]
‘how’

>>> list1[3:]
[‘how’, ‘are’, ‘you’]

>>> list1[:2]
[1, 2]

>>> list1[:2] = ‘and’

>>> list1
[‘a’, ‘n’, ‘d’, 3, ‘how’, ‘are’, ‘you’]

>>> list1[:2] = [‘and’]

>>> list1
[‘and’, 3, ‘how’, ‘are’, ‘you’]

>>> list1 = [12,34,56,’s’,’y’]

>>> list1
[12, 34, 56, ‘s’, ‘y’]

>>> list2 = [22,’22’]

We can add members of one list to the members of another list.
>>> list1[3:3] = list2

>>> list1
[12, 34, 56, 22, ’22’, ‘s’, ‘y’]

>>> list3 = [33,list2,’33’]

The following list is nested.
>>> list3
[33, [22, ’22’], ’33’]

>>> list3[1][1]
’22’

>>> list3[1][0]
22

The del statement :

It is used to remove an element from a list when the index of that element is given. It can be used to clear the entire list or remove the variable.

Programming in Python : Strings

Strings are also manupulated by python. Strings are represented within single quotes or double quotes. We can assign a string to a variable, and printed by using ‘print’. Strings can be surrounded by triple quotes “””. If there is \n the end of line, is echoed in the output.

>>> “Hello”+”world”
‘Helloworld’

>>> a=’how are you?’

>>> print a
how are you?

The slice notation :

The slice notation is used to return the elements of a list or characters of a string as specified by the indices.

>>> string1 = ‘hello world’

>>> string1
‘hello world’

>>> string1[0:1]
‘h’

>>> string1[0:4]
‘hell’

>>> string1[2:4]
‘ll’

>>> string1[:4]
‘hell’

>>> string1[3:]
‘lo world’

>>> string1[:0] = ‘Hai’
Traceback (most recent call last):
File “”, line 1, in
TypeError: ‘str’ object does not support item assignment

>>> string1[:0] + ‘Hai’
‘Hai’

>>> string1
‘hello world’

>>> string1[:] + ‘Hai’
‘hello worldHai’

>>> ‘Hai’ + string1[:]
‘Haihello world’

The negative indices are using to indicate from the right to the begining of the string.

>>> string1[-1]
‘d’

>>> string1[-4:-1]
‘orl’

>>> string1[-4:-2]
‘or’

>>> string1[-4:0]

>>> string1[-1:-4]

>>> string1[-11:-1]
‘hello worl’

Programming in Python : Arithmetic operations in python

Python is a functional programming language. Arithmetic operations are simply done as shown below in python interactive mode.

>>> 3+5
8

>>> (10+40)/10
5
A value can be assigned to sevaral variables simultaneously. Complex numbers are also supported. Imaginary numbers are written with a suffix j or J. In a complex number z, z.real and z.imag returns real and imaginary parts respectively. In interactive mode the last printed character is assigned to a variable’_’.

>>> x = y = z = 0

>>> -1j * 1j
(1+0j)

>>> a=678

>>> a*23
15594

>>> print _
15594

>>> _/22
708

Programming in Unix Environment : Using the shell

Command line structure :

A command usually ends with a newline but a semicolon ‘;’ is also a command terminator. Parentheses can be used to group commands. Data flowing through a pipe can be trapped and placed in a file by using ‘tee’ command. tee copies its input to the file and its output. Another command terminator is ‘&’ ampersand, which tells the shell not to wait for the command to complete, and we can enter another command to execute. The ‘sleep’ command waits the specified number of seconds before exiting. Here shows an example in which the the command after ampersand is ‘who’ it executes first and date executes after five seconds.

$ (date ; who) | wc
$ (who ; date) | tee file | wc
$ long-running-command &
$ sleep 5
$ (sleep 5 ; date) & who

Metacharacters :

The way to specify the metacharacters without its meaning – within single quotes.

$ echo ‘**’