Python Basics

Printing Hello World

In [11]:
print("Python attack")
Python attack

Using Python as a Calculator

In [23]:
7*2
Out[23]:
14
In [26]:
8-2
Out[26]:
6
In [28]:
2^2
Out[28]:
0
In [57]:
words=["hello","cruel","world"]

for word in words:
    print(word)
hello
cruel
world

Create Your Own Function

In [102]:
def strange(x):
    i=0
    n=x
    for i<x:
        i=i+1
        
    return (x-i)**n

Use Your Function

In [103]:
print(strange(3))
8

Import Useful Modules

In [110]:
from math import pi
In [109]:
print(pi)
3.14159265359

Exercise: Import sine (sin) Function from math Library and use it to compute sin($\pi$/2)

In [92]:
from math import sin
from math import pi
from math import factorial

print factorial(4)
print sin(pi/2)
24
1.0

Same but Using Dot Notation

In [104]:
import math
In [105]:
math.pi
Out[105]:
3.141592653589793

More About Dot Notation

Fido is a DOG.

He eats, runs and plays.

Fido.eats()

Fido.runs()

Fido.plays()

Exercise: Please use dot notation with sine (sin) function from the math library to compute sin(pi/2)

In [111]:
math.sin(pi/2)
Out[111]:
1.0
In [113]:
math.factorial(7)
Out[113]:
5040
In [ ]:
 
In [ ]: