Python Data Types
List
Output:
Initial blank List:
[]
List with the use of String:
['hello world']
List containing multiple values:
hello
world
Multi-Dimensional List:
[['hello', 'world'], ['hello']]
List with the use of Numbers:
[1, 2, 4, 4, 3, 3, 3, 6, 5]
List with the use of Mixed Values:
[1, 2, 'hello', 4, 'world', 6, 9]
Tuple:
Output:
Initial empty Tuple:
()
Tuple with the use of String:
('hello', 'world')
Tuple using List:
(1, 2, 4, 5, 6)
Tuple with a loop
('hello',)
(('hello',),)
Tuple with the use of function:
('h', 'e', 'l', 'l', 'o')
Tuple with Mixed Datatypes:
(5, 'Welcome', 7, 'to python')
Tuple with nested tuples:
((0, 1, 2, 3), ('python', 'world'))
Tuple with repetition:
('hello', 'hello', 'hello')
Set
Output:
Initial blank Set:
set()
Set with the use of String:
{'w', ' ', 'l', 'h', 'r', 'e', 'd', 'o'}
Set with the use of an Object:
{'w', ' ', 'l', 'h', 'r', 'e', 'd', 'o'}
Set with the use of List:
{'world', 'hello'}
Set with the use of Numbers:
{1, 2, 3, 4, 5, 6, 7}
Set with the use of Mixed Values
{1, 2, 4, 6, 7, 'hello', 'world'}
Dict
Output:
Empty Dictionary:
{}
Dictionary with the use of Integer Keys:
{1: 'hello', 2: 'world'}
Dictionary with the use of Mixed Keys:
{'Name': 'python', 1: [1, 2, 3, 4]}
Dictionary with the use of dict():
{1: 'welcome', 2: 'to', 3: 'python world'}
Dictionary with each item as a pair:
{1: 'hello', 2: 'world'}
List
In Python, list is a type of container in Data Structures,
which is used to store multiple data at the same time.
Lists in Python can be created by just placing the sequence inside the square brackets[].
List is mutable
# Creating a List
List = []
print("Initial blank List: ")
print(List)
# Creating a List with
# the use of a String
List = ['hello world']
print("\nList with the use of String: ")
print(List)
# Creating a List with
# the use of multiple values
List = ["hello", "world",]
print("\nList containing multiple values: ")
print(List[0])
print(List[1])
#Creating a Multi-Dimensional List
#(By Nesting a list inside a List)
List = [['hello', 'world'] , ['hello']]
print("\nMulti-Dimensional List: ")
print(List)
# Creating a List with
# the use of Numbers
# (Having duplicate values) s
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print("\nList with the use of Numbers: ")
print(List)
# Creating a List with
# mixed type of values
# (Having numbers and strings)
List = [1, 2, 'hello', 4, 'world', 6, 9]
print("\nList with the use of Mixed Values: ")
print(List)
Output:
Initial blank List:
[]
List with the use of String:
['hello world']
List containing multiple values:
hello
world
Multi-Dimensional List:
[['hello', 'world'], ['hello']]
List with the use of Numbers:
[1, 2, 4, 4, 3, 3, 3, 6, 5]
List with the use of Mixed Values:
[1, 2, 'hello', 4, 'world', 6, 9]
Tuple:
Tuple is a collection of Python objects much like a list.
The sequence of values stored in a tuple can be of any type, and they are indexed by integers.
The important difference between a list and a tuple is that tuples are immutable.
Tuples are hash able whereas lists are not.
# Creating an empty tuple
Tuple1 = ()
print("Initial empty Tuple: ")
print (Tuple1)
# Creating a Tuple with
# the use of Strings
Tuple1 = ('hello', 'world')
print("\nTuple with the use of String: ")
print(Tuple1)
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))
# Creating a Tuple
# with the use of loop
Tuple1 = ('hello')
n = 2
print("\nTuple with a loop")
for i in range(int(n)):
Tuple1 = (Tuple1,)
print(Tuple1)
# Creating a Tuple with the
# use of built-in function
Tuple1 = tuple('hello')
print("\nTuple with the use of function: ")
print(Tuple1)
# Creating a Tuple with
# Mixed Datatypes
Tuple1 = (5, 'Welcome', 7, 'to python')
print("\nTuple with Mixed Datatypes: ")
print(Tuple1)
# Creating a Tuple
# with nested tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('python', 'world')
Tuple3 = (Tuple1, Tuple2)
print("\nTuple with nested tuples: ")
print(Tuple3)
# Creating a Tuple
# with repetition
Tuple1 = ('hello',) * 3
print("\nTuple with repetition: ")
print(Tuple1)
Output:
Initial empty Tuple:
()
Tuple with the use of String:
('hello', 'world')
Tuple using List:
(1, 2, 4, 5, 6)
Tuple with a loop
('hello',)
(('hello',),)
Tuple with the use of function:
('h', 'e', 'l', 'l', 'o')
Tuple with Mixed Datatypes:
(5, 'Welcome', 7, 'to python')
Tuple with nested tuples:
((0, 1, 2, 3), ('python', 'world'))
Tuple with repetition:
('hello', 'hello', 'hello')
Set
#In Python, Set is an unordered collection of data type that is iterable, mutable
and has no duplicate elements.
# Creating a Set
set1 = set()
print("Initial blank Set: ")
print(set1)
# Creating a Set with
# the use of a String
set1 = set("hello world")
print("\nSet with the use of String: ")
print(set1)
# Creating a Set with
# the use of Constructor
# (Using object to Store String)
String = 'hello world'
set1 = set(String)
print("\nSet with the use of an Object: " )
print(set1)
# Creating a Set with
# the use of a List
set1 = set(["hello", "world"])
print("\nSet with the use of List: ")
print(set1)
# Creating a Set with
# a List of Numbers
# (Having duplicate values)
set1 = set([1, 2, 3,4,5,6,7])
print("\nSet with the use of Numbers: ")
print(set1)
# Creating a Set with
# a mixed type of values
# (Having numbers and strings)
set1 = set([1, 2, 'hello', 4, 'world', 6, 7])
print("\nSet with the use of Mixed Values")
print(set1)
Output:
Initial blank Set:
set()
Set with the use of String:
{'w', ' ', 'l', 'h', 'r', 'e', 'd', 'o'}
Set with the use of an Object:
{'w', ' ', 'l', 'h', 'r', 'e', 'd', 'o'}
Set with the use of List:
{'world', 'hello'}
Set with the use of Numbers:
{1, 2, 3, 4, 5, 6, 7}
Set with the use of Mixed Values
{1, 2, 4, 6, 7, 'hello', 'world'}
Dict
Python dictionary is an unordered collection of items.
While other compound data types have only value as an element.
# Creating an empty Dictionary
Dict = {}
print("Empty Dictionary: ")
print(Dict)
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'hello', 2: 'world'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
# Creating a Dictionary
# with Mixed keys
Dict = {'Name': 'python', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
# Creating a Dictionary
# with dict() method
Dict = dict({1: 'welcome', 2: 'to', 3:'python world'})
print("\nDictionary with the use of dict(): ")
print(Dict)
# Creating a Dictionary
# with each item as a Pair
Dict = dict([(1, 'hello'), (2, 'world')])
print("\nDictionary with each item as a pair: ")
print(Dict)
Output:
Empty Dictionary:
{}
Dictionary with the use of Integer Keys:
{1: 'hello', 2: 'world'}
Dictionary with the use of Mixed Keys:
{'Name': 'python', 1: [1, 2, 3, 4]}
Dictionary with the use of dict():
{1: 'welcome', 2: 'to', 3: 'python world'}
Dictionary with each item as a pair:
{1: 'hello', 2: 'world'}
Comments
Post a Comment