Python Tsarin Bayanai: Lissafi, Tuples, Saiti da ƙamus

List

  • A List tsari ne mai ƙarfi a cikin Python, yana ba ku damar adana ƙima daban-daban, kuma ana iya canza abubuwa bayan farawa.
  • Don ayyana a List, yi amfani da maƙallan murabba'i [].

Misali:

# 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 shine tsarin bayanai mara canzawa a cikin Python, galibi ana amfani dashi don kare bayanai daga canzawa bayan farawa.
  • Don ayyana wani Tuple, yi amfani da baka ().

Misali:

# 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 tsarin bayanai ne wanda bai ƙunshi abubuwa kwafi ba kuma ba shi da tsari.
  • Don ayyana a Set, yi amfani da takalmin gyaran kafa mai lanƙwasa {} ko set() aikin.

Misali:

# 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 tsarin bayanai ne da ba a ba da oda ba wanda ke adana bayanai cikin maɓalli-daraja nau'i-nau'i.
  • Don ayyana a Dictionary, yi amfani da takalmin gyaran kafa mai lanƙwasa {} kuma raba kowane maɓalli-daraja biyu tare da hanji :.

Misali :

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

Waɗannan tsarin bayanan suna ba masu shirye-shirye damar sarrafa bayanai da sassauƙa a cikin Python, dacewa da yanayin shirye-shirye daban-daban da dalilai.