0% found this document useful (0 votes)
2 views

Python DATATYPES

Python DATATYPES

Uploaded by

pgup2612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python DATATYPES

Python DATATYPES

Uploaded by

pgup2612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Presented by

Ms. Sushmita Chakraborty


Programme Coordinator (PGDCA) &
Assistant Professor
Department of Computer Science
Patna Women’s College
BITWISE OPERATORS
Bitwise operators act on individual bits(0 and 1) of
the operands. We can use bitwise operators directly on
binary numbers or integers also. When we use these
operators on integers these numbers are converted into
bits ( binary number system ) and then bitwise
operators act upon those bits . the results given by
these operators are always in the form of integers.
BITWISE OPERATORS
THERE ARE 6 TYPES OF BITWISE OPERATORS

Operator Meaning Example

& Bitwise AND x & y = 0 (0000 0000)

| Bitwise OR x | y = 14 (0000 1110)

~ Bitwise NOT ~x = -11 (1111 0101)

^ Bitwise XOR x ^ y = 14 (0000 1110)

>> Bitwise right shift x >> 2 = 2 (0000 0010)

x << 2 = 40 (0010
<< Bitwise left shift
1000)
EXAMPLE

x=10
y=11
print( ‘x &y= ‘, x & y)
print( ‘x |y= ‘, x | y)
print( ‘x ^y= ‘, x ^ y)
DATA TYPE IN PYTHON

• A data type represent the type of data stored into a variable or


memory .The data types which are already available in python
language are called built –in data types. Python has the following
data types built-in by default, in these categories:
• None type : none data type represents an object that does not contain
any value .
• Numeric types: the numeric types represent numbers . there are three
sub types int, float, complex
• Sequence types: list, tuple, range
• Mapping type: dict
• Set types: set, frozen set
DECLARING AND USING NUMERIC DATA
TYPES: INT, FLOAT, COMPLEX

• Int data type : the int datatype represent an integer number .An
integer number is a number without any decimal point or
fraction part .
• Float datatype :the float data type represents
Floating point numbers .A floating number is a number that
contains a decimal point. Example: 0.5,290.08
Floating numbers can also be written in scientific
notation use ‘e’ or ‘E’ to represent the power of 10. Example x
= 2.5e4.The convenience in scientific notation is that it is
possible to represent very big numbers using less memory.
• Complex data type : A complex number is a number that is written
in the form of a+bj or a + bj .The suffix j or J after b indicates the
square root value of -1.

• FOR EXAMPLE:
PYTHON PROGRAM TO ADD TWO COMPLEX NUMBERS.
C1=2.5+2.5J
C2=3.0-0.5J
C3= C1 + C2
PRINT(“SUM= ”, C3)
Representing Binary ,Octal and
Hexadecimal Numbers

• A binary number should be written by prefix ob (zero


and b)or 0b(zero and B) before the value. For example
0b110110, 0B101010011 are treated as binary numbers.
• Hexadecimal numbers are written by prefixing 0x(zero
and x) 0x (zero and big x) before the value similarly
octal number are indicated by prefix in 0o(zero and
small o) or 0o(zero and then o) before the actual value
for example : 00145 or 0o773 octal value
Q1. Write a program to calculate area of a circle

radius = float(input(“enter the radius of the circle :


”))
area = 3.14*radius*radius
circumference = 2*3.14*radius
Print(“Area = “+str(round(area,2))+”\t
CIRCUMFERENCE =
“+str(round(circumference,2)))
Converting the Data types

• Depending on the type of data, python internally assumes


the data type for the variable.
• But sometimes the programmer wants to convert one data
type into another data type on his own this is called type of
conversion or coercion.
• This is possible by mentioning the data type with
parentheses.
EXAMPLE TO CONVERT A NUMBER INTO
DATA TYPE
• x= 15.56
print (int(x))
• num= 15
print (float(num))
• a= 10
b =-5
print (complex(a,b))
Q2. Write a program to convert an integer into
the corresponding floating point number.

a = int(input (“enter any integer = ”))


Print(“The floating point variant of
”)+str(a)+(“ = ”)+str(float(a))
Q. A program to convert numbers from octal , binary
and hexadecimal systems into decimal system
python program to convert into decimal number system
n1= 0o17
n2= 0b1110010
n3= 0x1c2
n=int(n1)
print(‘octal 17 = ‘, n)
n=int(n2)
print(‘binary 1110010 = ‘, n)
n=int(n3)
print(‘hexadecimal 1c2= ‘ n)
SEQUENCE TYPES

A sequence represents a group of


elements or items. There are six type
of sequence in python
• Str
• List
• Tuple
• Range
USING STRING DATA TYPE AND STRING
OPERATIONS

str represent string data type. a stringe is represent


