Python_DataTypes

Data Types Declaration:
________________________

1.Static DT:
------------------
-->In static type programming languages programmer should define the data type to the variable explicitly.
Ex:
int x;
float f;
string s;
-->In static DT supported languages, one variable can store only one veraity of data.
-->C,C++,Java,.Net...........languages supports static data types.

2.Dynamic DT:
-----------------------
-->In dynamic data type supported languages, programmer should not define the data types to the variables explicitly.
-->At the time of execution of program, data type of variable is decided based on the data which we assign to the variable.

Ex:
x = 10-->int
y = 1.2-->float
z = '10'-->str

-->Python and JavaScript.....languages are supported dynamic data types.


Python data types:
----------------------------
1.Fundamental data types:
int,float,bool,str,
complex

2.Collection data types:
list,tuple,range,dict,
set,frozenset,bytes,bytearray,None


1.int data type:
-----------------------
We can use int data type to represent whole numbers(integral values)
Ex:
x = 10
type(x) #<class 'int'>

We can represent int values in 4-ways:
1.Decimal
2.Binary
3.Octol
4.Hexa decimal

1).Decimal Form:(Base-10)
----------------------------------------
-->It is the default number system in python
-->The allowed digits are 0 to 9
Ex:
x = 1010

2).Binary Form:(Base-2)
------------------------------------
-->The allowed digits are:0 & 1
-->Literal value should be prefixed with 0b or 0B
Ex:
a = 0b1010 #valid
a = 0B1010 #valid
a = 0B123 #Invalid

3).Octal Form(Base-8):
---------------------------------
-->The allowed digits are:0 to 7
-->Literal value should be prefixed with 0o or 0O
Ex:
a = 0o10 #Valid
a = 0O10 #Valid
a = 0o786 #Invalid

4).Hexa Decimal Form:(Base-16)
------------------------------------------------
-->The allowed digits are:0 to 9,a-f/A-F
-->Literal value should be prefixed with 0x or 0X
Ex:
a = 0XFACE #Valid
a = 0xBeef #Valid
a = 0XBeer #Invalid

Note:
------------------------------------------------

Being a programmer we can specify literal values in decimal, binary, octal and hexadecimal forms,
but PVM will always provides in decimal form

Ex:
a = 10
b = 0b10
c = 0o10
d = 0x10


a #10
b #2
c #8
d #16

__________________________________________________________

Base Conversions:
--------------------------
bin(),oct(),hex()

Ex:
bin(10) #'0b1010'
oct(10) #'0o12'
hex(10) #'0xa'
hex(20) #'0x14'

2).float data type:
---------------------------
We can use float data type to represent floating point values(decimal values)
Ex:
f = 1.234
type(f)

We can also represent floating point values by using exponential form(scintific notation)
Ex:
f = 1.2e3
f #1200.0
Instead of 'e' we can use 'E'
Ex:
f = 1.2E3
The main advantage of exponential form is we can represent big values in less memory.

3).bool data type:
--------------------------
-->The only allowed values for this data type are:
True and False
-->Internally python represents True as 1 and False as 0.
Ex:
a = True
type(a)
Ex:
10 < 20 #True
10 > 20 #False
Ex:
True + True #2
True - False #1

4).complex data type:
--------------------------------
A complex is of the form
a + bj

a-->Real part
b-->Imaginary part
j-->sqrt(-1)
a & b contains integers or floating point values.

Ex:
a = 3+4j
a = 1.5+2.5j
a = 3+0b1010j #Invalid
a = 0b1010 + 3j
Ex:
c = 10.5 + 3.6j
c.real #10.5
c.imag #3.6

1).int data type:
x = 10

-->Decmal Form:Base-10; 0 to 9
-->Binary Form:Base-2; 0 & 1; 0b/0B
-->Octol Form:Base-8;0 to 7; 0o/0O
-->Hexa Decimal Form:Base-16;0 to 9;a-f/A-F; 0x/0X

2).float data type:
f = 1.234
f = 1.2e3
f = 1.2E3

3).bool data type:
True---->1
False---->0

4).complex data type:
a + bj

5).str data type:
------------------------
-->str represents string data type.
-->A string is a sequence of characters within single or double or triple quotes.
Ex:
s = 'sunny'
s = "sunny"
s = '''sunny'''
s = """sunny"""

Slicing of string:
-------------------------
-->Slice means a piece.
-->[:] is called as slice operator, which can be used to retrieve parts of string.
-->In python string follows zero based index.
-->The index can be either +ve or -ve.
-->+ve index means forward direction from left to right.
-->-ve index means backward direction from right to left.

Ex:
-----
s = 'python'
s[0] #'p'
s[-1] #'n'
s[10] #IndexError
s[0:3]#'pyt'
s[:3]#'pyt'
s[3:]#'hon'
s[:]#'python'
s[::]#'python'
s[2:100]#'thon'
s[::-1]#'nohtyp'
s[-4:-1]#'tho'

