-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPython Tips.py
124 lines (57 loc) · 1.31 KB
/
Python Tips.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
#!/usr/bin/env python
# coding: utf-8
# # Swapping Values
# In[1]:
x,y = 10, 5
print(x,y)
# In[2]:
x,y =y,x
print(x,y)
# # Combining a list of strings into a single one
#
# In[3]:
sentence = ['Why','is','the','rum','gone','?']
concat_sen = "".join(sentence)
print(concat_sen)
# # Initiallizing a list
# In[4]:
[0]*10
# In[5]:
[7]*10
# # Merging List
# In[6]:
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z = {**x, **y}
print(z)
# # Reversing a String
# In[7]:
name = "Jack Sparrow"
name[::-1]
# # List Comprehension
# In[8]:
a = [1, 2, 3]
b = [i*2 for i in a] # Create a new list by multiplying each element in a by 2
print(b)
# # Iterating over a dictionary
# In[9]:
m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
for key, value in m.items():
print('{0}: {1}'.format(key, value))
# # Iterating over list values while getting the index too
# In[10]:
m = ['a', 'b', 'c', 'd']
for index, value in enumerate(m):
print('{0}: {1}'.format(index, value))
# # Removing useless characters on the end of your string
# In[11]:
name = "Jack "
name_2 = "Sparrow///"
name.strip() # prints "Jack"
# In[12]:
name_2.strip("/") # prints "Sparrow"
# # Found the most frequent value in the list
# In[13]:
test = [1, 2, 3, 4, 2, 2, 2, 3, 1, 4, 4, 4]
print(max(set(test), key = test.count))
# In[ ]: