Chapter 1
Chapter 1
Python
Python
1. Guido Van Rossum, 1991
2. Languages
a. Low Level:
i. Very Less English
ii. No Abstraction
iii. More Control Over Hardware
iv. Steep Learning Curve
v. Machine Code(Binary), Assembly Language (x86)
b. Medium Level:
i. More English
ii. Some Abstraction + Clear Connection to Hardware
iii. C, C++
c. High Level:
i. Mostly English
ii. Offers more Abstraction
iii. Easier Learning Curve – More User Friendly
iv. Python, Java, JavaScript, Ruby
3. Interpreted Language
a. Interpreter: Translates source code into machine code line by line, executing it
immediately (no .exe file)
i. Interpretation – Execution (As it is interpreted)
ii. Errors are detected during execution i.e. line by line.
iii. Generally, slower
iv. Python, Ruby, JavaScript
b. Compiler: Translates entire source code into machine code before execution
(generally .exe file)
i. Compilation – Execution
ii. Errors are detected during the compilation phase; the program will not run
until all compilation errors are resolved.
iii. Generally, faster because it is directly executed by machine.
iv. C, C++, Rust
4. Cross Platform Language
5. Interactive and Script
a. Interactive
i. One Line / Time
ii. Sandwiched Output
iii. Run Automatically
iv. Testing
b. Script
i. Large Program
ii. Separate Window
iii. Manual Running
iv. Programs
Encoding Schemes:
1. ASCII: American Standard Code of Information Interchange
a. A - Z: 65 - 90
b. a - z: 97 - 122
c. 0 - 9: 48 - 57
d. Space: 32
2. ISCII: Indian Standard Code of Information Interchange
3. UNICODE [UTF - Unicode Transformation Format]
a. UTF-8
b. UTF-16
c. UTF-32
Tokens: Smallest Individual Unit in a Python Program
1. Literals/Constants (Data Types): Type of data which we can give into a python program
a. Numeric
i. Integer
ii. Float
b. Character
i. Single Character
ii. Multi Character
1. Single Line
2. Multiline
c. None
d. Boolean
i. True (num != 0)
ii. False (num = 0)
e. Containers
i. Mutable
1. List
2. Dictionary
ii. Immutable
1. Tuple
2. String
2. Keywords: Software defined, reserved words with fixed meaning; Cannot change
meaning, spelling and case.
a. print()
i. sep: None or Str
ii. end: None or Str
iii. If sep or end is None, default values will be used
iv. If print() is empty, end will be printed.
b. type()
c. id()
d. chr() – character for a unicode value
e. ord() – ordinal number (unicode) for a character
3. Identifier: User defined words used to store data
Variables: (Identifier used to store data)
Lvalue (address in which data is stored) and Rvalue (data which is stored)
a. Naming Rules:
i. No Special Characters (_ is allowed)
ii. Starting character must not be a number
iii. No Keywords
iv. No Spaces
b. Defining a Variable:
i. Direct/Dynamic Way
x=2
ii. Using input()
c. Dynamic Typing
i. A variable storing a certain type of value can be made to store a value of
different type.
ii. The type of the variable is determined at runtime, rather while compiling;
You do not need to declare the type of a variable explicitly.
4. Operators: Symbols that perform operations on variables and values.
a. Arithmetic Operators
i. +
ii. -
iii. *
iv. / – returns float
v. // – floor division – returns integer when both operands are integer; else if
one is float, returns float
vi. % – modulo – returns integer when both operands are integer; else if one
is float, returns float
vii. **
b. Relational Operators
i. >
ii. <
iii. >=
iv. <=
v. !=
vi. ==
c. Boolean Operators
i. and – if the first operand is False, second operand will not be evaluated
ii. or – if the first operand is True, second operand will not be evaluated
iii. not
d. Membership Operators
i. in
ii. not in
e. Identity Operators
i. is
ii. is not
f. Shorthand Operators
i. +=
ii. -=
iii. *=
iv. //=
v. /=
vi. %=
vii. **=
Type Conversion: process of converting one datatype to another
1. Implicit: automatic conversion by the programming language
2. Explicit: manual conversion by the programmer
Operator Precedence:
1. ()
2. ** – right associative
3. *, /, //, %
4. +, -
5. All Relational Operators
6. Identity Operators
7. Membership Operators
8. not
9. and
10. or
11. Conditional Expression (one liner if-else)
12. Lambda Expression
Errors:
1. Syntax Errors: Code does not conform to the rules of the Python language.
Detected during the interpreting phase.
a. SyntaxError
b. IndentationError
2. Logical Errors: Code runs without crashing, but produces incorrect results.
3. Runtime Errors: Errors occur during execution of the program – cause the program to
terminate.
a. NameError
b. TypeError
c. ZeroDivisionError
d. FileNotFoundError
e. IndexError