0% found this document useful (0 votes)
30 views22 pages

Understanding Complex Numbers in Python

The document covers various data types in Python, including complex numbers, None type, Boolean data, type casting, and strings. It provides examples of operations on complex numbers and demonstrates type casting between different data types. Additionally, it discusses input and output functions, including how to handle user input and print formatted output.

Uploaded by

memu00133
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views22 pages

Understanding Complex Numbers in Python

The document covers various data types in Python, including complex numbers, None type, Boolean data, type casting, and strings. It provides examples of operations on complex numbers and demonstrates type casting between different data types. Additionally, it discusses input and output functions, including how to handle user input and print formatted output.

Uploaded by

memu00133
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Notebook

April 19, 2025

1 Complex Number
[ ]: any number that can be represented in the form of a+bj
where
a -----> Real number
b -----> Imaginary number

[1]: c = 5+4j

[2]: type(c)

[2]: complex

[4]: d = 99-100J
type(d)

[4]: complex

[7]: [Link]

[7]: 99.0

[8]: [Link]

[8]: -100.0

[10]:

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[10], line 1
----> 1 j**2==1

NameError: name 'j' is not defined

[ ]: # Operations on Complex numbers


Arithmetic operation

1
Logical operation
Assignment operation
Identity operation
equality operation

# can no perform
Comparision operation
Arithmetic operation(% modulus operation and floor division)

[11]: a = 40 +39J
b = 5+1J

[12]: a

[12]: (40+39j)

[13]: b

[13]: (5+1j)

[14]: a + b

[14]: (45+40j)

[15]: a > b

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[15], line 1
----> 1 a > b

TypeError: '>' not supported between instances of 'complex' and 'complex'

[16]: a = 40 +39J
c = 40 +39J

[18]: print(id(a))
print(id(c))

1710943534800
1710943533552

[19]: a is c

[19]: False

2
[20]: a is not c

[20]: True

[21]: a % c

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[21], line 1
----> 1 a % c

TypeError: unsupported operand type(s) for %: 'complex' and 'complex'

[23]: a // b

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[23], line 1
----> 1 a //b

TypeError: unsupported operand type(s) for //: 'complex' and 'complex'

[24]: 10+5J == 10+5j

[24]: True

2 None Type
• None is used to represent Missing values

[25]: a = None

[26]: type(a)

[26]: NoneType

[ ]:

3 Boolean data
[ ]: True (1)
False (0)

[ ]:

3
[28]: 1+1

[28]: 2

[29]: True + True

[29]: 2

[30]: 1 + True

[30]: 2

[31]: 1 + False

[31]: 1

[32]: int(True)

[32]: 1

[33]: int(False)

[33]: 0

[34]: False

[34]: False

4 Type Casting
• Changing one data type to other data type is type casting

[35]: a = 5.008653
type(a)

[35]: float

[36]: int(a)

[36]: 5

[37]: a

[37]: 5.008653

[40]: float_to_int = int(a)

[41]: float_to_int

4
[41]: 5

[42]: str1 = "2.23345"

[43]: str1

[43]: '2.23345'

[44]: type(str1)

[44]: str

[45]: int(str1)

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[45], line 1
----> 1 int(str1)

ValueError: invalid literal for int() with base 10: '2.23345'

[48]: str1

[48]: '2.23345'

[50]: int(float(str1))

[50]: 2

[53]: int(float(str1))

[53]: 2

[46]: str2 = "22"


str_to_int = int(str2)
str_to_int

[46]: 22

[47]: type(str_to_int)

[47]: int

[54]: str2 = "abc22"


str_to_int = int(str2)
str_to_int

5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[54], line 2
1 str2 = "abc22"
----> 2 str_to_int = int(str2)
3 str_to_int

ValueError: invalid literal for int() with base 10: 'abc22'

[55]: a = 10
float(a)

[55]: 10.0

[57]: complex(a)

[57]: (10+0j)

[58]: bool(a)

[58]: True

[60]: bool(None)

[60]: False

[66]: bool(700)

[66]: True

[67]: bool("Hello")

[67]: True

[68]: bool("")

[68]: False

[ ]:

[74]: a = 40 +39J
str(a)

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[74], line 2
1 a = 40 +39J

6
----> 2 float(str(a))

