I1 · Python Programming for AI

Data Types

A data type says what kind of value something is and what can be done with it. Python's basic types are int (whole numbers), float (numbers with a decimal point), str (text), bool (True or False) and NoneType (the value None, meaning 'nothing'). type() tells you a value's type.

8 min Intermediate · Sublevel Beginner Jump to the activity

What you need: The earlier topics of this module.

Definition

Is "5" the same as 5 to Python?

A data type says what kind of value something is and what can be done with it. Python's basic types are int (whole numbers), float (numbers with a decimal point), str (text), bool (True or False) and NoneType (the value None, meaning 'nothing'). type() tells you a value's type.

In simple words

5 is a number you can add to. "5" is a character, like a letter on a sign. 5 + 5 is 10, but "5" + "5" is "55", because joining text glues it together. True and False are for yes-or-no questions, and None means 'no value yet'.

Overview

  • A data type tells Python what kind of value something is and which operations make sense for it.
  • type(x) shows a value's type; isinstance(x, int) checks it.
  • Numbers come as int, float and complex; text is str; truth values are bool; None means no value.
  • Collections such as list, tuple, set and dict hold many values and have their own pages.
  • Some types are mutable (lists, dicts, sets can change) and others immutable (numbers, strings, tuples cannot).
  • Choosing the right type avoids errors, such as adding a number to text, and saves memory.

Why it matters

Many bugs come from the wrong type: text read from a file that should be a number, or a decimal where a whole number was expected. In AI, data must be the right type before a model can use it.

Key terms

int
Whole numbers of any size, such as 7, -3 or 10**20.
float
Numbers with a decimal point, stored approximately, such as 3.14.
str
Text: a sequence of characters in quotes.
bool
One of the two values True or False.
None
A special value meaning "no value".
Immutable
Cannot be changed after creation; int, float, str, bool and tuple are immutable.

Types

Python's built-in simple (scalar) data types are listed below. Every value you store is one of these, or a collection built from them.

1. Integer (int)

Whole numbers, positive, negative or zero, with no decimal point.

  • How it works: Python integers have no size limit: they grow as large as memory allows, and // and % give whole-number division and remainder.
  • Example: age = 16, population = 1_428_000_000, 17 // 5 = 3 and 17 % 5 = 2.
  • When to use it: Use it for counts, indexes, roll numbers and anything that cannot be fractional.

2. Floating-point number (float)

Numbers with a decimal point or in scientific notation.

  • How it works: Stored in 64-bit binary format (IEEE 754) with about 15–17 significant digits, so some decimals are approximate: 0.1 + 0.2 gives 0.30000000000000004.
  • Example: height = 1.72, temperature = -3.5, speed_of_light = 3e8.
  • When to use it: Use it for measurements and averages; use round() for display and the decimal module for exact money calculations.

3. String (str)

Text: a sequence of Unicode characters inside quotes.

  • How it works: Strings are immutable; you can index, slice, join and format them, and they hold any script, including Devanagari and Tamil.
  • Example: name = "Aarav", city = 'Pune', greeting = "नमस्ते".
  • When to use it: Use it for names, messages, file paths and any text data.

4. Boolean (bool)

One of two truth values: True or False.

  • How it works: Comparisons produce booleans; and, or and not combine them; True and False also behave as 1 and 0 in arithmetic.
  • Example: is_passed = marks >= 33; sum([True, False, True]) is 2.
  • When to use it: Use it for conditions, flags and yes-or-no answers.

5. NoneType (None)

A single special value meaning "nothing" or "no value yet".

  • How it works: Functions without a return statement return None; test for it with is None, not == None.
  • Example: middle_name = None until the user types one.
  • When to use it: Use it for missing or optional values and as a default that means "not given".

6. Complex number (complex)

Numbers with a real part and an imaginary part, written with j.

  • How it works: z = 3 + 4j stores both parts; z.real is 3.0, z.imag is 4.0 and abs(z) is 5.0.
  • Example: Electrical engineers write alternating current as complex numbers.
  • When to use it: Use it in engineering, signal processing and some maths; most data work never needs it.

Explained in detail

Why types matter

The type of a value decides what you can do with it. 7 + 3 adds numbers to make 10, while "7" + "3" joins text to make "73", and 7 + "3" is a TypeError because Python refuses to guess what you meant. Many beginner bugs come from a value having a different type from the one you expected, especially text read from input() or a file, which always arrives as a string. When something behaves strangely, print(type(x)) is often the first useful check.

Numbers: int and float

Python's int has no size limit: 2 ** 100 is computed exactly. Floats are different. They follow the IEEE 754 standard used by almost all computers, storing numbers in binary with about 15 to 17 significant digits. Many decimal fractions, such as 0.1, have no exact binary form, so 0.1 + 0.2 gives 0.30000000000000004. This is not a Python bug; the same happens in most languages. Never compare floats with == when exactness matters; use round(), or math.isclose(), and for money, work in whole paise or use the decimal module.

Text, truth and nothing

TypeGood to know
strStrings are Unicode, so "नमस्ते" and emoji work; single and double quotes are equivalent; triple quotes allow several lines
boolTrue and False are capitalised; comparisons such as 5 > 3 produce them; True behaves like 1 and False like 0 in arithmetic
NoneReturned by functions with no return statement; test for it with x is None

Truthiness

In an if or while condition, any value can be treated as true or false. Zero, an empty string "", an empty list [], an empty dictionary {} and None count as false; almost everything else counts as true. So if name: means "if name is not empty". This is convenient but can surprise you: a score of 0 is falsy, so if score: would skip a learner who genuinely scored zero. When zero is a meaningful value, write the comparison explicitly, such as if score is not None:.

Python also has complex numbers, written like 3 + 4j, used in engineering and signal processing, though you will rarely need them in this course.

How it works

TypeExamplesUsed for
int0, 42, -7Counts, ages, positions
float3.14, 0.5, -2.0Measurements, averages, probabilities
str"Riya", "AI", "42"Names, messages, any text
boolTrue, FalseYes-or-no conditions
NoneTypeNoneNo value yet

In code

type() reveals each value's type, and shows why 5 and "5" behave differently.

types.py
values = [42, 3.14, "AI", True, None]
for value in values:
    print(repr(value), "is", type(value).__name__)

print(5 + 5)
print("5" + "5")
print(10 / 4, "is a float, even though 10 and 4 are ints")

Output

42 is int
3.14 is float
'AI' is str
True is bool
None is NoneType
10
55
2.5 is a float, even though 10 and 4 are ints

Try it yourself

What type is it?

Put each value under its Python type.

  • "True"

  • "17"

  • 98.6

  • 7 / 7

  • False

  • 17

Common mistakes

  • Adding a number to text without converting: TypeError.
  • Writing true instead of True. Capital T and F matter.
  • Expecting 10 / 2 to be 5. It is 5.0, a float.

Check yourself

Question 1

1. What is type("42")?

Question 2

2. "5" + "5" gives…

Question 3

3. What does None mean?

Summary

  • Basic types: int, float, str, bool and None.
  • The type decides what operations make sense.
  • type() shows a value's type; / always gives a float.

Finished reading?

Sources: W3Schools Python Tutorial