Posts

Showing posts from May, 2017

Conditional Statements

Image
Relational Operators Boolean Operators: and, or, and not Rule of precedence: Arithmetic > Relational > Boolean Short-circuit evaluation: Boolean expression involving 'and', if first operand evaluates to False, Python stops evaluation even if some operands have not been evaluated yet and the entire expression is considered to be False. Similarly for Boolean expression involving 'or' and one of the operands is True

Basic Machine Architecture

Image
Basic Machine Architecture includes: Memory: store/load instructions Arithmetic Logic Unit (ALU): perform primitive operations Control Unit: has a program counter to keep track of where things are and ask the ALU to perform operations. It is initially pointed to the first instruction Below are basic steps: Read in instruction, ie. interpreter translate codes into instruction steps and stores inside Memory Interpreter execute first instruction pointed by program counter in Control Unit First instruction: runs through ALU, perform primitive operations, then stores it back into Memory Program counter increases by 1, ie. go to the next instruction Every once in a while, one of the instruction is a Test. If Test is true, it will change the Program Counter which allows the machine to run to a different instruction (whether back or forward), ie. changing where we are in the code Reaches the point where we're done, then it will Output Turing showed that using 6 primit...

Functions and Built-in Functions

Function: All functions start with reserved keyword 'def' All functions' definition ends with ':' By convention, indentation in Python is 4 spaces To write a comment, use '#' Every function returns a value. If there is no return statement, Python will return the special value None Note: print('hello ' + 'Huy') is the same as print('hello', 'Huy') def bye():     print('Goodbye') # print Goodbye and None

Variables, Expression and Assignment Statements

Image
Variable naming rule and convention: Can not start with a digit Uppercase and lowercase letters Digits Underscore '_' By convention, variable names are lowercase, separated by _ Variable names are case sensitive List of identifiers and reserved keywords:  https://docs.python.org/3/reference/lexical_analysis.html#identifiers Note above, since "area" was calculated before size change its value, area still 9 even though size is now 5 Syntax vs Semantic Errors: Above shows difference between syntax and semantic errors, though I really like this explanation. Expression vs Assignment Statement: Expression: evaluate, return/yield value/result Assignment: change state of variable/computaion and Python shell doesn't return anything Note: zipcode = '15245' zipcode + 3, Error because we can't add a type str and a type int zipcode * 3 will return 3 copies of zipcode, ie '152451524515245' z == (x * y...

Primitive Types, Arithmetic Expressions, Order of Operations

Image
Primitive Type: Arithmetic Expression: Order of Operation: Note: 2.1 + 2.2 != 4.3 because Python evaluates to 4.300000000000001. This is due to the binary addition of these values and approximations in the binary values of the equivalent decimal values. Therefore, checking for exact equality is not recommended for values of this type. -15 // 10 = -2 because Python use floor operation for Integer division (-1.5 becomes -2) 3 * 10 ** 2 = 300. Operators that have the same precedence are applied left to right except for exponentiation. Exponentiation is applied right to left. Exponentiation binds more strongly than multiplication 5.0 == 5 is True, 1 == True is True, 0 == False is True, 1.0 == True is true. Python equality is strict by default

Python

Hello, Welcome to Python. I'm picking up Python because it is the most popular programming language for Machine Learning. I'm taking 2 courses at the same time: Principles of Computing with Python by Carnegie Mellon University Introduction to Computer Science and Programming Using Python by MIT This blog is to post what I learned. Thanks for reading. Good luck. Huy