Skip to content

Commit 0d50cb8

Browse files
committed
Add complex numbers
1 parent 067cafd commit 0d50cb8

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Complex numbers
2+
3+
Python has a type representing complex numbers natively (`complex`). It can be created by adding a symbol `j` to the real numbers to denote the imaginary part. For example:
4+
5+
```python
6+
complex_value = 1 + 0.2j
7+
```
8+
9+
On this type, you can perform normal calculations, print it, etc. But what if we want to extract only the real or imaginary part from a complex number? For this purpose, we use the *attributes* `real` and `imag`. Attributes are similar to methods: they are used by entering their names after the dot like `variable.attribute`, but without the parentheses . E.g:
10+
11+
```python
12+
complex_value = 1 + 0.2j
13+
14+
print ("Re =", complex_value.real)
15+
print ("Im =", complex_value.imag)
16+
```
17+
18+
<hr/>
19+
20+
Published under [Creative Commons Attribution-NonCommercial-ShareAlike](https://creativecommons.org/licenses/by-nc-sa/4.0/) license.

04 More on Variables/6.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)