Python File handling

Python Data Types

Data Type means type of data available in variable.In a Python no need to specify data types of variable explicitly ,based on the value of variable data type assign automatically.

In-Buit Python Data Types

Inbuilt data types mean a data type that is predefined by python language.Python predefined data type categories based on values that store.

Every Python data types treated as objects.

Python Data Types
Fig 1: Python Data Types

Numeric Types

In this type of data type store a numeric value.

int type

This type is used to store whole number.The Int type value represented in decimal form, octal form, hexadecimal form, binary form.

  • Decimal Form

In decimal form store data in standard decimal format.Allowed number is 0 to 9. This is the base-10 number.

x=10;
type(x);
<class 'int'>
  • Hexadecimal Form

In Hexadecimal from value store in hexadecimal format.In In this allowed value is 0-9, a-f or A-F.This value must start with 0x or 0X.This is base 16 number.

x=0X5A
type(x)
print(x)
<class 'int'>
90

In this example store value in hexadecimal format and print value of x. At that time printing value python interpreter print that value in decimal form.

  • Octal Form

In Octal from value store in hexadecimal format.In In this allowed value is 0-7.This value must start with 0o or 0O.This is base 8 number.

x=0O132
type(x)
print(x)
<class 'int'>
90

In this example store value in octal format and print value of x. At that time printing value python interpreter print that value in decimal form.

  • Binary Form

In Binary from value store in Binary format.In In this allowed value is 0-1.This value must start with 0b or 0B.This is base 2 number.

x=ob1011
type(x)
print(x)
<class 'int'>
11

In this example store value in binary format and print value of x. At that time printing value python interpreter print that value in decimal form.

Base Conversion for int type

Python Have a some inbuilt function that help to convert integer number base.

  • hex( ) : This function is used to convert any form of integer number into Hexadecimal form
hex(90)
hex(0o39)
'0x5a'
'0x1f'
  • oct( ) : This function is used to convert any form of integer number into Octal form
oct(90)
oct(0x1f)
'0o132'
'0o37'
  • bin( ) : This function is used to convert any form of integer number into Binary form
bin(90)
bin(0x1f)
'0b1011010'
'0b11111'

float type

This type is used to store floating point numbers.The floating phone number is stored in either decimal format or exponential format.

x=12.03
type(x)
<class 'float'>
x=198e2
type(x)
print(x)
<class 'float'>
19800.0

Complex Type

This type used to store real and imaginary part in the form of [ <real> + <imaginary> j ].This type has some attribute that helps to retrieve the real and imaginary part.

  • <variable-name>.real
  • <variable-name>.imag
x=12 + 5j
type(x)
x.real
x.imag
print(x)
<class 'complex'>
12.0
5.0
(12+5j)

Boolean Type

This type of data type is used to store boolean values.

bool Type

This type allows only two values to store into variable.True and False is two boolean values in Python.

x=True
type(x)
y=False
type(x)
<class 'bool'>
<class 'bool'>

Text Type

In this type of data type store text values.

str Type

In this type store string value. String means a collection of characters. In Python char data type is not available that treated as str. String literals written in single quote or double quotes.For multiline string, use triple single quotes or double quotes.

s1="Mcaprogramming";
type(s1)
ch1='a';
type(a)
<class 'str'>
<class 'str'>
s1="Mcaprogramming"
print(s1)
s2='Mcaprogramming'
print(s2)
Mcaprogramming
Mcaprogramming

Multiline String

s1='''Mcaprogramming
Way to learn'''
print(s1)
'Mcaprogramming\nWay to learn'

More about string learn in upcoming tutorials.

Binary Types

In this type of data type store data in byte format.

bytes type

This type store collection of byte numbers like array.This type allows to store numbers between 0 to 255.If try to store value more than 255 then that generate an error.Once a value is assigned, then not allow change.

x=bytes([1,2,3])
type(x)
print(x)
<class 'bytes'>
b'\x01\x02\x03'

This allows to access individual values like array.

x[0]
1

bytearray Type

This type is same as but has an only one difference is this allow to change values of bytearray.

x=bytesarray([1,2,3])
type(x)
<class 'bytearray'>

Sequence Type

In this type of data type data store in sequence like array.

list Type

This type store data in sequence like array of other language.This store any kind of data item as a list item.This is dynamic size.In this all item written in square brackets [] with comma separated.More about list learn in an upcoming tutorial.

Syntax

<variable-name>=[<value1>,<value2>,.....]
list1=[1,2,"Mcaprogramming"]
typr(list1)
print(list1)
<class 'list'>
[1,2,"Mcaprogramming"]

tuple Type

This type is similar to list, but in this not allow to change tuple item once tuple is created.In this all item written in parenthesis () with comma separated.More about tuple learn in an upcoming tutorial.

Syntax

<variable-name>=(<value1>,<value2>,....)
tuple1=(1,2,"Mcaprogramming")
type(tuple1)
print(tuple1)
<class 'tuple'>
(1,2,"Mcaprogramming")

range Type

This type store sequence of numbers.This does not allow to change data stored.More about range learn in an upcoming tutorial.

x=range(10)
type(x)
print(list(x))
<class 'range'>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Set Types

This type of data types store data in sequence and unique values.

set Type

This type store unique data where order is not important.This is similar to a list, but this doesn’t allow duplicate values.In this all items written in curly brackets {} with comma separated values.

Syntax

<variable-name>={<value1>,<value1>,...}
set1={1,2,"Mcaprogramming"}
type(set1)
print(set1)
<class 'set'>
{1, 2, 'Mcaprogramming'}

frozenset Type

This is similar to set but in this not allow change variable value once it created.

set1={1,2,"Mcaprogramming"}
fset1=frozenset(set1)
type(fset1)
print(fset1)
<class 'frozenset'>
frozenset({1, 2, 'Mcaprogramming'})

Mapping Type

In this type of data type store values in key value pairs.

dict Type

In store this type data in key value order pair.This is similar like associative array of PHP.In this, all data items written in curly brackets {}, comma separated.In this key value is unique, but in value allow duplication.More about direct learn in upcoming tutorials.

Syntax

<variabele-name>={<key>:<value>,<key>:<value>,...}
dict1={1:"C Programming",2:"Python"}
type(dict1)
print(dict1)
<class 'dict'>
{1: 'C Programming', 2: 'Python'}

Other Type

NoneType

This represent a no value.This represent null value in Python .This is the return type of function that not return any value.

var1=None
type(var1)
<class 'NoneType'>

Reference Python.org

1 thought on “Python Data Types”

  1. Pingback: Python Type Casting | MCAProgramming

Leave a Comment

Your email address will not be published. Required fields are marked *