From 5fd45697e1ecc4ebc76674bb9d988e465ecd3c7e Mon Sep 17 00:00:00 2001 From: Ayushi Negi Date: Sat, 2 Jul 2016 13:51:06 +0530 Subject: [PATCH 1/4] Create Python-Function-Complex.md For issue #819 --- Python-Function-Complex.md | 66 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Python-Function-Complex.md diff --git a/Python-Function-Complex.md b/Python-Function-Complex.md new file mode 100644 index 0000000000..142b819a60 --- /dev/null +++ b/Python-Function-Complex.md @@ -0,0 +1,66 @@ +# Python Complex() + + +`complex()` is a built-in function in Python 3 that hanldles the creation of a complex number according to the arguments passed. It returns a complex number with the value `real + imag*1j` when called in form of `complex(real, imag)` or converts a string or number to a complex number. + +## Arguments and Return Values +Two types of arguments can be passed. + +1. **Numeric Arguments** + + - When called with two arguments, both being numbers of any numeric type (including complex), this function returns a complex number using both arguments. + + `complex(x, y)` function returns a complex number with the value + `x + y*1j` + + ```python + >>> complex(3, 2) + (3+2j) + >>> complex(3+4j, 6) + (3+10j) + ``` + + - When called with one numeric argument, it returns a complex number using the argument as the real part and an imaginary part of zero. + + ```python + >>> complex(4) + (4+0j) + ``` + - If called with no arguments, it returns a complex zero. + + ```python + >>> complex() + 0j + ``` + +2. **String Argument** + + - If the first argument is a string, it will be interpreted as a *complex number* and the function must be called without a second parameter. It will return the interpreted complex number. + + ```python + >>> complex('3+4j') + (3+4j) + ``` + + - The string must be in the form of a valid complex number, otherwise the function will raise a *ValueError* exception. + + - The string must not contain whitespace around the central + or - operator. For example, `complex('1+2j')` is fine, but `complex('1 + 2j')` raises *ValueError*. + +## Examples + +```python + +print(complex(4, 5)) # prints (4+5j) +print(complex(6)) # prints (6+0j) +print(complex()) # prints 0j +print(complex(2.5,5)) # prints (2.5+5j) +print(complex(5,6+8j)) # prints (-3+6j) as 5+(6+8j)*1j = -3+6j +print(complex('10+20j')) # prints (10+20j) + +``` + + +:rocket: [Run Code](https://repl.it/CTGi/3) + + +[Official Documentation](https://docs.python.org/3/library/functions.html#complex) From 546410c7056d48f21ea70922ae8d2b698ead1350 Mon Sep 17 00:00:00 2001 From: Ayushi Negi Date: Sat, 2 Jul 2016 16:10:37 +0530 Subject: [PATCH 2/4] Update Python-Function-Complex.md --- Python-Function-Complex.md | 66 ++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/Python-Function-Complex.md b/Python-Function-Complex.md index 142b819a60..f973365fb9 100644 --- a/Python-Function-Complex.md +++ b/Python-Function-Complex.md @@ -1,66 +1,54 @@ # Python Complex() - `complex()` is a built-in function in Python 3 that hanldles the creation of a complex number according to the arguments passed. It returns a complex number with the value `real + imag*1j` when called in form of `complex(real, imag)` or converts a string or number to a complex number. ## Arguments and Return Values -Two types of arguments can be passed. - -1. **Numeric Arguments** - - - When called with two arguments, both being numbers of any numeric type (including complex), this function returns a complex number using both arguments. - - `complex(x, y)` function returns a complex number with the value - `x + y*1j` - ```python - >>> complex(3, 2) - (3+2j) - >>> complex(3+4j, 6) - (3+10j) - ``` - - - When called with one numeric argument, it returns a complex number using the argument as the real part and an imaginary part of zero. +Two types of arguments can be passed. - ```python - >>> complex(4) - (4+0j) - ``` - - If called with no arguments, it returns a complex zero. +####Numeric Arguments - ```python - >>> complex() - 0j - ``` +When called with two arguments, both being numbers of any numeric type (including complex), this function returns a complex number using both arguments. +`complex(x, y)` function returns a complex number with the value `x + y*1j` -2. **String Argument** +```python +>>> complex(3, 2) +(3+2j) +>>> complex(3+4j, 6) +(3+10j) +``` +When called with one numeric argument, it returns a complex number using the argument as the real part and an imaginary part of zero. - - If the first argument is a string, it will be interpreted as a *complex number* and the function must be called without a second parameter. It will return the interpreted complex number. +```python +>>> complex(4) +(4+0j) +``` +If called with no arguments, it returns a complex zero. - ```python - >>> complex('3+4j') - (3+4j) - ``` +```python +>>> complex() +0j +``` +####String Argument - - The string must be in the form of a valid complex number, otherwise the function will raise a *ValueError* exception. +If the first argument is a string, it will be interpreted as a *complex number* and the function must be called without a second parameter. It will return the interpreted complex number. - - The string must not contain whitespace around the central + or - operator. For example, `complex('1+2j')` is fine, but `complex('1 + 2j')` raises *ValueError*. +```python +>>> complex('3+4j') +(3+4j) +``` +The string must be in the form of a valid complex number, otherwise the function will raise a *ValueError* exception and it must not contain whitespace around the central + or - operator. For example, `complex('1+2j')` is fine, but `complex('1 + 2j')` raises *ValueError*. ## Examples ```python - print(complex(4, 5)) # prints (4+5j) print(complex(6)) # prints (6+0j) print(complex()) # prints 0j print(complex(2.5,5)) # prints (2.5+5j) print(complex(5,6+8j)) # prints (-3+6j) as 5+(6+8j)*1j = -3+6j print(complex('10+20j')) # prints (10+20j) - ``` - - :rocket: [Run Code](https://repl.it/CTGi/3) - [Official Documentation](https://docs.python.org/3/library/functions.html#complex) From f85c41904708597ae03f65eea99062eb88fbd9f4 Mon Sep 17 00:00:00 2001 From: Ayushi Negi Date: Sat, 2 Jul 2016 17:24:57 +0530 Subject: [PATCH 3/4] Update Python-Function-Complex.md --- Python-Function-Complex.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Python-Function-Complex.md b/Python-Function-Complex.md index f973365fb9..1c1a261dad 100644 --- a/Python-Function-Complex.md +++ b/Python-Function-Complex.md @@ -6,7 +6,7 @@ Two types of arguments can be passed. -####Numeric Arguments +#### Numeric Arguments When called with two arguments, both being numbers of any numeric type (including complex), this function returns a complex number using both arguments. `complex(x, y)` function returns a complex number with the value `x + y*1j` @@ -29,7 +29,7 @@ If called with no arguments, it returns a complex zero. >>> complex() 0j ``` -####String Argument +#### String Argument If the first argument is a string, it will be interpreted as a *complex number* and the function must be called without a second parameter. It will return the interpreted complex number. From ebc188aa6c5a260da2a9efc81e22ccf6c04a6be8 Mon Sep 17 00:00:00 2001 From: Ayushi Negi Date: Sun, 3 Jul 2016 08:52:08 +0530 Subject: [PATCH 4/4] Update Python-Function-Complex.md --- Python-Function-Complex.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Python-Function-Complex.md b/Python-Function-Complex.md index 1c1a261dad..5db332fd90 100644 --- a/Python-Function-Complex.md +++ b/Python-Function-Complex.md @@ -1,10 +1,10 @@ # Python Complex() -`complex()` is a built-in function in Python 3 that hanldles the creation of a complex number according to the arguments passed. It returns a complex number with the value `real + imag*1j` when called in form of `complex(real, imag)` or converts a string or number to a complex number. +`complex()` is a built-in function in Python 3 that hanldles the creation of a [complex number](https://www.mathsisfun.com/numbers/complex-numbers.html) according to the arguments passed. It returns a complex number with the value `real + imag*1j` when called in form of `complex(real, imag)` or converts a string or number to a complex number. ## Arguments and Return Values -Two types of arguments can be passed. +This function takes two types of arguments which are discussed below : #### Numeric Arguments @@ -37,7 +37,7 @@ If the first argument is a string, it will be interpreted as a *complex number* >>> complex('3+4j') (3+4j) ``` -The string must be in the form of a valid complex number, otherwise the function will raise a *ValueError* exception and it must not contain whitespace around the central + or - operator. For example, `complex('1+2j')` is fine, but `complex('1 + 2j')` raises *ValueError*. +The string must be in the form of a valid complex number, otherwise the function will raise a `ValueError` exception and it must not contain whitespace around the central + or - operator. For example, `complex('1+2j')` is fine, but `complex('1 + 2j')` raises `ValueError`. ## Examples