In [4]:

```
#Strings are 'immutable' meaning
s = 'hello'
s[0] = 'y' #gives an error
```

In [3]:

```
#but can be changed like below
s = 'y' + s[1:]
s
```

Out[3]:

- Approximation (guess and check)

- You assume a number and check with small increments

In [27]:

```
#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

In [26]:

```
#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)$$

In [120]:

```
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))
```

In [37]:

```
# 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.¶

In [18]:

```
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¶

In [ ]:

```
# 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)
```

In [28]:

```
#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)
```

Out[28]:

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.

In [33]:

```
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()
```

In [36]:

```
nameHandle= open('kids', 'r') #opens file in read access and prints the information
for line in nameHandle:
print(line)
nameHandle.close()
```

In [11]:

```
A = 'ABC'
A.index("B")
```

Out[11]:

In [12]:

```
str2 = 'Number one - the larch'
str2.find('n')
```

Out[12]:

In [15]:

```
len("abc")
```

Out[15]:

Reference

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