Tutorials Logic, IN info@tutorialslogic.com

Python Data Types int, float, str, list, dict

Type Basics

Python data types describe the kind of value a name currently points to: number, text, boolean, list, tuple, set, dictionary, or None.

Python keywords are reserved words that already have language meaning. You cannot use them as variable names because Python needs them for syntax.

A strong beginner skill is choosing the right data type before writing more code: one value, many ordered values, unique values, or key-value records.

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

Data Types Examples

Data Types Examples
# Numeric types
x = 42           # int
y = 3.14         # float
z = 2 + 3j       # complex

print(type(x))   # <class 'int'>
print(type(y))   # <class 'float'>
print(type(z))   # <class 'complex'>

# String
name = "Python"
print(type(name))  # <class 'str'>

# Boolean
is_active = True
print(type(is_active))  # <class 'bool'>

# 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

Numbers

Numbers
# 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.

All Python Keywords

All Python Keywords
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

Important Keywords

Keywords in Use

Keywords in Use
# 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 - keep a block intentionally empty
def handle_later():
    pass   # add the body when the behavior is known

# assert - debugging check
age = 20
assert age >= 0, "Age cannot be negative"
Data type checkpoint

Can You Identify Python Values?

5 checks
  • Text means str; a typical example is "hello", 'world'.
  • Numeric means int, float, complex; a typical example is 42, 3.14, 2+3j.
  • Boolean means bool; a typical example is True, False.
  • Sequence means list, tuple, range; a typical example is [1,2,3], (1,2).
  • Python Data Types includes str, int, float, complex, bool, and list, tuple, range.

Type Decisions

0 of 2 checked

Q1. What does input() return before you convert it?

Q2. Which item is a Python keyword, not a built-in function?

Type Confusion to Catch

  • Guessing the type from how a value looks

    Use type(value) when a number may actually be text from input, CSV, JSON, or an API.
  • Confusing keywords with function names

    Keywords such as if and for are syntax; built-ins such as len and print are callable names.
  • Treating None like an empty string

    Check value is None when a missing value is different from blank text.

Try this next

Inspect Values in Code

0 of 2 completed

  1. Print type() for an integer, float, string, list, and None.
  2. Separate keywords, built-in function names, and your own variable names into three groups.

Questions About Type Keyword

Use type(value) while learning or debugging. In real logic, prefer behavior checks and clear conversion at input boundaries.

They are Python keywords. Python reserves them for syntax, so using them as names would make code ambiguous.

Start with lists for ordered values and dictionaries for named values. Then learn tuples for fixed records and sets for unique membership.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.