-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path007_lists.py
120 lines (64 loc) · 1.71 KB
/
007_lists.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
# List's in Python
# A list is ordered set of objects/items
#
# Empty List
names = []
# Integers List
numbers = [1, 34, 65, 67]
# Float List
float_numbers = [20.45, 56.89]
# String List
string_list = ['anay', 'vinu', 'vinayak']
# Mixed list
mixed_list = [1, 34.56, 'anay']
# List concatenation
new_lst = numbers + float_numbers + string_list + mixed_list
print(new_lst)
# List of List
list_of_list = [[1, 2], [3, 4], ['anay']]
# Python zip()
# The purpose of zip() is to map the similar index of multiple containers so that they can be used just using as single entity.
name = ['Anay', 'Vinayak', 'Vinu']
age = [3, 4, 5]
birth_year = [2016, 2015, 2014]
new_list = zip(name, age, birth_year)
# see the output
print(new_list)
# see the output
print(list(new_list))
# List Method
age = [3, 4, 5]
# append
# to add 1 item at a time
age.append(6)
print(age)
# range
print(range(9))
# ranger start with 0 ends with 10 with difference of 2
range(0,11, 2)
# to get the length of the list
len(age)
print(len(age))
# indexes in list
# Python lists are zero-indexed. This means that the first element in a list has index 0, rather than 1.
# Here are the index numbers for that list:
age = [2, 4, 6]
# 0 1 2
# to get the element from the list a particular index
age[0]
age[1]
# list slicing
age = [1, 2, 3, 4, 6, 9]
age[0:3]
print(age[0:3])
# sort and sorted
age = [34, 54, 12, 32, 90, 45, 67, 78, 35]
# this changes the age and now when we print it will always return the sorted age
age.sort()
print(age)
# just in case if we dont want to change the original age list we can use sorted
age = [34, 54, 12, 32, 90, 45, 67, 78, 35]
age_sorted = sorted(age)
print(age)
print(age_sorted)
print(age)