Learn Python – For Beginners

python programming
Image by Johnson Martin from Pixabay

Python is a general purpose interpreted high level programming language. Python is one of the best programming languages out there and learn python is one of the best decisions a computer or programming enthusiast can make. Python is comparatively easy when compared to other languages. Python can be used for web development, machine learning, data visualization, game development or anything that can be associated with computers.

There are two methods to use Python:

1.Downloading and Installing on your computer

2.Use a service like google colab or azure notebooks.

Downloading and Installing Python

You can download Python from its official website from this link. In our case we are downloading the latest version of python

Downloading Python from their official website

Installing Python

Open the file you downloaded.

Add Python to the Path

Check Add Python to the path and choose customize installation.

More installation options

You can modify the settings as per your needs. Click next and choose the installation path and install Python. It may take a few minutes depending on the power of your computer.

installation completed

If you get a similar message choose disable path length limit and you are good to go.

Python has been successfully installed and configured on your computer.

To use a service like colab or azure notebooks just go to their official sites and create an account.

Writing Your First Program

print("Hello World ! ")

To write our first program I will be using IDLE and for the rest of the programs I will be using azure notebooks which is available for free.

First open idle and press Ctrl + N to open a new file and copy the code in the above code window.

Hello World Program

Now click on run and select run module. Now save the program and output will be displayed.

Output of hello world program

Now lets run the same program on Azure Notebook.

Using Azure Notebook or Google Colab is more easy.

This is the same Hello World program on azure notebook.

hello world running on azure notebook

A program to add some numbers

In this program we will be declaring three variables called x,y,z and p. We will assign a value to x,y and z. We will add those three variables and store the sum on variable z. We will print variable z on the screen. The code for the program –

x = 78.69
y = 71.25
z = 57.61
p = x + y + z
print(p)

using python to add numbers

In the above program we assigned values to the variables before execution of the program. Now lets learn how to take input from the user while the program is being executed.

x = input("Enter the first number to add: ")
num1 = int(x)
y = input("Enter the second number to add: ")
num2 = int(y)
z = num1 + num2
print("Sum: ",z)

Now let me explain the above program. We declared a variable ‘x’ and using ‘input’ we accepted data from the user. Currently the data is a string. But we can’t do mathematical operations on strings. So we declared a new variable called ‘num1’ and converted the data entered by the user in variable x to an integer and stored the integer value on ‘num1’. We repeated the same step for second input and converted it to an integer value stored on the variable ‘num2’. We added the numbers and stored it on a new variable ‘z’. At last we printed the output.

Now the above code was a bit unnecessarily long. I deliberately made it so in order to better explain everything. The code below does the same thing and is short and simple.

x = int(input("Enter the first number to add: "))
y = int(input("Enter the second number to add: "))
print("The Sum is: ",x+y)

The output of the above program is given below

Learn Python: Output of the above program

The above code is much simpler. But our calculator can only add integers which is not very useful. So if we want to add and subtract decimal numbers we want to use ‘float’ instead of ‘int’. The program given below can add both integers and decimals.

x = input("Enter the first number to add: ")
num1 = float(x)
y = input("Enter the second number to add: ")
num2 = float(y)
z = num1 + num2
print("Sum: ",z)
Learn Python: Output of the above program that can add decimals.

Even though our program is now much better. It can only add numbers. In order to subtract, multiply or divide numbers it is very easy just replace the ‘+’ with ‘-‘ , ‘*’ or ‘/’ respectively.

x = float(input("Enter the first number to add: "))
y = float(input("Enter the second number to add: "))
print("The Sum is: ",x+y)
print("The Answer is: ",x-y)
print("The product is: ",x*y)
print("The Quotient is: ",int(x/y))
print("The Remainder is: ",x%y)

To obtain remainder you can use this ‘%’ between numbers. I added ‘int’ in line 6 because I wanted to only print the Quotient as integers. In line 7 we print the remainder of the division.

The output of the above program

If you want to end a line you can use ‘\n’ . This is a escape sequence. It starts with a backslash .

This is enough for our first tutorial. Below I am attaching a program for a very basic calculator that we will be explaining about in the coming tutorials.

#A very basic but helpful calculator i made in python with just the basics
#By RahulVk
import math

print("Welcome to the PyCalculator\n")
print("Choose which function you need\n")
print("1.Addition")
print("2.Subtraction")
print("3.Multiplication")
print("4.Division")
print("5.Squareroot")
print("6.Quadratic Equation Solver")
user_choice = input("Enter your choice here: ")
x = int(user_choice)
if(x == 1):
  print("You choose Addition\n")
  a = input("Enter the first number to Add: ")
  b = input("Enter the second number to Add: ")
  c = int(a) + int(b)
  print("\nSum: ",c)
elif(x == 2):
  print("You choose Subtraction")
  d = input("Enter the first number to Subtract: ")
  e = input("Enter the second number to Subtract: ")
  f = int(d) - int(e)
  print("\nAnswer: ",f)
elif(x == 3):
  print("You choose Multiplication")
  g = input("Enter the first number to Multiply: ")
  h= input("Enter the second number to Multiply: ")
  i = int(g) * int(h)
  print("\nProduct: ",i)
elif(x == 4):
  print("You choose Division")
  j = input("Enter the number to Divide: ")
  k = input("Enter the Divisor: ")
  l = int(j) / int(k)
  m = int(j) % int(k)
  print("\nQuotient: ",int(l))
  print("Remainder: ",m)
elif(x == 5):
    p = int(input("Enter a number to find its squareroot: "))
    q = math.sqrt(p)
    print("Squareroot: ",q)
elif(x == 6):
    a = float(input('Enter the coefficient of x square: '))
    b = float(input('Enter the coefficient of x: '))
    c = float(input('Enter constant: '))
    y=int(a)
    print("\nSo the equation is :",y,"x^2+",b,"x+",c)
    d = (b**2) - (4*a*c)
    if(d>0):
        sol1 = (-b-math.sqrt(d))/(2*a)
        sol2 = (-b+math.sqrt(d))/(2*a)
        print('The solutions are ',sol1,"and",sol2)
    else:
        print("No real roots")
else:
  print("\nINVALID CHOICE")
  

The output of the above program is given below

A Simple Calculator Made in Python

This is enough for our first tutorial. Let us know your thoughts in the comments below. Check out our other articles here .

6 thoughts on “Learn Python – For Beginners

Leave a Reply

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