0% found this document useful (0 votes)
6 views10 pages

Week 01 Python Basics

The document provides an overview of various data types in Python, including int, float, bool, complex, string, bytes, bytearray, list, tuple, set, frozenset, and dict, along with examples of their usage. It also covers operators such as arithmetic, assignment, relational, logical, and miscellaneous operators, demonstrating their functionality with code snippets. The document serves as a comprehensive guide for understanding and utilizing these data types and operators in Python programming.

Uploaded by

siriichandana27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Week 01 Python Basics

The document provides an overview of various data types in Python, including int, float, bool, complex, string, bytes, bytearray, list, tuple, set, frozenset, and dict, along with examples of their usage. It also covers operators such as arithmetic, assignment, relational, logical, and miscellaneous operators, demonstrating their functionality with code snippets. The document serves as a comprehensive guide for understanding and utilizing these data types and operators in Python programming.

Uploaded by

siriichandana27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Types

• 1.int
• 2.float
• 3.bool
• 4.complex
• 5.string
• 6.bytes
• 7.bytearray
• 8.list
• 9.tuple
• 10.set
• 11.frozenset
• 12.dict

int data type


a=10
print(a)
print(type(a))

10
<class 'int'>

a=5
b=6
c=a+b
print('Sum:',c)

Sum: 11

float data type


a=14.78
print(a)
print(type(a))

14.78
<class 'float'>

x=1.2e4
print(x)
print(type(x))

12000.0
<class 'float'>
bool data type
b=True
print(b)
print(type(b))
print(b+b)

True
<class 'bool'>
2

complex data type


a=3+5j
print(a)
print(type(a))
print(a.real)
print(a.imag)

(3+5j)
<class 'complex'>
3.0
5.0

a=3+9j
b=6-12j
c=a+b
print(c)

(9-3j)

string data type


• It is a sequence of characters.
str1='Hey folks!, how are you all?'
str2="It's monday today"
print(str1)
print(str2)

Hey folks!, how are you all?


It's monday today

s1='Welcome to "Python" Programming!'


s2="Welcome to 'Python' Programming!"
print(s1)
print(s2)

Welcome to "Python" Programming!


Welcome to 'Python' Programming!
review='kingdom movie starring VD\
is the movie of the year \
with great action scenes\
and good story line'
print(review)

kingdom movie starring VDis the movie of the year with great action
scenesand good story line

review='''kingdom movie starring VD


is the movie of the year
with "great action" scenes
and good story line'''
print(review)

kingdom movie starring VD


is the movie of the year
with "great action" scenes
and good story line

what is the real life usage of data types int, float, bool, string and
complex. Explain with multiple examples
bytes data type
• It is a collection of byte numbers.
• when a list of byte numbers given to bytes() function a bytes
data type is created
• bytes must be in range(0, 256)
• It supports indexing
• It is immutable in nature
l1=[10,20,30,40,50]
b1=bytes(l1)
#print(b1)
print(type(b1))
for x in b1:
print(x,end=' ')

<class 'bytes'>
10 20 30 40 50

l1=[10,20,30,40,50,256]
b1=bytes(l1)

----------------------------------------------------------------------
-----
ValueError Traceback (most recent call
last)
Cell In[8], line 2
1 l1=[10,20,30,40,50,256]
----> 2 b1=bytes(l1)

ValueError: bytes must be in range(0, 256)

l1=[10,20,30,40,50]
b1=bytes(l1)
print(b1[0])
b1[0]=111

10

----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[6], line 4
2 b1=bytes(l1)
3 print(b1[0])
----> 4 b1[0]=111

TypeError: 'bytes' object does not support item assignment

bytearray data type


• It is same as bytes data type but it is mutable in nature
• When a list of values are given to bytearray() function a
bytearray data type is created
l1=[10,20,30,40,50]
ba=bytearray(l1)
#print(ba)
print(type(ba))
for x in ba:
print(x,end=' ')

<class 'bytearray'>
10 20 30 40 50

list Data type


• It is a collection of homogeneous and heterogeneous elements.
• It's elements are represented in square brackets [ ] seperated by
comma.
• It supports indexing and slicing.
• It preserves the order.
• It allows duplicates.
• It is mutable in nature.
list1=[10,20,30,10,20,True,'Kingdom',14.89]
print(list1)
print(type(list1))

[10, 20, 30, 10, 20, True, 'Kingdom', 14.89]


<class 'list'>

list1=[10,20,30,40,50]
print(list1)
print(type(list1))
for x in list1:
print(x)

[10, 20, 30, 40, 50]


<class 'list'>
10
20
30
40
50

list1=[10,20,30,40,50]
print(list1[0])
print(list1[-1])
print(list1)
list1[0]=111
print(list1)

10
50
[10, 20, 30, 40, 50]
[111, 20, 30, 40, 50]

list1=[10,20,30,40,50]
print(list1)
list1.append(60)
print(list1)
list1.insert(2,25)
print(list1)
list1.remove(40)
print(list1)

[10, 20, 30, 40, 50]


[10, 20, 30, 40, 50, 60]
[10, 20, 25, 30, 40, 50, 60]
[10, 20, 25, 30, 50, 60]

list1=[]
print(list1)

[]
list1=list()
print(list1)

[]

tuple data type


