-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuseful_methods.qs
More file actions
52 lines (40 loc) · 1.29 KB
/
Copy pathuseful_methods.qs
File metadata and controls
52 lines (40 loc) · 1.29 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
# Useful methods and functions
# Lists: append adds one item.
numbers = [3, 1, 2]
numbers.append(4)
print("after append:", numbers)
# Lists: extend adds many items from another list.
numbers.extend([6, 5])
print("after extend:", numbers)
# Lists: sort puts items in order.
numbers.sort()
print("after sort:", numbers)
# Methods can also return a new value inside an expression.
more = [2, 1].append(3).sort()
print("method chain:", more)
# Strings: split breaks a string into a list.
words = "hello world foo".split(" ")
print("split:", words)
csv = "red,green,blue".split(",")
print("csv split:", csv)
chars = "abc".split("")
print("char split:", chars)
# Strings: join glues list items together.
names = ["Ali", "Sara", "Mina"]
line = ", ".join(names)
print("joined:", line)
# Numbers: abs returns the positive distance from zero.
print("abs:", abs(-12))
# len works on strings and lists.
print("name count:", len(names))
print("letter count:", len("Quanto"))
# type() returns the type name as a string.
print("type of 42:", type(42))
print("type of hi:", type("hi"))
print("type of true:", type(true))
print("type of null:", type(null))
print("type of list:", type([1, 2]))
print("type of map:", type({"a": 1}))
# str() converts a value to a string.
print("str(42):", str(42))
print("str([1,2]):", str([1, 2]))