A complex number is represented as:
a + bjWhere:
a→ real partb→ imaginary partj→ imaginary unit
(j² = -1, soj = √-1)
⚠️ Python usesj, noti
a = 5 + 2j
print(a)
a = 5 + 2J
print(a)✅ Capital J is allowed
❌ i or I is not allowed
a = 5 + 2i # ❌ Invalida = 5 + 2j
print(type(a)) # <class 'complex'>a = 0b1101 + 2j
print(a) # (13+2j)❌ Imaginary part must be decimal
a = 0b1101 + 0b101j # ❌ Errora = 2 + 2j
b = 5 + 3j
print(a + b)
print(a - b)
print(a * b)
print(a / b)a = 2 + 2j
print(a.real) # 2.0
print(a.imag) # 2.0a = 3 + 4j
print(abs(a)) # 5.0Boolean values can be only:
TrueFalse
a = True
print(type(a)) # bool
b = False
print(type(b)) # bool❌ Invalid
a = false # ❌ Errora = 10
b = 20
c = a > b
print(c) # False
print(type(c)) # boolInternally:
True→ 1False→ 0
a = True
b = False
print(a + b) # 1
print(a * b) # 0
print(type(a + b)) # intAnything enclosed in quotes is a string.
' ' single quotes
" " double quotes
''' ''' triple single quotes
""" """ triple double quotes (docstring)❌ Python has no
chartype Single character is also a string
a = "Apple"
b = "A"
c = 'A'All are str.
a = 'Learning "python" is fun'
b = "Learning 'python' is fun"a = """Learning
python
is
fun"""
print(a)Works with ''' also.
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
a b c d e f g h i j
0 1 2 3 4 5 6 7 8 9
a = "abcdefghij"
print(a[2]) # c
print(a[-8]) # ca[start : end : step]a = "Learning python is fun"
print(a[9]) # p
print(a[9:15]) # python
print(a[:15]) # Learning python
print(a[:]) # full stringa = "Learning python is "
b = "fun"
print(a + b)
print(3 * b)print("#" * 50)
print("Hello")
print("#" * 50)a = "apple"
print(len(a))a = "hello"
temp = a[0].upper() + a[1:]
print(temp)a = "hello"
temp = a[:-1] + a[-1].upper()
print(temp)- int
- float
- complex
- bool
- str
print(int(15.2)) # 15
print(int("123")) # 123❌ Invalid
int("10.5")
int("0b101")
int(2+4j)print(float(10)) # 10.0
print(float(True)) # 1.0❌ Invalid
float(10+2j)
float("hello")print(complex(10))
print(complex(10.5))
print(complex(True))
print(complex(False))
print(complex("10.5j"))
print(complex(10, 5))print(bool(0)) # False
print(bool(10)) # True
print(bool("")) # False
print(bool("False")) # Trueprint(str(10))
print(str(10.5))
print(str(3+6j))
print(str(True))Once an object is created, it cannot be changed. Any modification creates a new object.
b = 10
print(id(b))
b = b + 3
print(id(b))a = 10
b = 10
print(id(a))
print(id(b))
print(a is b) # True