-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp6.py
More file actions
121 lines (73 loc) · 2.57 KB
/
p6.py
File metadata and controls
121 lines (73 loc) · 2.57 KB
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
# Tuple
#it is also data structure
#it can store any data type
#but it is imm utable, that means we can not update tuple
#data inside tuple
#tuple are faster than lists
example=("one","two","three")
#no-append no-insert no-pop no-remove
#methods
#count, index
#len function
#Slicing
print(example[:2]) #o/p:('one', 'two')
# example[0]=1
# print(example)#Error
#looping in tuple #o/p:
mixed=(1,2,3,4.0) # 1
for i in mixed: # 2
print(i) # 3
#we can also use while loop # 4.0
i=0
while i<len(mixed):
print(mixed[i])
i+=1
#tuple with one element
mixed=(1,2,3,4.0)
print(type(mixed)) #o/p:<class 'tuple'>
mix1=(3) #this is not tuple
print(type(mix1)) #o/p:<class 'int'>
mix2=(3,) #if we want to make single element tuple, we have to use column after element
print(type(mix2)) #o/p:<class 'tuple'>
s=("Word") #this is not tuple
print(type(s)) #o/p:<class 'str'>
s1=("words",)
print(type(s1)) #o/p:<class 'tuple'>
#tuple with out parenthesis
guiter='honda',"hero",'passion' #we can create tuple without parethesis
print(type(guiter)) #o/p:<class 'tuple'>
#tuple unpcking
tuple_1='12',"23",'34','45','56'
a,b,c,d,e=tuple_1 #here we have to take number variables same as a number of elements in tuple
print(a) #o/p:12
print(b) #o/p:23
print(c) #o/p:34
print(d) #o/p:45
print(e) #o/p:56
#list inside tuples
tuple_1=("Ravin",["rupen","yash"])
#here we can add remove items in list
tuple_1[1].pop()
print(tuple_1) #o/p:('Ravin', ['rupen'])
tuple_1[1].append("Me also")
print(tuple_1) #o/p:('Ravin', ['rupen', 'Me also'])
# some other function
# min(),max(),sum
#function returning two values
def func(a,b):
add=a+b
mul=a*b
return add,mul
print(func(3,4)) #o/p:(7, 12)
#or
add1,multi=func(3,4)
print(add1) #o/p:7
print(multi) #o/p:12
#something more about tuple, list, string
nums=tuple(range(1,11))
print(nums) #o/p:(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
nu_list=list(range(1,11))
print(nu_list) #o/p:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
nu_string=str((1,2,3,4,5,6,7,8,9,10))
print(nu_string) #o/p:(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(type(nu_string)) #o/p:<class 'str'>