Data Types & Keywords
Python Data Types
Python has several built-in data types. Since Python is dynamically typed, the type is determined at runtime based on the value assigned.
| Category | Type | Example |
|---|---|---|
| Text | str | "hello", 'world' |
| Numeric | int, float, complex | 42, 3.14, 2+3j |
| Boolean | bool | True, False |
| Sequence | list, tuple, range | [1,2,3], (1,2) |
| Mapping | dict | {"key": "value"} |
| Set | set, frozenset | {1, 2, 3} |
| Binary | bytes, bytearray | b"hello" |
| None | NoneType | None |
# Numeric types
x = 42 # int
y = 3.14 # float
z = 2 + 3j # complex
print(type(x)) #
print(type(y)) #
print(type(z)) #
# String
name = "Python"
print(type(name)) #
# Boolean
is_active = True
print(type(is_active)) #
# None (absence of value)
result = None
print(result is None) # True
# Sequence types
my_list = [1, 2, 3] # mutable
my_tuple = (1, 2, 3) # immutable
my_range = range(1, 10, 2) # 1,3,5,7,9
# Mapping
person = {"name": "Alice", "age": 25}
# Set
unique = {1, 2, 3, 2, 1} # {1, 2, 3}
Numeric Types in Detail
# int - unlimited precision
big = 123456789012345678901234567890
print(big)
# float - 64-bit double precision
pi = 3.141592653589793
sci = 1.5e10 # scientific notation = 15000000000.0
# complex
c = 3 + 4j
print(c.real) # 3.0
print(c.imag) # 4.0
print(abs(c)) # 5.0 (magnitude)
# Useful numeric functions
print(abs(-7)) # 7
print(round(3.567, 2)) # 3.57
print(pow(2, 10)) # 1024
print(divmod(17, 5)) # (3, 2) - quotient and remainder
Python Keywords
Keywords are reserved words that have special meaning in Python. You cannot use them as variable names, function names, or identifiers.
import keyword
print(keyword.kwlist)
# Python 3 keywords:
# False await else import pass
# None break except in raise
# True class finally is return
# and continue for lambda try
# as def from nonlocal while
# assert del global not with
# async elif if or yield
Key Keywords Explained
# True, False, None
flag = True
empty = None
# and, or, not
print(True and False) # False
print(True or False) # True
print(not True) # False
# in, not in - membership test
fruits = ["apple", "banana"]
print("apple" in fruits) # True
print("mango" not in fruits) # True
# is, is not - identity test
x = None
print(x is None) # True
print(x is not None) # False
# del - delete a variable
temp = 42
del temp
# print(temp) # NameError: name 'temp' is not defined
# pass - empty placeholder
def todo():
pass # implement later
# assert - debugging check
age = 20
assert age >= 0, "Age cannot be negative"
Related Python Topics