## Basic operations¶

In :
1.7

Out:
1.7
In :
#comments are written like this these lines after '#' are not executed

In :
type(1)

Out:
int
In :
type(2.3)

Out:
float
In :
type("hello") #quotes important

Out:
str
In :
type(True)

Out:
bool
In :
float(1)

Out:
1.0
In :
int(2.7)

Out:
2
In :
3+2

Out:
5
In :
3.0+2 #answer float

Out:
5.0
In :
print(3+5)

8

In :
5-7

Out:
-2
In :
7*8

Out:
56
In :
5/2 #float answer

Out:
2.5
In :
5//2 #integer answer

Out:
2
In :
5%2 # reminder

Out:
1
In :
5**2 #power

Out:
25

operations prefernce

- parentheses ()
- power **
- multiplication *
- division /
- addition or substraction + or -
- executed from left to right 
In :
--4

Out:
4
In :
-4

Out:
-4
In :
++4

Out:
4
In :
+4

Out:
4

## Variables¶

In :
pi= 3.14 # varibales store values here pi is a variable
r = 3 # always value on the right is assigned to left like in math a = 1 and 1 = a  second will not work in programing
area = pi * r**2
area

Out:
28.26
In :
area = pi * (r**2)
area

Out:
28.26
In :
area = (pi * r)**2
area #wrong way but to show the impotance of brackets

Out:
88.7364
In :
x = 1
x

Out:
1
In :
x = 1
x= x+1
x

Out:
2
In :
x = 1
x += 1 # same as x=x+1 called shorthand operator
x

Out:
2

## Operators and Branching¶

In :
2>3.0

Out:
False
In :
5/5 == 1 #check if equal too remember double ==

Out:
True
In :
3>3

Out:
False
In :
3>=3

Out:
True
In :
2<2

Out:
False
In :
2<=2

Out:
True
In :
2!=2 #not equal too

Out:
False
In :
2!=3

Out:
True
In :
2==2 and 2+2 ==4 #and gives true when both conditions are TRUE

Out:
True
In :
2==2 and 2+2 == 5 #Left one false

Out:
False
In :
2==3 and 2+2 == 5 #both false

Out:
False
In :
2==2 or 2+2 ==4 #or gives when at least one is TRUE

Out:
True
In :
2==2 or 2+2 == 5 #Left expression false but right expression is true

Out:
True
In :
2==3 or 2+2 == 5 #both false

Out:
False
In :
x = 4
if x%2 ==0: # statement should end with a ':'
print("even") #indentation is imporant hence use tab after if statement
else:
print("odd")

even

In :
#same code as before with x assigned to odd
x = 7
if x%2 ==0: # statement should end with a ':'
print("even")
else:
print("odd")

odd

In :
x = 0 # just an another one
if x%2 ==0: # statement should end with a ':'
print("even")
else:
print("odd")

even

In :
#Nested if conditions and notice the indendations
x = 9
if x%2 ==0:
if x%3 ==0:
print("Divisible by both 2 and 3")
else:
print("Divisible by 2 but not 3")
else:
if x%3==0:
print("Not divisible by 2 but divisible by 3")
else:
print("Not divisible by both 2 and 3")

Not divisible by 2 but divisible by 3

In :
#one example of same code
x = 6
if x%2 ==0:
if x%3 ==0:
print("Divisible by both 2 and 3")
else:
print("Divisible by 2 but not 3")
else:
if x%3==0:
print("Not divisible by 2 but divisible by 3")
else:
print("Not divisible by both 2 and 3")

Divisible by both 2 and 3

In :
# use of conditions
x=2
y=3
z=5

if x<y and x<z:
print("x is smallest")
elif y<z and y<x:           #notice the elif same as else if
print("y is smallest")
else:
print("z is smallest")  # and logic failes when the values if x,y and z are equal
# can you guess the output the answer when x=y=z

x is smallest

In :
x=float(input("x=")) #takes user input
y=float(input("y="))
z=float(input("z="))

if x<y and x<z:
print("x is smallest")
elif y<z and y<x:
print("y is smallest")
else:
print("z is smallest")

x=7
y=8
z=15
x is smallest

In :
3 > 4 or 4 < 5 and 10 < 9 #conditions execuited left to right

Out:
False
In :
#keywords can't be used as varibales


#### Swaping values in variables¶

In :
x=2
y=3
temp = x #using  extra 'temp' varibale
x = y
y = temp
print("x=",x)
print("y=",y)

x= 3
y= 2


### Strings¶

In :
h = "hello" #double or single quotes will work
h

Out:
'hello'
In :
'hello' + 'there'

Out:
'hellothere'
In :
'hello' +' '+ 'there!'

Out:
'hello there!'
In :
s = "Hello"
s*3

Out:
'HelloHelloHello'
In :
s = "Hello " #space after o
s*3

Out:
'Hello Hello Hello '
In :
s

Out:
'H'
In :
s

Out:
'o'
In :
'MIT'

Out:
'I'
In :
'hello'[1:3] # 2nd and 3rd element

Out:
'el'
In :
'hello'[1:]  # everything from 1st element

Out:
'ello'
In :
'hello'[:3] #everything upto 3rd element

Out:
'hel'
In :
'hello'[:]

Out:
'hello'
In :
'hello'[:-1] #everything but last

Out:
'hell'
In :
'hello'[-3:] #last 3 elements

Out:
'llo'
In :
"hello"[1:5:2] #every second element

Out:
'el'
In :
"hello"[:2]

Out:
'he'
In :
"hello"[:2:-1] # -step gives elements not slected and the reverse jumping
# selected last 2 elements and goes in opposite direction , little tricky pay attention

Out:
'ol'
In :
len("hello")

Out:
5
In :
x = "hello"
len(x)

Out:
5
In :
"hello" == x

Out:
True
In :
'e' in x

Out:
True
In :
a = "there"
print("Hello"+a)

Hellothere

In :
print("Hello"+" "+ a +"!")

Hello there!

In :
type(a)

Out:
str
In :
input() #by defalut expects a string

a

Out:
'a'
In :
x = input()

1

In :
type(x)

Out:
str
In :
y = int(x)
type(y)

Out:
int

## Loops¶

In :
n= int(input("enter a number ")) # be carefull about infinite loop

while n != 1:
print("Entered number is not 1")
n= int(input("enter a number"))
print ("Finally you entred 1!")

enter a number2
Entered number is not 1
enter a number3
Entered number is not 1
enter a number4
Entered number is not 1
enter a number5
Entered number is not 1
enter a number6
Entered number is not 1
enter a number345
Entered number is not 1
enter a number1
Finally you entred 1!

In :
#while looop
n=0
while n<5:
print(n)
n= n+1

0
1
2
3
4

In :
#for loop
i= 0
for i in range(5):
print(i)

0
1
2
3
4

In :
i= 0
intsum = 0
for i in range(3,6): # only when i was equal to 3,4,5 this loop was executed hence intsum is 3+4+5
intsum += i
print(intsum)

12

In :
i= 0
intsum = 0
for i in range(3,6,2): # only when i was equal to 3,5 this loop was executed, last number represents "steps" jumops 2 steps
intsum += i
print(intsum)

8

In :
i=0
intsum = 0
for i in range(3,6,2): # only when i was equal to 3,5 this loop was executed, last number represents "steps" jumops 2 steps
intsum += i
if i == 3:
break # lets you get out of the loops when you need
print(intsum)

3

In :
for i in "hello": #loop through strings
print(i)

h
e
l
l
o


Reference

• edX course offered by MIT
• 6.00.1x Introduction to Computer Science and Programming Using Python