Friday, April 17, 2015

Python - Conditionals

If you have to compare data's and then display the result  conditionals are used.

if, elif, else

Ex for if

v1,v2=3,4
if v1<v2:
    print("v2 is greater")

Ex for else

v1,v2=7,4
if v1<v2:
    print("v2 is greater")
else:
    print("v1 is greater")

Ex for elif

v1,v2=7,7
if v1<v2:
    print("v2 is greater")
elif v1>v2:
    print("v1 is greater")
else:
    print("v1 is equal to v2")

Conditional expression:
Compare in one line and display

Ex:
v1, v2=5,3
ans="v1 is greater" if v1>v2 else "v2 is greater"
print(ans)

Switch Alternative

selc=dict(
          one="Fan",
          two="TV",
          three="Frdige",
          four="Microwave"         
          )

v='two'
print(selc[v])


 O/P:
TV 

If in above example v is assigned with other values than mentioned in dictionary then results in error.  Below example shows how to hide the error and display the result


selc=dict(
          one="Fan",
          two="TV",
          three="Frdige",
          four="Microwave"         
          )

v='six'
print(selc.get(v,"Wrong selection"))


O/P
Wrong selection
 

Thursday, April 16, 2015

Python - Lists and Tuples

Tuples are immutable (Cannot change). Use () brackets
Lists are mutable (can change).use [] brackets


ex for tuple:

v1=(1,2,3,4,5)
print(type(v1),v1)


O/P
<class 'tuple'> (1, 2, 3, 4, 5)


ex for list
v1=[1,2,3,4,5]
print(type(v1),v1)


O/P
<class 'list'> [1, 2, 3, 4, 5]

Since list is mutable you can append or insert values.

Ex:
v1=[1,2,3,4,5]
v1.append(8)
print(type(v1),v1)


O/P
<class 'list'> [1, 2, 3, 4, 5, 8]

Below example will insert 99 in third position

v1=[1,2,3,4,5]
v1.insert(2,99)
print(type(v1),v1)


O/P
<class 'list'> [1, 2, 99, 3, 4, 5]


Sequential with strings:

Below example displays 6th character of the string
Ex:
v1="This is my string"
print(type(v1),v1[5])


O/P
<class 'str'> i

Slicing the string

Below example displays values between 5th and 15th character
Ex:
v1="This is my string"
print(type(v1),v1[5:15])


O/P
<class 'str'> is my stri

Dictionary:

use {} for defining


Ex:
v1={'one':1, 'two':2, 'three':3,'four':4}
print(type(v1),v1)


O/P
<class 'dict'> {'four': 4, 'two': 2, 'three': 3, 'one': 1}


Below ex displays only keys 

v1={'one':1, 'two':2, 'three':3,'four':4}
print(type(v1),v1)
for i in v1:
    print(i)


O/P
<class 'dict'> {'two': 2, 'four': 4, 'one': 1, 'three': 3}
two
four
one
three


Below example will display both keys and values

Ex:
v1={'one':1, 'two':2, 'three':3,'four':4}
print(type(v1),v1)
for i in v1:
    print(i,v1[i])


O/P
<class 'dict'> {'two': 2, 'four': 4, 'three': 3, 'one': 1}
two 2
four 4
three 3
one 1

Python Variable - Strings

Strings can be assigned to variable in either double " " or single quotes ' '. Both will have same output

EX:
str1='This is my first line'
str2="This is my second line"
print(str1)
print(str2)

O/P
This is my first line
This is my second line

Using escape character in the string.

Ex for \n

str1='This is my \n first line'
print(str1)

O/P
This is my
 first line

Raw string:

If you want to display the escape character /n in the string then use r in brfore the string quote

str1=r'This is my \n first line'
print(str1)

O/P
This is my \n first line

Formatting and replacing of variables in string

n=35
str1=r'This is my {}th line'.format(n)
print(str1)

O/P
This is my 35th line 


To display multiple lines without escape character.

For this use single quote or double quote three times

Ex:
str1='''Hi
all. This displays in
many lines.
 Try out'''
print(str1)


O/P
Hi
all. This displays in
many lines.
 Try out

Python Variables - Numbers

Everything in python3 is object.
Every object has its own id, type and value.

id is unique identifier of the object.
type is class of the object.
values is the contents of the object.

Ex:
#!/usr/bin/python3
x=9
print(x)
print(type(x))
print(id(x))


Output:
9
<class 'int'>
10455296


Numbers:

Numbers assigned to variable can be two types: integer and float

Integer is a whole number whereas float is a decimal number.

Integer example:

no=56
print(type(no),no) 


Output:
<class 'int'> 56


Float example:
no=56.0
print(type(no),no)



Output:
<class 'float'> 56.0

Examples:
no=45/7
print(type(no),no)


Output:
<class 'float'> 6.428571428571429

Rounding the number
no=round(45/7)
print(type(no),no)


Output:
<class 'int'> 6

Setting to two decimal points.
no=round(45/7, 2)
print(type(no),no)


Output:
<class 'float'> 6.43


Syntax

Python script starts with the line

#!/usr/bin/python3


/usr/bin/python3 is the path of the python interpreter



Whitespaces:

In python, block of  codes are manintained using whitespaces. Unlike other programming language where { } are used to represent the block of codes.

Generally 4 spaces are used to represent the block of codes.

Ex:
#!/usr/bin/python3

def test():
    print ("Line1:This shows first line of the block")
    print ("Line2:This shows second line of the block")
print ("Line3:This line is out of the block ")

test()

In above example Line3 print statement is out of test() function since it doesnt start with four spaces


Comments:

In python, comment starts with pound sign #.

Ex:

# This code executes when n < 0


Assignment:

A value is assigned to a variable using =

Ex:
var1=56
var2="Hello World"

Here var1 is assigned with integer value 56 and var2 with string "Hello World"

You can assign multiple variable at once

Ex:
var3, var4 = 66, 77

Conditionals:

If you have to compare datas and then display the result  conditionals are used.

if, elif, else

Ex for if

v1,v2=3,4
if v1<v2:
    print("v2 is greater")

Ex for else

v1,v2=7,4
if v1<v2:
    print("v2 is greater")
else:
    print("v1 is greater")

Ex for elif

v1,v2=7,7
if v1<v2:
    print("v2 is greater")
elif v1>v2:
    print("v1 is greater")
else:
    print("v1 is equal to v2")

Conditional expression:
Compare in one line and display

Ex:
v1, v2=5,3
ans="v1 is greater" if v1>v2 else "v2 is greater"
print(ans)