by a group of characters . str are enclosed in single
quotes or double quotes.
for example
str=“welcome”
str=‘welcome’ # here str is name of string type variable.
we can also write strings inside “””(triple double
quotes) or ‘’’(triple single quotes)
the triple double quotes or triple single quotes are useful to
embed a string inside another string
STRING OPERATIONS
• The slice operator represents square represents [ and ] to
retrieve pieces of a string .
for example , the characters in a string are counted from 0
onwards. hence, str[0] indicates the 0th character or
beginning character in the string.
Example : s = ‘welcome to python’
print(s)
print(s[0]) # 0th character from s
print(s[3:6]) # from 3rd to 6th characters
print(s[11:]) # from 11th characters onwards till end
print(s[-1]) # first character from the end
STRING CONCATENATION

• When the + >>> a = 'Hello'


>>> b = a + 'There'
operator is applied
>>> print b
to strings, it means HelloThere
"CONCATENATION” >>> c = a + ' ' + 'There'
>>> print c
Hello There
>>>
USING IN AS AN OPERATOR
>>> fruit = 'banana’
• The in keyword can also be >>> 'n' in fruit
used to check to see if one True
string is "in" another string >>> 'm' in fruit
False
• The in expression is a
>>> 'nan' in fruit
logical expression and True
returns true or false and >>> if 'a' in fruit :
can be used in an if ... print 'Found it!’
...
statement Found it!
>>>
STRING COMPARISON
if word == 'banana':
print 'All right, bananas.'

if word < 'banana':


print 'Your word,' + word + ', comes before
banana.’
elif word > 'banana':
print 'Your word,' + word + ', comes after
banana.’
else:
print 'All right, bananas.'
STRING LIBRARY
• Python has a number of string
functions which are in the string >>> greet = 'Hello
library Bob'>>> zap =
greet.lower()>>> print
• These functions are already built
zaphello bob
into every string - we invoke
them by appending the function
>>> print greet
to the string variable Hello Bob>>> print 'Hi
There'.lower()
• These functions do not modify
hi there
the original string, instead they
>>>
return a new string that has
been altered
>>> stuff = 'Hello world’
>>> type(stuff)<type 'str'>
>>> dir(stuff)
['capitalize', 'center', 'count', 'decode', 'encode',
'endswith', 'expandtabs', 'find', 'format', 'index',
'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'partition', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate',
'upper', 'zfill']

http://docs.python.org/lib/string-methods.html
SEARCHING A STRING

• We use the find() function


to search for a substring
b a n a n a
within another string 0 1 2 3 4 5
• Find() finds the first
>>> fruit = 'banana'
occurance of the substring
>>> pos = fruit.find('na')
• If the substring is not >>> print pos
found, find() returns -1 2
• Remember that string >>> aa = fruit.find('z')
position starts at zero >>> print aa
-1
LIST IS A KIND OF COLLECTION
• A collection allows us to put many values in a single “variable”
• A collection is nice because we can carry all many values
around in one convenient package.
• A list is similar to an array that consists of a group of elements
or items . lists are more versatile and useful than array.
• List is mutable

friends = [ 'Joseph', 'Glenn', 'Sally' ]

carryon = [ 'socks', 'shirt', 'perfume' ]


CREATING LIST []

• empty list l1= [ 10,20,30] or l1=list()


• long list l1=[1,2,3,……………10000]
• nested list l1=[10,20,[30,40,50],60,70]
LIST CONSTANTS

• List constants are


>>> print [1, 24, 76]
surrounded by square [1, 24, 76]
brackets and the >>> print ['red', 'yellow', 'blue']
elements in the list are ['red', 'yellow', 'blue']
separated by >>> print ['red', 24, 98.6]
['red', 24, 98.599999999999994]
commas.
>>> print [ 1, [5, 6], 7]
• A list element can be [1, [5, 6], 7]
any python object - >>> print []
[]
even another list
• A list can be empty
LOOKING INSIDE LISTS

•Just like strings, we can get at any single


element in a list using an index specified in
SQUARE BRACKETS
>>> friends = [ 'Joseph', 'Glenn',
'Sally' ]
>>> print friends[1]
Joseph Glenn Sally Glenn
>>>
0 1 2
LISTS ARE MUTABLE
>>> fruit = 'Banana’
• STRINGS ARE "IMMUTABLE" - >>> fruit[0] = 'b’
WE CANNOT CHANGE THE Traceback
TypeError: 'str' object does not
CONTENTS OF A STRING -
support item assignment
WE MUST MAKE A NEW >>> x = fruit.lower()
STRING TO MAKE ANY >>> print x
banana
CHANGE
>>> lotto = [2, 14, 26, 41, 63]
• LISTS ARE "MUTABLE" - WE >>> print lotto[2, 14, 26, 41, 63]
>>> lotto[2] = 28
CAN CHANGE AN ELEMENT >>> print lotto
OF A LIST USING THE INDEX [2, 14, 28, 41, 63]
OPERATOR
Delete List Elements
To remove a list element, use either the del statement

For example −
list1 = ['physics', 'chemistry', 1997, 2000];
print list1 del list1[2];
print "After deleting value at index 2 :
print list1

Output−
['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 : ['physics', 'chemistry', 2000]
Basic List Operations
Python Expression Results Description

len([1, 2, 3]) 3 Length

[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4 Concatenation


, 5, 6]
['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repetition

3 in [1, 2, 3] True Membership

for x in [1, 2, 3]: print x, 123 Iteration

You might also like