v9.1.1
Menu

Type Checker

easyFREEVariables & TypesPython BasicsPython Foundations

Problem Statement

Identify the types of different Python values.

Background

Python has several built-in types:

  • int - integers (1, 42, -5)
  • float - floating point numbers (3.14, 2.0)
  • str - strings ("hello", 'world')
  • bool - booleans (True, False)
  • list - ordered collections ([1, 2, 3])
  • dict - key-value mappings ({"a": 1})

Your Task

Write a function get_types(values) that takes a list of values and returns a list of their type names as strings.

Output Format

Return a list of type names (e.g., ["int", "str", "float"]).

Example

Input
[42, 'hello', 3.14, True, [1,2], {'a': 1}]
Output
['int', 'str', 'float', 'bool', 'list', 'dict']
Reasoning
  • Input: values is a list of 66 mixed Python values: a whole number, a piece of text, a decimal number, a truth value, a list and a dictionary.
  • Mapping: each value is replaced by the name of the built-in type it belongs to, written as text: 42 is a whole number, so it becomes 'int'; 'hello' becomes 'str'; 3.14 has a fractional part, so it becomes 'float'.
  • True becomes 'bool'. Notice that even though a boolean can be used in arithmetic like 11, its own exact type is boolean, not integer.
  • [1,2] becomes 'list' and {'a': 1} becomes 'dict': only the outer container is classified, its contents are never inspected.
  • Output: a list of 66 strings, one per input value and in the same order, ['int', 'str', 'float', 'bool', 'list', 'dict']. Any other value works the same way, e.g. the empty value None is reported by its own type name.

Constraints

  • Use type() function
  • Return type name as string using name

Learn this in

More from Python Foundations

solution.py

Test results

Sign in to run your code against the test cases.