Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

A Beginner’s Guide to Python Programming Language

Last Updated on November 29, 2022 by Prepbytes

Python is a general-purpose programming language created by Guido van Russum in the year 1991. It became very popular because it is easy to learn and it has a vast number of applications. In this article, we will learn more about what is python language and, what is python used for.

What is python language?

Python is an Interpreted, high-level, multi-purpose, object-oriented programming language.

  • Interpreted:- It executes the program line by line. It compiles a program by itself. It converts source code into byte code.

  • High-level:- we don’t need to convert source code into machine code, it automatically converts it for us.

  • Object-oriented:- Python also supports object-oriented programming. We can write object-oriented, procedural, or functional code in python.

What is python used for?

Let’s see what is python used for:

  • Web Applications:- Python has some popular frameworks to build web applications like Django and Flask.
  • Machine Learning:- With help of python libraries like TensorFlow, Scikit Learn, and Pytorch Python makes machine learning very easy.
  • Data Analysis:- Python libraries like Pandas, Numpy, and Matplotlib are used to do data analysis.
  • Software Development:- Software Development becomes very easy with the use of Python libraries like Scons, Buildbot, Apache Gump, Roundup, and Trca.
  • Desktop GUI Applications:- Kivy, Tkinter, and PyQt are the Python libraries used to build Desktop GUI Applications.
  • Image Processing:- Image Processing in Python can be done using Python libraries like OpenCV, Pillow, and SimpleITK.

How does python works?

We need to know some features of python to see how python works.

  • To write comments in python we can use a hashtag. Comments are generally written to understand code to others or ourselves.
  • Python has many inbuilt data types including int, float, boolean, string, lists, set, and, tuples. We can use all of the data types according to our needs.
  • There are many keywords in python like True, False, int, in, and not. Which are used to perform specific functions.
  • We can use types of loops in python for and while. These loops are used to repeat a process any number of times.

How to install python?

Installation of python depends on the type of operating system you are using like Windows, Mac, or Linux. Generally, In Mac and Linux operating systems python is installed by default. Let’s see how can we install Python in various operating systems.

Installing Python on windows:-

  • You can download the latest version of python from its official website: https://www.python[com]org/downloads/
  • After the completion of the download, you can click on the installer to install python on your device.
  • Now you can wait till the installation process is complete.
  • You can check whether python is installed by typing the below command in cmd.python

If the above command shows the python version as output then python is successfully installed on your device.

Installing Python on Mac:-

  • First, you need to install Homebrew from this website https://brew.sh/.
  • After the installation, go to brew and type the below command to install python3 on your device.
    brew update
    brew install python3
  • We don’t need to set the path explicitly. Homebrew do it for us.

Installing Python on Linux:-

  • To install Python on Linux, open the terminal and type the below command to install python on Linux.

    Sudo apt install python3

How to run python code?

There are multiple ways to run python code. Let’s see some ways to run a python code.

Run Python code with CMD:-

First, open cmd and type python in cmd.

C:\Users\Admin>python

We will get the below output if python is installed on your device.

Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.

Now we can write code in the python console.

>>> print(“Hello World!”)

We will get the below output.

Hello World!

We can also write code of arithmetic operations.

>>> 2*3+6

We will get the below output.

12

Run Python code with a .py file.

First, open any simple text editor and write the below python code in that file.

print(“Hello World Again”)
print(5+10*2)
print(“This is .py file”)

Now save the file with the .py extension. For example mycode.py here mycode is the name of the file and .py is an extension.

After that open cmd where you saved your file and type the below command.

E:\>python mycode.py

The output is given below

Hello World Again
25
This is .py file

Indentation in Python:-

We know that many programming languages like C, C++, and Java use curly brackets to define their scope. While Python uses indentation to define the scope. Indentation is a white space in front of the beginning of the line. Let’s see what indentation looks like with help of an example.

class Prepbytes:
         def my_method(self):
                print(“Hello World!”)

In the above, example we can see there is white space before the second line and a third line which is called indentation in python. If we do not provide indentation the code will throw an error.

How to write comments in Python?

We can write comments using the hashtag (#) in Python. Comments are used to describe what is logic behind the implementation of a particular piece of code. Comments do not affect the program flow. Let’s see how we can write comments in python.

Single-line comments:-

It is very easy to write single-line comments in python. We can use the hashtag and after that, we can write a single-line comment. Let’s see it with help of an example.

This method will return the sum of two numbers
def find_sum(int num1,int num2):
return num1+num2

In the above example, we can see that a single-line comment is used to describe the working of a method.

Multi-line comments:-

We can write multi-line comments in python by writing a hashtag before each line of comments. Let’s see how to do that with help of an example.
This class is used to perform some arithmetic operations
There are two methods to perform addition, subtraction, multiplication, and, division

class Calculator:
def addition(self,num1,num2):
return num1+num2

    def subtraction(self,num1,num2):
        return num1-num2

    def multiplication(self,num1,num2):
        return num1*num2

    def division(self,num1,num2):
        return num1/num2

In the above program, we added multi-line comments to describe what the class is and another line to describe what is inside the class.

Variables in Python:-

Variables are used to strode the data or information. In other programming languages like C, C++, and Java we need to define what type of variable it is (for ex: int num) but in python, we don’t need to define the type of a variable. We can directly define a variable in python. Let’s see how to define a variable in python with help of an example.

num=10
name=”prepbytes”
amount=230.50

In the above program, we created three variables one variable store the int value, the second variable stores the string value, and the third variable stores the float value. But we did not give data type to any of the variables because python does it automatically.

Leave a Reply

Your email address will not be published. Required fields are marked *