Escolar Documentos
Profissional Documentos
Cultura Documentos
Python Lists
Python has a great built-in list type named "list". List literals are written within
square brackets [ ]. Lists work similarly to strings -- use the len() function and
square brackets [ ] to access data, with the first element at index 0.
colors = ['red', 'blue', 'green']
print colors[0] ## red
print colors[2] ## green
print len(colors) ## 3
List Comprehension: Elegant way to
create new List
List comprehension is an elegant and concise way to create new list from an existing list
in Python.
Here is an example to make a list with each item being increasing power of 2.
print(pow2)
print items
The output should be: ['t', 'i', 'a', 'l', 'o', 'w']
QP: Read a text from the user and print out the first letter of each word in the text using list
comprehension
a="hello how r u"
#b=[]
b=a.split(' ')
print b
w=[x[0] for x in b]
print w
# If you now just print that function with a value in it, it should look like this:
>>> print double(10)
20
We can easily use list comprehension on that function.
print double
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Output
('python', 'geeks')
('python', 'geeks')
Concatenation of Tuples
# Code for concatenating 2 tuples
tuple1 = (0, 1, 2, 3)
tuple2 = ('python', 'geek')
tuple1 = (0, 1, 2, 3)
tuple2 = ('python', 'geek')
tuple3 = (tuple1, tuple2)
print(tuple3)
Output :
Repetition in Tuples
# Code to create a tuple with repetition
tuple3 = ('python',)*3
print(tuple3)
Output
Try the above without a comma and check. You will get tuple3 as a string
‘pythonpythonpython’.
Immutable Tuples
tuple1[0]=4
TypeError: 'tuple' object does not support item assignment
Slicing in Tuples
# code to test slicing
tuple1 = (0 ,1, 2, 3)
print(tuple1[1:])
print(tuple1[::-1])
print(tuple1[2:4])
Run on IDE
Output
(1, 2, 3)
(3, 2, 1, 0)
(2, 3)
Deleting a Tuple
# Code for deleting a tuple
tuple3 = ( 0, 1)
del tuple3
print(tuple3)
Run on IDE
Error:
print(tuple3)
Output:
(0, 1)
list1 = [0, 1, 2]
print(tuple(list1))
print(tuple('python')) # string 'python'
Run on IDE
Output
(0, 1, 2)
Takes a single parameter which may be a list,string,set or even a dictionary( only keys
are taken as elements) and converts them to a tuple.
Tuples in a loop
#python code for creating tuples in a loop
tup = ('geek',)
n = 5 #Number of time loop runs
for i in range(int(n)):
tup = (tup,)
print(tup)
Output :
(('geek',),)
((('geek',),),)
(((('geek',),),),)
((((('geek',),),),),)
(((((('geek',),),),),),)
Output
Not the same
Note: max() and min() checks the based on ASCII values. If there are two strings in a tuple,
then the first different character in the strings are checked.
my_tuple = ('a','p','p','l','e',)
Python Tuple Method
# Count
# Output: 2
Method Description print(my_tuple.count('p'))
# Index
count(x) Return the number of items that is equal to x
# Output: 3
print(my_tuple.index('l'))
index(x) Return index of first item that is equal to x
Print how many time ‘o’ is repeated in a given string?(“hello,how are you?”)