-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_syntax.py
More file actions
81 lines (71 loc) · 1.59 KB
/
basic_syntax.py
File metadata and controls
81 lines (71 loc) · 1.59 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
# get data form console and print
# data = input("please input: ")
# print(data)
# print
print('a', 'b') # a b
print('a', 'b', sep='.') # a.b
print('a', 'b', end=',') # a b,c
print('c')
# list combine
a = ['a', 'b']
b = [1, 2]
c = a + b # combine
d = a * 3 # repeat 3 times
print(c)
print(d)
# list delete elements by index
arr = [1, 2, 3]
print("before delete:", arr)
del arr[0]
print("after delete:", arr)
# find element
arr = [1, 2, 3]
print(1 in arr)
print(4 in arr)
# fast value assignment
arr = [1, 2, 3]
first, second, third = arr
print(first, second, third, sep=',')
# list add method: append(), insert()
arr = [1, 2, 3]
arr.append(4)
print(arr) # [1, 2, 3, 4]
arr.insert(0, 0) # insert an element where index is 0
print(arr) # [0, 1, 2, 3, 4]
# list remove method: remove(val)
arr = [1, 2, 3, 2]
arr.remove(2)
print(arr) # [1, 3, 2]
# list sort
arr = [1, 2, 3, 2]
arr.sort()
print(arr) # [1, 2, 2, 3]
arr.sort(reverse=True)
print(arr) # [3, 2, 2, 1]
arr = ["a", "A", "a", "B", "b"]
arr.sort() # ascii ordered
print(arr)
arr = ["a", "A", "a", "B", "b"]
arr.sort(key=str.lower) # dictionary ordered
print(arr)
# string isX()
str = "abc"
print(str.islower())
print(str.isupper())
print(str.isalpha())
print(str.isnumeric())
# startwith endwith
print("abc".startswith('a'))
print("abc".endswith('c'))
# join split
print(','.join("abc")) # a,b,c
print('a b c'.split()) # ['a', 'b', 'c']
# text align rjust() ljust() center()
print('abc'.rjust(5))
print('abc'.ljust(5))
print('abc'.center(5))
print('abc'.center(10, '*'))
# trim str
print(' abc '.strip())
print(' abc '.lstrip())
print(' abc '.rstrip())