- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Modulo of tuple elements in Python
If the modulo of tuple elements is required to be determined, the 'zip' method and a generator expression can be used.
Generator is a simple way of creating iterators. It automatically implements a class with '__iter__()' and '__next__()' methods and keeps track of the internal states, as well as raises 'StopIteration' exception when no values are present that could be returned.
The zip method takes iterables, aggregates them into a tuple, and returns it as the result.
Below is a demonstration of the same −
my_tuple_1 = ( 67, 45, 34, 56) my_tuple_2 = (99, 123, 10, 56) print ("The first tuple is : " ) print(my_tuple_1) print ("The second tuple is : " ) print(my_tuple_2) my_result = tuple(elem_1 % elem_2 for elem_1, elem_2 in zip(my_tuple_1, my_tuple_2)) print("The modulus tuple is : ") print(my_result)
The first tuple is : (67, 45, 34, 56) The second tuple is : (99, 123, 10, 56) The modulus tuple is : (67, 45, 4, 0)
- Two tuples are defined, and they are displayed on the console.
- The two tuples are zipped using the 'zip' method, and iterated over, using generator expression.
- The modulus operation is performed on every element from first tuple and corresponding element of second tuple.
- This is converted to a tuple, and is stored in a variable.
- This variable is the output that is displayed on the console.
- Related Articles
- Delete Tuple Elements in Python
- Raise elements of tuple as power to another tuple in Python
- How to append elements in Python tuple?
- Python - Join tuple elements in a list
- Python – Concatenate Rear elements in Tuple List
- Finding unique elements from Tuple in Python
- Python Program to print elements of a tuple
- Count the elements till first tuple in Python
- Python Program to Replace Elements in a Tuple
- What is modulo % operator in Python?
- Count occurrence of all elements of list in a tuple in Python
- Python – Filter tuple with all same elements
- Maximum and Minimum K elements in Tuple using Python
- How to get unique elements in nested tuple in Python
- Python Program to add elements to a Tuple