Pyhthon - Dasar

From ccitonlinewiki
Revision as of 11:05, 2 October 2019 by Admin (talk | contribs)
Jump to: navigation, search
  1. TUPLES
rec = ('Shahzad','Rumi','Alvaro',(5,2,15)) # Ini adalah sebuah tuple
NamaAwal,NamaTengah,NamaAkhir,tanggallahir = rec # Unpacking the tuple
print(NamaTengah) # akan dicetak Rumi

Rumi

TahunLahir = tanggallahir[2]
print(TahunLahir) # akan dicetak 15
name = rec[0] + ' ' + rec[1]
print(name) # akan dicetak Shahzad Rumi 

Shahzad Rumi

print(rec[0:3]) # akan dicetak ('Shahzad','Rumi','Alvaro')

('Shahzad','Rumi','Alvaro')

  1. LISTS

a = [1.0, 2.0, 3.0] # Create a list

a.append(4.0) # Append 4.0 to list

print(a)

a.insert(0,0.0) # Insert 0.0 in position 0

print(a)

print(len(a)) # Determine length of list

a[2:4] = [1.0, 1.0, 1.0] # Modify selected elements

print(a)