# Python - MIT OCW | Ana Bell

---

### Lecture - 01

* Declarative Knowledge : statements or facts
    
* Imperative knowledge : how-to or recipe of making it (Thats in CS)
    
* Programming is about writing recipes to generate facts. or writing set of instruction to perform by computer
    
* The computer only do what you tell it to do
    
* Computers are machine that executes algorithms. Till 1940 we had fixed programs(algorithms like add/substract/multiply etc). After 1940 computers started to store values and then executing those based on input.
    
    STORED PROGRAM COMPUTER
    
    \=&gt; Sequence of instructions stored inside computer (built from predefined set of primitive instructions (Like Arithmatic and Logical, simple sets, Moving data)
    
    \=&gt; Special program (Interpreter) executes each instruction in order.
    
* Memory and its workflow
    
    ![Memory (Input and Output)](https://cdn.hashnode.com/res/hashnode/image/upload/v1721741563787/5678f5ca-67cb-4236-baec-9d4c88050737.png align="center")
    
    predefined values in Control Unit, Arithmetic Logic unit, Memory locations. According to input control unit and arithmetic unit does the operation and result being stored in required memory location. calling memory location gives value assigned to that location.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1721742017542/36fe4738-ec53-4ca3-85ec-72de000a2444.png align="center")
    
    at control unit: Instructions are given like add numbers at memory locations 3456 and 3457 (control unit checks for these numbers and send them to ALU to do the addition) then the output should be stored at memory location 3458. similarly for 7889,7890 and store them at 7891. after evaluating using ALU the next instruction is comparing the both outputs and printing it as Output (TRUE).
    

### Primitives :

> Building blocks/fundamental data types built in programming language.

### Objects/Types :

> Programs manipulate Objects. (Scalar objects and Non-scalar objects) - scalars(cannot be subdivided) , Non-scalar(have structure like list,dictionaries, sequence of characters.
> 
> int,float,boolean,NonType etc
> 
> Objects have types. Eg: type\[30\] = 30 is integer type
> 
> \*\*CASTING\*\* \[Type Conversions\]
> 
> if u decide object (int 30) , its memory location is fixed, means while casting it to float version of this, it will create new object at new memory location.
> 
> ```python
> type(30)           float(4)        int(4+5)  ## Expression (not-stored)
> >>> int            >>> 4.0         >>> 9     ##value (stored)
> int(4.5)           round(4.8)
> >>> 4              >>> 5
> ########################################################
> #  Python evaluates Expressions and stores the VAlue   #
> #  It does not stores Expressions!!                    #                                         
> ########################################################
> Syntax 
> <object> <expression> <object>
> ```

<mark>BIG-IDEA = Replace complex Expression with one Value</mark>

<mark>Operators in Python</mark>:

![5/3 = 1.666 but 5//3 = 1 (gives only int part)](https://cdn.hashnode.com/res/hashnode/image/upload/v1724045382467/f62387a8-0e46-4f7f-a040-212ed28e38c1.png align="center")

> { pi = 30/6 } -- flow is , first evaluate the value at RHS then assign it to LHS
> 
> use following to swap value
> 
> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724046355381/935e9c3e-fff9-4c1a-8365-a07dafbc340d.png align="center")
> 
> <mark>BIG-IDEA : Lines are Evaluated one by one</mark>

### Lecture - 02

Object type : <mark>string</mark>

> sequence of case sensitive characters. written b/w double quotes / single quotes
> 
> h = 3 =&gt; this is int h = '3' =&gt; this is string be careful while con-catenating them
> 
> len(h) gives how many number of characters are there in string
> 
> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724057665100/01483a9a-69d2-4626-8055-5c73bbc585cb.png align="center")
> 
> ***Slicing of String***
> 
> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724057962453/701a07fc-c066-4f7f-aa10-404f18909d71.png align="center")
> 
> **Slicing to get Substring \[start:stop:step\]**
> 
> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724058241288/1d7307cf-0dbc-433c-8fb0-f5512f27fc22.png align="center")
> 
> strings are immutable : cannot be modified, you can create new-objects that are versions of original one
> 
> ```python
> a = "The"
> b = 3
> c = "idiots"
> print(a,b,c)               >>> The 3 idiots    #(,) gives space
> print(a+b+c)               >>> Gives error as concatenation of string and int is not OK
> print(a+str(b)+c)          >>> The3idiots     #(+) concatenate without space
> print(4*a)                 >>> TheTheTheThe
> text = input("your name: ")   >>> user input will be value of text as string
> ```
> 
> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724060064999/16722e65-b878-49b4-97f1-045b2a043acf.png align="center")
> 
> af

