Python Basics Continued Chapter 2 3 Lecture by

Python Basics Continued Chapter 2 & 3 Lecture by : Abdullah Al Fahad

Why is Python a Hybrid Language? Although popularly knows as an interpreted language, in reality, Python is neither a true compiled nor a pure interpreted Language. It’s a mix of both which makes it a hybrid language. Following are the reasons why: Ø Whenever we download Python, we get an interpreter and a support library. o Support library is where we have all the built in functions, definitions, modules, exceptions, constants etc. o Interpreter is just a software that takes the source code, reads it line by line and execute it line by line to produce the output. General perception by programmers of an interpreter: ----------------- > However, when we run python source code, there are more that is happening behind the scene inside an interpreter. You might be wondering, what ‘s a compiler doing inside an interpreter since we Know python is not a compiled language. We know this that for a compiled language, Compiler (programmer) converts source code to machine code or binary code. Here, inside the interpreter for python, the compiler works a bit different.

Ø Why is Python a Hybrid Language? The 2 step process: Ø The source code (. py extension) is first compiled to byte code (. pyc extension) Ø The virtual machine such as PVM then interprets the byte code line by line and execute line by line to machine code for the CPU to display desired output and in the process uses the library modules. o Byte Code: Byte code is a lower level, platform independent and intermediate representation of your source code which is easily readable by a virtual machine. o PVM: The PVM or the Python Virtual Machine is the runtime engine of Python that interprets your compiled byte code. Quiz Questions: • • Who/what converts a HLL? A Compiler Machine code is a LLL Byte code is an OIL (optimized interpreted language) Java Python C++ are HLL CPU cannot read bytecode, It reads machine code Who interprets/reads Byte code? Virtual Machine such as PVM Does a programmer compiles python? No.

Python Functions Ø What is a functions in Python: A function is a block of organized, reusable code that is used to perform a single, related task. In simple words for your understanding, • We write a bunch of lines of code to perform a specific action. (2 nd Step) • To make it reusable, we define (reserve) these lines of code by typing a keyword “def” with your chosen function name. Here beside the function name, we may have some parameters or arguments to be more specific. (1 st Step) • And then, to re-use the action these lines of code perform, we call the defined function name only. (Last step) Basic Format of Functions As you already know, Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions. . Let’s Try some Basic Examples: Code Console

Python Functions Code Console Ø Now, Let’s use a parameter/argument and make it fun: Code Console Ø Multiple arguments: Code Console

Python Functions Ø Multiple arguments: Code Console Question: What is the difference here?

Python Functions Ø Once a function is defined, we can use the function as many times as we want and in between other coding. Code Console

Python Functions Ø More Practice. Imagine the usefulness of functions. Code Console Ø Default Parameter Value This example shows how to use a default parameter value. If we call the function without parameter, it uses the default value: Code Console
- Slides: 8