Monday, 25 May 2020
Tuesday, 28 April 2020
For Loop in Python
Loops are an essential part of any programing language. This will help not only in reducing the rewriting of code but also helps in iterating over the lists, tuple.
Here in this blog, I will be iterating over the nested list in order to find its two diagonals and then performing the arithmetic functions over it.
Note: I have taken the 2-D array having an equal number of rows and columns.
Sample two dimensional list:-
Output:-
0
Here in the above piece of code, at first I ma adding the two different diagonals of the 2-d array and then I am taking the difference of these two.
Explanation:-
1st diagonal : 1 + 5 + 9 = 15
2nd diagonal : 3 + 5 + 7 = 15
difference will be zero(0).
Here in this blog, I will be iterating over the nested list in order to find its two diagonals and then performing the arithmetic functions over it.
Note: I have taken the 2-D array having an equal number of rows and columns.
Sample two dimensional list:-
arr = [[1,2,3], [4,5,6], [7,8,9]] sum2 = 0sum1 = 0for i,j in zip(reversed(range(len(arr))), range(len(arr))): sum1 = sum1 + arr[j][j] sum2 = sum2 + arr[j][i] diff = sum1 - sum2 print(abs(diff))
Output:-
0
Here in the above piece of code, at first I ma adding the two different diagonals of the 2-d array and then I am taking the difference of these two.
Explanation:-
1st diagonal : 1 + 5 + 9 = 15
2nd diagonal : 3 + 5 + 7 = 15
difference will be zero(0).
Tuesday, 21 April 2020
Difference Between Python Lists, Tuples and Sets
Difference Between Python Lists, Tuples and Sets
List | Tuple | Set |
---|---|---|
mutable or changeable | immutable or unchangeable | mutable or changeable |
can have duplicate values | can have duplicate values | don't have duplicate values |
eclosed in square brackets [] | enclose in round brackets () | enclosed in curly brackets |
e.g. [1,2,3] | e.g. (1,2,3) | e.g. {1,2,3} |
Kindly provide your feedback.
Wednesday, 11 March 2020
Zip Function In Python
Zip function in python returns a new tuple object where the first element of the tuple will be the combination of the first element of each iterator passed and so on.
e.g. list1 = [1,2,3]
list2 = [4,5,6]
print(tuple(zip(list1,list2)))
output:
((1,4),(2,5),(3,6))
e.g. list1 = [1,2,3]
list2 = [4,5,6]
print(tuple(zip(list1,list2)))
output:
((1,4),(2,5),(3,6))
Subscribe to:
Posts (Atom)
Sending email using Python
Python has provided smtplib library in order to send email. import smtplib #domain name for the smtp server and port number conn = smtplib.S...
-
Loops are an essential part of any programing language. This will help not only in reducing the rewriting of code but also helps in iterati...
-
It is very common in IT world where we have to compare to compare the two columns of excel and then present a report based on the outco...
-
Zip function in python returns a new tuple object where the first element of the tuple will be the combination of the first element of each...