Python Tech Series… Day 1: Introduction To Programming and Python's Interpreter, PVE.

Hello Learner!. Welcome to Python Tech Series. I am Sai Murali, Software Engineer who will guide you about Python via this Tech Series. We will start right from Basics to Medium / Advance level by implementing different programs, and questions, and solving a lot of real-time problems. Interested!. Let's Dive in. Before you proceed further, create an account on GitHubfor open-source contribution, Install Python IDE from Python's Official website, Install Visual Studio Code or your favorite IDE, Git to track your program progress, and at last Pen and Book :P

The Goal of this Tech Series is to make you think like a computer scientist. A scientist observes the problems, draws predictions, computes things, and formulates the solution. Likewise, An Important Skill for any computer Scientist is Problem Solving. Problem Solving is the ability to understand the problem, formulate solutions to the problem, Perform Hypotheses, and explain the solutions very accurately.

The Programming Language: In this series, we are going to Learn/use Python language. Python(.py) is a High-Level programming language like c, c++, java, etc. In contrast, there are low-level programming languages such as assembly language, and machine language. Computers can run only programs written in a low-level language. we will process the High-Level written program to a low-level written program before they get executed. This extra processing takes some time, which is a small disadvantage of high-level languages. on the flip side, there are many advantages like being easy to read, write, understand, debug, maintainable, and Portable (Once written can you run in different OS). Low-level languages written in one OS need to be re-written in another OS. But what is programming??

According to Wiki, Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming involves tasks such as analysis, generating algorithms, profiling algorithms' accuracy and resource consumption, and the implementation of algorithms in a chosen programming language (commonly referred to as coding). In a nutshell, Computer programming or coding, is telling the computer to do something using a medium of instruction called programming language which understands by computers.

Coding involves Reading, understanding, modifying, Refactoring, and Testing the code. A good Code is short, Fast, elegant, and easy to read and understand.

Suppose you go for a jog every morning. You have to get a jog dress, shoes, water, Music device. Unknowingly we evaluate lots of data in this situation. We will check whether the shoes are cleaned or not. Have enough water in the bottle or not. Make sure the dress is cleaned. We will wear the dress and shoes, fill the water bottle, set the music based on mood, starts the run, and repeat the whole process every day.

Now let's relate the same situation to computer programming, you have to get a water bottle, water, dress, shoes, and music device (Gathering Resources) and check if the shoes are clean, the water is full, the device is on, (Conditions) based on the conditions we initiated the actions like filling the water, charge the music device, etc,(Methods or actions). At end of the procedure, we enjoy the nice morning jogging(Returning End result) and if you repeat the same task every day (Iteration).

When we write a program, we instruct the computer to do some tasks, but where do these actions happen? in many places, such as Ram (load and store the code and Data), CPU (gives processing power, performs Arithmetic and logical operations, keeps track of next command), and I/O- takes data from the input devices. Etc.

What is programming? At a basic level, A computer program is a recipe of instruction we tell the computer to perform a task. when we write a program, we write step-by-step instructions on what needs to be done to complete the task. when the computer executes the program - it read what we wrote and executes the program to the letter. The Recipe is written in code called - PROGRAMMING LANGUAGE

Programming language: are languages are like human language as they have syntax (the rules for how a sentence is constructed) and semantics (the actual meaning of the statements.).

image.png

There are two kinds of programs that process High-Level Programs into Low-Level Programs. 1. Interpreter: An Interpreter reads the program and executes it. It processes the program a little at a time, alternately reading lines and performing computations. An Interpreter converts each high-level program statement, one by one, into the machine code, during the program run.

2. Compiler: A compiler takes the whole project and converts the high level written source code to Object code or executable.

key Differences:

  • a. Compiler runs faster in comparison with Interpreter.
  • b. Compiler displays errors at a time (compile time or run time) where Interpreter displays errors line by line on go.
  • c. The compiler is based on the translation linking-loading model, whereas the Interpreter is based on Interpretation Method.

The 4W's of Python:

1. Who & When:

Python is a widely-used general-purpose, high-level programming language. It was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code(*gfg).

2. Why:

2. a : Protable: Python code once written, can be imported/used in any other Operating system. It's just a matter of copying the script between the machines. Python provides various options for coding portable graphical user interfaces, database access programs, web-based systems, OS interfaces, etc.

2. b: Integration with other software libraries: Python scripts can easily communicate with other parts of the application, Python can invoke C, and C++ libraries, and can Parse XML, and HTML files also.

2. c: Library Support (Personal Fav): Python comes with a large collection of prebuilt and portable functionality, known as the standard library. This library supports an array of application-level programming tasks, from text pattern matching to network scripting. In addition, Python can be extended with both homegrown libraries and a vast collection of third-party application support software. (*lp)

