-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_Python_methods.py
More file actions
140 lines (96 loc) · 2.64 KB
/
Copy path3_Python_methods.py
File metadata and controls
140 lines (96 loc) · 2.64 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
a = 'harry' # Single quoted string
b = "harry" # Double quoted string
c = '''harry''' # Triple quoted string for multiple lines
## SLICING WITH SKIP VALUE
word = "amazing"
word[1:6:2] # mzn
word[-7:-1] # amazin
word[:7] # amazing
word[0:] # amazing
print(word)
## STRING FUNCTIONS
text = " Hello Python "
print("Length:", len(text))
print("Lower:", text.lower())
print("Upper:", text.upper())
print("Title:", text.title())
print("Strip:", text.strip())
print("Capitalize:", text.capitalize())
print("Replace:", text.replace("Python", "World"))
print("Find:", text.find("Python"))
print("Count:", text.count("o"))
print("Starts with Hello:", text.strip().startswith("Hello"))
print("Ends with Python:", text.strip().endswith("Python"))
## list of string methods
# Create a list
numbers = [10, 20, 30]
# append()
numbers.append(40)
print("append():", numbers)
# insert()
numbers.insert(1, 15)
print("insert():", numbers)
# extend()
numbers.extend([50, 60])
print("extend():", numbers)
# remove()
numbers.remove(20)
print("remove():", numbers)
# pop()
numbers.pop(1)
print("pop():", numbers)
# count()
print("count(30):", numbers.count(30))
# index()
print("index(30):", numbers.index(30))
# sort()
numbers.sort()
print("sort():", numbers)
# reverse()
numbers.reverse()
print("reverse():", numbers)
# copy()
new_list = numbers.copy()
print("copy():", new_list)
# clear()
numbers.clear()
print("clear():", numbers)
## TUPLE METHODS
t = (10, 20, 30, 20, 40)
print(t.count(20))
print(t.index(30))
## DICTIONARY METHODS
student = {"name": "Vijay", "age": 21, "course": "Python"}
print("get():", student.get("name"))
print("keys():", student.keys())
print("values():", student.values())
print("items():", student.items())
student.update({"age": 22})
print("update():", student)
student.pop("course")
print("pop():", student)
student.setdefault("city", "Bangalore")
print("setdefault():", student)
new_dict = student.copy()
print("copy():", new_dict)
student.clear()
print("clear():", student)
## SET METHODS
set1 = {10, 20, 30}
set2 = {30, 40, 50}
set1.add(60)
print("add():", set1)
set1.update([70, 80])
print("update():", set1)
print("union():", set1.union(set2))
print("intersection():", set1.intersection(set2))
print("difference():", set1.difference(set2))
print("symmetric_difference():", set1.symmetric_difference(set2))
set1.remove(20)
print("remove():", set1)
set1.discard(100) # No error
print("discard():", set1)
copy_set = set1.copy()
print("copy():", copy_set)
set1.clear()
print("clear():", set1)