#1 INTRODUCTION TO PYTHON

Updated: Jun 14, 2020

Programming languages ​​such as C, Pascal and FORTRAN are more focused on the functional aspects of programming. In these languages, more emphasis is placed on writing code using functions. For example, you can imagine a C program as a combination of several functions. Computer scientists thought it would be easier for humans to understand whether programming was based on real-world examples. Therefore, we developed object-oriented programming languages ​​like Java and NET, and programming was done through classes and objects. Programmers started the move from C to Java, and Java quickly became the most popular language in the software community.


In Java, programmers need to express logic using only classes and objects. It is not possible to write a program without creating at least one class. This makes programming long. For example, a simple program in Java that adds two numbers looks like this:


//JAVA PROGRAM TO MULTIPLY TWO NUMBERS
class Multi
{
    public static void main(String args[])
    {
        int a=5, b=4;
        int c=a*b;
        System.out.println("Multiplication of 4 and 5 is: "+c);
    }
}

Programmers have found that this type of coding is more time consuming in certain cases where you do not need to go to classes or objects. In such cases, I don't want to create a class or object. Rather, they want to write C-style coding. The same program that adds two numbers can be written in C as:


// C PROGRAM TO MULTIPLY TWO NUMBERS

#include<stdio.h>
void main()
{
     int a=5, b=4;
     int c=a*b;
     printf("Multiplication of 4 and 5 is %d",c);
}

Of course, the above program is almost the same as the Java program. The code length has been improved. Another problem is that in the C language of programmers, you miss the object-orientation that C lacks. Object orientation is an advantage when you want to work with heavy projects. Today, programmers need C-style coding as well as Java-style object orientation. Java style coding. The only answer to their requirements is Python!


Python

Python is a programming language that combines the features of C and Java. It provides an elegant style of C-like program development. When programmers want object orientation, Python provides Java-like classes and objects. In Python, the program that adds two numbers looks like this:


a=4
b=5
c=a*b
print("Multiplication of 4 and 5 is ",c)

The above code is easy to understand and develop. Therefore, Python is gaining popularity among programming people. Of course, Python has a few other features that we'll cover later, so most programmers can choose. Looking back a little history, Python was developed by Guido Van Rossum in 1991 at the Center for Mathematics and Computer Science, which is controlled by the Dutch government. Van Rossum is working on a project to develop a system utility in C and had to interact with the Bourne shell available on UNIX. He felt the need to develop a language that bridges the gap between C and the shell, which led to the creation of Python. Van Rossum chose the Python name from the Flying Circus of the television show Monty Python as the name of the new show. The first working version of Python was ready by the beginning of 1990, and Van Rossum released it to the public on February 20, 1991. The Python logo shows a snake with Iwo intertwined, as shown in Figure 1.1.



Python is open source software. This means anyone can freely download it from www.python.org and use it to develop their programs. Its source code can be accessed and modified in the project as needed.


Python features

There are many reasons why Python is so popular in the programming community. Here are some of the key features of Python:


  • Simple: Python is a simple programming language. When reading a Python program, we feel like reading English sentences. That means clearer and less stress in understanding the syntax of the language. Therefore, the program is easier to develop and understand.

  • Easy to learn: Python uses very few keywords. The program uses a very simple structure. This makes program development in Python easier. Also, Python is similar to C. Most C language constructs are also available in Python. Therefore, the transition from C to Python is easy for programmers.

  • Open Source: No need to buy Python software. Python is freely downloadable from the www.python.org website. The source code can be read, modified, and used by the program as needed by the programmer.

  • High-level languages: There are two types of programming languages, low-level and high-level. Low-level languages ​​use machine instructions to develop programs. These instructions interact directly with the CPU. Machine and assembly languages ​​are called low-level languages. High-level languages ​​use English words to develop programs. These are easy to learn and use. Like COBOL, PHP, or Java, Python is called a high-level programming language because it uses English words in its programs.

  • Dynamically Typed: In Python, an assignment statement does not have to declare what binds a name to an object, and an object can be of any type. If a name is assigned to one type of object, it can later be assigned to another type of object. This is the meaning of the word Python is a dynamically typed language. Languages ​​such as C and Java are statically typed. These languages ​​require the variable names and data types to be well documented. Attempting to assign an object of the wrong type to a variable name will trigger an error or exception.

  • Platform Independent: Byte-codes are generated when Python programs are compiled using the Python compiler. Python byte-code represents a fixed set of instructions that are executed on all operating systems and hardware. The Python Virtual Machine (PVM) allows anyone to execute these byte-code instructions on any computer system. Therefore, Python programs are not dependent on any particular operating system. Python can be used on almost any operating system, including UNIX, Linux, Windows, Macintosh, Solaris, OS/2, Amiga, AROS, AS/400. This makes Python a programming language for any network or Internet.

  • Portable: A program is said to be portable if it has the same result on any computer in the world. Python programs are platform independent, so you get the same results. Once you've written a Python program, you can run it on any computer system that uses PVM. However, Python also contains some system-dependent modules (or code) that are specific to the operating system. If a programmer wants their software to be completely portable, such code should be kept in mind during software development.

  • Procedural and Object-Oriented: Python is a procedural-oriented, object-oriented programming language. In procedure-oriented programming languages ​​(such as C and Pascal), programs are built using functions and procedures. But in object-oriented languages ​​(such as C++ and Java), programs use classes and objects.

