Difference between revisions of "Pyhthon - Dasar"
m |
m |
||
Line 23: | Line 23: | ||
a.append(4.0) # Append 4.0 to list | a.append(4.0) # Append 4.0 to list | ||
print(a) | print(a) | ||
+ | |||
+ | [1.0, 2.0, 3.0, 4.0] | ||
+ | |||
a.insert(0,0.0) # Insert 0.0 in position 0 | a.insert(0,0.0) # Insert 0.0 in position 0 | ||
print(a) | print(a) | ||
+ | |||
+ | [0.0, 1.0, 2.0, 3.0, 4.0] | ||
+ | |||
print(len(a)) # Determine length of list | print(len(a)) # Determine length of list | ||
− | + | ||
− | + | 5 | |
+ | |||
+ | a[2:4] = [1.0, 1.0, 1.0] # Modify selected elements | ||
+ | print(a) | ||
+ | |||
+ | [0.0, 1.0, 1.0, 1.0, 1.0, 4.0] |
Latest revision as of 11:11, 2 October 2019
- 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')
- LISTS
a = [1.0, 2.0, 3.0] # Create a list a.append(4.0) # Append 4.0 to list print(a)
[1.0, 2.0, 3.0, 4.0]
a.insert(0,0.0) # Insert 0.0 in position 0 print(a)
[0.0, 1.0, 2.0, 3.0, 4.0]
print(len(a)) # Determine length of list
5
a[2:4] = [1.0, 1.0, 1.0] # Modify selected elements print(a)
[0.0, 1.0, 1.0, 1.0, 1.0, 4.0]