Modules and Packages in Python

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)

Modules and Packages in Python

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 .

employee(Package)
| __init__.py (Constructor)
| contact.py (Module)
| department.py (Module)
| salary.py (Module)

What are the key difference between lists and tuples in python

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.LISTTUPLE
1Lists are defined using square brackets [ ] Tuples are defined in round brackets ( )
2Lists are mutableTuples are immutable
3Lists 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
4lists having variable lengthTuples has fixed length
5The Lists are less memory efficient than the tuplesThe tuples are more memory efficient than the list

What are the important built-in data types in Python ?

These are the important built-in data types in Python

1) None Type:
None Type is for the null value in python

2) Numeric Types:

Numeric Types are handled by int , float, complex and bool

  • int : Init type store integer values,it include binary . octa ,hex numbers
  • float: float type store float numbers
  • complex : complex type store complex numbers
  • bool: bool store Boolean numbers

3) Sequence Types:Their are three Sequence Types lists, tuples, and range

  • Lists: Sequence Types used to store collection of item, Lists is Mutable
  • tuples: Sequence Types used to store collection of item, Lists is Immutable
  • range : Sequence Types used to generated sequence of numbers during execution

4) Mapping Types: Stores comma-separated list of key: value pairs

5) Set Types: set types are – set and frozenset

6) Modules: built-in type supported by the Python Interpreter

7) Callable Types: Callable types are the types to which function call can be applied