Introduction to Python

0
130
Introduction to Python

Hi, everyone, I hope you all are doing great and learning new things so to enhance your knowledge today I will give a brief introduction to Python programming language and why is it important to learn it in today’s era.

What is Python and Why do we use it?

Python is a general-purpose powerful high-level programming language like java, ruby, and Perl. It has clear, readable syntax and has many modules, classes, and exceptions.  It comes with a large standard library that supports many common programming tasks such as connecting to web servers, searching text with regular expressions, reading and modifying files.

Python supports many operating systems. Python is an interpreted programming language which means  It allows you to run the same code on multiple platforms without recompiling it which lets you work quickly and the integration with other systems more efficient.

There are two major Python versions:

  • Python 2
  • Python 3

And there are quite some differences with both versions. For example, one difference between Python 2 and 3 is the print statement. In Python 2 “print” statement is not a function, and therefore it is called without parentheses. However, in Python 3 it is a function and must be called with parentheses.

Python 2:-
print "Hello World"

Python 3:-
print("Hello World")

Python is a very simple language and has a very straightforward syntax. It encourages programmers to program without being prepared to code. The simplest command in Python is the “print” command- it simply prints out a line.

Also Read: Web Scrapping Using Python

Comments in Python:

Comments in any programming languages are used to describe what the code does so if someone else is looking at your code they don’t have to pick up the phone to call you just to know what function you wrote in the code so it is always a best practice to write comments for your code.

In order to all Comments in your code you just have to use # to start the comment. Everything after the # sign is ignored by the interpreter.

Unfortunately, Python does not support Multiline comments.

#This command prints out hello world
print("Hello World")

Indentations in Python:

The main thing in Python is it uses indentation to define code blocks like where a function starts and where it ends, instead of curly braces. Python supports both tabs and spaces, but the standard indentation requires standard Python code to use four spaces. For Example:

a = 1
if a == 1:
    #four spaces indentation.
    print("a is 1.")

Beginning with Python programming:

1) Finding an Interpreter:

Before we start Python programming, we need to have an interpreter to interpret and run our programs. There are certain online interpreters like http://ideone.com/ or http://codepad.org/  that can be used to start Python without installing an interpreter.

Windows: There are many interpreters available freely to run Python scripts like IDLE ( Integrated Development Environment ) which is installed when you install the python software from http://python.org/

Linux: For Linux, Python comes bundled with the Linux.

Also Read: Introduction to Shell Scripting

Writing the first Program:

Here you go with the first Python Code:

#This command prints out hello world
print("Hello World")

Output:

Hello World

Now let’s analyze the code line by line for better understanding.

Line 1: As already told above that Comments begin with #. So whatever is written after the # is only for the readability of the code and understanding what it does and it is ignored by the Python interpreter.

Line 2: In a Python script to print something on the console print() function is used – it simply prints out a line ( and also includes a newline unlike in C ).

If you like the article please comment down below let us know if you want any specific topic to be covered share with your friends and stay tuned for more articles coming up.