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

Python_Data_Types

The document explains various Python data types including lists, dictionaries, sets, integers, strings, and tuples, highlighting their characteristics such as order, mutability, and allowance of duplicates. Each data type is defined with examples demonstrating how to create, access, and modify them. Additionally, a comparison table summarizes key differences among these data types.

Uploaded by

Amit kumar Dev
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python_Data_Types

The document explains various Python data types including lists, dictionaries, sets, integers, strings, and tuples, highlighting their characteristics such as order, mutability, and allowance of duplicates. Each data type is defined with examples demonstrating how to create, access, and modify them. Additionally, a comparison table summarizes key differences among these data types.

Uploaded by

Amit kumar Dev
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Data Types Explained

List
A list is an ordered, mutable (changeable) collection that can hold a variety of data types.
Lists are defined using square brackets [].

Example:

my_list = [1, 2, 3, "apple", True]


print(my_list) # Output: [1, 2, 3, 'apple', True]

# Accessing elements
print(my_list[1]) # Output: 2

# Adding elements
my_list.append("orange")
print(my_list) # Output: [1, 2, 3, 'apple', True, 'orange']

Dictionary
A dictionary is an unordered, mutable collection of key-value pairs. Dictionaries are defined
using curly braces {}.

Example:

my_dict = {"name": "Alice", "age": 25, "is_student": True}


print(my_dict) # Output: {'name': 'Alice', 'age': 25, 'is_student': True}

# Accessing values
print(my_dict["name"]) # Output: Alice

# Adding key-value pairs


my_dict["city"] = "New York"
print(my_dict) # Output: {'name': 'Alice', 'age': 25, 'is_student': True, 'city': 'New York'}

Set
A set is an unordered, mutable collection of unique elements. Sets are defined using curly
braces {} or the set() function.

Example:

my_set = {1, 2, 3, 4, 4, 5}
print(my_set) # Output: {1, 2, 3, 4, 5} (duplicates are removed)

# Adding elements
my_set.add(6)
print(my_set) # Output: {1, 2, 3, 4, 5, 6}

Integer (int)
Integers are whole numbers, positive or negative, without decimals.

Example:

my_int = 42
print(my_int) # Output: 42

# Arithmetic operations
print(my_int + 8) # Output: 50

String
A string is a sequence of characters enclosed in single ' or double " quotes.

Example:

my_string = "Hello, Python!"


print(my_string) # Output: Hello, Python!

# Accessing characters
print(my_string[0]) # Output: H

# String concatenation
print(my_string + " How are you?") # Output: Hello, Python! How are you?

Tuple
A tuple is an ordered, immutable collection. Tuples are defined using parentheses ().

Example:

my_tuple = (1, 2, 3, "apple", True)


print(my_tuple) # Output: (1, 2, 3, 'apple', True)

# Accessing elements
print(my_tuple[1]) # Output: 2
# Tuples are immutable, so we can't modify elements
# my_tuple[0] = 10 # This will raise an error

Key Differences
Type Ordered? Mutable? Duplicates Allowed?
List Yes Yes Yes
Dictionary No Yes No (for keys)
Set No Yes No
Integer N/A Immutable N/A
String Yes Immutable Yes
Tuple Yes No Yes

You might also like