0% found this document useful (0 votes)
4 views

Python_Data_Types_Full_Comparison

This document explains various Python data types: Strings, Tuples, Sets, Lists, and Dictionaries, detailing their functions, differences, and mutability. It includes examples of common methods for each data type and compares their characteristics. Additionally, it highlights the mutable and immutable nature of these data types.

Uploaded by

john28223672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python_Data_Types_Full_Comparison

This document explains various Python data types: Strings, Tuples, Sets, Lists, and Dictionaries, detailing their functions, differences, and mutability. It includes examples of common methods for each data type and compares their characteristics. Additionally, it highlights the mutable and immutable nature of these data types.

Uploaded by

john28223672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Data Types: Strings, Tuples, Sets, Lists, and Dictionaries

This document provides a detailed explanation of different Python data types, including
Strings, Tuples, Sets, Lists, and Dictionaries. It covers their functions, differences, mutability,
and comparison with examples.

1. String Functions
A string is a sequence of characters and is immutable. Common string methods include:

Function Description Example


s.upper() Converts all characters to "hello".upper() → "HELLO"
uppercase
s.lower() Converts all characters to "HELLO".lower() → "hello"
lowercase
s.strip() Removes leading/trailing " hello ".strip() → "hello"
whitespace
s.split(separator) Splits string into a list "hello world".split() →
["hello", "world"]
s.replace(old, new) Replaces occurrences of old "hello".replace("h", "y") →
with new "yello"

2. Tuple Functions
A tuple is an immutable ordered collection. Common tuple methods include:

Function Description Example


t.count(value) Counts occurrences of a (1,2,3,1).count(1) → 2
value
t.index(value) Finds the first occurrence (1,2,3).index(2) → 1
index

3. Set Functions
A set is an unordered collection of unique elements. Common set methods include:

Function Description Example


s.add(value) Adds an element {1,2}.add(3) → {1,2,3}
s.remove(value) Removes an element {1,2}.remove(1) → {2}
s.union(B) Combines sets {1,2}.union({2,3}) → {1,2,3}
s.intersection(B) Finds common elements {1,2,3}.intersection({2,3,4})
→ {2,3}

4. List Functions
A list is an ordered, mutable collection. Common list methods include:

Function Description Example


l.append(value) Adds an element [1,2].append(3) → [1,2,3]
l.insert(index, value) Inserts at index [1,3].insert(1,2) → [1,2,3]
l.pop() Removes and returns last [1,2,3].pop() → 3
element
l.sort() Sorts list [3,1,2].sort() → [1,2,3]

5. Dictionary Functions
A dictionary stores key-value pairs. Common dictionary methods include:

Function Description Example


d.keys() Returns all keys {"a":1,"b":2}.keys() →
dict_keys(["a", "b"])
d.values() Returns all values {"a":1,"b":2}.values() →
dict_values([1,2])
d.get(key) Gets value for key {"a":1}.get("a") → 1
d.update({key: value}) Updates dictionary {"a":1}.update({"b":2}) →
{"a":1,"b":2}

6. Mutable vs Immutable
Some Python objects can be modified (mutable), while others cannot (immutable).

Data Type Mutable or Immutable? Example


String (str) ❌ Immutable "hello"
Tuple (tuple) ❌ Immutable (1, 2, 3)
List (list) ✅ Mutable [1, 2, 3]
Set (set) ✅ Mutable {1, 2, 3}
Dictionary (dict) ✅ Mutable {"a": 1, "b": 2}

7. Comparison of Data Types


The following table compares strings, tuples, sets, lists, and dictionaries based on various
characteristics.

Feature String Tuple Set List Dictionary


Definition Sequence of Immutable Unordered Ordered, Key-value
characters ordered unique mutable pairs
collection collection collection
Mutable? ❌ No ❌ No ✅ Yes ✅ Yes ✅ Yes
Ordered? ✅ Yes ✅ Yes ❌ No ✅ Yes ✅ Yes (since
Python 3.7)
Allows ✅ Yes ✅ Yes ❌ No ✅ Yes Keys ❌,
Duplicates? Values ✅
Indexing ✅ Yes ✅ Yes ❌ No ✅ Yes Keys act as
Supported? indexes
Slicing ✅ Yes ✅ Yes ❌ No ✅ Yes ❌ No
Supported?
Change ❌ No ❌ No ✅ Yes ✅ Yes ✅ Yes
Elements?
Common upper(), count(), add(), append(), keys(),
Methods split() index() remove() sort() values()
Use Case Text Fixed Unique Dynamic, Fast key-
manipulatio collection elements ordered data value lookup
n

You might also like