ValueError: could not convert string to float: '(40+39j)'

[75]: [Link]

[75]: 40.0

[76]: int([Link])

[76]: 40

5 Strings
[ ]: string is a data type

[ ]: anything written inside


'' ----> Single quotes
"" ----> double quotes
''' ''' ----> Single triple quotes
""" """ ---> Double triple quotes

[77]: a = 'Hello we are in python class'

[78]: a

[78]: 'Hello we are in python class'

[79]: type(a)

[79]: str

[80]: a = 'Hello we are in python's class'

Cell In[80], line 1


a = 'Hello we are in python's class'
^
SyntaxError: unterminated string literal (detected at line 1)

[81]: a = "Hello we are in python's class"


a

[81]: "Hello we are in python's class"

7
[84]: a = "Hello we are in python"s class"
a

Cell In[84], line 1


a = "Hello we are in python"s class"
^
SyntaxError: unterminated string literal (detected at line 1)

[85]: a = '''Hello we are in python"s class'''


a

[85]: 'Hello we are in python"s class'

[86]: W_S = ''' William Shakespeare[a] (c. 23[b] April 1564 – 23 April 1616)[c] was␣
↪an English playwright, poet and actor.

He is widely regarded as the greatest writer in the English language and the␣
↪world's pre-eminent dramatist.

He is often called England's national poet and the "Bard of Avon" (or simply␣
↪"the Bard"). His extant works,

including collaborations, consist of some 39 plays, 154 sonnets, three long␣


↪narrative poems and a few other verses,

some of uncertain authorship. His plays have been translated into every major␣
↪living language and are performed more often than

those of any other playwright. Shakespeare remains


arguably the most influential writer in the English language, and his works␣
↪continue to be studied and reinterpreted.'''

[88]: print(W_S)

William Shakespeare[a] (c. 23[b] April 1564 – 23 April 1616)[c] was an English
playwright, poet and actor.
He is widely regarded as the greatest writer in the English language and the
world's pre-eminent dramatist.
He is often called England's national poet and the "Bard of Avon" (or simply
"the Bard"). His extant works,
including collaborations, consist of some 39 plays, 154 sonnets, three long
narrative poems and a few other verses,
some of uncertain authorship. His plays have been translated into every major
living language and are performed more often than
those of any other playwright. Shakespeare remains
arguably the most influential writer in the English language, and his works
continue to be studied and reinterpreted.

[89]: W_S = """ William Shakespeare[a] (c. 23[b] April 1564 – 23 April 1616)[c] was␣
↪an English playwright, poet and actor.

8
He is widely regarded as the greatest writer in the English language and the␣
↪world's pre-eminent dramatist.

He is often called England's national poet and the "Bard of Avon" (or simply␣
↪"the Bard"). His extant works,

including collaborations, consist of some 39 plays, 154 sonnets, three long␣


↪narrative poems and a few other verses,

some of uncertain authorship. His plays have been translated into every major␣
↪living language and are performed more often than

those of any other playwright. Shakespeare remains


arguably the most influential writer in the English language, and his works␣
↪continue to be studied and reinterpreted."""

[92]: print(W_S)

William Shakespeare[a] (c. 23[b] April 1564 – 23 April 1616)[c] was an English
playwright, poet and actor.
He is widely regarded as the greatest writer in the English language and the
world's pre-eminent dramatist.
He is often called England's national poet and the "Bard of Avon" (or simply
"the Bard"). His extant works,
including collaborations, consist of some 39 plays, 154 sonnets, three long
narrative poems and a few other verses,
some of uncertain authorship. His plays have been translated into every major
living language and are performed more often than
those of any other playwright. Shakespeare remains
arguably the most influential writer in the English language, and his works
continue to be studied and reinterpreted.

[93]: type(W_S)

[93]: str

[94]: W_S

[94]: ' William Shakespeare[a] (c. 23[b] April 1564 – 23 April 1616)[c] was an English
playwright, poet and actor.\nHe is widely regarded as the greatest writer in the
English language and the world\'s pre-eminent dramatist.\nHe is often called
England\'s national poet and the "Bard of Avon" (or simply "the Bard"). His
extant works,\nincluding collaborations, consist of some 39 plays, 154 sonnets,
three long narrative poems and a few other verses,\nsome of uncertain
authorship. His plays have been translated into every major living language and
are performed more often than \nthose of any other playwright. Shakespeare
remains\narguably the most influential writer in the English language, and his
works continue to be studied and reinterpreted.'

