Python Data Types - TechDB

Latest

All about Database Programming, Performance Tuning and Best Practices.

BANNER 728X90

Monday, 17 February 2020

Python Data Types

CSS Template Everything is an object in Python programming. Data types are actually classes, and variables are instance (object) of these classes.

Python has six standard Data Types:
  • Numbers
  • String
  • List
  • Tuple
  • Set
  • Dictionary

Python Data Type – Numeric

Python numeric data type is used to hold numeric values like
  • int – Integers can be of any length, it is only limited by the memory available.
  • float- holds floating precision numbers and it’s accurate upto 15 decimal places.

Input

var1 = 5
var2 = 5.0
print(var1, "is of type",type(var1))
print(var2, "is of type",type(var2))

Output

5 is of type &lt class 'int' &gt
5.0 is of type &lt class 'float' &gt

Input

varfloat = 0.1234567890123456789
print(varfloat, "is of type", type(varfloat))
print(varfloat)

Output

0.12345678901234568 is of type &lt class 'float'&gt
0.12345678901234568

  • complex- holds complex numbers. – An complex number is represented by “x + yi”. Python convert the real number x and y into complex using the funxtion complex(x+y). The real part can be accessed using the function real() and imaginary part can be represented by imag().

Input

var3 = 1+2j
print(var3, "is complex number?", isinstance(1+2j,complex))
print(var3, "is of type",type(var3))

Output

(1+2j) is complex number? True
(1+2j) is of type &lt class 'complex'&gt

These are defined as int, float and complex class in python.

In Python we need not to declare datatype while declaring a variable like C or C++. We can simply just assign values in a variable. But if we want to see what type of numerical value is it holding right now, we can use type(), like this:

Python Data Type – String

The string is a sequence of characters. Python supports Unicode characters. Generally, strings are represented by either single or double quotes. And triple quotes for multi line

Input

String1 = 'This is a String in single quote'
String2 = "This is a String in double quote"
String3 = '''This is a
string in triple quote'''

print('Single quote---', String1)
print('Double quote---', String2)
print('Triple quote/Multi line---', String3)

Output

Single quote--- This is a String in single quote
Double quote--- This is a String in double quote
Triple quote/Multi line--- This is a string in triple quote

Python Data Type – List

List is an ordered sequence of items. It is one of the most used datatypes in Python and is very flexible. All the items in a list do not need to be of the same type.

Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ].

Input

varlist = [1, 5.3, 'Python']

print(varlist, "is of type", type(varlist))
print(varlist)

Output

[1, 5.3, 'Python'] is of type &lt class 'list' &gt
[1, 5.3, 'Python']

We can use the slicing operator [ ] to extract an item or a range of items from a list. Index starts form 0 in Python.

Input

varlist = [5,10,15,20,25,30,35,40]
# varlist[2] = 15
print("varlist[2] = ", varlist[2])
# varlist[0:3] = [5, 10, 15]
print("varlist[0:3] = ", varlist[0:3])
# varlist[5:] = [30, 35, 40]
print("varlist[5:] = ", varlist[5:])

Output

varlist[2] = 15
varlist[0:3] = [5, 10, 15]
varlist[5:] = [30, 35, 40]

List is mutable. That means data in a List is updateable.

Input

varlist[1] = 100
varlist[5] = 300

print(varlist)

Output

[5, 100, 15, 20, 25, 300, 35, 40]

Python Data Type – Tuple

Tuple is another data type which is a sequence of data similar to list. But it is immutable. That means data in a tuple is write protected. Data in a tuple is written using parenthesis () and commas (,). It is also an ordered sequence of item same as list.

Input

varTuple = (1, 5.3, 'Python')
print(varTuple, "is of type", type(varTuple))
print(varTuple)

Output

(1, 5.3, 'Python') is of type &lt class 'tuple' &gt
(1, 5.3, 'Python')

We can use the slicing operator [] to extract items but we cannot change its value.

Input

varTuple = (5,10,15,20,25,30,35,40)

# varlist[2] = 15
print("varTuple[2] = ", varTuple[2])

# varlist[0:3] = [5, 10, 15]
print("varTuple[0:3] = ", varTuple[0:3])

# varlist[5:] = [30, 35, 40]
print("varTuple[5:] = ", varTuple[5:])

Output

varTuple[2] = 15
varTuple[0:3] = (5, 10, 15)
varTuple[5:] = (30, 35, 40)

Tuple it is immutable. That means data in a tuple is write protected

Input

varTuple[1] = 100
varTuple[5] = 300

print(varTuple)

Output

TypeError Traceback (most recent call last)
in
----> 1 varTuple[1] = 100
2 varTuple[5] = 300
3 print(varTuple)
TypeError: 'tuple' object does not support item assignment

Python Data Type – Set

Set is an unordered collection of unique items.
Set is defined by values separated by comma inside braces { }.
Items in a set are not ordered.
Set objects does not support indexid.

Input

varSet = {5,2,3,1,4}

print(varSet, "is of type", type(varSet))
print(varSet)

Output

{1, 2, 3, 4, 5} is of type &lt class 'set' &gt
{1, 2, 3, 4, 5}

We can perform set operation like union, intersection on two sets. Set have unique values. They eliminate duplicates.

Input

varSet = {5,2,3,1,4,1,2,3,4}
print(varSet)

Output

{1, 2, 3, 4, 5}

Since, set are unordered collection, indexing has no meaning. Hence the slicing operator [] does not work.

Input

varSet[1]

Output

TypeError Traceback (most recent call last)
in
----> 1 varSet[1]

TypeError: 'set' object is not subscriptable

Python Data Type – Dictionary

Python Dictionary is also an unordered sequence of data of key-value pair form.
It is similar to the hash table type.
Dictionaries are written within curly braces { } in the form key:value. Key and Value can be any type.
It is very useful to retrieve data in an optimized way among a large amount of data.

Input

vardict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

print(vardict, "is of type", type(vardict))
print(vardict)

Output

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964} is of type
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

You can access the items of a dictionary by referring to its key name, inside square brackets: And also using Get() function.

Input

print(vardict["model"])
print(vardict.get("model"))

Output

Mustang
Mustang

No comments:

Post a Comment