Python dictionary – clear() : method

Python Dictionary : clear()

  1. In order to remove the all items from the dictionary we use clear().
  2. It does not take any parameter
  3. Method does not return any value.

Syntax

Below syntax is used for clear() method –

dict.clear()

Python Dictionary : clear() Example

#!/usr/bin/python

dict = {'Word1': 'Meaning1', 'Word2': 'Meaning2'};

print ("Dictionary Length : %d" %  len(dict))
dict.clear()
print ("Dictionary Length : %d" %  len(dict))

Output :

After running the program we will get below output –

>>> 
Dictionary Length : 2
Dictionary Length : 0
>>> 

Python Comparison Operators

Python comparison operators are used to compare the two numbers. Python comparison operators are very useful in decision making

Python comparison operators :

#!/usr/bin/python

num1 = 20 > 10;
num2 = 20 < 10;
num3 = 20 == 10;

print ("Line 1 - Value of num1 : ", num1)
print ("Line 1 - Value of num2 : ", num2)
print ("Line 1 - Value of num3 : ", num3)

Output :

>>> 
Line 1 - Value of num1 :  True
Line 1 - Value of num2 :  False
Line 1 - Value of num3 :  False
>>>

Some Python comparison operators

Consider the following values of num1 and num2

num1 = 10
num2 = 5
OperatorExplanationResult
== It returns true if the value of two operands are equal and false if two operands are not equalfalse
!= It returns true if the value of two operands are not equal and false if two operands are equaltrue
> It returns true if the value of left operands is greater than right operand else it returns falsetrue
< It returns true if the value of left operands is less than right operand else it returns falsefalse
>= It returns true if the value of left operands is greater than or equal to right operand else it returns falsetrue
<= It returns true if the value of left operands is less than or equal to right operand else it returns falsefalse

Python Examples : Comparison Operator

Example #1 : Comparison Operator

#!/usr/bin/python

res = 20 == 10;
print ("Line 1 - Value of res : ", res)

res = 20 != 10;
print ("Line 2 - Value of res : ", res)

res = 20 > 10;
print ("Line 3 - Value of res : ", res)

res = 20 < 10;
print ("Line 4 - Value of res : ", res)

res = 20 >= 10;
print ("Line 5 - Value of res : ", res)

res = 20 <= 10;
print ("Line 6 - Value of res : ", res)

Output :

>>> 
Line 1 - Value of res :  False
Line 2 - Value of res :  True
Line 3 - Value of res :  True
Line 4 - Value of res :  False
Line 5 - Value of res :  True
Line 6 - Value of res :  False
>>> 

Example #2 : Comparison Operator

#!/usr/bin/python

num1 = 10
num2 = 5

if(num1 == num2)
   print ("Line 1 - True Condition")
else
   print ("Line 1 - False Condition")

if(num1 != num2)
   print ("Line 2 - True Condition")
else
   print ("Line 2 - False Condition")

if(num1 > num2)
   print ("Line 3 - True Condition")
else
   print ("Line 3 - False Condition")

if(num1 < num2)
   print ("Line 4 - True Condition")
else
   print ("Line 4 - False Condition")

if(num1 >= num2)
   print ("Line 5 - True Condition")
else
   print ("Line 5 - False Condition")

if(num1 <= num2)
   print ("Line 6 - True Condition")
else
   print ("Line 6 - False Condition")

Output :

>>> 
Line 1 - False Condition
Line 2 - True Condition
Line 3 - True Condition
Line 4 - False Condition
Line 5 - True Condition
Line 6 - False Condition
>>> 

Python Assignment Operators

In this chapter we will be learning about Python assignment operators and some compound assignment operators supported in Python

Python Assignment operators :

Python Assignment operator is used for assigning the value of right operand to the left operand.

#!/usr/bin/python

num1 = 10
num2 = 20.20
num3 = 'A'

print ("Line 1 - Value of num1 : ", num1)
print ("Line 2 - Value of num2 : ", num2)
print ("Line 3 - Value of num3 : ", num3)

Output :

>>> 
Line 1 - Value of num1 :  10
Line 2 - Value of num2 :  20.2
Line 3 - Value of num3 :  A
>>> 

Python Compound Assignment Operators

Consider the value of num1 = 10 and num2 = 5

OperatorExplanation
+=Add right operand to left and assigns addition to the left operand
-=Subtract right operand from left and assigns subtraction to the left operand
*=Multiply right operand to left and assigns multiplication to the left operand
/=Divide right operand by left and assigns division to the left operand
%=Divide right operand by left and assigns remainder of division to the left operand
//=Divide right operand by left and assigns results of division to the left operand by removing digits after the decimal point
**=Perform exponential calculation of two operands and then assign result to left operand.

Assignment Arithmetic operator

In these kind of compound assignment operators we are also performing indirect python arithmetic operations.

Example:

#!/usr/bin/python

num1 = 10
num2 = 5
res = num1 + num2

res += num1
print ("Line 1 - Result of + is ", res)

res *= num1
print ("Line 2 - Result of * is ", res)

res /= num1
print ("Line 3 - Result of / is ", res)

res //= num1
print ("Line 4 - Result of // is ", res)

res **= num1
print ("Line 5 - Result of ** is ", res)

res %= num1
print ("Line 6 - Result of % is ", res)

res -= num1
print ("Line 7 - Result of - is ", res)

Output :

>>> 
Line 1 - Result of + is  25
Line 2 - Result of * is  250
Line 3 - Result of / is  25.0
Line 4 - Result of // is  2.0
Line 5 - Result of ** is  1024.0
Line 6 - Result of % is  4.0
Line 7 - Result of - is  -6.0
>>> 

Python Arithmetic Operators

Python Arithmetic operators :

Python provides some arithmetic operators which perform the task of arithmetic operations. Below are some arithmetic operators –

Consider the value of num1 = 10 and num2 = 5

OperatorExplanationExample
+It is used to perform addition of two numbers num1 + num2 will give 15
It is used to perform subtraction of two numbers num1 – num2 will give 5
*It is used to perform multiplication of two numbers num1 * num2 will give 50
/It is used to perform division of two numbers num1 / num2 will give 2
%It is used to perform modulo operator where num1 is divided by num2 and remainder will be returned num1 % num2 will give 0
**It is used to perform exponential calculation of two numbers num1 ** num2 will give 10 to the power 5
//It is used to perform floor division of two numbers 10//2 will give 5

Floor Division :

In the Python floor division, when a number is divided by another number then digits after the decimal point of result will be removed.

OperatorExplanation
10 // 3Result of the operation is 3 mathematically in case of integer division. Floor division also provide result = 3
10.0 // 3Result of the operation is 3.33 mathematically but using floor division result would be 3, as it removes digits after decimal point

Example:

#!/usr/bin/python

num1 = 10
num2 = 5
res = 0

res = num1 + num2
print ("Line 1 - Result of + is ", res)

res = num1 - num2
print ("Line 2 - Result of - is ", res)

res = num1 * num2
print ("Line 3 - Result of * is ", res)

res = num1 / num2
print ("Line 4 - Result of / is ", res)

res = num1 // num2
print ("Line 5 - Result of // is ", res)

res = num1 ** num2
print ("Line 6 - Result of ** is ", res)

res = num1 % num2
print ("Line 7 - Result of % is ", res)

Output :

>>> 
Line 1 - Result of + is  15
Line 2 - Result of - is  5
Line 3 - Result of * is  50
Line 4 - Result of / is  2.0
Line 5 - Result of // is  2
Line 6 - Result of ** is  100000
Line 7 - Result of % is  0
>>>