← Back to Chapters

Python Data Types

? Python Data Types

⚡ Quick Overview

Data types define the type of data a variable can hold. Python automatically assigns a data type based on the value you assign to the variable. This feature is called dynamic typing.

Understanding data types helps you:

  • Store values correctly in memory.
  • Choose the right structure for numbers, text, collections, etc.
  • Avoid type errors when combining different kinds of values.

? Key Concepts

  • Dynamic typing: Python decides the type at runtime based on the value.
  • Built-in types: Numbers, strings, sequences, sets, dictionaries, booleans, binary types, and NoneType.
  • type() function: Used to check the data type of a value or variable.
  • Type casting: Converting values between types using int(), float(), str(), etc.
  • Booleans & None: Special types for representing truth values and “no value”.

? Built-in Data Type Categories

Python has the following built-in data type categories:

Category Types
Text Type str
Numeric Types int, float, complex
Sequence Types list, tuple, range
Mapping Type dict
Set Types set, frozenset
Boolean Type bool
Binary Types bytes, bytearray, memoryview
None Type NoneType

? Syntax and Basic Usage

You create variables in Python simply by assigning values to names. Python automatically infers the data type from the assigned value:

? Code Examples

? View Basic Data Type Example
x = 10           # int
y = 3.14         # float
name = "Alice"   # str
is_active = True # bool
? View type() Example
x = "Hello"
print(type(x))  # <class 'str'>
⚙️ View Type Casting Example
x = "5"
y = int(x)
z = float(x)

print(type(x))  # str
print(type(y))  # int
print(type(z))  # float

? Live Output / Explanation

? What You Will See

  • For the type() example:
    • x = "Hello" creates a string variable.
    • type(x) prints <class 'str'>, which means the type is string.
  • For the type casting example:
    • x is the string "5".
    • y = int(x) converts it to integer 5.
    • z = float(x) converts it to floating point 5.0.
    • The calls to type() confirm each resulting type.
  • If you try to add a string and a number directly like "Age: " + 25, Python gives a TypeError. You must convert the number to a string first: "Age: " + str(25).

? Tips & Best Practices

  • Use type() frequently while learning to understand what type a variable currently holds.
  • Always use str() to convert numbers before concatenating them with strings.
  • Remember that input() returns a string; convert it with int() or float() if you need a number.
  • Use booleans (True / False) to control if-statements and loops.
  • Use None to represent “no value yet” rather than using fake values like 0 or empty strings.

? Try It Yourself

  • Declare variables with int, float, and str values and print their types using type().
  • Convert a string like "12.5" to a float, multiply it by a number, and print the result and its type.
  • Write a program that takes name and age as input and prints: "Hello Alice, you are 20 years old." (replace with your own values).
  • Create a list, a tuple, and a set with a few values, then print each object and its data type.
  • Assign a variable to None, print it, and check its type using type().