Just assign a value: x = 5
. Python automatically detects the data type.
int
, float
, str
, bool
, list
, tuple
, dict
, and set
.
Use type(x)
.
Yes, Python variables are dynamically typed.
Python uses dynamic typing, which means you don’t have to declare a variable’s type explicitly. The interpreter determines it at runtime:
x = 10 # int
x = "hello" # now it's a str
list
, dict
, set
).int
, float
, str
, tuple
).Use built-in functions:
int("10") # Converts string to int
float("3.14") # Converts string to float
str(100) # Converts int to string
list("abc") # Converts string to list
is
and ==
?==
compares values.is
compares object identity (memory location).a = [1, 2]
b = [1, 2]
print(a == b) # True
print(a is b) # False
None
, False
, and 0
?None
: Represents the absence of a value.False
: Boolean value.0
: Numeric zero.Python has built-in support for complex numbers:
z = 2 + 3j
print(z.real) # 2.0
print(z.imag) # 3.0
Yes, Python supports multiple assignment:
a, b, c = 1, 2, 3
x = y = z = 0 # All variables assigned 0