#!/usr/bin/python

'''
This program covers some basic stuff to learn python.
This message is part of multi line comments
'''

import sys
sys.stdout.write('.')

### Basics
 
x,y,z,first,second = 15,2,3,'second','first'
print(x,y,z,first,second)

### condition logic
x=15

if x<5 or (x>10 and x<25):
	print ("the value is ok")

if x<5 or 10<x<20:
	print ("the value is ok")

### conditional operator
a='FAIL'
value = ['red', 'green'][a == 'SUCCESS']
print (value)

### Loops:iterative logic

for i in [25,34,56,12,7]:
	print(i),

print ''
x=4
while x>=0:
	print(x),
	x=x-1

print ''
for value in range (2,5):
	print (value),

print ''

### Exception hanlding
try:
    t = x[0]
except TypeError:
    print ('TypeError: x is not a list')
finally:
    print 'Finally block Always executed'


### Slicing example
veg = "tomatoe"
print '\n### String slicing example'
print(veg[-1]) # last element
print(veg[1:]) # second element through last element
print(veg[:1]) # first element
print(veg[:-1]) # First element upto last (excluding last element)
print(veg[:])  #all elements
print(veg[1:4]) # 2nd, 3rd, 4th elements
print (veg[1::2]) # starting from 2nd element, every 2nd element
print (veg[::3])  # starting from 1st element, every 3rd element


#str = input("enter some number:")
#s=int(str)  # convert string to int
#print ("the square of the number is:",s*s)

### LIST Examples

# Lists are ordered, mutable

print '\n###List Example'
new_list = [x for x in range(5)]
new_dict = {key: str(key) for key in range(5)}
new_set = { x for x in 'missisippi'}

namelist = ["bangalore","chennai","hyderabad","mumbai","delhi","vizag","chirala","bapatla","guntur"]
print "Length of list len(list): %i"%(len(namelist))
print "Finding the number of occurances of bangalore: %s" %(namelist.count('bangalore'))
print "Index of 'delhi': %s"%(namelist.index('delhi'))

for index,value in enumerate(namelist):
    print(index,value),;


namelist.append("tiruapty")
namelist.insert(3,'darsi')
print "\nRemoving last element in the list '%s'"%(namelist.pop())
namelist.extend(["nellore","ongole"])
namelist.remove('ongole')
print(namelist)
print("Reversing the list")
namelist.reverse()
print (namelist)
print("Sorting the list")
namelist.sort()
print (namelist)

anotherList = [1,2,"hello",4,"\'5.6\'",7.1]  # List can have different kinds of data types. List can have another list
# Example of lists in list
books = ["Head First Python","Head First Programming","Head First Java Script",["programming",["python","genera;","java script"]]]
'''
for book in books:
    if isinstance(book,list):
            for data in book:
                print(data)
    else:
        print(book)
'''

# tuples are similar to list but tuples are immutable and used to define list of constants.. tuple can be converted to list and vice versa

myTuple = (1,2,3)
myList = list(myTuple)
myList.append(4)
    

### Dictionary examples

#Dictionary is like set of key, value pairs. You may add/modify key,value to dictionary etc. Keys are unique and each key can have one value.
print '\n###Dictionary example ###'

phone_dict = {"saikumar":9845198763,"neeraja":7032364646}
print(phone_dict["saikumar"])
print(phone_dict)


phone_dict["saikumar"]='010-6650-4486'
phone_dict["neeraja"]= '010-6630-4486'

for a in phone_dict:       # get keys
    print (a,phone_dict[a]),

print ''
for key in phone_dict.keys():  # get keys
    print (key),

print ''
for value in phone_dict.values():  # get values
    print(value),

print ''
for key,value in phone_dict.items():  # get key,value pairs
    print(key,value),

### Type conversions

print '\n\n###Type conversion examples###'
a="0"
b=2
print(int(a)+b)   #converts string to Integer

c=21
d="hello"
print(str(c)+d)   #converts integer to string

e="11.6"
f=5
g=10.3
print(float(e)+f) #converts string to float
print(e+str(f))

print(type(e),type(f),type(g))

###Formatting strings/floats

x = 21.453
y = 32.567

print ("the float value is %f"%x)
print ("the float value upto 2 deciamls is %.2f"%x)

print ("x is %f"%x+" and y is %f"%y)

str = 'hello world'
print ("%s"%str)
print ("%.4s"%str)


### Mutable/Immutable examples

# Immutable -- string, integer,float and tuples  
a= 3
print "\n\nImmutable example: a = %i, id(a)=%i"%(a,id(a))
a=a+1
print "After changing a value: a = %i, id(a)=%i"%(a,id(a))

#  Mutable - dictionary , list etc -- id won't change after changing the values
b=[]
print "Mutable example:  b = %s, id(b)=%i"%(b,id(b))
b.append(23)
b.append(56)
print "After adding values to list: b = %s, id(b)=%i \n\n"%(b,id(b))
print(sys.maxsize)
print(sys.maxsize+1)

### functions

def bar(*args):
    print args

def foo(a, b=2, c= 3):
    print a, b, c

bar([1,2], 2, 3)

d = {'a':5, 'b':6, 'c':7}
foo(**d)

print list(range(1,100))

