Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Python Program to Find Square Root of a Number (Examples)
Have you ever wondered how to find square root in Python? Well, it is one of the fundamental mathematical operations that can be calculated using a Python program or code.
The square root of a number is a mathematical operation that determines a value which, when multiplied by itself, gives the original number. For example, the square root of 9 is 3 because 3 multiplied by itself (3 x 3) equals 9. Similarly, the square root of 25 is 5 because 5 multiplied by itself (5 x 5) equals 25.
The square root is denoted by the symbol √ and is commonly used in various fields such as mathematics, physics, engineering, and computer science. It plays a fundamental role in solving equations, calculating distances, determining proportions, and many other applications.
There are several ways to find the square root of a number in Python using built-in functions, libraries, or even custom algorithms. These methods provide different levels of accuracy and flexibility, allowing you to choose the most suitable approach based on your specific requirements.
In this tutorial, we will use the Python program to find square root of a number. Whether you are a math enthusiast, a budding programmer, or simply curious about the inner workings of numbers, this tutorial will provide you with the necessary code and examples to calculate square roots effortlessly.
So, let’s get started and understand Python square root program with examples, output, and explanation.
Python Program to Find Square Root of a Number
Code
# Square root program in Python
import math
# Take input from the user
num = float(input("Enter a number: "))
# Calculate the square root
sqrt = math.sqrt(num)
# Print the result
print("The square root of", num, "is", sqrt)
Output
Enter a number: 1024
The square root of 1024 is 32
Explanation
-
We first import the math module, which provides various mathematical functions and constants, including the square root function sqrt().
-
Next, we prompt the user to enter a number using the input() function and convert it to a floating-point number using float().
-
Then, we calculate the square root of the entered number using the sqrt() function from the math module and store the result in the variable sqrt.
-
Finally, we print the original number and its square root using the print() function, along with appropriate messages.
When you run this program, it will ask you to enter a number. After you provide the input, it will calculate and display the square root of the number.
Square Root Program in Python for Complex Numbers
Code
# How to find square root in Python for complex numbers
import cmath
# Take input from the user for the complex number
real_part = float(input("Enter the real part: "))
imaginary_part = float(input("Enter the imaginary part: "))
# Create a complex number using the input
complex_num = complex(real_part, imaginary_part)
# Calculate the square root
sqrt = cmath.sqrt(complex_num)
# Print the result
print("The square root of", complex_num, "is", sqrt)
Output
Enter the real part: 2
Enter the imaginary part: 6
The square root of (2+6j) is (2.0401660864175692+1.4704685172312866j)
Explanation
-
We first import the cmath module, which provides complex number support and related mathematical functions.
-
Next, we use the input() function to prompt the user to enter the real and imaginary parts of the complex number. We convert the input to floating-point numbers using float().
-
Then, we create a complex number using the complex() function, passing the real and imaginary parts as arguments. This complex number is stored in the variable complex_num.
-
After that, we calculate the square root of the complex number using the sqrt() function from the cmath module. The result is stored in the variable sqrt.
-
Finally, we print the original complex number and its square root using the print() function, along with suitable messages.
When you run this program, it will ask you to enter the real and imaginary parts of the complex number. After you provide the input, it will calculate and display the square root of the complex number.
Find Square Root in Python Using NumPy
Code
# Python Square Root Program Using NumPy
import numpy as np
# Take input from the user
num = float(input("Enter a number: "))
# Calculate the square root using NumPy
sqrt = np.sqrt(num)
# Print the result
print("The square root of", num, "is", sqrt)
Output
Enter a number: 10
The square root of 10 is 3.1622776601683795
Explanation
-
We first import the numpy module and alias it as np, which is a common convention.
-
Next, we use the input() function to prompt the user to enter a number, and we convert the input to a floating-point number using float().
-
Then, we calculate the square root of the number using the np.sqrt() function from the NumPy module. The result is stored in the variable sqrt.
-
Finally, we print the original number and its square root using the print() function, along with appropriate messages.
NumPy provides efficient and optimized numerical operations, making it particularly useful when working with arrays or performing complex mathematical computations. The np.sqrt() function can handle not only single numbers but also arrays of numbers, allowing you to calculate square roots for multiple values simultaneously.
Find Square Root in Python Using ** (Exponential Operator)
Code
# Calculate square root using exponential operator
# Take input from the user
num = float(input("Enter a number: "))
# Calculate the square root using the exponentiation operator
sqrt = num ** 0.5
# Print the result
print("The square root of", num, "is", sqrt)
Output
Enter a number: 196
The square root of 196 is 14
Explanation
-
We prompt the user to enter a number using the input() function and convert it to a floating-point number using float().
-
Then, we calculate the square root by raising the number to the power of 0.5 using the exponentiation operator **. The result is stored in the variable sqrt.
-
Finally, we print the original number and its square root using the print() function, along with suitable messages.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program