Python Data Types
§ Data types specify the types of values that can be
stored to the variable.
§ Python variables do not need an explicit declaration
to reserve memory space.
§ The declaration happens automatically when you assign
a value to a variable.
§ Python contains the following inbuilt data types
1. int 2. float
3.complex
4.bool
5.str
6.bytes
7.bytearray
8.range
9.list
10.tuple
11.set
12.frozenset
13.dict
14.None
§ Variables are
fundamental to all programming languages.
§ They are data
items that represent a memory storage location in the computer.
§ Variables are
containers that hold data such as numbers and strings.
§ Variables have a name, a type,
and a value.
num = 5; // name is
"num", value is 5, type is numericfriend = "Peter"; // name is "friend", value is "Peter", type is string
ü Python is a dynamic type language, means you don't need to specify type of the
variable because it is dynamically used by python.
ü The type of variable will be decided when we assign the value to the variable.
For example:-
a= 40; //holding number
b=
"Rahul"; //holding string
Numbers data type§ Numbers have four types in Python. Int, float, complex, and long.
int for integral values
float for decimal
values
long for long values
complex for complex values
int_num = 10 #int
value
float_num = 10.2 #float
value
complex_num = 3.14j #complex
value
long_num = 1234567L #long value
§ Integral values can be represented in different form.
1.
Decimal form
2.
Binary Form
3.
Octal form
4.
Hexa decimal
1. Decimal (Base-10)
§ Allowed digits- (0 to 9)
E.g.: -
>>> a=7898
>>> a
7898
§ Allowed digits - (0 and 1)
§ Represent in
Binary Form prefix with 0b or 0B
E.g.: -
>>> a=1111
>>> a
1111
>>> a=0b1111
>>> a
15
>>> a=0B1111
>>> a
15
3. Octal (Base- 8)
§ Allowed digits - 0 to 7
§ Represent in Octal Form prefix with 0o or 0O
a=0o57
b=0O20
print(a)
print(b)
Output
47
16
Hexa Decimal Form (Base-16):
§ The allowed digits are: 0 to 9, a-f (both lower and upper cases are
allowed) Literal value should be prefixed with 0x or 0X
Note: -
Being a programmer, we can specify literal values in decimal, binary, octal and hexa decimal forms. But PVM will always provide values only in decimal form.
a=10
b=0o10
c=0X10
d=0B10
print(a)#10
print(b)#8
print(c)#16
print(d)#2
§
To convert decimal to binary or to octal
or to hexadecimal
§
Python provide the following in-built
functions for base conversions
bin(x)
oct(x)
hex(x)
bin(x):
§
We can use bin() to convert from any
base to binary
print('Deciaml to Binary = ',bin(7)) #output will be '0b111'
print('Octal to Binary = ',bin(0o123)) #output will be '0b1010011'
print('Hexadecimal
to Binary = ',bin(0x10)) #output will be '0b10000'
§
We can use oct() to convert from any
base to octal
print('Deciaml to Octal
= ', oct(20)) #output will be '0o24'
print(Binary to Octal = ', oct(0b11)) #output
will be '0o3'
print('Hexadecimal
to Octal = ', oct(0x6A)) #output will be '0o152'
§ We can use hex() to convert from any base to hexa
decimal.
print('Deciaml to Hexadecimal
= ', oct(20)) #output will be '0o24'
print('Octal to Hexadecimal
= ', oct(0b11)) #output will be '0o3'
print('Binaryl
to Hexadecimal = ',
oct(0x6A)) #output will be '0o152'
provide value should be int type
print('Deciaml to
Binary = ',bin(15))
print('Deciaml to
Octal = ',oct(15))
print('Deciaml to
Hexadecimal = ',hex(15))
print('Octal to
Binary = ',bin(0o777))
print('Hexadecimal
to Binary =',bin(0X123))
output
Deciaml to Binary
= 0b1111
Deciaml to Octal = 0o17
Deciaml to Hexadecimal
= 0xf
Octal to Binary = 0b111111111
Hexadecimal
to Binary = 0b100100011
float data Type:
§ Holds floating precision numbers and it’s accurate up to 15 decimal places.
price:123.45
salary:46778.89
f=123.45
print(f) #output
will be 123.45
print(type(f)) #output will be <class 'float'>
Note:-
§There
is no way to specify floating point value in the formofBinary,octal,hexadecimal form.§ For floating point value exponential form is allowed.
§ Scientific notation is another way to write a number. In scientific notation, the letter E is used to mean "10 to the power of."
f=1.5e3
print(f) #output will be 1500.0
print(type(f)) #output will be <class 'float'>
complex data type:
§ complex- holds complex numbers.
§
Complex
numbers are specified as <real part>+<imaginary part>j
a+bj
a==>
real
b==>
imaginary
10+20j
10.5+23.4j
c=2+4j
print(c) #output will be (2+4j)
print(type(c)) #output will be <class 'complex'>
c=2.6+4.3j
print(c) #output
will be (2.6+4.3j)
c=2.6+4.3i
print(c)
c=2.6+4.3i
^
SyntaxError: invalid
syntax
c=2.6+j4.3
print(c)
c=2.6+j4.3
^
SyntaxError: invalid
syntax
c1=2+4.3j
print(c1) #output will be (2+4.3j)
c2=0b11+2j
print(c2) #output will be (3+2j)
c3=0o11+2j
print(c3) #output will be (9+2j)
c4=2+ob11j
print(c4)
c4=2+ob11j
NameError: name 'ob11j'
is not defined
a=10+20j
b=20+30j
print(a+b) #output
will be (30+50j)
print(a-b) #output
will be (-10-10j)
print(a*b) #output
will be (-400+700j)
a=50+60j
print('Complex
Number = ',a)
print('Real Part = ',a.real)
print('Imag
part= ',a.imag)
Output:
Complex Number = (50+60j)
Real Part = 50.0
Imag part= 60.0
Boolean data type
§
To
represent Boolean value python provides bool data type.
bool:
§ only allowed is- True/False
True
- 1
False-
0
True+True
= 2
True+False
= 1
>>>print(True+True)
2
>>>print(False+False)
0
>>>print(True+False)
1
>>>print(True/True)
1.0
>>>print(True/False)
print(True/False)
ZeroDivisionError:
division by zero
String Data Type
str
§ String are identified as a contiguous set of characters represented in the
quotation marks.
§ Python allows for either pairs of single or double quotes.
§ Strings are immutable sequence data type.
§ immutable means whose content can't be modified. If we are trying to
modified with those modification a new object is created and existing
object will not be modified.
single quotes ==> str
double quotes ==> String
triple quotes ==> multi line string literals
# Python Program for
# Creation of String
# Creating a String
# with single Quotes
String1 = 'Welcome
to the Gravity4Tech'
print("String
with the use of Single Quotes: ")
print(String1)
print(type(String1))
# Creating a String
# with double Quotes
String1 = "I'm
a Gravity4Tech"
print("\nString
with the use of Double Quotes: ")
print(String1)
print(type(String1))
# Creating a String
# with triple Quotes
String1 = '''I'm
a Gravity4Tech and I live in a world of "Gravity"'''
print("\nString
with the use of Triple Quotes: ")
print(String1)
print(type(String1))
# Creating String with
triple
# Quotes allows
multiple lines
String1 = '''Gravity
For
Tech'''
print("\nCreating
a multiline String: ")
print(String1)
output:
String with the use of
Single Quotes:
Welcome to the
Gravity4Tech
<class 'str'>
String with the use of
Double Quotes:
I'm a Gravity4Tech
<class 'str'>
String with the use of
Triple Quotes:
I'm a Gravity4Tech and
I live in a world of "Gravity"
<class 'str'>
Creating a multiline
String:
Gravity
For
Tech
Note:-
|- In python both positive and negative
indexing is possible.
s='python'
print(s) #output will be whole string. python
print(s[-1]) #output will be last character. N
print(s[10])
print(s[10])
IndexError: string index out of range
Slice Operator:
s='python'
s[begin:end]
|- returns substring from begin index to end-1 index
|- begin index is optional.
e.g- s[:4]==>pyth
|- end index is optional.
e.g- s[1:]==> ython
|- begin index and end index both are optional
e.g- s[:]==> python
s='python'
s='python'
print(s) #output will be whole string. python
a_str = 'Hello World'
print(a_str) #output
will be whole string. Hello World
print(a_str[0]) #output
will be first character. H
print(a_str[0:5])
#output will
be first five characters. Hello
s='python'
print(s) #output will be whole
string. python
print(s[100])
print(s[100])
IndexError: string index
out of range
Note:-
|- char data type is not available.
char==> str type only
Testing the type of variables
§ In python, we can check the datatype of an object
using the built-in function type.
c='a'
print(type(c))
output<class 'str'>
a=122
print(type(a))
output
<class 'int'>output
§ long data type in python 2 is available but in python3
long data type is not available
Mutable and Immutable Data Types
§ An object is called mutable if it can be changed. For
example, when you pass a list to some function, the list can be changed:
§ An object is called immutable if it cannot be changed
in any way. For example, integers are immutable, since there's no way to change
them:
§ Data types whose instances are mutable are called
mutable data types, and similarly for immutable objects and datatypes.
Examples of immutable Data Types:
· int
· long
· float
· complex
· str
· bytes
· tuple
· frozenset
Examples of mutable Data Types:
·
bytearray
· list
·
set
·
dict
Note:-
§ These five are fundamental data types
·
int
·
float
·
complex
·
bool
·
str
§ In Python, we can represent char values also by using
str type and explicitly char type is not available.
ch='a'
print(ch)
print(type(ch))
#The Output Will be
a
<class 'str'>
§ long
Data Type is available in Python2 but not in Python3. In Python3 long values
also we can represent by using int type only.
Post a Comment