Input/Output

> Input (input) Output (print) Equivalancy
> 
> ```python
> a = "The"
> b = 3
> c = "idiots"
> print(a,b,c)               >>> The 3 idiots    #(,) gives space
> print(a+b+c)               >>> Gives error as concatenation of string and int is not OK
> print(a+str(b)+c)          >>> The3idiots     #(+) concatenate without space
> print(4*a)                 >>> TheTheTheThe
> text = input("your name: ")   >>> user input will be value of text as string
> ```
> 
> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724060064999/16722e65-b878-49b4-97f1-045b2a043acf.png align="center")
> 
> (=) is assigning , but (==) is comparision (gives boolian output)
> 
> Also as you studied gates in Maths ( T + T = T ) like , think about them here also
> 
> Eg: `(2<3) and (4<3)` &gt;&gt;&gt; then output will be `True` as both the expressions are `True`

Conditioning/Branching

if else , if elif , if elif else

> ```python
> pset_time = ???
> sleep_time = ???
> if (pset_time + sleep_time) > 24:    #conditions that evaluates to boolean
>    print("impossible")
> elif (pset_time + sleep_time) >= 24:  #If previous is FALSE then
>    print("Full schedule")
> else:           #This block runs only if previous conditions are all FALSE
>    leftover = abs(24 - pset_time - sleep_time)
>    print(leftover,"hrs of free time")
> print("End of the Day")
> ```
> 
> Indentations Matters
> 
> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724061480864/32dbf427-b83e-4f93-b2a3-2c5955b7e158.png align="center")

zxcv

mhv

nbcv

bc

\*\*\*\*\*\* Lect - 05 : Floats and Approximation Methods \*\*\*\*\*\*\*

```python
## code to convert Decimal to Binary 
if num < 0:
   is_neg = TRUE
   num = abs(num)
else:
   is_neg = FALSE
result = ''
if num == 0:
   result == '0'
if num > 0:
   result = str(num%2) + result  
   num = num//2 
## pre-pending the number to pre. result value
## num%2 will give remainder which is either 0 or 1,doing it str only bcz we dont
# need to add result ,we just need to put that number before the result value.
#hense result = '0' is string and str(num%2) is also string , putting + in b/w
#them means ,python will consider it as just keeping two string together.
if is_neg:
   result = '-' + result
```

FOR fraction's :

As in decinmal <mark>0.abc</mark> means <mark>a*(1/10) + b*(1/100) + c*(1/1000)</mark> which is 10^(-1) , 10^(-2) and so on , similarly in binary its <mark>a</mark>*<mark>2^(-1) + b</mark>*<mark>2^(-2) + c*2^(-3)</mark>

we already know how to find an integers binary , for fraction values if u find binary values of a,b,c then its done. To find those value...

1. Eg: for 3/8 = 0.375 = 3\*10^(-1)+7\*10^(-2)+5\*10^(-3)
    
    \=&gt; for making 0.375 binary , if anyhow u convert that number to whole number then its easy. Hence findout a number which is multiple of 2 and if multiplied by 0.375 should give a <mark>whole number (an integer)</mark>
    
    \=&gt; like <mark>(0.375 * 2(**3) = 3)</mark> , now convert 3 to its binary (11) and bcz we have power 3 , shift (.) by three digit from left : ans = <mark>0.011 is binary for 0.375</mark>
    
2. Now our task is to find-out , a <mark>magical number</mark> by which number we have to multiply 2 to get integer. But its not always true
    

```python
x = 0.625
p = 0
while ((p**2)*x)%1 != 0:  ## %1 gives only fraction part , 1.11%1 is 0.11
      print('Remainder = ' + str((p**2)*x - int((p**2)*x))) #find power of 2 to make it integer
      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 decimal ' + str(x) + 'is' + str(result))
```

asg

sdfg

sfg

sfg

sfg
