Skip to main content

Command Palette

Search for a command to run...

Python - MIT OCW | Ana Bell

Published
6 min read
M

I am a developer from Mumbai with 2.5 years of experience in C,Python and shell scripting. Also worked as GreenPlum database administrator for more than a year at National Stock Exchange of India. 2x aws certified in solution architect


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

    \=> Sequence of instructions stored inside computer (built from predefined set of primitive instructions (Like Arithmatic and Logical, simple sets, Moving data)

    \=> Special program (Interpreter) executes each instruction in order.

  • Memory and its workflow

    Memory (Input and Output)

    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.

    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.

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>

BIG-IDEA = Replace complex Expression with one Value

Operators in Python:

5/3 = 1.666 but 5//3 = 1 (gives only int part)

{ pi = 30/6 } -- flow is , first evaluate the value at RHS then assign it to LHS

use following to swap value

BIG-IDEA : Lines are Evaluated one by one

Lecture - 02

Object type : string

sequence of case sensitive characters. written b/w double quotes / single quotes

h = 3 => this is int h = '3' => this is string be careful while con-catenating them

len(h) gives how many number of characters are there in string

Slicing of String

Slicing to get Substring [start:stop:step]

strings are immutable : cannot be modified, you can create new-objects that are versions of original one

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

af

Input/Output

Input (input) Output (print) Equivalancy

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

(=) 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) >>> then output will be True as both the expressions are True

Conditioning/Branching

if else , if elif , if elif else

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

zxcv

mhv

nbcv

bc

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

## 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 0.abc means a(1/10) + b(1/100) + c(1/1000) which is 10^(-1) , 10^(-2) and so on , similarly in binary its a2^(-1) + b2^(-2) + c2^(-3)

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)

    \=> 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 whole number (an integer)

    \=> like (0.375 2(*3) = 3) , now convert 3 to its binary (11) and bcz we have power 3 , shift (.) by three digit from left : ans = 0.011 is binary for 0.375

  2. Now our task is to find-out , a magical number by which number we have to multiply 2 to get integer. But its not always true

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