0% found this document useful (0 votes)
5 views38 pages

Data Types in Python

Uploaded by

yuvrajadhav680
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
5 views38 pages

Data Types in Python

Uploaded by

yuvrajadhav680
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 38

Data types in Python

Topics to be discussed

Writing our first python program


Executing a python program
Comments in python
How python see variables
Built in data types
Identifiers
Reserved words
Writing first python program

#python program to add two numbers


a=10
b=20
c=a+b
print(“sum=“,c)
Comments in python

There are two types of comments in python


Single line comments: Start with # symbol
A line starting with a # is treated as a
comment
Ex:# To find sum of two numbers
Multi line comments: When we want to make
several lines as comment, then writing #
symbol in the beginning of every line
#This is my first program
# It adds two numbers
Executing a python program

Four ways to execute a python program


Using Spyder IDE
Using Jupyter Notebook
Using command line prompt
Using Console
Contd..
Writing a group of statements inside “”” (triple double
quotes) or ‘’’(triple single quotes) in the beginning and
ending of the block as
“””
This is my first program
Program to add two numbers
“””
Or
‘’’
This is my first program
Program to add two numbers
‘’’
triple double quotes(“””)or triple single quotes(‘’’) are called
multiline comments
Docstrings

Python supports only single line comments.


Multiline comments are not available in
python.
Hence if strings are written in triple single or
double quotes memory will be allocated to
strings. If these strings are not assigned to
any variable then they are removed from
memory by the garbage collector
Contd.

If strings are written in “”” or ‘’’ and if these


strings are written as first statement in a
module,function,class or method then these
strings are called documentation strings. We
can access these docstrings using
the __doc__ attribute.
Example: filename temp.py
How python Sees Variables

In c, java and many other languages the


concept of variable is connected to memory
location.
A block of memory is allocated with the
variable name and in the block value
assigned to variable is stored
a=11 2
a=2 2 b=a

a a b
Contd..

In python a variable is seen as a tag that is tied


to some value.
The statement a=1 means the value 1 is
created
first in memory and then a tag by the name a is
created
Data types in python

• Data type represents the type of data


stored in memory or variable.
• The data types which are already available
in python language are called bulit-in type.
• The data types which can be created by the
programmers are called user defined data
types
Built in data types

 None type
 numeric types
 sequences
 sets
 Mappings
None type

None data type represents an object that does


not contain any value. None keyword is used
to define a null value or no value at all.
type(variable/constants)
x=None
print(x,type(x))
Output
None <class 'NoneType'>
Numeric data types

Represents numbers. There are three sub types


int
float
complex
int: int data type represents an integer
numbers.
An integer number is a whole number followed
by optional positive or negative sign
Ex: 1000, +30,-5000
int contd

int data type has no limit for the size, hence


we can store a very large integer number in
int variable
x=10
print(x, type(x))
Output
10 <class 'int'>
int keyword is used as type conversion
Int contd..
Int(): int(variable/constants) converts to
integer type
1. x=int()
print(x,type(x))
Output
0 <class 'int'>
2. x=int("10")
print(x,type(x))
output
10 <class 'int'>
int contd..
3. x=int(30.55)
print(x,type(x))
output
30 <class 'int'>

int(string,base) constructor converts string to integer


number for given base

4. a=int("12",16)
print(a)
print(hex(a),type(a))
output
18
0x12 <class 'int'>
Int contd..

5. a=int("10",8)
print(a)
print(oct(a),type(a))
Output
8
0o10 <class 'int'>
Integer constants

Decimal integer constants( made up of 0-9)


Ex 10,+3000,-40
0o47
Octal integer constants( made up of digits 0-7
with leading 0o)
Ex: 0o67,-0o65
Hexadecimal integer constants(made up of 0-
F/0-f with leading 0x)
Ex:0x1768,0x6fd
Binary constants: (made up of digits 0 and 1
with leading 0b) ex: 0b1101,0b0110
int(octal/hexdecimal constant) converted to
decimal constant
x=int(0b1001)
y=int(0o34)
z=int(0xb1f)
print(x,type(x))
print(y,type(y))
print(z,type(z))
Output
9 <class 'int'>
28 <class 'int'>
2847 <class 'int'>
Float data type
Float data type represents floating point
numbers Any constants that contains decimal
digits or exponentiation (e/E) with an optional
plus or minus sign are known as floating point
constant.
ex: 0.02, 12.34, -45.67, 12e2, 2.5E4
objects of type float can be created using
explicit constructor of class float. This
constructor also act as type converter.
Contd..
x=10.20
y=float(13.45)
print(x,type(x))
print(y,type(y))
Output
10.2 <class 'float'>
13.45 <class 'float'>
Type conversions
float(variable/constants) converts to float type

