How To Add Comments in Python Code ?: "Divyanshu" "Aditi"

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

How to add Comments in Python Code ?

In [6]:

# Using '#' we can create a single line comment.

# A comment does not get evaluated during running of a program.

In [7]:

print("Divyanshu")
# print("Yash")
print("Aditi")

Divyanshu
Aditi

List
In [2]:

# How to declare a List ?

a = [1,2,3,4,"Divyanshu"] # Method 1

print(type(a))

<class 'list'>

In [13]:

# How to declare a empty list ?

# By using empty sqaure brackets.

a = [] # Method 1
print(type(a))

<class 'list'>

In [14]:

# How to declare a empty list ?

# By using list() function.

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]:

# How to add element to a list ?

# Using the append() function.

# Syntax : <listVariable>.append(<entity you want to add to list>)

In [18]:

x.append("Suhani")

In [19]:

Out[19]:

[1, 'Divyanshu', 2, 'Yash', 'Aditi', 'Suhani']

insert()

In [21]:

# How to insert element in a list ?

# Using the insert() function.

# Syntax : <listVariable>.insert(<index at which you want to insert>,<entity you want t


o insert>)

In [22]:

x.insert(3,"Rishika")

In [23]:

Out[23]:

[1, 'Divyanshu', 2, 'Rishika', 'Yash', 'Aditi', 'Suhani']


In [26]:

x.insert(8,"Parth")

# whenever we try to insert a element at a index greater the length of list.


# insert function will add that element to the end of the list.

In [25]:

Out[25]:

[1, 'Divyanshu', 2, 'Rishika', 'Yash', 'Aditi', 'Suhani', 'Parth']

In [31]:

a = [1,2,3,4,5]
b = [6,7,8,9,10]

In [32]:

a.insert(5,b)

In [33]:

Out[33]:

[1, 2, 3, 4, 5, [6, 7, 8, 9, 10]]

In [34]:

a[5]

Out[34]:

[6, 7, 8, 9, 10]

extend()

In [35]:

# How to extend a list from other list ?

# We can do this by using extend() function.

# Syntax : <listVariable you want to extend>.extend(<listvariable from which you want t


o extend>)

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]:

# How to remove a element from a list ?

# We can do this by using remove() function.

# Syntax : <listVariable>.remove(<entity you want to remove>)

# 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.

ValueError: list.remove(x): x not in list

pop()

In [ ]:

# How to remove last element from a list ?

# We can do this by using pop() function.

# 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.

IndexError: pop from empty list

len()

In [76]:

# len() function is used to calculate the length of any collection.

# Collection could be anything like String, List, Tuples, Set, etc.

# return length of collection passed

# 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]:

# If we want to delete all the elements from a list ?

# We can use clear() fuction for this.

# 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 [ ]:

You might also like