Think about objects and classes. Objects are those that physically exist in the real world. Almost everything is included in this definition. Take a dog named Snoopy. Snoopy is an object because it physically exists in our home. An object has a behavior represented by its attributes (or properties) and actions. For example, Snoopy has attributes such as height, weight, age, and color. These attributes are represented in programming by variables. Similarly, Snoopy can perform actions such as barking, biting, and eating. These actions are represented in programming by methods. Therefore, the object contains variables and methods.

On the other hand, the class does not physically exist. Classes are just abstract concepts that describe the general behavior of some objects. For example, dog is a class. When you talk about dogs, pictures of your head, body, legs and tail come to mind. This imaginary image is called a class. When we take Snoopy, she has all the features that are in our minds, but she is physically present and therefore subject to the dog class. Similarly, all other dogs like Tommy, Charlie and Sophie behave like Snoopy. Therefore, they are all objects of the same class, the dog class. It's important to understand that an object called Snoopy physically exists, but a class dog does not. This is just a picture in our mind, including some attributes and actions on an abstract level. Taking Snoopy, Tommy, Charlie, and Sophie, they are all dog-class objects because they have these attributes and actions.

As explained in the previous paragraph, classes represent the general behavior of objects. This general behavior is represented by attributes and actions. The attribute a represented by the variable and the action is executed by the method (function). Therefore, like objects, cl also contains variables and methods. Figure 1.2 shows the relationship between classes and their objects.



Similarly, parrots, sparrows, pigeons, and crows are objects of birds. It should be understood that birds (classes) are just the idea of ​​defining some attributes and actions. Parrots and sparrows have the same attributes and behaviors, but physically exist. Therefore, they are objects of class bird.

Object-oriented languages ​​such as Python, Java, and NET use the concept of classes and objects programmatically. No memory is allocated when the class is created because the class does not physically exist. However, because the object physically exists, another memory block is allocated when the object is created. The Python language treats variables, lists, functions, arrays and everything as objects.


  • Interpretation: Program code is called source code. After writing a Python program, you need to compile the source code using a Python compiler. Python compilers convert Python programs into intermediate code called bytecode. This bytecode is executed by PVM. Inside PVM, the interpreter translates bytecode instructions into machine code that the processor understands, executes, and produces results.

  • Extensible: Any program or piece of code written in C or C++ can be integrated into Python and run using PVM. This is what you'll see in standard Python downloaded from www.python.org. There are other flavors of Python that allow you to integrate programs in other languages ​​into Python. For example, Jython helps you integrate Java code into a Python program and run it in a JVM (Java Virtual Machine). Similarly, IronPython helps to integrate .NET programs and libraries into Python programs to run in the CLR (Common Language Runtime).

  • Embeddable: You can insert a Python program into a C or C++ program. Some applications are already developed in Python and can be integrated into other programming languages ​​such as C, C++, Delphi and PHP. Java and NET. This means programmers can use these applications for the benefit of various software.

  • Huge Library: Python has a large library that can be used on any operating system, including UNIX, Windows, Macintosh. Programmers can use the modules available in the Python library to develop their projects very easily.

  • Scripting language: Scripting language is a programming language that does not use a compiler to execute source code. Rather, it uses an interpreter to translate the source code into machine code (on the fly) on the fly. In general, scripting languages ​​perform supporting tasks for larger application and software examples. PHP is a scripting language that performs the supporting tasks of getting an in from an HTML page and sending it to the web server software. Python is considered a u-scripting language because it is interpreted and used on the Internet to support other software.

  • Database connection: A database represents the software that stores and manipulates data. For example, Oracle is a popular database that you can use to store and manipulate data in the form of tables. Python provides an interface for connecting your program to all major databases such as Oracle, Sybase, MySQL and more.

  • Scalable: A program is scalable if it can be moved to another operating system or hardware and can take full advantage of the new environment in terms of performance. Python programs are scalable because they can run on any platform and effectively use the features of the new platform.

  • Battery Included: Python's huge library contains some small applications (or small packages) already developed and ready for programmers. These small packages are easy to use and maintain. Therefore, programmers often do not need to download separate packages or applications. This will give you a head start on many projects. These libraries are called battery included. Here are some interesting batteries or packages.