print(float())
print(float(10))
print(float("25"))
x=33
X=float(x)
print(float(x),type(x))
Output
0.0
10.0
25.0
33.0 <class 'int'>
Complex data type

complex data type allows us to represents


complex number. A complex number is a
number that is written in the form of a+bj or
a+bJ. a is real part and b is imaginary part.
These may be integer or floating point
numbers
ex: -2+2j, 8.9-4j, -1-5.5j, 3+5j, 0.2+10.5J
complex() is explicit constructor to create a
complex object and also act as type
converter.
Contd..

c=2+3j
s=complex(-4,6.0)
print(s,type(s))
print(c,type(c))
print(c.real,c.imag)
Output
0j <class 'complex'>
(2+3j) <class 'complex'>
2.0 3.0
complex() as type converter
x=complex(2,3)
print(x,type(x))
y=complex("2")
print(y,type(y))
z=complex(2)
print(z,type(z))
a=complex(-4.5,0.6)
print(a,type(a))
b=complex("2+3j")
print(b,type(b))

Output
(2+3j) <class 'complex'>
(2+0j) <class 'complex'>
(2+0j) <class 'complex'>
(-4.5+0.6j) <class 'complex'>
(2+3j) <class 'complex'>
bool data type
bool data type allows us to store boolean
value True or False in boolean object. True
and False are python keyword. Objects of
type bool can be created using bool
constructor which also converts int, float and
string to bool type.
x=True
print(x,type(x))
y=bool()
print(y,type(y))
output
True <class 'bool'>
False <class 'bool'>
x=bool(1)
print(x,type(x))
y=bool("hello")
print(y,type(y))
z=bool(-3.5)
print(z,type(z))
x1=bool(0)
print(x1,type(x1))
y1=bool("")
print(y1,type(y1))
z1=bool(0.0)
print(z1,type(z1))
Output
True <class 'bool'>
True <class 'bool'>
True <class 'bool'>
False <class 'bool'>
False <class 'bool'>
False <class 'bool'>
Sequence Data type

string
list
tuple
set
range
byte
bytearray
dcitionary(mapping)
Contd

str represents string datatype. String is


represented as group of characters enclosed
in single, double, triple double or triple single
quotes. Objects of type str can be created
using explicit constructor str() and will also
act as type converter
Example:
s="hello"
print(s,type(s))
p=str("bec bgk")
print(p,type(p))
Output
hello <class 'str'>
bec bgk <class 'str'>
Type converter str(variable/constants) converts to string

s=str(10)
print(s,type(s))
a=str(10.45)
print(a,type(a))
b=str(3+4j)
print(b,type(b))
c=str()
print(c,type(c))
Output
10 <class 'str'>
10.45 <class 'str'>
(3+4j) <class 'str'>
<class 'str'>
String constants in python

Group of characters enclosed in single, double,


triple single or triple double quotes
Example
print('hello ! world')
print(" hello ! world")
print(""" hello ! world """)
print(''' hello ! world '‘’)
Output
hello ! world
hello ! world
hello ! world
contd
Advantage of single, double, triple single or
triple double quotes is we can embed a string
inside another string
Example:
print('hello """world"""')
print(" i am 'ram' i am in 'bec' ")
print(''' "this" is """ core python """ ''')
print(""" 'Hi' ! "how" are '''you''' """)
print(" 'me' here ''' ? ''' ")
print(" hello \"world\" ")
print(" '''hello''' ! \"""world\""" ")
Guess the output
Characters in python
C and Java has char data type. Char data type
represents a single character. Python does
not have a char data type. To represents a
single character str data type itself is used.
Example:
ch='a'
print(ch,type(ch))
s=" hello"
print(s[1],type(s[1]))
Output
a <class 'str'>
h <class 'str'>
Identifiers and reserved words
An identifiers is name given to variable,
function, class, packages, modules, methods etc.
Rules to form Identifiers:
1. They can include letters, numbers and
underscore characters(_)
2. They must start with a nonnumeric characters
3. Special characters are not permitted
4. Uppercase and lower case letters are
identified separately.
5.Keywords should not be used.
Ex:
Salary,name11,gross_income, Salary
Naming conventions in python

Packages, modules, global variable, instance


variable, functions, methods name must be
written in lower case letters. In case multiple
words separate them using underscore(_)
Class name must start with first letter
captialized
Constants names should be written in all
capital letters. If a constant has several worlds
they should be separated by underscore(_)
and except lambda with

Reserved as finally nonlocal while

words assert false None yield

Words for which break for not


particular task is
assigned . class from or
Following is the list of
continue global pass
reserved keywords in
Python 3 def if raise

del import return

elif in True

else is try
THANK YOU

You might also like