Type Casting:
---------------------
-->We can convert one type to other type. This conversion is called as type casting or type coersion.

1).int():
To convert values from other type to int.
Ex:
-----
int(12.99) #12
int(True) #1
int(False)#0
int('10')#10
int(10+3j)#Invalid
int('10.5')#Invalid
int('0B1010')#Invalid
int(0b1010)#10

2).float():
-------------
float(10)#10.0
float(True)#1.0
float(False)#0.0
float('10')#10.0
float('10.5')#10.5
float(10+3j)#Invalid

3).bool():
-------------
-->zero means False
-->non-zero means True
-->Empty string always False

bool(0)#False
bool(1)#True
bool(10)#True
bool(0.1)#True
bool(0.0)#False
bool(10+3j)#True
bool(0+0j)#False
bool(' ')#True
bool('')#False
bool(None)#False

4).complex():
------------------
complex(10)#(10+0j)
complex(1.5)#(1.5+0j)
complex(True)#(1+0j)
complex(False)#0j
complex(10,3)#(10+3j)
complex(True,False)#(1+0j)

5).str():
-----------
str(10)#'10'
str(10.5)#'10.5'
str(True)#'True'
str(10+3j)#'(10+3j)'


1.bytes data type:
---------------------------
bytes data type represents a group of byte numbers just like an array.
Ex:
x = [10,20,30,40]
b = bytes(x)
type(b)#<class 'bytes'>
b[0]#10
b[-1]#40

Conclusion-1:
--------------------
The only allowed values for bytes data type are 0 to 256.
Ex:
x = [10,20,30,300]
b = bytes(x)#ValueError: bytes must be in range(0, 256)

Conclusion-2:
--------------------
Once we create bytes data type value, we cant change its values. It is immutable.

x = [10,20,30,40]
b = bytes(x)
b[0] = 100 #TypeError: 'bytes' object does not support item assignment

2.bytearray data type:
---------------------------------
bytearray is exactly same as bytes data type except that it is mutable.

Ex:
x = [10,20,30,40]
b = bytearray(x)
type(b)#<class 'bytearray'>
b[0]#10
b[0] = 100
for i in b:print(i)
100
20
30
40

3).list data type:
-------------------------
-->Values should be enclosed with square brackets [ ].
-->Insertion order is preserved.
-->Hetrogenious objects are allowed.
-->Duplicates are allowed.
-->Growable in nature.
-->Indexing and slicing are applicable.
-->It is mutable. [ Ability to change ]

4).tuple data type:
----------------------------
-->Values should be enclosed with parenthesis ( ) is an optional.
-->Insertion order is preserved.
-->Hetrogenious objects are allowed.
-->Duplicates are allowed.
-->Growable in nature not applicable.
-->Indexing and slicing are applicable.
-->It is immutable.

5).range data type:
----------------------------
-->Range data type represents a sequence of numbers.
-->It is immutable.
Ex:
r = range(10):generates 0 to 9
r = range(10,20):generates 10 to 19
r = range(10,50,5):generates values:10,15,20,25,30,35,40,45

6).set data type:
------------------------
-->Values should be enclosed with { }.
-->Insertion order is not preserved.
-->Hetrogenious objects are allowed.
-->Duplicates are not allowed.
-->Growable in nature.
-->Indexing and slicing are not applicable.
-->It is mutable.

7).frozenset data type:
---------------------------------
-->It is exactly same as set except that it is immutable.
-->We cant use add or remove functions.

8).dict data type:
-------------------------
If we want to represent a group of key values pairs then we should go for dict data type.

9).None data type:
----------------------------
-->None means nothing or no value associated.
-->It is something like null value in java

__________________________________________________________________

Input 12 in Key board

cpu reads the input 12 and keep / save /store in the memory

inform cpu to execute 12 * 12

cpu invokes the processor to perform the task given by Tvisha

processor uses python language

processor retrieves the 12 from memory

processor uses python language to calculate 12 * 12

processor store/ save the 144 in memory

processor informs cpu that I have completed my task

cpu takes the result 144

cpu shows the results 144 on monitor


__________________________________________________________________________________________


Bit
Byte
Nibble
Byte
KB
Mb
GB
TB


__________________________________________________________________________________________

Data Types

create a variable to store the name "Damodar" and tell the type of data
create a variable to store the age "25" and tell the type of data
create a variable to store the height "5.4" and tell the type of data
create a variable to store the gender male is "true" and tell the type of data
create a variables to store the marks for maths, english, science
Find the total and average

__________________________________________________________________________________________

Comments

Popular posts from this blog

Python_While_Loop

Python_Lists_Loops

clinical_app