• It is same as a list but it is immutable in nature.
• It's elements are represented with in parenthesis ( ) seperated
by comma.
t1=(10,20,30,40)
print(t1)
print(type(t1))
print(t1[0])
t1[0]=111

(10, 20, 30, 40)


<class 'tuple'>
10

----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[21], line 5
3 print(type(t1))
4 print(t1[0])
----> 5 t1[0]=111

TypeError: 'tuple' object does not support item assignment

set data type


• It is a collection of homogeneous and heterogeneous elements.
• It doesn't allow duplicates.
• It doesn't support indexing.
• It's elements are represented within the braces { } separated by
comma.
• It doesn't preserve the order.
s1={10,20,10,20,30,True,'Kingdom'}
print(s1)
print(type(s1))

{True, 20, 10, 30, 'Kingdom'}


<class 'set'>

• set operations
s1={10,20,30,40}
s2={30,40,50,60}
print('Union:',s1.union(s2))
print('Intersection:',s1.intersection(s2))
print('Difference:',s1.difference(s2))
print('Symmetric Difference:',s1.symmetric_difference(s2))

Union: {40, 10, 50, 20, 60, 30}


Intersection: {40, 30}
Difference: {10, 20}
Symmetric Difference: {10, 50, 20, 60}

frozenset Data type


• It is same as a set but it is immutable.
• When a set of values are given to a frozenset() method a
frozenset data type is created.
s1={10,20,30,10,30,40}
fs=frozenset(s1)
print(s1)
print(type(s1))
print(fs)
print(type(fs))

{40, 10, 20, 30}


<class 'set'>
frozenset({40, 10, 20, 30})
<class 'frozenset'>

dict data type


• It is a collection of key-value pair elements(like empno:ename,
name:phoneno).
• Here, keys cannot be duplicated but the values can be duplicated.
d1={101:'Jay',102:'Raj',103:'Michael',104:'Akhira'}
print(d1)
print(type(d1))

{101: 'Jay', 102: 'Raj', 103: 'Michael', 104: 'Akhira'}


<class 'dict'>

d1={101:'Jay',102:'Raj',103:'Michael',104:'Akhira'}
print(d1.keys())
print(d1.values())
print(d1.items())

dict_keys([101, 102, 103, 104])


dict_values(['Jay', 'Raj', 'Michael', 'Akhira'])
dict_items([(101, 'Jay'), (102, 'Raj'), (103, 'Michael'), (104,
'Akhira')])

d1={101:'Jay',102:'Raj',103:'Michael',104:'Akhira'}
print('No. of elements:',len(d1))
for x in d1.keys():
print(f"{x}--->{d1[x]}")
for k,v in d1.items():
print(f"{k}---->{v}")

No. of elements: 4
101--->Jay
102--->Raj
103--->Michael
104--->Akhira
101---->Jay
102---->Raj
103---->Michael
104---->Akhira

Operators
• Arithmetic Operators(+, -, *, /, %, //, **)
• Assignment Operators(=, +=, -=, *=. /=)
• Relational Operators(==,!=,<,<=,>,>=)
• Logical Operators(and, or, not)
• Miscelleneous Operators
– Identity Operators(is, is not)
– Membership Operaators(in, not in)
#Arithmetic Operators Demo
a=5
b=2
print('a+b:',a+b)
print('a-b:',a-b)
print('a*b:',a*b)
print('a/b:',a/b)
print('a%b:',a%b)
print('a//b:',a//b)
print('a**b:',a**b)

a+b: 7
a-b: 3
a*b: 10
a/b: 2.5
a%b: 1
a//b: 2
a**b: 25
#Assignment Operators
a=10
print('Present value of a:',a)
print('Present id of a:',id(a))

a+=5 #a=a+5
print('Updated value of a:',a)
print('Updatedid of a:',id(a))

Present value of a: 10
Present id of a: 140715785018072
Updated value of a: 15
Updatedid of a: 140715785018232

#Comparision or Relational Operators


a=20
b=20
print('a:',a,'b:',b)
print('a==b:',a==b)
print('a!=b:',a!=b)

a: 20 b: 20
a==b: True
a!=b: False

a=20
b=30
print('a<b:',a<b)
print('a<=b:',a<=b)
print('a>b:',a>b)
print('a>=b:',a>=b)

a<b: True
a<=b: True
a>b: False
a>=b: False

#Logical Operators
a=30
b=30
print('a:',a,'b:',b)
print('a==b:',a==b)
print('a>b:',a>b)

print('a==b and a>b:',a==b and a>b)


print('a==b or a>b:',a==b or a>b)

a: 30 b: 30
a==b: True
a>b: False
a==b and a>b: False
a==b or a>b: True

Miscelleneous/ Special Operators


Identity Operators(is, is not)
• These operators are used for address comparision
#Identity Operators
a=10
b=10
print(id(a))
print(id(b))
print(a is b) #address comparision
print(a is not b)
print(a==b) #content comparision

140723864492760
140723864492760
True
False
True

a=[10,20,30]
b=[10,20,30]
print(id(a))
print(id(b))
print(a is b)
print(a==b)

1876057074240
1876056816832
False
True

#Membership Operators
a=[10,20,30,40]
print(40 in a)#True
print(50 not in a)#True
print(60 in a)#False
print(10 not in a)#False

True
True
False
False

You might also like