Python Sintaksis: Variabel, Jinis Data, Kondisi, Loop

Variabel lan Jinis Data

Python minangka basa pamrograman sing diketik kanthi dinamis, tegese sampeyan ora perlu ngumumake jinis variabel sadurunge nggunakake. Ing ngisor iki conto deklarasi variabel lan sawetara jinis data umum:

Deklarasi variabel:

variable_name = value

Tipe data umum:

  • Integer( int): age = 25
  • Angka floating-point( float): pi = 3.14
  • String( str): name = "John"
  • Boolean( bool): is_true = True

 

Pranyatan kondisional

Pernyataan kondisional ing Python digunakake kanggo mriksa kahanan lan nglakokake pernyataan adhedhasar asil evaluasi. Struktur if, else, lan elif(liyane yen) digunakake kaya ing ngisor iki:

if pratelan:

if condition:  
    # Execute this block if condition is True  

else pratelan:

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

elif (else if) pratelan:

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

 

Loop

Python ndhukung rong jinis loop sing umum digunakake: for loop lan while loop, mbisakake eksekusi statement sing bola-bali.

for loop:

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

while loop:

while condition:  
    # Execute statements while the condition is True  

 

Tuladha spesifik:

# 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  

Nalika dieksekusi, kode ing ndhuwur bakal mriksa umur lan nyithak pesen sing cocog, banjur puter maneh pesen Hello there!  kaping lima kanthi nggunakake for loop, lan pungkasane nyithak nilai while loop.