What is Python and How to use it?

Guido Van Rossum, created a high-level general purpose programming language in 1991 which came to know as Python. Python is very easy to learn and has immense benefits. It is often used for Machine Learning and in Data Science. It is an open source software.

How to download Python?

Visit https://www.python.org/downloads/ to download the latest version of python.

How to open?+

After installing it in your system, search for ‘IDLE (Python)‘ in your system. Open the python idle. It acts as a shell. Press CTRL+N to open the editor window.

Creating Variables in Python

To create variables in python, you need to require to mention the datatype as traditionally done in C or C++. Just type the following code.

a= 4

b= “Hello World!”

c= 2.35

For the above code, the value of 4 gets stored in the variable ‘a’, while the string i.e. “Hello World!” gets stored in variable ‘b’ and the float value 2.35 gets stored in ‘c’. You can call these variables anytime during the program.

Now save the file using ‘CTRL+S’ and run the console. Well! Nothing will get printed unless you give the print command.

To print these variable, add the following code

print(a)

print(b)

print(c)

Save the file again and run either using the run button in the above menu or using F5.

To know the type of variable use the following code

print(type(a))

print(type(b))

As the 4 is an integer stored in variable ‘a’, we shall get the output of print(type(a)) as int, whereas for ‘b’ it shall be ‘str’ as it is a string. Why don’t you implement it and check?

To take user input into your variable and print it, use the following command

d=input(“Enter any number”)

print(d)

Isn’t it interesting? Here let’s go further and study arithmetic operations in python.

—> Write a python program to print the sum of two user input numbers.

a= input(“Enter the first number: “)

b= input(“Enter the second number: “)

c= a+b

print(“The sum of entered number is”, c)

Similarly, for subtraction, multiplication and division replace ‘+’ operator by ‘ – ‘, ‘ * ‘ and ‘/’ operators respectively.

Here are a few more important operators:

S. No. Operator Name Operator Symbol Operator Function Example

1

Modulus Operator % Gives remainder after the division of operated numbers. 11%3= 2

2

Floor Division Operator

// Gives Quotient after the division of operated terms. 11%3= 3

3

Increment Operator

++

Increments the operated number by 1

b=a++, if a=1 then b=2

4 Decrement Operator Decrements the operated number by 1

b=a– if a=3 then b=2

Following is another important operator:

5

Exponential Operator ** A number to the power another number 2**4= 16

Now let’s study if-elif-else statement in python:

Study other operators by accessing the following link: https://www.tutorialspoint.com/python/python_basic_operators.htm

Check your knowledge: Go for this free test: https://goo.gl/forms/13XRd6TuUfjycBGm1

If – Elif -Else Statement in Python

If-Elif-Else is decision making a statement in python. If a particular case is true then the code inside that specific case is implemented otherwise it jumps over to the next case. Let’s understand it using an example:

–> Write a python program to check whether the entered number is even or odd.

a=input(“Enter the number”)

if(a%2==0):

print(“The Entered Number is even”)

else:

print(“The Entered Number is odd”)

In this program, the user will give an input number which will be stored in ‘a’.

If the remainder when a is divided by 2 is zero, then a will be even. That is what meant by line 2 and 3 of the program. Or if the condition is false then it will be odd.

Note that in the above program we have given some spaces in line 3 and 5. These are called indents. In C and C++, the code inside if the function was written inside curly braces { }. But here we write to them by giving such indents. If indents are not given then the python program will show error.

Now try doing a program by yourself.

–> Write a program to check whether the sum of two numbers is positive, negative or zero.

Hint: If there are more than 2 cases then use elif: function for all the intermediatary statement.

Now further, in the coming chapters, we will study functions in python and many more.

One thought on “What is Python and How to use it?

Leave a comment