Skip to content

Commit 8c7fc52

Browse files
committed
Add basics, and inner-working of python 😎
1 parent b2366fd commit 8c7fc52

File tree

6 files changed

+184
-1
lines changed

6 files changed

+184
-1
lines changed
465 Bytes
Binary file not shown.

01_basics/hello_world.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
print("Hello world")
1+
print("Hello world")
2+
3+
def chai(n):
4+
print(n)
5+
6+
chai("lemon tea")
7+
8+
chai_one="lemon tea"
9+
chai_two="ginger tea"
10+
chai_three="masala chai"

01_basics/instructions.txt

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
📘 Python Notes — Basics: os, sys, import, reload
2+
=================================================
3+
4+
📍 Location:
5+
/home/malik-waseem/Malik waseem/IMPORTANT MEGA/python/01_basics
6+
7+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
8+
1️⃣ MODULE: os — Working with Operating System
9+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
10+
11+
🔹 THEORY:
12+
The `os` module in Python provides functions to interact with the operating system.
13+
Commonly used to get or change directories, manage files and folders.
14+
15+
🔹 PRACTICAL:
16+
>>> import os
17+
>>> os.getcwd()
18+
'/home/malik-waseem/Malik waseem/IMPORTANT MEGA/python/01_basics'
19+
20+
✅ `getcwd()` returns the **current working directory**.
21+
22+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
23+
2️⃣ FOR LOOP — Python Iteration
24+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
25+
26+
🔹 THEORY:
27+
The `for` loop is used to iterate over a sequence (like a string, list, tuple).
28+
29+
🔹 ERROR EXAMPLE:
30+
>>> for c in "chai":
31+
... print(c)
32+
IndentationError: expected an indented block after 'for' statement
33+
34+
❌ Python expects an indented block after control structures like `for`.
35+
36+
🔹 CORRECTED:
37+
>>> for c in "chai":
38+
... print(c)
39+
c
40+
h
41+
a
42+
i
43+
44+
✅ Always indent the body of the loop using 4 spaces or a tab.
45+
46+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
47+
3️⃣ MODULE: sys — System-specific Parameters
48+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
49+
50+
🔹 THEORY:
51+
The `sys` module provides access to variables and functions used or maintained by the interpreter.
52+
53+
🔹 PRACTICAL:
54+
>>> import sys
55+
>>> sys.platform
56+
'linux'
57+
58+
✅ `sys.platform` tells you which OS Python is running on.
59+
60+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
61+
4️⃣ MODULE: Importing a Custom Python File
62+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
63+
64+
🔹 SCENARIO:
65+
You created a custom Python file: `hello_world.py`
66+
67+
It contains:
68+
-----------------------
69+
print("Hello world")
70+
chai_one = "lemon tea"
71+
72+
def chai(name):
73+
print(name)
74+
-----------------------
75+
76+
🔹 PRACTICAL:
77+
>>> import hello_world
78+
Hello world
79+
lemon tea
80+
81+
✅ The code at the top level runs when imported.
82+
✅ Use functions and variables:
83+
>>> hello_world.chai("mint tea")
84+
mint tea
85+
86+
>>> hello_world.chai_one
87+
'lemon tea'
88+
89+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
90+
5️⃣ RELOAD MODULE using importlib
91+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
92+
93+
🔹 THEORY:
94+
If you change the contents of a module file, Python won't automatically reload it.
95+
You must use `importlib.reload()` to reflect changes without restarting Python.
96+
97+
🔹 PRACTICAL:
98+
>>> from importlib import reload
99+
>>> reload(hello_world)
100+
Hello world
101+
lemon tea
102+
103+
✅ This re-runs the module code and updates its content in memory.
104+
105+
106+

01_basics/word.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from hello_world import chai
2+
3+
chai("ginger tea")

inner-working/image.png

29.9 KB
Loading