[96]: 'hello this is python"s class'

[96]: 'hello this is python"s class'

9
[97]: a = 'Hello we are in pythons class'

[98]: a

[98]: 'Hello we are in pythons class'

[99]: a = 'Hello we are in


pythons class'

Cell In[99], line 1


a = 'Hello we are in
^
SyntaxError: unterminated string literal (detected at line 1)

[101]: a = '''Hello we are in


pythons class'''

[103]: print(a)

Hello we are in
pythons class

6 Input Output functions


[ ]: Task - take 2 variables assign some int value
and get the sum of those two numbers

[104]: a = 10
b = 20
a+b

[104]: 30

[ ]: input()
- it is a function to get the user input from the end user

[105]: a = input("Enter the first number ")


b = input("Enter the Second number ")

Enter the first number 10


Enter the Second number 20

[106]: type(a)

[106]: str

10
[107]: type(b)

[107]: str

[ ]: Task - take 2 values as user input


and get the sum of those two numbers

[108]: a = input("Enter the first number ")


b = input("Enter the Second number ")

Enter the first number 70


Enter the Second number 30

[111]: # type casting


a = input("Enter the first number ")
b = input("Enter the Second number ")
a = int(a)
b = int(b)
a+b

Enter the first number 10


Enter the Second number 30

[111]: 40

[110]: a + b

[110]: '7030'

[109]: "Hello" + "World"

[109]: 'HelloWorld'

[113]: # type casting


a = float(input("Enter the first number "))
b = float(input("Enter the Second number "))
a+b

Enter the first number 20


Enter the Second number 30

[113]: 50.0

[114]: a

[114]: 20.0

[115]: b

[115]: 30.0

11
[ ]: 10
10.0

[ ]: eval

[120]: a = eval(input("Enter the first number "))


b = eval(input("Enter the Second number "))
a+b

Enter the first number "Hello"


Enter the Second number 2+3j

[121]: print(type(a))
print(type(b))

<class 'str'>
<class 'complex'>

[122]: a = eval(input("Enter the first number "))


b = eval(input("Enter the Second number "))
print(type(a))
print(type(b))

Enter the first number 10


Enter the Second number 20.55
<class 'int'>
<class 'float'>

[123]: c = input("Enter the first number ")


d = input("Enter the Second number ")
print(type(c))
print(type(d))

Enter the first number 10


Enter the Second number 20.55
<class 'str'>
<class 'str'>

7 Output Function
[ ]: print()

[124]: help(print)

Help on built-in function print in module builtins:

print(*args, sep=' ', end='\n', file=None, flush=False)


Prints the values to a stream, or to [Link] by default.

12
sep
string inserted between values, default a space.
end
string appended after the last value, default a newline.
file
a file-like object (stream); defaults to the current [Link].
flush
whether to forcibly flush the stream.

[125]: print("hello")

hello

[126]: print(20000)

20000

[131]: a = 10
b = 20
c = 30
print(a , end="$$$")
print(b)
print(c)

10
20
30

[ ]: '\n"
New line

[ ]: 10$$$20
30

[ ]:

[132]: a = 10
b = 20
c = 30
print(a , end="@@@\n")
print(b)
print(c)

10@@@
20
30

13
[ ]: 10@@@
20
30

[134]: a = 10
b = 20
c = 30
print(a,b,c , sep=" ")

10 20 30

[135]: a = 10
b = 20
c = 30
print(a,b,c , sep="-----------------")

10-----------------20-----------------30

[139]: a = 10
b = 20
c = 30
print(a,b,c , sep="\n")

10
20
30

[ ]: 10
20
30

[ ]: a = 10
b = 20
c = 30
print(a )
print(b)
print(c)

[ ]: print
print
print
print
print
print
print
print

[140]: a = 10
b = 20

14
c = 30
print(a,b,c , sep="\n")
print(a)

10
20
30
10

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

15
[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

16
[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

17
[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

18
[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

19
[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

20
[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

21
[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

This notebook was converted with [Link]

22

You might also like