1.7
#comments are written like this these lines after '#' are not executed
type(1)
type(2.3)
type("hello") #quotes important
type(True)
float(1)
int(2.7)
3+2
3.0+2 #answer float
print(3+5)
5-7
7*8
5/2 #float answer
5//2 #integer answer
5%2 # reminder
5**2 #power
operations prefernce
- parentheses () - power ** - multiplication * - division / - addition or substraction + or - - executed from left to right
--4
-4
++4
+4
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
area = pi * (r**2)
area
area = (pi * r)**2
area #wrong way but to show the impotance of brackets
x = 1
x
x = 1
x= x+1
x
x = 1
x += 1 # same as x=x+1 called shorthand operator
x
2>3.0
5/5 == 1 #check if equal too remember double ==
3>3
3>=3
2<2
2<=2
2!=2 #not equal too
2!=3
2==2 and 2+2 ==4 #and gives true when both conditions are TRUE
2==2 and 2+2 == 5 #Left one false
2==3 and 2+2 == 5 #both false
2==2 or 2+2 ==4 #or gives when at least one is TRUE
2==2 or 2+2 == 5 #Left expression false but right expression is true
2==3 or 2+2 == 5 #both false
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")
#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")
x = 0 # just an another one
if x%2 ==0: # statement should end with a ':'
print("even")
else:
print("odd")
#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")
#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")
# 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=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")
3 > 4 or 4 < 5 and 10 < 9 #conditions execuited left to right
#keywords can't be used as varibales
x=2
y=3
temp = x #using extra 'temp' varibale
x = y
y = temp
print("x=",x)
print("y=",y)
h = "hello" #double or single quotes will work
h
'hello' + 'there'
'hello' +' '+ 'there!'
s = "Hello"
s*3
s = "Hello " #space after o
s*3
s[0]
s[4]
'MIT'[1]
'hello'[1:3] # 2nd and 3rd element
'hello'[1:] # everything from 1st element
'hello'[:3] #everything upto 3rd element
'hello'[:]
'hello'[:-1] #everything but last
'hello'[-3:] #last 3 elements
"hello"[1:5:2] #every second element
"hello"[:2]
"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
len("hello")
x = "hello"
len(x)
"hello" == x
'e' in x
a = "there"
print("Hello"+a)
print("Hello"+" "+ a +"!")
type(a)
input() #by defalut expects a string
x = input()
type(x)
y = int(x)
type(y)
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!")
#while looop
n=0
while n<5:
print(n)
n= n+1
#for loop
i= 0
for i in range(5):
print(i)
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)
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)
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)
for i in "hello": #loop through strings
print(i)
Reference