Skip to content

Commit 8c1fbf9

Browse files
authored
Add files via upload
1 parent 2d8f9ac commit 8c1fbf9

4 files changed

+1039
-0
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<small><small><i>\n",
8+
"All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n",
9+
"</i></small></small>"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"# Python Program to Find the Square Root\n",
17+
"\n",
18+
"In this program, you'll learn to find the square root of a number using exponent operator and **`cmath`** module.\n",
19+
"\n",
20+
"To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n",
21+
"\n",
22+
"* **[Python Input, Output and Import](https://github.com/milaan9/01_Python_Introduction/blob/main/011_Python_Input_Output_Import.ipynb)**\n",
23+
"* **[Python Data Types](https://github.com/milaan9/01_Python_Introduction/blob/main/009_Python_Data_Types.ipynb)**\n",
24+
"* **[Python Operators](https://github.com/milaan9/01_Python_Introduction/blob/main/012_Python_Operators.ipynb)**"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 1,
30+
"metadata": {
31+
"ExecuteTime": {
32+
"end_time": "2021-07-23T14:42:37.705957Z",
33+
"start_time": "2021-07-23T14:42:37.673735Z"
34+
}
35+
},
36+
"outputs": [
37+
{
38+
"name": "stdout",
39+
"output_type": "stream",
40+
"text": [
41+
"The square root of 9.000 is 3.000\n"
42+
]
43+
},
44+
{
45+
"data": {
46+
"text/plain": [
47+
"'\\n>>Output/Runtime Test Cases:\\n \\nThe square root of 9.000 is 3.000\\n'"
48+
]
49+
},
50+
"execution_count": 1,
51+
"metadata": {},
52+
"output_type": "execute_result"
53+
}
54+
],
55+
"source": [
56+
"#Example: For positive numbers\n",
57+
"# Python Program to calculate the square root\n",
58+
"\n",
59+
"# Note: change this value for a different result\n",
60+
"num = 9 \n",
61+
"\n",
62+
"# To take the input from the user\n",
63+
"#num = float(input('Enter a number: '))\n",
64+
"\n",
65+
"num_sqrt = num ** 0.5\n",
66+
"print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))\n",
67+
"\n",
68+
"'''\n",
69+
">>Output/Runtime Test Cases:\n",
70+
" \n",
71+
"The square root of 9.000 is 3.000\n",
72+
"'''"
73+
]
74+
},
75+
{
76+
"cell_type": "markdown",
77+
"metadata": {},
78+
"source": [
79+
"**Explanation:**\n",
80+
" \n",
81+
"In this program, we store the number in **`num`** and find the square root using the **`**`** exponent operator. This program works for all positive real numbers. But for negative or complex numbers, it can be done as follows. "
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 2,
87+
"metadata": {
88+
"ExecuteTime": {
89+
"end_time": "2021-07-23T14:42:37.828028Z",
90+
"start_time": "2021-07-23T14:42:37.707913Z"
91+
}
92+
},
93+
"outputs": [
94+
{
95+
"name": "stdout",
96+
"output_type": "stream",
97+
"text": [
98+
"The square root of (1+2j) is 1.272+0.786j\n"
99+
]
100+
},
101+
{
102+
"data": {
103+
"text/plain": [
104+
"'\\n>>Output/Runtime Test Cases:\\n \\nThe square root of (1+2j) is 1.272+0.786j\\n'"
105+
]
106+
},
107+
"execution_count": 2,
108+
"metadata": {},
109+
"output_type": "execute_result"
110+
}
111+
],
112+
"source": [
113+
"# Find square root of real or complex numbers\n",
114+
"# Importing the complex math module\n",
115+
"\n",
116+
"import cmath\n",
117+
"\n",
118+
"num = 1+2j\n",
119+
"\n",
120+
"# To take input from the user\n",
121+
"#num = eval(input('Enter a number: '))\n",
122+
"\n",
123+
"num_sqrt = cmath.sqrt(num)\n",
124+
"print('The square root of {0} is {1:0.3f}+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag))\n",
125+
"\n",
126+
"'''\n",
127+
">>Output/Runtime Test Cases:\n",
128+
" \n",
129+
"The square root of (1+2j) is 1.272+0.786j\n",
130+
"'''"
131+
]
132+
},
133+
{
134+
"cell_type": "markdown",
135+
"metadata": {},
136+
"source": [
137+
"**Explanation:**\n",
138+
" \n",
139+
"In this program, we use the **`sqrt()`** function in the **`cmath`** (complex math) module.\n",
140+
"\n",
141+
">**Note:** If we want to take complex number as input directly, like **`3+4j`**, we have to use the **`eval()`** function instead of **`float()`**.\n",
142+
"The **`eval()`** method can be used to convert complex numbers as input to the **`complex`** objects in Python. To learn more, visit **[Python eval() function](https://github.com/milaan9/04_Python_Functions/blob/main/002_Python_Functions_Built_in/021_Python_eval().ipynb)**.\n",
143+
"\n",
144+
"Also, notice the way in which the output is formatted. To learn more, visit **[string formatting in Python](https://docs.python.org/3/library/string.html#format-string-syntax)**."
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": []
153+
}
154+
],
155+
"metadata": {
156+
"hide_input": false,
157+
"kernelspec": {
158+
"display_name": "Python 3",
159+
"language": "python",
160+
"name": "python3"
161+
},
162+
"language_info": {
163+
"codemirror_mode": {
164+
"name": "ipython",
165+
"version": 3
166+
},
167+
"file_extension": ".py",
168+
"mimetype": "text/x-python",
169+
"name": "python",
170+
"nbconvert_exporter": "python",
171+
"pygments_lexer": "ipython3",
172+
"version": "3.8.8"
173+
},
174+
"toc": {
175+
"base_numbering": 1,
176+
"nav_menu": {},
177+
"number_sections": true,
178+
"sideBar": true,
179+
"skip_h1_title": false,
180+
"title_cell": "Table of Contents",
181+
"title_sidebar": "Contents",
182+
"toc_cell": false,
183+
"toc_position": {},
184+
"toc_section_display": true,
185+
"toc_window_display": false
186+
},
187+
"varInspector": {
188+
"cols": {
189+
"lenName": 16,
190+
"lenType": 16,
191+
"lenVar": 40
192+
},
193+
"kernels_config": {
194+
"python": {
195+
"delete_cmd_postfix": "",
196+
"delete_cmd_prefix": "del ",
197+
"library": "var_list.py",
198+
"varRefreshCmd": "print(var_dic_list())"
199+
},
200+
"r": {
201+
"delete_cmd_postfix": ") ",
202+
"delete_cmd_prefix": "rm(",
203+
"library": "var_list.r",
204+
"varRefreshCmd": "cat(var_dic_list()) "
205+
}
206+
},
207+
"types_to_exclude": [
208+
"module",
209+
"function",
210+
"builtin_function_or_method",
211+
"instance",
212+
"_Feature"
213+
],
214+
"window_display": false
215+
}
216+
},
217+
"nbformat": 4,
218+
"nbformat_minor": 4
219+
}

0 commit comments

Comments
 (0)