inner-working/instructions.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Python is an object-oriented programming language like Java. Python is called an interpreted language. Python uses code modules that are interchangeable instead of a single long list of instructions that was standard for functional programming languages. The standard implementation of Python is called "cpython". It is the default and widely used implementation of Python.
2+
Internal working of Python
3+
4+
Python doesn't convert its code into machine code, something that hardware can understand. It converts it into something called byte code. So within Python, compilation happens, but it's just not in a machine language. It is into byte code (.pyc or .pyo) and this byte code can't be understood by the CPU. So we need an interpreter called the Python virtual machine to execute the byte codes.
5+
6+
7+
Internal Working of Python
8+
How is Python Source Code Converted into Executable Code
9+
10+
The Python source code goes through the following to generate an executable code
11+
12+
Step 1: The Python compiler reads a Python source code or instruction in the code editor. In this first stage, the execution of the code starts.
13+
Step 2: After writing Python code it is then saved as a .py file in our system. In this, there are instructions written by a Python script for the system.
14+
Step 3: In this the compilation stage comes in which source code is converted into a byte code. Python compiler also checks the syntax error in this step and generates a .pyc file.
15+
Step 4: Byte code that is .pyc file is then sent to the Python Virtual Machine(PVM) which is the Python interpreter. PVM converts the Python byte code into machine-executable code and in this interpreter reads and executes the given file line by line. If an error occurs during this interpretation then the conversion is halted with an error message.
16+
Step 5: Within the PVM the bytecode is converted into machine code that is the binary language consisting of 0's and 1's. This binary language is only understandable by the CPU of the system as it is highly optimized for the machine code.
17+
Step 6: In the last step, the final execution occurs where the CPU executes the machine code and the final desired output will come as according to your program.
18+
19+
How Python Internally Works?
20+
21+
Code Editor: Code Editor is the first stage of programs where we write our source code. This is human-readable code written according to Python's syntax rules. It is where the execution of the program starts first.
22+
Source code: The code written by a programmer in the code editor is then saved as a .py file in a system. This file of Python is written in human-readable language that contains the instructions for the computer.
23+
Compilation Stage: The compilation stage of Python is different from any other programming language. Rather than compiling a source code directly into machine code. python compiles a source code into a byte code. In the compilation stage python compiler also checks for syntax errors. after checking all the syntax errors, if no such error is found then it generates a .pyc file that contains bytecode.
24+
Python Virtual Machine(PVM): The bytecode then goes into the main part of the conversion is the Python Virtual Machine(PVM). The PVM is the main runtime engine of Python. It is an interpreter that reads and executes the bytecode file, line by line. Here In the Python Virtual Machine translate the byte code into machine code which is the binary language consisting of 0s and 1s. The machine code is highly optimized for the machine it is running on. This binary language is only understandable by the CPU of a system.
25+
Running Program: At last, the CPU executes the given machine code and the main outcome of the program comes as performing task and computation you scripted at the beginning of the stage in your code editor.
26+
27+
Python Libraries/Modules
28+
29+
When you import libraries or modules in your Python program. Firstly python checks if the given module is built-in, and executes the corresponding C code. If the module is not built-in then the list of directories is defined in sys. path. the directory of the input script, and directories listed in the PYTHONPATH. if a .py file corresponds to the modules imported, Python creates a new module object, On executing the code in the .py file within the object's namespace. Then Python compiles source code into byte code( the .pyc file), allowing for quicker execution
30+
Compiler Vs Interpreter
31+
32+
In the system both the compiler and interpreter are the same they convert high-level code to machine code. The interpreter converts source code into the machine when the program runs in a system while a compiler converts the source code into machine code before the program runs in our system.
33+
34+
Compiler
35+
36+
37+
Interpreter
38+
39+
The compiler is faster, as conversion occurs before the program executes.
40+
41+
42+
The interpreter runs slower as the execution occurs simultaneously.
43+
44+
Errors are detected during the compilation phase and displayed before the execution of a program.
45+
46+
47+
Errors are identified and reported during the given actual runtime.
48+
49+
Compile code needs to be recompiled to run on different machines.
50+
51+
52+
Interpreted code is more portable as it can run on any machine with the appropriate interpreter.
53+
54+
It requires more memory to translate the whole source code at once.
55+
56+
57+
It requires less memory than compiled ones.
58+
59+
Debugging is more complex due to batch processing of the code.
60+
61+
62+
Debugging is easier due to the line-by-line execution of a code.
63+
64+
65+

0 commit comments

Comments
 (0)