Python Miundo ya Data: Orodha, Nakala, Seti na Kamusi

List

  • A List ni safu inayobadilika katika Python, inayokuruhusu kuhifadhi thamani nyingi tofauti, na vipengele vinaweza kubadilishwa baada ya kuanzishwa.
  • Kutangaza a List, tumia mabano ya mraba [].

Mfano:

# 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 ni muundo wa data usiobadilika katika Python, mara nyingi hutumika kulinda data isibadilishwe baada ya kuanzishwa.
  • Kutangaza a Tuple, tumia mabano ().

Mfano:

# 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 ni muundo wa data ambao hauna vipengele vinavyorudiwa na hauna mpangilio.
  • Ili kutangaza Set, tumia viunga vilivyopindapinda {} au set() kitendakazi.

Mfano:

# 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 ni muundo wa data ambao haujapangwa ambao huhifadhi maelezo katika jozi za thamani-msingi.
  • Ili kutangaza Dictionary, tumia viunga vilivyopindapinda {} na utenganishe kila jozi ya thamani ya ufunguo na koloni :.

Mfano :

# 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'}  

Miundo hii ya data huruhusu watayarishaji programu kudhibiti na kuchakata data kwa urahisi katika Python, inayofaa kwa matukio na madhumuni mbalimbali ya programu.