-
Notifications
You must be signed in to change notification settings - Fork 6
/
data types 2.py
148 lines (147 loc) · 3.8 KB
/
data types 2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> '''Q.1- Write a program to create a tuple with different data types and do the following operations.1. Find the length of tuples '''
'Q.1- Write a program to create a tuple with different data types and do the following operations.1. Find the length of tuples '
>>> t1=("python","ashok","nitish","mohit","raghav","manish","rahul","ankit","ayush","ashish","rajayt")
>>> t2=(55,22,8,6,3,4,7,1,5,1,6,8,2,3,1,9,1,3,8)
>>> len(t1)
11
>>> len(t2)
19
>>> #Q.2-Find largest and smallest elements of a tuples
>>> max(t1)
'rajayt'
>>> min(t1)
'ankit'
>>> min(t2)
1
>>> max(t2)
55
>>> #Q.3- Write a program to find the product of all elements of a tuple.
>>> ts=(2,5,8,7,3,8)
>>> ts
(2, 5, 8, 7, 3, 8)
>>> tc=(2*5*8*7*3*8)
>>> tc
13440
>>> #
>>>
>>>
>>>
>>> # SETS
>>>
>>>
>>>
>>>
>>> '''Q.Q.1- Create two set using user defined values.
1. Calculate difference between two sets.
2. Compare two sets.
3. Print the result of intersection of two sets.'''
'Q.Q.1- Create two set using user defined values. \n\n1. Calculate difference between two sets.\n2. Compare two sets.\n3. Print the result of intersection of two sets.'
>>> seta={2,8,6,7,6,8,2,7,2,1}
>>> type(seta)
<class 'set'>
>>> setb={5,9,6,7,4,2,,8,4,2,3,1,4}
SyntaxError: invalid syntax
>>> setb={5,9,6,7,4,2,8,4,2,3,1,4}
>>> seta
{1, 2, 6, 7, 8}
>>> setb
{1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> seta-setb
set()
>>> setb-seta
{9, 3, 4, 5}
>>> seta & setb
{1, 2, 6, 7, 8}
>>> seta | setb
{1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> 1 in seta
True
>>> 0 in seta
False
>>> c=seta|setb
>>> c
{1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> d=seta & setb
>>> d
{1, 2, 6, 7, 8}
>>>
>>>
>>>
>>> type(c)
<class 'set'>
>>>
>>>
>>> # DICTIONARIES
>>>
>>>
>>> #Q1:- Create a dictionary to store name and marks of 10 students by user inputs
>>> a={"ashish":"90", "rajat":"99","manish":"44","raghav":"99","millan":"88","rajesh":"58", "nilesh":"44", "nikita":"55","kanika":"88","mohit":"55"}
>>> type(a)
<class 'dict'>
>>>
>>>
>>> # Q.2 sorting dictionary acc to marks
>>>
>>> a
{'ashish': '90', 'rajat': '99', 'manish': '44', 'raghav': '99', 'millan': '88', 'rajesh': '58', 'nilesh': '44', 'nikita': '55', 'kanika': '88', 'mohit': '55'}
>>> sorted(a.values())
['44', '44', '55', '55', '58', '88', '88', '90', '99', '99']
>>> c=sorted(a.values())
>>> c
['44', '44', '55', '55', '58', '88', '88', '90', '99', '99']
>>>
>>>
>>> #Q.3- Count the number of occurrence of each letter in word "MISSISSIPPI". Store count of every letter with the letter in a dictionary.
>>>
>>> d={"MISSISSIPPI"}
>>> type(d)
<class 'set'>
>>> d.count(m)
Traceback (most recent call last):
File "<pyshell#67>", line 1, in <module>
d.count(m)
AttributeError: 'set' object has no attribute 'count'
>>> d.count("m")
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
d.count("m")
AttributeError: 'set' object has no attribute 'count'
>>> d=("MISSISSIPPI")
>>> d.count("M")
1
>>> d.count("I")
4
>>> d.count("S")
4
>>> d.count("P")
2
>>> c=dict()
>>> type(c)
<class 'dict'>
>>> a=d.count("M")
>>> a
1
>>> b=d.count("I")
>>> b
4
>>> c=d.count("S")
>>> c
4
>>> e=d.count("P")
>>> e
2
>>> dicionary=dict()
>>> dictionary={a,b,c,e}
>>> dictionary
{1, 2, 4}
>>> dictionary.append(b)
Traceback (most recent call last):
File "<pyshell#87>", line 1, in <module>
dictionary.append(b)
AttributeError: 'set' object has no attribute 'append'
>>> dictionary={a,b,c,e}
>>> dictionary
{1, 2, 4}
>>>