For positive numbers
# Python Program to calculate the square root
# Note: change this value for a different result
num = 8
# To take the input from the user
#num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
For real or complex numbers
# Find square root of real or complex numbers
# Importing the complex math module
import cmath
num = 1+2j
# To take input from the user
#num = eval(input('Enter a number: '))
num_sqrt = cmath.sqrt(num)
print('The square root of {0} is
{1:0.3f}+{2:0.3f}j'.format(num,num_sqrt.real,num_sqrt.imag))
In this program, we use the sqrt() function in the cmath (complex math) module.
Note: If we want to take complex number as input directly, like 3+4j , we have to use
the eval() function instead of float() .
The eval() method can be used to convert complex numbers as input to
the complex objects in Python.
,2. Python Program to Calculate the Area of a Triangle
If a , b and c are three sides of a triangle. Then,
s = (a+b+c)/2
area = √(s(s-a)*(s-b)*(s-c))
Source Code
# Python Program to find the area of triangle
a=5
b=6
c=7
# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
In this program, area of the triangle is calculated when three sides are given
using Heron's formula.
If you need to calculate area of a triangle depending upon the input from the
user, input() function can be used.
3. Python Program to Solve Quadratic Equation
The standard form of a quadratic equation is:
ax2 + bx + c = 0, where
a, b and c are real numbers and
a≠0
The solutions of this quadratic equation is given by:
,(-b ± (b ** 2 - 4 * a * c) ** 0.5) / 2 * a
Source Code
# Solve the quadratic equation ax**2 + bx + c = 0
# import complex math module
import cmath
a=1
b=5
c=6
# 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))
Output
Enter a: 1
Enter b: 5
Enter c: 6
The solutions are (-3+0j) and (-2+0j)
We have imported the cmath module to perform complex square root. First, we
calculate the discriminant and then find the two solutions of the quadratic equation.
4. Python Program to Swap Two Variables
Source Code: Using a temporary variable
# Python program to swap two variables
x=5
y = 10
, # To take inputs from the user
#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))
Output
The value of x after swapping: 10
The value of y after swapping: 5
In this program, we use the temp variable to hold the value of x temporarily. We then
put the value of y in x and later temp in y . In this way, the values get exchanged.
Source Code: Without Using Temporary Variable
In Python, there is a simple construct to swap variables. The following code does the
same as above but without the use of any temporary variable.
x=5
y = 10
x, y = y, x
print("x =", x)
print("y =", y)
If the variables are both numbers, we can use arithmetic operations to do the same. It
might not look intuitive at first sight. But if you think about it, it is pretty easy to figure
it out. Here are a few examples
Addition and Subtraction
x=x+y
y=x-y
x=x-y