python data types

@amitmund June 17, 2026

Python Data Types

What is a Data Type?

A data type defines the kind of value a variable can store.

Python automatically determines the data type when a value is assigned.


Numeric Data Types

Integer (int)

Stores whole numbers.

age = 25
temperature = -10

print(type(age))

Output:

<class 'int'>

Float (float)

Stores decimal numbers.

price = 99.99
pi = 3.14159

print(type(price))

Output:

<class 'float'>

Complex (complex)

Stores complex numbers.

num = 3 + 4j

print(num)
print(type(num))

Output:

(3+4j)
<class 'complex'>

Boolean Data Type

Boolean (bool)

Stores either True or False.

is_logged_in = True
is_admin = False

print(type(is_logged_in))

Output:

<class 'bool'>

String Data Type

String (str)

Stores text data.

name = "John"

print(type(name))

Output:

<class 'str'>

Sequence Data Types

List (list)

Ordered and mutable collection.

fruits = ["Apple", "Banana", "Mango"]

print(type(fruits))

Output:

<class 'list'>

Modifying List

fruits[0] = "Orange"

print(fruits)

Output:

['Orange', 'Banana', 'Mango']

Tuple (tuple)

Ordered but immutable collection.

numbers = (10, 20, 30)

print(type(numbers))

Output:

<class 'tuple'>

Common Tuple Mistake

❌ Wrong

numbers = (10, 20, 30)

numbers[0] = 100

Output:

TypeError

Range (range)

Represents a sequence of numbers.

numbers = range(5)

print(list(numbers))

Output:

[0, 1, 2, 3, 4]

Set Data Types

Set (set)

Unordered collection of unique values.

numbers = {1, 2, 3, 4}

print(type(numbers))

Output:

<class 'set'>

Duplicate Values Removed

numbers = {1, 1, 2, 2, 3}

print(numbers)

Output:

{1, 2, 3}

Frozen Set (frozenset)

Immutable version of a set.

data = frozenset([1, 2, 3])

print(data)

Output:

frozenset({1, 2, 3})

Mapping Data Type

Dictionary (dict)

Stores data as key-value pairs.

student = {
    "name": "John",
    "age": 21
}

print(type(student))

Output:

<class 'dict'>

Accessing Values

print(student["name"])

Output:

John

Binary Data Types

Bytes (bytes)

Immutable binary data.

data = b"Hello"

print(data)

Output:

b'Hello'

Bytearray (bytearray)

Mutable binary data.

data = bytearray([65, 66, 67])

print(data)

Output:

bytearray(b'ABC')

Memory View (memoryview)

Provides memory access without copying.

data = memoryview(bytes(5))

print(data)

Output:

<memory at ...>

None Data Type

NoneType

Represents no value.

value = None

print(type(value))

Output:

<class 'NoneType'>

User Defined (Custom) Data Types

Python allows creation of custom data types using classes.

Example

class Student:
    pass

s1 = Student()

print(type(s1))

Output:

<class '__main__.Student'>

Summary of Built-in Data Types

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

Common Data Type Mistakes

Mistake 1: Mixing String and Number

❌ Wrong

age = 20

print("Age: " + age)

Output:

TypeError

✅ Correct

age = 20

print("Age: " + str(age))

Mistake 2: Assuming List and Tuple are Same

❌ Wrong

data = (1, 2, 3)

data[0] = 10

Output:

TypeError

Type Casting and Type Conversion

What is Type Casting?

Converting one data type into another.


Integer to String

age = 25

text_age = str(age)

print(text_age)
print(type(text_age))

Output:

25
<class 'str'>

String to Integer

age = "25"

number = int(age)

print(number)

Output:

25

Integer to Float

num = 10

result = float(num)

print(result)

Output:

10.0

Float to Integer

price = 99.99

result = int(price)

print(result)

Output:

99

Note: Decimal part is removed, not rounded.


String to Float

num = "25.5"

print(float(num))

Output:

25.5

List to Tuple

numbers = [1, 2, 3]

print(tuple(numbers))

Output:

(1, 2, 3)

Tuple to List

numbers = (1, 2, 3)

print(list(numbers))

Output:

[1, 2, 3]

Common Type Casting Mistakes

Mistake 1

❌ Wrong

int("10.5")

Output:

ValueError

✅ Correct

int(float("10.5"))

Output:

10

Mistake 2

❌ Wrong

int("hello")

Output:

ValueError

String Functions

upper()

Converts to uppercase.

name = "python"

print(name.upper())

Output:

PYTHON

lower()

name = "PYTHON"

print(name.lower())

Output:

python

title()

name = "python programming"

print(name.title())

Output:

Python Programming

capitalize()

name = "python"

print(name.capitalize())

Output:

Python

swapcase()

name = "PyThOn"

print(name.swapcase())

Output:

pYtHoN

strip()

text = "   hello   "

print(text.strip())

Output:

hello

lstrip()

text = "   hello"

print(text.lstrip())

Output:

hello

rstrip()

text = "hello   "

print(text.rstrip())

Output:

hello

replace()

text = "I like Java"

print(text.replace("Java", "Python"))

Output:

I like Python

split()

text = "apple,banana,mango"

print(text.split(","))

Output:

['apple', 'banana', 'mango']

join()

items = ["Apple", "Banana", "Mango"]

print("-".join(items))

Output:

Apple-Banana-Mango

find()

text = "Python"

print(text.find("t"))

Output:

2

index()

text = "Python"

print(text.index("t"))

Output:

2

startswith()

print("Python".startswith("Py"))

Output:

True

endswith()

print("Python".endswith("on"))

Output:

True

count()

text = "banana"

print(text.count("a"))

Output:

3

isalpha()

print("Python".isalpha())

Output:

True

isdigit()

print("123".isdigit())

Output:

True

isalnum()

print("Python123".isalnum())

Output:

True

center()

print("Python".center(20))

Output:

       Python

zfill()

print("10".zfill(5))

Output:

00010

Difference Between Function and Method

Function

A function is an independent block of code.

Example:

numbers = [1, 2, 3]

print(len(numbers))

Output:

3

Here:

len()

is a function.


Method

A method belongs to an object.

Example:

name = "python"

print(name.upper())

Output:

PYTHON

Here:

upper()

is a method.


Function vs Method

Function Method
Independent Belongs to an object
Called directly Called using dot notation
Example: len() Example: upper()
Works on data passed to it Works on the object itself

Example Comparison

Function

numbers = [1, 2, 3]

print(len(numbers))

Method

name = "python"

print(name.upper())

Homework 1

Identify the data type:

a = 10
b = 10.5
c = "Python"
d = [1, 2, 3]
e = (1, 2, 3)

Solution

print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))

Homework 2

Convert:

"100"

into integer.

Solution

num = int("100")

print(num)

Homework 3

Convert:

"python programming"

into:

PYTHON PROGRAMMING

Solution

text = "python programming"

print(text.upper())

Homework 4

Count the number of "a" characters in:

"banana"

Solution

print("banana".count("a"))

Output:

3

Homework 5

Replace:

Java

with

Python

in:

I love Java

Solution

text = "I love Java"

print(text.replace("Java", "Python"))

Output:

I love Python

Summary

Topic Key Concept
Data Types Type of data stored
Type Casting Convert one type to another
String Functions Manipulate text
Function Independent block of code
Method Function attached to an object

Master these topics before moving to:

  1. Operators
  2. Conditional Statements
  3. Loops
  4. Functions
  5. Modules
  6. Exception Handling
  7. Object-Oriented Programming
0 Likes
23 Views
0 Comments

Filters

No filters available for this view.

Reset All