#Strings are 'immutable' meaning
s = 'hello'
s[0] = 'y' #gives an error
#but can be changed like below
s = 'y' + s[1:]
s
- Approximation (guess and check)
- You assume a number and check with small increments
#example for guess and check
x = 1678
epsilon = 0.01
step = 0.1
guess = 0.0
num_steps = 0
while guess <= x:
num_steps += 1
if abs(guess**2 -x) < epsilon:
break
else:
guess += step
if abs(guess**2 - x) >= epsilon:
print('failed')
else:
print('succeeded: ' + str(guess))
print("Number of steps taken is "+str(num_steps))
- Bisection search
- eleminate half of the range based on the input
#example for bisection search
x = 1678
high = x
low = 0
ans = 0
num_steps = 0
while ans <= x:
num_steps += 1
ans = (high+low)/2
if abs(ans**2 -x) < epsilon:
break
elif (ans**2 -x) > 0:
high = ans
else:
low = ans
if abs(ans**2 - x) >= epsilon:
print('failed')
else:
print('succeeded: ' + str(ans))
print("Number of steps taken is "+str(num_steps))
- If g is an approximation to the root, then
$$ g-p(g)/p'(g)$$
is a better approximation where p' is derivative of p
for square root $$ F(x)= x^2 - 24 $$ solutions to this equation is the square root of 24.
$$ F'(x)= 2*x $$
there fore we have, when for 24 $$x=g$$ $$ g - (g^2-24)/(2*g)$$
x = 1678
epsilon = 0.01
guess = x/2
num_steps = 0
while abs(guess**2 -x) >= epsilon:
num_steps += 1
guess = guess - ((guess**2 - x)/(2*guess))
if abs(guess**2 - x) >= epsilon:
print('failed')
else:
print('succeeded: ' + str(guess))
print("Number of steps taken is "+str(num_steps))
# converting fraction into binary
x = float(input('Enter a decimal number between 0 and 1: '))
p = 0
while ((2**p)*x)%1 != 0:
p += 1
num = int(x*(2**p))
result = ''
if num == 0:
result = '0'
while num > 0:
result = str(num%2) + result
num = num//2
for i in range(p - len(result)):
result = '0' + result
result = result[0:-p] + '.' + result[-p:]
print('The binary representation of the decimal ' + str(x) + ' is ' + str(result))
Reuseable piece of code is called Function.¶
def f(x, y):
'''
This is how comments inside a functions are written
'''
print(x,"Inside fucntion X")
return x + y - 2 #values of x and y are only valid inside the function - local variable
x=5 #globel variable
print(f(2,3),"Function return value") #calling the function
print(x,"Outside Function X")
Function is called in itself in loop to perform certain tasks¶
# multiplication a*b
def mult(a,b):
if b==1: #last condition
return a
else:
return a + mutl(a,b-1) #same function is called for reduced variable
mult(10,5)
#finding factorial of a number
def fact(n):
if n == 1 or n == 0: # last condition
return 1
else:
return n*fact(n-1) #same function is called for reduced variable
fact(5)
Suppose module circle.py has two function area and circumference it can be use as below
import circle
print(circle.area(3))
and
print(circle.circumference(3))
Different way of importing
from circle import * # will import all the modules
- every operating system has its own way of handling files; Python provides an operating-system independent means to access files, using a file handle
- nameHandle= open(‘kids’, ‘w’)
- creates a file named kids and returns file handle which we can name and thus reference. The windicates that the file is to opened for writing into.
nameHandle= open('kids', 'w') #creats and opens file named kids with write option
for i in range(2):
name = input('Enter name: ')
nameHandle.write(name + " ")
nameHandle.close()
nameHandle= open('kids', 'r') #opens file in read access and prints the information
for line in nameHandle:
print(line)
nameHandle.close()
A = 'ABC'
A.index("B")
str2 = 'Number one - the larch'
str2.find('n')
len("abc")
Reference