Python Data Types Unit I
Python Data Types Unit I
Python Data Types are used to define the type of a variable. It defines what type of data we are
going to store in a variable. The data stored in memory can be of many types. For example, a
person's age is stored as a numeric value and his or her address is stored as alphanumeric
characters.
Python has various built-in data types which we will discuss with in this tutorial:
var1 = 1
var2 = 10
var3 = 10.023
Python supports four different numerical types −
long (long integers, they can also be represented in octal and hexadecimal)
print (tuple[1:3]) # Prints elements of the tuple starting from 2nd till 3rd
print (tuple[2:]) # Prints elements of the tuple starting from 3rd element
Examples
Following is a program which uses for loop to print number from 0 to 4 −
for i in range(5):
print(i)
This produce the following result −
0
1
2
3
4
Now let's modify above program to print the number starting from 1 instead of 0:
print(i)
This produce the following result −
1
2
3
4
Again, let's modify the program to print the number starting from 1 but with an increment of 2
instead of 1:
print(i)
This produce the following result −
1
3
Python Dictionary
Python dictionaries are kind of hash table type. They work like associative arrays or hashes
found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but
are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.
Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using
square braces ([]). For example −
dict = {}
a = True
print(a)
print(type(a))
This produce the following result −
true
<class 'bool'>
Following is another program which evaluates the expressions and prints the return values:
# Returns false as a is not equal to b
a=2
b=4
print(bool(a==b))
print(a==b)
a = None
print(bool(a))
a = ()
print(bool(a))
# Returns false as a is 0
a = 0.0
print(bool(a))
# Returns false as a is 10
a = 10
print(bool(a))
This produce the following result −
False
False
False
False
False
True
frozenset in python
Overview
In Python, frozenset() is a function that takes an iterable and returns its frozenset object
counterpart. A frozenset object is an immutable, unordered data type. A frozenset contains
unique values, which makes it very similar to set data type. It is immutable like tuples.
The frozenset() function accepts only one parameter, i.e., an iterable (list, tuple, string,
dictionary, or a set).
The return value of the frozenset() function is a frozenset object (an immutable, set like data
type).
Example
Output
The above example converts a list into a frozenset using the frozenset() function.