PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
  • Quizzes
  • Code Editor
Home » Python » Programs and Examples » Python Find Square Root of a Number

Python Find Square Root of a Number

Updated on: March 27, 2025 | Leave a Comment

The square root of a number x is a value y such that when y is multiplied by itself, it equals x.

  • Mathematical Representation: √x = y where y × y = x
  • For Example: √25 = 5 because 5 × 5 = 25

Python has several methods to find the square root of a number. Here are a few common approaches, each with an explanation and example.

Each method has its use case, depending on whether you’re working with simple numbers, complex numbers, or more advanced mathematical operations.

1. Using the Exponentiation Operator (**)

The ** operator in Python is the exponentiation operator, used to raise a number to a power (e.g., a ** b computes a^b). This is the simplest method in Python for calculating the square root of a given number. The square root of a number is equivalent to raising the number to the power of 0.5.

Code Example

num = float(input("Enter a number: "))
sqrt = num ** 0.5
print(f"The square root of {num} is {sqrt}")Code language: Python (python)

Output

# Enter a number: 0
# The square root of 0.0 is 0.0

# Enter a number: 64
# The square root of 64.0 is 8.0

# Enter a number: 3.5
# The square root of 3.5 is 1.8708286933869707

# Enter a number: -64
# The square root of -64.0 is (4.898587196589413e-16+8j)

Explanation

  • Here, we are taking the number as input from the user. And converting it to the float.
  • The expression num ** 0.5 calculates the square root by raising the number to the power of 0.5.
  • Note: This calculation does not work for negative numbers. In case of a negative number, it gives an invalid answer.

2. Using math.sqrt() Function

math.sqrt() is a function in Python’s math module that returns the square root of a given non-negative number. This method is helpful because it’s a dedicated function for this operation and ensures precision.

Syntax – math.sqrt(x)

  • It takes one argument (a number) and returns the square root.
  • x must be non-negative (x ≥ 0).
  • Returns a floating-point value.
  • Raises a ValueError if x is negative.

Code Example

import math

print('square root of 0: ', math.sqrt(0))
print('square root of integer number 64: ', math.sqrt(64))
print('square root of float number 5.6: ', math.sqrt(5.6))Code language: Python (python)

Output

square root of 0:  0.0
square root of integer number 4: 8.0
square root of float number 5.6: 2.3664319132398464

3: Using cmath.sqrt() Function for complex numbers

To calculate the square root of a number in Python the cmath module can be used when dealing with both real and complex numbers. It is useful for finding square roots of negative numbers.

Code Example

import cmath

print('square root of integer number 64: ', cmath.sqrt(64))
print('square root of float number 5.6: ', cmath.sqrt(5.6))
print('square root of negative number -64: ', cmath.sqrt(-64))Code language: Python (python)

Output:

square root of integer number 64:  (8+0j)
square root of float number 5.6: (2.3664319132398464+0j)
square root of negative number -64: 8j

Explanation

  • cmath.sqrt() can handle both real and complex numbers.
  • If the input is negative, this function returns a complex number as the square root.
  • For example, cmath.sqrt(-144) returns 12j and cmath.sqrt(144) returns 12+0j
  • In Python, 12+0j and 12j are both complex numbers, but they represent different values.
    • 12+0j: This is a complex number with a real part of 12 and an imaginary part of 0.
    • 12j: This is a purely imaginary number with no real part and an imaginary part of 12.

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Image

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2025 pynative.com

Advertisement