Python Notes by Ankush-Chap03-FirstProgram
Python Notes by Ankush-Chap03-FirstProgram
Chapter - 03
Python As a Calculator
Python can be used as a powerful calculator for performing a wide range of
arithmetic operations.
2+5 add two numbers
print(10/5) divide two numbers
P y thon No te s by Ankush
Comments: Comments are used to annotate codes, and they are not
interpreted by Python. It starts with the hash character #
Comments are used as notes or short descriptions along with the code to increase
its readability.
1. Lexical Analysis: The interpreter breaks down the code into smaller parts
called tokens, identifying words, numbers, symbols, and punctuation.
2. Syntax Parsing: It checks the structure of the code to ensure it follows the
rules of Python syntax. If there are any errors, like missing parentheses or
incorrect indentation, it stops and shows a SyntaxError.
3. Bytecode Generation: Once the code is validated, the interpreter translates
it into a simpler set of instructions called bytecode. This bytecode is easier
for the computer to understand and execute.
4. Execution by PVM: The Python Virtual Machine (PVM) takes the bytecode
and runs it step by step. It follows the instructions and performs calculations,
assigns values to variables, and executes functions.
5. Error Handling and Output: If there are any errors during execution, like
trying to divide by zero or accessing a variable that doesn't exist, the
interpreter raises an exception. If the code runs without errors, it displays
any output, such as printed messages or returned values, to the user.
Python Syntax
The syntax of the Python programming language, is the set of rules that defines
how a Python program will be written and interpreted (by both the runtime system
& by human readers).
my_name = "Madhav" ⬛
my_name = Madhav +
Use quotes "" for strings in flython
Interpreter Compiler
An interpreter translates and A compiler translates the entire code
executes a source code line by line into machine code before the
as the code runs. program runs.