DataType:
Dynamic DataType
type() - inbuilt function in Python to know type of data
int
float
complex
bool
str
set
frozenset
bytes
bytearray
list
tuple
dict
range
None
int:::::::-
decimal form
binary form
octal form
hexadecimal form
---------------------
float::::::::-
decimal values represented by floating point
23.34
4.4e3
we cant use any int forms in float except decimal form
bool::::-
True or False
1 or 0
Complex:::::::::-
a+bj
a.real
b.imag are attributes
we cant use any int forms in imag part expect decimal
we can use int and float in both real and imag parts
we can perform operations on complex
this complex type used in scientific appls and electrical engineer apps
str:
sequence of characters closed in single quotes or double quotes
we use triple single quotes or triple double quotes to represent multiple
lines in string
Slicing:
________
( ) this operator is called slicing opereator ,,,,is used to separate the
string or to create substring
index values starts form 0
it will have both - and + index values
(begin:end-1)
(begin:end-1:step)
begin value should be lesser than end value
if ther is no begin value than it is count as staring index
if there is no end value than it is counted as ending index
if there is no step value than it is counted as 1
eg:
>>> s="Naveen"
>>> s[1:5]
'avee'
>>> s[-6:-1]
'Navee'
>>> s[-1:-5]
''
>>> s[:5]
'Navee'
>>> s[0:]
'Naveen'
>>> s[1:6]
'aveen'
>>> s[1:100]
'aveen'
>>> s[100]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> s[1:5:2]
'ae'
s*3
>>> s*10
'NaveenNaveenNaveenNaveenNaveenNaveenNaveenNaveenNaveenNaveen'
>>> len(s)
6
>>>
Type Casting:
Int: int()
we can convert float value to int
we can convert bool value to int
we cant convert complex value to int
we can convert str value to int only if str contains int(decimal form)
values
>>> int(99.99)
99
>>> int(0.123)
0
>>> int(1.9)
1
>>> int(4+8j)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a real
number, not 'complex'
>>> int(True)
1
>>> int(False)
0
>>> int('naveen')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'naveen'
>>> int('123')
123
>>> int('12.9')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '12.9'
>>>
Float: float()
we can convert int value to float
we can convert bool value to int
we cant convert complex value to int
we can convert str value to int only if str contains int(decimal form)
and floating decimal values
>>> float(45)
45.0
>>> float(34+56j)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: float() argument must be a string or a real number, not
'complex'
>>> float(True)
1.0
>>> float('naveen')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'naveen'
>>> float('123')
123.0
>>> float('34.87')
34.87
>>> float('0b11')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '0b11'
>>>
Complex:
complex() function is used to convert any data type to complex type
In form 1 we can convert str value to complex only if str contains
int(decimal form) and floating decimal values
In form 2 we cant convert str value to complex because complex() can't
take second arg if first is a string
there are two forms :
form 1 : complex(x) --> here x means real part and image part will be
zero
eg;
>>> complex(10)
(10+0j)
>>> complex(15.9)
(15.9+0j)
>>> complex(True)
(1+0j)
>>> complex(False)
0j
>>> Complex('10')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Complex' is not defined. Did you mean: 'complex'?
>>> complex('10')
(10+0j)
>>> complex('10.9')
(10.9+0j)
>>> complex('0b10')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: complex() arg is a malformed string
>>>
form 2:
complex(x,y) here x is real part and y is image part
>>> complex(10,20)
(10+20j)
>>> complex(10.9,3.4)
(10.9+3.4j)
>>> complex(10,3.4)
(10+3.4j)
>>> complex(true,true)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined. Did you mean: 'True'?
>>> complex(True,True)
(1+1j)
>>> complex('10')
(10+0j)
>>> complex('10','20' )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: complex() can't take second arg if first is a string
>>>
Str:
we can convert any type to str type
bool:
we can covert int type to bool i.e if int value is zero then bool value
will be false otherwise true
we can covert float type to bool i.e if float value is zero then bool
value will be false otherwise true
we can covert complex type to bool i.e if complex value is zero i.e image
and real part is zero then bool value will be false otherwise true
we con convert str type to bool ii.e if str value is non-empty then bool
value will be true otherwise false ( space is treated as character only)
Fundamental data types v/s Immutablility:
----------------------------------------------------------
-> in python everything is object
-> all fundamental data types i.e int float complex bool str are
immutability
-> Immutabilliy means not changeable
->If any object is created then if we to change the content of object
then present object will be go to garbage collection and new object will
be created
-> Immutability approch will be like - if several references are
connecting to same object then if we change one reference content then it
will refered to its own object it will never change content of exiting
object becaues exiting object already refered by several references
for complex and float types these immutability approch does'nt work
id() to get address of that variable
is operator retures true or false
=========================================================================
======================================
int
float
complex
bool
str
bytes
bytearray
list
tuple
------------------------------------------
int()
-decimal base 10
- octal base 8
- binary base 2
- hexadecimal base 16
float() - floating values
comples a+bj
bool------------------
bytes-
-----
a order of values same as like an array
bytes is immutable - can not change
s = [10,20,30]
b = bytes(s)
range is 0 to 256
bytearray()
mutable
list:
[]
a order of values that as insertion preserved and duplicates allowed and
growable and heterogenous
list is mutable
tuple:
tuple is immutable
=========================================================================
=====================
what is python ?
who invented python ? dob
what programming features are included in python?
where is python used?
what companies are using python?
----------------------------------------------
Features of Python?
Identifiers?
Keywords?
Datatypes?
int float complex bool str range bytes bytearray list tuple set frozenset
dict None
Escape Characters
\n\t\r\b\f\v\'\"\\
Constant:
=====================================
OPerator:
which performs some operation:
arithmetci operator
relational operator or comparision
logical operator
bitwise operator
assignment operator
special operator
+ - addition
- - substractin
* - multiplication
/ - division
% - modulo division
// - floor division
** - power operator
/ - float will be output
// - int will be output if arguments are int , float will be output if
both arguments are float ..if any argument is float then it will be float
+ and * is used for string data type
+ string concatination - both arguments are to be strings
* is string multiply operator- one arguments should be string and another
should be int
relational operator:
< <= > >=
a is always > A
chaining of relational operato
a<d<d>d
if any condition is false then it is false
if all conditions are true then it is true
=========================================================================
======
Operator:
which operates some funtion
Aithmetic
relation or comparison
logical
bitwise
assignment
special
airthemtic operator:
addtion
substractin
multiplication
division
modulo divisiomn
floor divisin
power or exponentionl operator
+ - * / % // **
/ and // - - / return floot vlaue
// - return int and float value
+ and * string data type also
relational opertor
> >= < <=
easy to learn
high level lang
platform indepen
portable
dyanamic
extendable
embedded
extensive libraries
procedural and oops
free and opensouce
interpretent
idenfier
keyword
datatype
intfloatcomplexboolstrbytesbytearrayrangelisttuplesetfrozensetdictNone
int() decimal binary octal hexadecimal
bin()
oct()
hex()
Operators
input/ouput
import math module