Basic Python Programs | Python Examples

Basic Python Programs | Python Examples

11 June 2023 1 By Anshul Pal

The best way to learn any programming language is by practicing examples. This article contains the basic python programs with simple concepts. We totally recommend you solve the example by yourself and if you are not able to solve the examples, then jump to see the solution.

Python Program to Add Two Numbers.

this program add two numbers by taking input form user.

# Python program to Add two numbers
a = int(input("Enter the first number : "))
b = int(input("Enter the Second number : "))
sum = a + b 
print("The sum of", a ,  "and", b , "is" , sum)

In the above example, firstly we are taking input from users in the form of  variable a and variable b, and take int data type  because we want input in integer form (1,2,3,….). After that, we take a third variable which is named the sum . In sum, we add our a and b variables and then we use a print function to print our output. In a print function, we take the string “the sum of”. After that, we print our variable a and then the string “and”. After that, our variable b with string “is” and finally we are printing our sum variable. Now, you can see the output in the image below.Try again

Python Program to Find Area of a Triangle

To find the area of a triangle, you must know the mathematical formula to find the area of a triangle. Area of triangle – 1/2(length*breadth).

# Program for area of triangle
l = float(input("Enter the length of the triangle : "))
b = float(input("Enter the breadth of the triangle : "))
area = 1/2*(l*b)
print("The area of the triangle is " , area)

In this program, we take the input of length and breadth in a float data type because length or height may be given by the user. It will be an integer or decimal. After that, we take another variable area and store the formula of area in this variable. Now is the time to print the value of the area variable. So,we write a print function and in print we write a string and after that we write our area variable to see the output.

Try again

Python Program to Swap Values of two Variables

Swapping values between two variables in python. There are different methods to swap values of two variables. In this article we can see three methods to swap variables.

  • METHOD 1
# Program for swap two variables
x = int(input("Enter the value of the x : "))
y = int(input("Enter the value of the y : "))
temp = x
x = y
y = temp
print("the value of x after swapping : " , x)
print("the value of y after swapping : " , y)

First, we take input from the user and after that we take a temporary variable named ‘temp’, and give the value of x to  this variable. Now the temp variable has the value of x variable. After that, we give the y variable value to the x variable , and at last we give the value of the temp variable to the y variable. Now you can see the output in the given image below.Python Program

  • METHOD 2
# Alternative way to swap two variables
 x = 5
 y = 10
 x, y = y, x
 print("x =", x)
 print("y =", y)
This is an alternative way to swap two variables. Firstly, we take input from the user and, in the third line, according to python rules, the value of the y variable is assigned to the x variable and the value of the x variable is assigned to the y variable. Now its time to print the output by using the print function. So we write a string and variable in a print function.
Python ProgramAs you can see in the image, the values are swapped.
  • METHOD 3
# Another way to Swap two variables
x = input("Enter the value of x : ")
y = input("Enter the value y : ")
print("the value of y is " ,x[::])
print("the value of x is " ,y[::])
Another way to swap two variables. By using the python index method.
Python Program

Python Program To Find Square and Square root of any number

In this python program we find square and square root of numbers with user input.

# To find the square of any number
Num = int(input("Enter your number : "))
Sqr = Num * Num
print("Square of ",Num,"is : ",Sqr)

# To Find Square root of any number
Sqrt = Num ** 0.5
print("Square root of ",Num, "is : ",Sqrt)
We knew that square of any number is double multiple of that number. So here we take Sqr name variable and give them value of num * num(means variable num value multiply by its own value). “After”, that print the value of sqr variable. Now to find the square root we have inbuilt function (Sqrt) in python but here we use exponential operator of python to find square root of given input.
Try again

Python program to reverse a string

In this python program we are going to reverse a string by using a for loop.

a = input("Enter your string : ")
b = ""
for x in a:
    b = x + b     
print(b)
Firstly we take input in variable (a) in form of string from user. After that, we take an empty variable named b. Now we are using for loop to iterate the string and here iterate variable is x.
Working of for loop:
first time when x goes in loop it gives value of variable a which is p and then in second line it will be store value p in variable b. Now second time when x enters in loop it gives y value and when it comes in second line then value of variable b is YP. Similarly, for all value and in last time it looks likes — NOHTYP. As you can see in image below:-
Python Program

Python Program to find Simple Interest and Amount

In this python program we find simple interest and amount.

  • Formula of Simple Interest – (principle*rate*time)/100.
  • Formula of Amount – (Simple Interest + Principle)
# Python program to find simple interest and amount
p = float(input("Enter your P : ")) 
r = float(input("Enter your r : ")) 
t = float(input("Enter your t : ")) 
Si = (p*r*t)/100 print("Simple interest is :  " ,  Si) 
A = Si + p print("Total amount is : " , A)

To find simple interest we need principle, rate and time. So we are taking three variables p, r, t for principle, rate, time respectively and taking input in float data type. After that we take a third variable Si and give them formula of simple interest and then print SI variable. For amount, we take a variable A and then give formula of amount and after that we print variable A. Now you can see output in below image.

Try again
Suggested Readings :-
We hope that you like this article. If you like this then share our article with your friends and want to learn python just click. Thanks for reading our blog!