-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLAB2.py
More file actions
143 lines (138 loc) · 2.44 KB
/
LAB2.py
File metadata and controls
143 lines (138 loc) · 2.44 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Python Lists
# ex1
fruits = ["apple", "banana", "cherry"]
print(fruits[1])
# ex2
fruits = ["apple", "banana", "cherry"]
fruits[0] = "kiwi"
# ex3
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
# ex4
fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "lemon")
# ex5
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
# ex6
fruits = ["apple", "banana", "cherry"]
print(fruits[-1])
# ex7
fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(fruits[2:5])
# ex8
fruits = ["apple", "banana", "cherry"]
print(len(fruits))
# Python Tuples
# ex1
fruits = ("apple", "banana", "cherry")
print(fruits[0])
# ex2
fruits = ("apple", "banana", "cherry")
print(len(fruits))
# ex3
fruits = ("apple", "banana", "cherry")
print(fruits[-1])
# ex4
fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruits[2:5])
# Python Sets
# ex1
fruits = {"apple", "banana", "cherry"}
if "apple" in fruits:
print("Yes, apple is a fruit!")
# ex2
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
# ex3
fruits = {"apple", "banana", "cherry"}
more_fruits = ["orange", "mango", "grapes"]
fruits.update(more_fruits)
# ex4
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
# ex5
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
# Python Dictionaries
# ex1
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car.get("model"))
# ex2
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car["year"] = 2020
# ex3
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car["color"] = "red"
# ex4
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.pop("model")
# ex5
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.clear()
# Pythom While Loops
# ex1
i = 1
while i < 6:
print(i)
i += 1
# ex2
i = 1
while i < 6:
if i == 3:
break
i += 1
# ex3
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
# ex4
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
# Python For Loops
# ex1
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
# ex2
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
# ex3
for x in range(6):
print(x)
# ex4
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)