Executing a Python program

Suppose you create a Python program named x. py. Where x is the program name and .py is the extension name. All Python programs are typed with the extension name py. After entering the program, the next step is to compile the program using the Python compiler. The compiler translates a Python program into another piece of code called bytecode.


Bytecode represents a fixed set of instructions that represent all operations such as arithmetic operations, comparison operations, memory-related operations performed on any operating system and hardware. This means that byte instructions are either system-independent or platform-independent. Each bytecode instruction is 1 byte in size, so it is called with the name bytecode. These bytecode instructions are contained in the x.pyc file. Here, the x.pyc file represents a Python compiled file.


The next step is to run the program. It cannot be executed by passing the bytecode directly to the computer. Any computer can only run binary code consisting of 1s and 0s. Binary code is also known as machine code because it is easy for a machine (computer) to understand. Therefore, you need to translate the bytecode into machine code so that the computer can understand and execute it. For this purpose you must use PVM (Python Virtual Machine).


PVM uses an interpreter that understands the bytecode and translates it into machine code. PVM first understands the computer's processor and operating system. It then translates the bytecode into machine code that the processor understands, and a format that the operating system understands. The processor then executes these machine code instructions and displays the results.


The interpreter translates the program source code line by line. Therefore, it's slow. The interpreter inside PVM runs the Python program slowly, fixing this problem. Some types of Python add a compiler to PVM. This compiler translates bytecode into machine code, but is faster than an interpreter. This compiler is called the JIT (Just in Time) compiler. The advantage of the JIT compiler is that it speeds up Python program execution and improves performance.


Keep in mind that JIT compilers are not available in all Python environments. For example, standard Python software is called CPython and is written using C, which does not include a JIT compiler. However, written in the Python language itself, PyPy uses a JIT compiler in addition to the PVM interpreter. Therefore, Python's PyPy flavor definitely runs Python programs faster than CPython. The figure shows the steps to run a Python program.


Normally, when I compile a Python program, I can't see the .pyc file generated by the Python compiler and the machine code generated by PVM. This is done inside the memory and eventually the output is displayed. For example, if your Python program name is x.py, you can compile it with a Python compiler as follows:


C:/>python x.py

In the above statement, python is the command to call the Python compiler. The compiler needs to convert the x.py file to the bytecode equivalent of the file x.pyc. Instead of doing this, the compiler will display the output or results directly.


If you look, you won't find any files with the extension .pyc in the directory. The reason is that every step in the sequence is: source code → byte code → machine code → output Internally completed, the final result is displayed. So after running, I cannot find the .pyc file in the directory.


You can use the following command to create a .pyc file separately from the source code.

C:/>python -m py_compile x.py

The above command calls the Python compiler with the -m option. -m represents the module and the module name is .py_compile. This module will generate a .pyc file for the specified .py file. The compiler uses name _pycache_ to create another directory that stores .pyc files in the current directory. The pyc file name will look like x.cpython-34.pyc. The word cpython here indicates that you are using a Python compiler written using C. This is of course the standard Python compiler.


So the question is that I know that the "What is the purpose of the pyc file?" profile contains bytecodes. These files will be retrieved after the compilation is complete. So the first step is done. The next step is to interpret them using PVM. This can be done by calling the Python compiler with:

C:/>python x.cpython-34.py

In this case, the pyc file references the Python compiler. Now the python compiler frees up the bytecode in this pye file, skipping the first step which needs to convert the source code to bytecode. This file is run directly by PVM to produce the output. Therefore, the program execution time is reduced and performance is improved. This is why after the project is complete, the PYE file is distributed to users who can run it directly using PVM and view the output.



52 views0 comments

Recent Posts

See All
 
  • Facebook
  • Instagram
  • Twitter
  • YouTube
  • Pinterest
  • Tumblr Social Icon

© Copyright MH35.ONLINE . All Rights Reserved

Designed by Krushna_005