Skip to content
VLSI Mentor

Python Part I · Module 2 · Variables and Data Types

Checking a Type with type()

Three separate chapters have now told you that two values can print identically and still be completely different things, and each time you have had to take it on trust. This chapter ends that. Python provides a way to ask any value what kind it is, and the answer settles the question immediately. That makes it the single most useful diagnostic tool a beginner has: when a result makes no sense, the cause is very often that a value is not the type you assumed, and one line will tell you. The output has a slightly odd shape the first time you see it, and this chapter explains exactly how much of that you need to understand right now, which is less than it appears.

Foundation8 min readPythonPart IFundamentalstypeDiagnostics

Part I · Module 2 · Chapter 2.10 · Checking a Type with type()

1. Asking Python What It Has

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print(type(4210))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
<class 'int'>

type is a name Python provides, used the same way print is: write the name, then brackets, then the value you are asking about.

Here it is applied to each of the value kinds from this module:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print(type(4210))
print(type(182.4))
print(type("axi_smoke"))
print(type(True))
print(type(None))
print(type(3 + 4j))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>
<class 'complex'>

Six values, six types. The part you care about is the word in quotes: int, float, str, bool, NoneType, complex.

2. Why It Says "class"

The obvious question, answered plainly.

Python organises types through a general system in which a type is described by something called a class. <class 'int'> is Python telling you the type of this value is the one called int.

You do not need that machinery yet. Classes are taught properly in Module 16 — Object-Oriented Programming, at a point where the rest of the language makes them worth having.

3. Settling the Earlier Questions

Three chapters made a claim you could not check. Now you can.

4210 and "4210"

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print(type(4210))
print(type("4210"))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
<class 'int'>
<class 'str'>

Chapter 2.9 §3 said these were different values that print identically. Here is the proof.

True and "True"

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print(type(True))
print(type("True"))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
<class 'bool'>
<class 'str'>

None and "None"

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print(type(None))
print(type("None"))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
<class 'NoneType'>
<class 'str'>

In every case the second value is a str. Quotes make text, and text is never the thing it spells — now demonstrated rather than asserted.

4. Checking a Name

type works on a name just as well as on a written-out value, because a name refers to a value:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
seed = 4210
seed_text = "4210"

print(type(seed))
print(type(seed_text))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
<class 'int'>
<class 'str'>

This is the form you will actually use, because in a real script the value came from somewhere else and you cannot see it written down.

5. Using It as a Diagnostic

This is what the tool is for.

Something in a script does not behave the way you expect. A number will not add up; a comparison gives the wrong answer; a value that should be missing is not. Very often the cause is that a value is not the type you assumed.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
# Read from a file, a report or a command line.
# It looks like a number. Is it?
value = "12"

print(value)
print(type(value))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
12
<class 'str'>

print showed 12 and told you nothing useful. type showed that it is text. That is the answer, and it took one line.

6. What to Do About It

Knowing a value is the wrong type is half the problem. The other half — converting it — is the next chapter.

For now: type diagnoses, it does not fix. It changes nothing about the value; it only reports.

7. Predict, Run, Observe

Predict each line of output — the full <class '...'> form — then run the file.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print(type(0))
print(type(0.0))
print(type("0"))
print(type(False))
print(type(""))
print(type(None))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'str'>
<class 'NoneType'>

Two to check yourself on. 0.0 is a float even though the value is whole — Chapter 2.6 §2, the decimal point decides. And "" is a str even though it contains nothing — empty text is still text, which is why Chapter 2.9 §6 insisted it is not None.

8. Exercise

Exercise 1 — predict all six. Without running, write down the type of each: 500, "500", 5.0, True, "True", None. Then check every one.

Exercise 2 — diagnose it. Here is a fragment where something is wrong:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
error_count = "0"
print(error_count)

The output looks correct. Add one line that reveals the problem, and say in a sentence what a script trying to use this value would find.

Exercise 3 — name or value. Show that type gives the same answer whether you pass a written-out value or a name referring to it. Use a seed as the example.

Exercise 4 — the odd one out. Of these five, four share a type. Which is the odd one, and what is each type?

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print(type("PASS"))
print(type("4210"))
print(type(4210))
print(type("True"))
print(type(""))

9. Summary

  • type(value) reports the kind of a value. Use it like print — name, brackets, value.
  • Read <class 'int'> as "this value has type int". The word class comes from machinery taught in Module 16 and is not needed now.
  • The six types in this module: int, float, str, bool, NoneType, complex.
  • It works on names as well as written-out values, which is the form real scripts use.
  • It is a diagnostic: when something behaves oddly, check the type before anything else. print shows what a value looks like; type shows what it is.
  • It reports, it does not convert. Changing a type is the next chapter.

You can now find out that a value is text when you needed a number. The next chapter is about what to do when that happens — and it is the chapter most beginner log parsers eventually fail on.

Where this fits

Part of the Python curriculum.