int
. It is immutable and created through numeric literals and output of arithmetic expressions and functions that return numeric values. In Python 2.x there are two types int and long. But in Python 3.x, there is only one type int having unlimited range subject to available memoryTrue
and False
. Booleans have the type bool
. A Boolean is used to determine whether the logic of an expression or a comparison is correct. It plays a huge role in data comparisons.w = 10
x = 20.5
y = complex(3, 2)
y = 3 + 2j
z = True
print(type(w))
print(type(x))
print(type(y))
print(type(z))
<class 'int'> <class 'float'> <class 'complex'> <class 'bool'>
# Example of Boolean data type
y = 10 < 100
print(y)
print(type(y))
True <class 'bool'>
"""
boolean data type in Python?
In Python, True represents the value as 1 and False as 0.
"""
x = (-1 == True)
y = (1 == False)
print("x is", x)
print("y is", y)
x is False y is False
# Booleans are automatically converted to `int`s when used in arithmetic operations.
# `True` is converted to `1` and `False` is converted to `0`.
a = True + 9
b = False + 15
print("a:", a)
print("b:", b)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /var/folders/1t/g3ylw8h50cjdqmk5d6jh1qmm0000gn/T/ipykernel_27014/3417399837.py in <module> 1 # Booleans are automatically converted to `int`s when used in arithmetic operations. 2 # `True` is converted to `1` and `False` is converted to `0`. ----> 3 a = "" + 9 4 b = False + 15 5 TypeError: can only concatenate str (not "int") to str
Any value in Python can be converted to a Boolean using the bool
function. Only the following values evaluate to False
(they are often called falsy values):
False
itself0
0.0
None
""
[]
()
{}
set()
range(0)
Everything else evaluates to True
(a value that evaluates to True
is often called a truthy value).
The None type includes a single value None
, used to indicate the absence of a value. None
has the type NoneType
. It is often used to declare a variable whose value may be assigned later or as a return value of functions that do not return a value
bool(0)
bool(0.0)
False
bool(None)
False
bool("")
False
bool([])
False
var1 = None
type(var1)
NoneType
Use of Complex Numbers:
- Complex numbers are mostly used when you are dealing with electronics, dynamics, and control systems.
- In electronics, the state of a circuit element is defined by the voltage (V) and the current (I). Circuit elements can also have a capacitance (c) and inductance (L) that describes the circuit's tendency to resist changes in V and I.
- Rather than describing the circuit element's state by V and I, it can be described as
z = V + Ij
.- The laws of electricity can then be expressed using the addition and multiplication of complex umbers.
'''
Floating-point numbers are stored in computer hardware in IEEE-754 format
Be watchful while performing various operations on floating point numbers
'''
a = 3.3
b = 1.0 + 2.0
c = (a == b)
a, b, c
(3.3, 3.0, False)
a
3.3
b
3.3000000000000003
float
if any of the operands is a float
. Also, the division operator /
always returns a float
, even if both operands are integers. Use the //
operator if you want the result of the division to be an int
.a = 2 + 3
b = 2 + 3.0
type(a), type(b)
(int, float)
a = 3 * 5
b = 3 * 5.0
type(a), type(b)
a = 4/2
b = 4/2.0
type(a), type(b)
int (a)
function¶x = '21'
type(x), x
y = int(x)
type(y), y
z = bool(-5)
type(z), z
# You can also use hex() and oct() functions
print(hex(21))
print(oct(21))
float (a)
function¶a = 54
type(a), a
b = float(a)
type(b), b
# Note that a and be are two different objects
id(a), id(b)
str (a)
function¶num = 341
type(num), num
mystr = str(num)
type(mystr), mystr
complex (a, b)
function¶a = 2.6
b = 3.2
x = complex(a,b)
type(x), x
Try answering the following questions to test your understanding of the topics covered in this notebook:
23e-12
represent?/
and //
?