list in python

# declaring a list
list_t = ["my", "name", "is", "abhishek"]
print(list_t)
# accessing first list item
print(list_t[0])
# accessing all item of list
for x in list_t:
    print(x)
# inserting item at position in list .
list_t.insert(0,"hello")print(list_t)

# extending a list
lastname  = ["rathor"]
list_t.extend(lastname)
print(list_t)
# removing item from list
list_t.pop(0)print(list_t)
list_t.remove("rathor")print(list_t)
# index of item in list
print(list_t.index('name'))
# sorting an index
list_t.sort()print(list_t)
# reverse is also possible in index
list_t.reverse()print(list_t)
# constructing a list with constructor
list2  = list(("abhishek", "pratap", "singh" ,"rathor"))
print(list2)
# tuple and list both behave same except tuple are unchangeable and ordered 
collection# so inserting and removing is not possible in tuple .
# tuple are presented in ()
tuple1  = ("hi","my","name","is","tuple")print(tuple1)







# after executing above print function output will be like as 




['my', 'name', 'is', 'abhishek']
my
my
name
is
abhishek
['hello', 'my', 'name', 'is', 'abhishek']
['hello', 'my', 'name', 'is', 'abhishek', 'rathor']
['my', 'name', 'is', 'abhishek', 'rathor']
['my', 'name', 'is', 'abhishek']
1
['abhishek', 'is', 'my', 'name']
['name', 'my', 'is', 'abhishek']
['abhishek', 'pratap', 'singh', 'rathor']
('hi', 'my', 'name', 'is', 'tuple')




Comments

Popular posts from this blog

Install Mysql from ZIP without MySQL Installer or Exe

Logging in Python .

Dictionary in Python