learn python with me python introduction

Python Introduction – Learn Python with me

Why Python?

Welcome to this chapter of Python introduction. Python has been one of the most promising programming languages in the market for a long time now. And all thanks to its simpler way of coding, it is a go-to choice for the machine learning and artificial intelligence aspirants today.

When Guido Van Rossum initially introduced Python in 1991, other programming languages were doing pretty well in the market already. However, they had a few drawbacks as well. For instance, the C programming language stressed more on the functional characteristics of the programming. And when object-oriented programming languages like Java and .Net began grabbing the eyeballs, the coding experience became easier through the concepts of classes and objects. However, it was still lengthier to write a program, even for a small operation such as addition or subtraction.

Let’s now look at the programs from Java and Python for the subtraction operation to understand the former’s verbose nature.

//Java program to subtract two numbers
class Subtract //creates a class
{
	public static void main(String args[]) //execution starts from main method in Java
	{
		int a,b; //declare two variables
		a=b=5;
		System.out.println("Subtraction result: "+ (a-b)); //this code will display the result
	}
}
#Python program to subtract two numbers
a=b=5 #this stores the value 5 in both a and b variables
print("Subtraction result= ", (a-b)) #shows the output

As you can see in the programs above, the Python code is comparatively shorter than the Java code. This is one reason why Python is becoming so popular, as it is easier to write and understand the code.

You might also like: Install Python and Run First Python program

Python Introduction

Python is a programming language that combines the features of both C and Java. For example, you can follow the stylish C-like coding along with an object-oriented approach in Python. Yes, Python supports classes and objects like Java. Python is still an open-source language despite its amazing features, and you can download it from https://www.python.org/. We will go through the downloading steps of Python later and let us now dive into the features of Python

Features of Python

  • Simple: As mentioned above, Python is a simple programming language, and the code in Python is also more like English sentences.
  • Easy to learn: Since the programming approach is not very complex, one can easily learn Python, and for those of you who already have prior coding experience, learning Python will be comfortable for you.
  • Open Source: Python is open-source software, and you don’t have to pay money to install it on your machine.
  • High-Level Language: As Python has code written in English words like Java and PHP languages, it is considered a high-level language.
  • Dynamic Style: It’s not really necessary to declare in Python. A name assigned to an object of a particular type can be assigned later to an object of a different type. That’s why Python is a dynamically typed language.
  • Platform independent: The byte code on a computer that is generated by Python compiler after compiling you can execute a Python program on any other computer system that runs on UNIX, Windows, Linux, Solaris, Macintosh, OS/2, AS/400, AROS, AMIGA operating systems and has Python Virtual Machine (PVM) installed in it.
  • Portable: Since Python is a platform-independent language, it can be run on any platform and produce the same results. But, based on the operating system used, the dependency might exist for certain Python modules.
  • OO and PO: Python language is both Object-Oriented and Procedure Oriented.

A peek at Object-Oriented Programming Language

An object is anything that exists physically in this world, and a Class is an abstract idea that defines the behavior of objects. For example, let’s consider a dog named Tommy, who exists in the real world. If Tommy is considered as an object, then Dog is the class. Tommy will action all the characteristics that are defined under Dog class. The attributes of the dog class such as Color, Age, Weight, and Height will be the attributes for Tommy and any other dog on this planet. Let’s understand it in simpler terms in the image below.

A peek at Object Oriented Programming Classes and Objects copy

As you can see, the dog class has attributes such as Color, Age, Weight, and Height. It also has methods which are the activities of dogs such as Sleeping, Running, and Barking. So when we create the objects for the real dogs Tommy and Jimmy, these objects will also have attributes and methods of the class Dog. Easy, isn’t it? No wonder why object-oriented programming languages became so popular in the past few decades.

What happens when you run a Python Program?

Every Python program has an extension of .py which means that if you want to create a Python file with the name Mountains, it should be saved as Mountains.py in the directory. After entering the code in the Mountains.py file, the next step would be to save the file. You will then need to compile the Mountains.py file using the Python compiler.

Python Program Execution

The Python compiler converts the source code in the Mountains.py file into another code called byte code in a .pyc extension file, containing instructions of memory-related operations, comparison operations, arithmetic operations, etc. Python Virtual machine (PVM) uses an interpreter to convert the byte code into machine code that can be understandable to the computer machine on which the Python program runs. The processor then executes the instructions from the machine code, and the output is eventually displayed on the screen.

Why PVM (Python Virtual Machine)?

A computer understands the instructions of machine code. That’s why the source code in the Python program must be converted to machine code, as mentioned above, and the Python Virtual Machine is the key component that drives this conversion.

Differences between C and Python

CPython
Procedure OrientedObject Oriented
FasterSlower compared to C
Supports Switch StatementDoes not support Switch Statement
C uses pointersDoes not use pointers
Variable in for loop does not increment automaticallyVariable in for loop increments automatically
Follows Static approachFollows Dynamic approach
Does not support classes, objects, inheritance, PolymorphismSupports classes, objects, inheritance, polymorphism

Differences between Java and Python

JavaPython
Mandatory to declare the datatypesType declaration is not a must in Python
Java supports Switch statementPython does not support Switch statement
Java supports Single and Multi-Dimensional arraysPython supports Single Dimensional arrays. Users should rely on third party apps like numpy to work with Multi-Dimensional arrays
Array index should be positiveArray index can be negative or positive
Memory is allocated and deallocated by Java Virtual MachineMemory is allocated and deallocated by Python Virtual Machine
Java programs are lengthyPython programs are compact
Java has do..while, while, for, and for each loopsPython has while and for loops
Scroll to Top