Skip to content

Latest commit

 

History

History
89 lines (83 loc) · 2.86 KB

File metadata and controls

89 lines (83 loc) · 2.86 KB

Data Structures

Lists

  • for dynamic arrays, stacks, queues
  • Lists are ordered collections of items.
  • They are created using square brackets [].
  • Lists can contain elements of different data types.
  • example
    my_list = [1, 2, 3, "hello", True]
  • common operations
    • accessing elements by index
      • my_list[0] returns the first element
    • slicing
      • my_list[1:3] returns a new list with elements 2 and 3
    • adding elements
      • my_list.append(4) adds 4 to the end
    • removing elements
      • my_list.remove(2) removes the first occurrence of 2

Tuples

  • immutable and can used when ordered collection of itmes are needed
  • tuples are similar to lists but immutable(cannot be modified once created)
  • they are created using parentheses ()
  • tuples can also contain elements of different data types
  • example
    my_tuple = (1, 2, 3, "wolrd")
  • common operations
    • accessing elements by index
      • my_tuple[0] returns the first element
    • tuple packing and unpacking
      • x, y = my_tuple

Dictionaries

  • uesful for key-value pair storage and quick lookups
  • store data in key-value pairs
  • created using curly vraces {} or the dict() constructor
  • 키로 사용 가능한 데이터 타입은 해시 가능한(Hashable) 데이터 타입이어야 함. 해시 가능한 타입은 불변(immutable)하고 해시 가능한 함수를 지워하는 타입
  • example
    my_dict = {"name": "Alice", "age": 30, "city": "New York"}
  • common operations
    • accessing values by key
      • my_dict["name"] returns "Alice"
    • adding key-value pairs
    • removing key_value pairs
      • del my_dict["age"]

Sets

  • for unique value stroage and set operations
    • set operations: union, intersection, complement, difference, symmetric difference, subset, superset, disjoin sets
  • unordered collections of unique elements
  • created using curly braces {} or the set()
  • example
    my_set = {1, 2, 3, 3, 4}
  • common operations
    • adding elements
      • my_set.add(5)
    • removing elements
      • my_set.remove(3)
    • set operations like union, intersection, and difference
      • my_set.union(other_set)

Strings

  • sequences of characters and are used for text processing
  • can be created using single, double, or triple quotes
  • example
    my_string = "Hello, World!"
  • common operations
    • slicing
    • concatenation
    • string methods like split(), strip(), replace()

Lists, Tuples, Dictionaries, and Sets Comprehensions

  • Python provides a concise way to create these data structures using comprehensions
  • comprehensions allow to create new data structures by specifying a set of expressions
  • example
    squares = [x**2 for x in range(1, 6)]