Python data structures

Created By: chatGPT

In Python, data structures are important because they allow you to organize and manage data effectively. Let's explore some of the most commonly used data structures: lists, tuples, sets, and dictionaries.
Lists are ordered, mutable (changeable) collections of items. You can create a list by enclosing items in square brackets. Here's how to create and manipulate lists:
my_list = [1, 2, 3, 'a', 'b', 'c']

# Accessing elements
first_element = my_list[0]  # 1

# Modifying elements
my_list[1] = 5  # [1, 5, 3, 'a', 'b', 'c']
Tuples are similar to lists, but they are immutable, meaning you cannot change them after they are created. Tuples are defined using parentheses:
my_tuple = (1, 2, 3, 'a', 'b', 'c')

# Accessing elements
second_element = my_tuple[1]  # 2

# Tuples cannot be modified
# my_tuple[1] = 5  # This will raise an error
Sets are unordered collections of unique items. You can use sets to eliminate duplicate values from a list. Sets are defined using curly braces:
my_set = {1, 2, 2, 3, 4}  # {1, 2, 3, 4}

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

# Removing elements
my_set.remove(2)  # {1, 3, 4, 5}
Dictionaries are collections of key-value pairs. They are useful for storing related information, and you can access values using their corresponding keys. Here's how you can work with dictionaries:
my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}

# Accessing values
name = my_dict['name']  # 'Alice'

# Adding a new key-value pair
my_dict['job'] = 'Engineer'  # {'name': 'Alice', 'age': 25, 'city': 'New York', 'job': 'Engineer'}
Introduction And SetupVariablesData TypesIntFloatStrBoolListTupleSetDictMethodsString MethodsList MethodsDict MethodsSet MethodsControl FlowIf StatementElif StatementElse StatementFor LoopWhile LoopBreak StatementContinue StatementFunctionsDef StatementReturn StatementLambda FunctionsScopeParametersArgumentsDefault ParametersVariable Length ArgumentsOperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsAssignment OperatorsIdentity OperatorsMembership OperatorsData StructuresListsTuplesSetsDictionariesCommentsSingle Line CommentsMulti Line CommentsSyntaxIndentationDocstringsEvent HandlingError HandlingTry StatementExcept StatementFinally StatementRaise StatementAssert StatementContext ManagersWith Statement