[10, 20, 30, 40]
["spam", "bungee", "swallow"]
["hello", 2.0, 5, [10, 20]]
empty = []
>>>vocabulary = ["Sabir", "Ahmad", "Namra"]
>>>numbers = [17, 123]
>>> empty = []
>>>print(vocabulary)
['Sabir', 'Ahmad', 'Namra']
>>> print(numbers)
[17, 123]
print(empty)
[]
nums = [12, 45 , 34, 50, 100]
>>> nums[0]
12
>>> nums[4]
100
>>> nums[5]
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
IndexError: list index out of range
nums = [12, 45 , 34, 50, 100]
>>> nums[-1]
100
>>> nums[-2]
50
>>> nums[-3]
34
horsemen = ["war", "famine", "pestilence", "death"]
i = 0
while i < 4:
print horsemen[i]
i += 1
horsemen = ["war", "famine", "pestilence", "death"]
i = 0
num = len(horsemen)
while i < num:
print horsemen[i]
i += 1
>>> horsemen = ['war', 'famine', 'pestilence', 'death']
>>> 'pestilence' in horsemen
True
>>> 'debauchery' in horsemen
False
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print c
[1, 2, 3, 4, 5, 6]
>>> [0] * 4
[0, 0, 0, 0]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> a_list = ['a', 'b', 'c', 'd', 'e', 'f']
>>> a_list[1:3]
['b', 'c']
>>> a_list[:4]
['a', 'b', 'c', 'd']
>>> a_list[3:]
['d', 'e', 'f']
>>> a_list[:]
['a', 'b', 'c', 'd', 'e', 'f']
>>> a_list[1:3]
>>> range(1, 5)
[1, 2, 3, 4]
>>>range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 10, 2)
[1, 3, 5, 7, 9]
>>> range(20, 4, -5)
[20, 15, 10, 5]
>>> my_list = ['T', 'E', 'S', 'T']
>>> my_list[2] = 'X'
>>> my_list
['T', 'E', 'X', 'T']
>>> a_list = ['a', 'b', 'c', 'd', 'e', 'f']
>>> a_list[1:3] = ['x', 'y']
>>> print a_list
['a', 'x', 'y', 'd', 'e', 'f']
>>> a_list = ['a', 'b', 'c', 'd', 'e', 'f']
>>> a_list[1:3] = []
>>> print a_list
['a', 'd', 'e', 'f']
a_list = ['a', 'd', 'f']
>>> a_list[1:1] = ['b', 'c']
>>> print a_list
['a', 'b', 'c', 'd', 'f']
>>> a_list[4:4] = ['e']
>>> print a_list
['a', 'b', 'c', 'd', 'e', 'f']
>>> a = ['one', 'two', 'three']
>>> del a[1]
>>> a
['one', 'three']
>>> a_list = ['a', 'b', 'c', 'd', 'e', 'f']
>>> del a_list[1:5]
>>> print (a_list)
['a', 'f']
for VARIABLE in LIST:
BODY
for number in range(20):
if number % 3 == 0:
print(number)
for fruit in ["banana", "apple", "quince"]:
print ("I like to eat " + fruit + "s!")
I like to eat bananas!
I like to eat apples!
I like to eat quinces!
fruit = ["banana", "apple", "quince"]
for index, value in enumerate(fruit):
print (index, value)
0 banana
1 apple
2 quince
>>>nested = ["hello", 2.0, 5, [10, 20]]
>>>print(nested[3])
[10,20]
>>>print(nested[3][1])
20
>>>url = [["Udacity","www.udacity.com"],["Urdu Mehfil","www.urduweb.org/mehfil"]]
>>>print(url[0][0])
Udacity
>>>print(url[1][1])
www.urduweb.org/mehfil
url[0][0] #first element of first list
Udacity
url[1][1] #second element of second list
www.urduweb.org/mehfil
song = "The rain in Spain..."
print(str.split(song))
['The', 'rain', 'in', 'Spain...']
song = "The rain in Spain..."
print(song.split())
Output:
['The', 'rain', 'in', 'Spain...']
words = ['crunchy', 'raw', 'unboned', 'real', 'dead', 'frog']
s = ' '.join(words)
print(s)
crunchy raw unboned real dead frog