top of page

5) Python program to swap two variables

Variable swapping:

In computer programming, swapping two variables specifies the mutual exchange of values of the variables. It is generally done by using a temporary variable.

For example: 

  1. data_item x := 1  

  2. data_item y := 0  

  3. swap (x, y)  

After swapping:

  1. data_item x := 0  

  2. data_item y := 1  

See this example:

back.png

# Python swap program   

x = input('Enter value of x: ')  

y = input('Enter value of y: ')  

  

# create a temporary variable and swap the values  

temp = x  

x = y  

y = temp  

  

print('The value of x after swapping: {}'.format(x))  

print('The value of y after swapping: {}'.format(y))  

6) Python program to generate a random number

Quadratic equation:

Quadratic equation is made from a Latin term "quadrates" which means square. It is a special type of equation having the form of:

ax2+bx+c=0

Here, "x" is unknown which you have to find and "a", "b", "c" specifies the numbers such that "a" is not equal to 0. If a = 0 then the equation becomes liner not quadratic anymore.

In the equation, a, b and c are called coefficients.

Let's take an example to solve the quadratic equation 8x2 + 16x + 8 = 0

See this example:

back.png

# import complex math module  

import cmath  

a = float(input('Enter a: '))  

b = float(input('Enter b: '))  

c = float(input('Enter c: '))  

  

# calculate the discriminant  

d = (b**2) - (4*a*c)  

  

# find two solutions  

sol1 = (-b-cmath.sqrt(d))/(2*a)  

sol2 = (-b+cmath.sqrt(d))/(2*a)  

print('The solution are {0} and {1}'.format(sol1,sol2))   

bottom of page