Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the Square Root in Python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to find the square root in Python. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. Open the IDLE,File--New File of python and create a new file

2. Enter the code in the empty file, File--Save, or Ctrl+s to save the file anywhere on the computer, press the keyboard F5 directly, or Run--Run Module to execute the code.

3. When running, enter a number and press enter on the keyboard to get the following result:

4. The * * used in the above part calculates the square root, which is only suitable for positive numbers. It cannot be satisfied when negative and complex numbers need to be calculated. You can enter the following figure code.

The running result is as follows:

Second, code analysis

Print (the square root of '0.3f is 0.3f (num,num_sqrt))

% 0.3f is in format, keeping 3 decimal places

Import cmath # Import plural mathematics module

Import the cmath module in the existing python before you can use the function sqrt to calculate the square root

Num_sqrt=cmath.sqrt (num)

Print (the square root of'{0} is {1num,num_sqrt.real,num_sqrt.imag 0.3f} + {2num,num_sqrt.real,num_sqrt.imag 0.3f} j'.format))

{0} corresponds to the value of num, {1 num_sqrt.imag 0.3f}, the value representing num_sqrt.real retains 3 decimal places, and {2 num_sqrt.imag value retains 3 decimal places.

If num_sqrt is a real number, then num_sqrt.real represents the real part of num_sqrt and num_sqrt.imag represents the imaginary part of num_sqrt

Knowledge expansion:

Matters needing attention

1. When entering each line of code, each line must be aligned, and if there are spaces, the number of spaces must be the same.

2, pay attention to the double quotation marks "", where the double quotation marks function is the same as the single quotation marks, must be in the English input method, can not be entered under the Chinese input method, otherwise the program will run error.

About how to find the square root in Python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report