How To Add Comments in Python Code ?: "Divyanshu" "Aditi"
How To Add Comments in Python Code ?: "Divyanshu" "Aditi"
How To Add Comments in Python Code ?: "Divyanshu" "Aditi"
In [6]:
In [7]:
print("Divyanshu")
# print("Yash")
print("Aditi")
Divyanshu
Aditi
List
In [2]:
a = [1,2,3,4,"Divyanshu"] # Method 1
print(type(a))
<class 'list'>
In [13]:
a = [] # Method 1
print(type(a))
<class 'list'>
In [14]:
b = list() # Method 2
print(b)
print(type(b))
[]
<class 'list'>
In [15]:
x = [1,"Divyanshu",2,"Yash","Aditi"]
In [16]:
x[3]
Out[16]:
'Yash'
append()
In [17]:
In [18]:
x.append("Suhani")
In [19]:
Out[19]:
insert()
In [21]:
In [22]:
x.insert(3,"Rishika")
In [23]:
Out[23]:
x.insert(8,"Parth")
In [25]:
Out[25]:
In [31]:
a = [1,2,3,4,5]
b = [6,7,8,9,10]
In [32]:
a.insert(5,b)
In [33]:
Out[33]:
In [34]:
a[5]
Out[34]:
[6, 7, 8, 9, 10]
extend()
In [35]:
In [40]:
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
In [41]:
b.extend(a)
In [42]:
Out[42]:
[1, 2, 3, 4, 5]
In [43]:
Out[43]:
[6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
remove()
In [44]:
# remove is used when you know what element you have to delete.
In [45]:
a = [1,2,3,4,5,6,7,8,9]
In [46]:
a.remove(5)
In [47]:
Out[47]:
[1, 2, 3, 4, 6, 7, 8, 9]
In [48]:
a.remove(7)
In [49]:
Out[49]:
[1, 2, 3, 4, 6, 8, 9]
In [51]:
a.remove(10) # Whenever I try to delete a elemnt which does not exist in the list
# I get an error.
--------------------------------------------------------------------------
-
ValueError Traceback (most recent call las
t)
<ipython-input-51-6bf5f67c44d7> in <module>
----> 1 a.remove(10) # Whenever I try to delete a elemnt which does not ex
ist in the list
2 # I get an error.
pop()
In [ ]:
# Syntax : <listVariable>.pop()
# For using pop you need not to know about what the last element of the list is.
In [54]:
Out[54]:
[1, 2, 3, 4, 6, 8]
In [55]:
z = a.pop()
In [59]:
print(z)
In [57]:
Out[57]:
[1, 2, 3, 4, 6]
In [58]:
a.pop()
Out[58]:
In [60]:
z = []
In [62]:
z.pop() # when we try to pop out something from a empty list, it gives us error.
--------------------------------------------------------------------------
-
IndexError Traceback (most recent call las
t)
<ipython-input-62-dcaa59a41acf> in <module>
----> 1 z.pop() # when we try to pop out something from a empty list, it g
ives us error.
len()
In [76]:
# len(<collection>)
In [65]:
a = len("Divyanshu")
In [66]:
print(a)
In [73]:
x = [1,2,3,4,5]
y = len(x)
In [74]:
print(len(x))
5
In [75]:
print(y)
clear()
In [77]:
# Syntax : <listVariable>.clear()
In [82]:
a = [1,2,3,4,5,6]
In [83]:
Out[83]:
[1, 2, 3, 4, 5, 6]
In [84]:
a.clear()
In [85]:
Out[85]:
[]
In [ ]: