Modules and Packages are the most important and widely used concept in python.Modules and Packages support reusability and easy coding for programmers.
What is a Modules in Python
A Modules is simply a single python file with extension .py. Modules contains global variables and group of functions .Modules is a single executable .py files.we can use functions and global variables in module in any python page by simply importing that module in that page.Here is the example usage of a Modules
Module calculator.py
def sum(a,b):
sum = a + b
print("The sum is: ", sum)
Importing and calling that Module
import calculator
calculator.sum(3,4)
What is a Packages in Python
Packages is a collections of modules, it simple a directory that contains lots of modules and a __init__.py file.The package may contains sub package .
List and tuples both are used to store multiple items in single variable, list is created using square brackets but tuples are create using parentheses.Their are difference and similarity between lists and tuples in python.Let we check difference between lists and tuples in python.
Difference between lists and tuples
1) List are mutable but tuples are immutable, mutable means that can be modified ,automatically immutable means that can not be modified,So list can be modified, appended , we cant do any of this with tuples. suppose if my_list is already exists ,any of the element from the given list can be reassigned.but we cant reassign tuple elements.
2) Since list is mutable list having additional functionalities like insert , sort and pop are available.But tuples doesn’t support any of this functionalities .
3) Tuples are allocated with large block memory, but list having small memory blocks.so when their are large number of elements tuples is faster than List .
4) Tuples has fixed length while list having variable length.So the length of tuples always initial length that is constant.But List length may differ.
Syntax and example of lists in Python
my_list = [2, 4, 6.8,10]
my_list2 = ['even', 4, 6.8,10]
my_list2 [0] = 'old'; it change list to
my_list2 = ['old', 4, 6.8,10]
Syntax and example of tuples in Python
my_tuple = (2, 4, 6.8,10)
my_tuple2 = ('even', 4, 6.8,10)
my_tuple2 [0] = 'old'; it throws an error
Sno.
LIST
TUPLE
1
Lists are defined using square brackets [ ]
Tuples are defined in round brackets ( )
2
Lists are mutable
Tuples are immutable
3
Lists are many built-in methods: Since list is mutable list having additional functionalities like insert , sort and pop are available.
Tuples are less in-built methods: tuples doesn’t support any of this functionalities
4
lists having variable length
Tuples has fixed length
5
The Lists are less memory efficient than the tuples
The tuples are more memory efficient than the list