-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlists.py
More file actions
51 lines (40 loc) · 1.53 KB
/
Copy pathlists.py
File metadata and controls
51 lines (40 loc) · 1.53 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
# List is a heterogenous datatype
fruits = ["apple", "banana", "orange", "mango", 1, 2, 3, True]
# print(fruits[0], fruits[-1])
# print(fruits[0:2])
# print(len(fruits))
# print(dir(fruits))
"""
['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
"""
# append: Add elements to a list "at end"
# If the output of an operation is None, then its an inplace operation
fruits1 = fruits.append(5)
# print(fruits, fruits1)
# Iterable: Something you can iterate on
# 'int' object is not iterable
# fruits.extend(6) # Throws an error
fruits.extend("6")
# print(fruits)
fruits.append(["apple", "banana", "orange", "mango"])
# print(fruits)
fruits.extend(["apple", "banana", "orange", "mango"])
# print(fruits)
# Append vs extend
# Append operation adds the entire element as-is
# Extend operation iterates over the iterable and appends the element to the original list
# print(fruits.count("apples"))
# mango_index = fruits.index("mango")
# print(mango_index)
# print(fruits[::-1])
sample_str = "Welcome to python session"
sample_str_list = list(sample_str) # typecasting: converting one datatype to another datatype
# print(sample_str_list)
sample_str_list = "".join(list(sample_str)) # typecasting: converting one datatype to another datatype
# print(sample_str_list)
sample_list = [1, 2, 3, 5, 4, 6]
# sample_list.sort() # inplace operation
print(sample_list)
sample_list_stored = sorted(sample_list)
print(sample_list, sample_list_stored)
# .sort() is an inplace operation and sorted() returns the sorted list