Python Struktur Data: Dhaptar, Tuple, Set lan Kamus

List

  • A List punika Uploaded dinamis ing Python, ngijini sampeyan kanggo nyimpen sawetara nilai beda, lan unsur bisa diganti sawise initialization.
  • Kanggo ngumumake a List, gunakake tanda kurung [].

Tuladha:

# Declare a List containing integers  
numbers = [1, 2, 3, 4, 5]  
  
# Access and print elements in the List  
print(numbers[0])  # Output: 1  
print(numbers[2])  # Output: 3  
  
# Modify the value of an element in the List  
numbers[1] = 10  
print(numbers)  # Output: [1, 10, 3, 4, 5]  

 

Tuple

  • A Tuple minangka struktur data sing ora bisa diganti ing Python, asring digunakake kanggo nglindhungi data saka owah-owahan sawise initialization.
  • Kanggo ngumumake a Tuple, gunakake tanda kurung ().

Tuladha:

# Declare a Tuple containing information of a student  
student_info =('John', 25, 'Male', 'New York')  
  
# Access and print elements in the Tuple  
print(student_info[0])  # Output: John  
print(student_info[2])  # Output: Male  

 

Set

  • A Set minangka struktur data sing ora ngemot unsur duplikat lan ora ana urutan.
  • Kanggo wara-wara a Set, nggunakake kurung kriting {} utawa set() fungsi.

Tuladha:

# Declare a Set containing colors  
colors = {'red', 'green', 'blue', 'red', 'yellow'}  
  
# Print the Set to check duplicate elements are removed  
print(colors)  # Output: {'red', 'green', 'blue', 'yellow'}  

 

Dictionary

  • A Dictionary minangka struktur data unordered sing nyimpen informasi ing pasangan kunci-nilai.
  • Kanggo ngumumake a Dictionary, gunakake kurung kriting {} lan misahake saben pasangan nilai kunci kanthi titik loro :.

Tuladha :

# Declare a Dictionary containing information of a person  
person = {'name': 'John', 'age': 30, 'city': 'New York'}  
  
# Access and print values from the Dictionary  
print(person['name'])  # Output: John  
print(person['age'])   # Output: 30  
  
# Modify the value of a key in the Dictionary  
person['city'] = 'Los Angeles'  
print(person)  # Output: {'name': 'John', 'age': 30, 'city': 'Los Angeles'}  

Struktur data iki ngidini programer kanggo ngapusi lan ngolah data kanthi fleksibel ing Python, cocok kanggo macem-macem skenario program lan tujuan.