-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay22.py
70 lines (52 loc) · 1.37 KB
/
Day22.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
# LIST
# Introduction to Lists in Python
# Lists are ordered collection of data items.
# They store multiple items in a single variable.
# List items are separated by commas and enclosed within square brackets[].
# Lists are changeable meaning we can alter them after creation.
# a basic implementation of List
marks = [3, 5, 6, 7, 7, 6, 8, "Light", "Dark"]
print(marks)
print(type(marks))
print(marks[0])
print(marks[1])
print(marks[2])
# Can have variety of diff data types together.
details = ["Light", 10, "Dark", 6.8, True]
print(details)
# Indexing
# Negative indexing is used to access the items, but from the end of list.
# The last item on the list has index [-1], second last [-2] ,...
print(marks[-3]) # negative indexing
print(marks[len(marks) - 3]) # negative to positive, easier to understand.
# Checking if an item is in list
if 7 in marks:
print("Yes")
else:
print("No")
if 65 in marks:
print("Yes")
else:
print("No")
if "Light" in marks:
print("Yes")
else:
print("No")
# Printing all elements of marks
# More Examples
print(marks)
print(marks[:])
print(marks[1:-1])
print(marks[1:-4])
# jump index
print(marks[1:4:2])
# List comprehension
print("\n\nList comprehension")
lst = [i for i in range(4)]
print(lst)
lst = [i for i in range(6)]
print(lst)
lst = [i for i in range(40)]
print(lst)
lst = [i for i in range(10) if i % 2 == 0]
print(lst)