2. d: Speed of Development: to write a logic No of lines in python is one-third of the no of lines written in c, c++, Java, or any other high-level language. This enhances the speed of development, debugging, reading, and understanding of the program.

On the Flip Side, One Drawback of Python is its Execution Speed may not be as faster as compiled languages such as C, C++, Java, etc. Python translates the source code to an intermediate format known as Byte code and then interprets the byte code. Byte code provides portability, as it is a platform-independent format. However, because Python is not normally compiled all the way down to binary machine code. (*lp).

3. Where Python is being used: Google uses it for Search engine activities, Google keyboard, and Youtube use it for recommendations. dropbox for maintaining storage spaces, Pixer for animations, etc.

Once you write the program, how Python program gets executed in nutshell!.

Python Program Execution: Python program execution involves 3 steps. code, byte code, Python virtual machine (PVM).

image.png

The interpreter is another software program running other programs. It's a software package that reads the python prog & carryout all instructions written in the program. Python interpreter will be installed during the python installation only. Once any text file with some python code stored with the .py extension is fed into the interpreter and interpreter evaluates the code and creates a python version's byte code and sent to PVM for execution.

Once code is fed to the interpreter, first it evaluates for any syntax errors, if not interpreter generates a file containing a byte code version of the high-level code with a filename and extension .pyc (filename. pyc). Before Python Version 3.2 the .pyc files are created in the same directory where code resides and is hidden but after Python 3.2 we will store the .pyc files in a Pycache subdirectory. If we import any module, we will actually generate the byte code of that module. ex: import NumPy installs NumPy.pyc. .pyc will not change until the new source code changes and is fed again. We can port the byte code file to another operating system (python needs to be installed there).

The same Byte code file sent to PVM (Python Virtual Machine) is a big code loop that takes the byte code file and carries out the instructions written to the point.

But why the point, Python is slower: We will run the Python version Byte code (not intel x80 or AMD instr set) using Python's Virtual Machine but not on CPU Loop. This needs extra little time and memory. Hence Python is slower than compiled languages such as C, and C++.

Python Virtual Environments: Before we dive into Python Virtual environments, let's discuss a scenario. In your office, you are working on a project using Django 2. x version, and one of your teammates came up with a crazy idea that needs to be implemented using Django 3. x version. As every dev does, you create a directory and imported the Django 3. x at the System level. Suddenly your office project using Django 2. x stopped working and got incompatible versions error. This is because if we import modules at the System level, That will impact the project with unidentified errors. To avoid this, it's always suggested that we have to import/write the code logic by creating a virtual Env. Python has an option called venv which lets you create a virtual env in your specified directory and all technical imports, and debugging will be part of that Env. if you are working on a new project, you will create another python virtual Env and switch back between when necessary.

Let's get this in Practice. go to the directory open visual studio code pointing to that directory and follows the steps in the below image.

python -m venv <project PVE folder name>
#once installed, go the scripts in that venv lib
cd .\PVE folder_name\Scripts
#use command activate to actiavate and deactivate for deactivate
.\activate #activate the PVE
.\deactivate #deactivate the PVE.

image.png

Programming is always error-prone. Tracking the errors is called bugs and the process of tracking them down is called Debugging.

Syntax Errors: The rules for how a sentence is constructed. Python is very strict about syntax, if any statement is written and not following the exact syntax python will through Syntax Error and quit the further execution. ex: (1+8) is valid syntax, where +8) is not a valid Syntax.

Runtime Errors: These errors will appear during the execution time. These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened.

Semantic Errors: Program will get executed without any Syntax or Runtime errors but it will not do the right thing. The problem is that the program you wrote is not the program you wanted to write. The meaning of the program (its semantics) is wrong. Identifying semantic errors can be tricky because it requires you to work backward by looking at the output of the program and trying to figure out what it is doing. (*tp)

Let's try our first Program.. 'hello world'. open your visual studio, create venv, create a new file with any name and extension .py and write the below code snippet.

print("Hello World"); #print is an output function that prints the value inside the brackets to the user output console.

image.png

'#' is used for single-line comments and 
'" three quotes
             for Multiline 
                          comments. '''

Whoo!. It's a lot.. let's start with Basics such as Keywords, Variables, etc on Day 2. find the same in the below link.

Next Up. Day 2: Part1: saimuralipavanrayavarapu.hashnode.dev/pytho..

Day 2: Part2: saimuralipavanrayavarapu.hashnode.dev/pytho..

Copyrights as mentioned: gfg: geeks for geeks. lp: learning python 5th edition book. tp: Think Python.

Thanks for reading!. Keep Upskilling.