Python Haɗin kai: Maɓalli, Nau'in Bayanai, Sharuɗɗa, madaukai

Canje-canje da Nau'in Bayanai

Python Harshen shirye-shirye ne da aka buga a hankali, ma'ana ba kwa buƙatar bayyana nau'ikan masu canzawa kafin amfani da su. A ƙasa akwai misalan furci mai canzawa da wasu nau'ikan bayanai gama gari:

Sanarwa mai canzawa:

variable_name = value

Nau'in bayanan gama gari:

  • lamba( int): age = 25
  • Lambar wurin iyo( float): pi = 3.14
  • Zaren( str): name = "John"
  • Boolean( bool): is_true = True

 

Kalamai na Sharadi

Ana amfani da bayanan sharadi a ciki Python don bincika yanayi da aiwatar da bayanai dangane da sakamakon kimantawa. Ana amfani da tsarin if, else, da elif(wani idan) kamar haka:

if sanarwa:

if condition:  
    # Execute this block if condition is True  

else sanarwa:

else:  
    # Execute this block if no preceding if statement is True  

elif (else if) sanarwa:

elif condition:  
    # Execute this block if condition is True and no preceding if or else statement is True  

 

madaukai

Python yana goyan bayan nau'ikan madauki guda biyu da aka saba amfani da su: for madauki da while madauki, yana ba da damar maimaita aiwatar da maganganun.

for madauki:

for variable in sequence:  
    # Execute statements for each value in the sequence  

while madauki:

while condition:  
    # Execute statements while the condition is True  

 

Misali na Musamman:

# Variable declaration  
age = 25  
name = "John"  
  
# Conditional statement  
if age >= 18:  
    print("You are of legal age.")  
else:  
    print("You are not of legal age.")  
  
# Loop  
for i in range(5):  
    print("Hello there!")  
  
count = 0  
while count < 5:  
    print("Loop number:", count)  
    count += 1  

Lokacin da aka aiwatar, lambar da ke sama za ta bincika shekaru da buga saƙon da ya dace, sannan madauki saƙon Hello there!  sau biyar ta amfani da for madauki, sannan a buga ƙimar madauki while.