-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path30-python-snippets.html
188 lines (188 loc) · 6.24 KB
/
30-python-snippets.html
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=yes"
/>
<title>30-python-snippets</title>
<style type="text/css">
code {
white-space: pre-wrap;
}
span.smallcaps {
font-variant: small-caps;
}
span.underline {
text-decoration: underline;
}
div.column {
display: inline-block;
vertical-align: top;
width: 50%;
}
</style>
</head>
<body>
<h1 id="helpful-python-snippets-that-you-can-learn-in-30-seconds-or-less">
30 Helpful Python Snippets That You Can Learn in 30 Seconds or Less
</h1>
<blockquote>
<p>
Short Python snippets that you can quickly learn and use in your work or
personal needs
</p>
</blockquote>
<p>
<a
href="https://fatosmorina.medium.com/?source=post_page-----69bb49204172--------------------------------"
><img
src="https://miro.medium.com/fit/c/56/56/1*K5svz0DwQPKGfnX0LJZozw.jpeg"
alt="Fatos Morina"
/></a>
</p>
<figure>
<img
src="https://miro.medium.com/max/60/0*a4SfQa9ogzbuspzy?q=20"
alt="Image for post"
/>
<figcaption>Image for post</figcaption>
</figure>
<figure>
<img
src="https://miro.medium.com/max/9698/0*a4SfQa9ogzbuspzy"
alt="Image for post"
/>
<figcaption>Image for post</figcaption>
</figure>
<p>
Photo by
<a
href="https://unsplash.com/@jantined?utm_source=medium&utm_medium=referral"
>Jantine Doornbos</a
>
on
<a href="https://unsplash.com/?utm_source=medium&utm_medium=referral"
>Unsplash</a
>
</p>
<p>
Python represents one of the most popular languages that many people use
it in data science and machine learning, web development, scripting,
automation, etc.
</p>
<p>
Part of the reason for this popularity is its simplicity and easiness to
learn it.
</p>
<p>
If you are reading this, then it is highly likely that you already use
Python or at least have an interest in it.
</p>
<p>
In this article, we will briefly see 30 short code snippets that you can
understand and learn in 30 seconds or less.
</p>
<p>
The following method checks whether the given list has duplicate elements.
It uses the property of <em>set()</em> which removes duplicate elements
from the list.
</p>
<p>
This method can be used to check if two strings are anagrams. An anagram
is a word or phrase formed by rearranging the letters of a different word
or phrase, typically using all the original letters exactly once.
</p>
<p>This snippet can be used to check the memory usage of an object.</p>
<p>This method returns the length of a string in bytes.</p>
<p>
This snippet can be used to print a string <em>n</em> times without having
to use loops to do it.
</p>
<p>
This snippet simply uses the method <em>title()</em> to capitalize first
letters of every word in a string.
</p>
<p>This method chunks a list into smaller lists of a specified size.</p>
<p>
This method removes falsy values (False, None, 0 and “") from a list by
using <em>filter()</em>.
</p>
<p>This snippet can be used to transpose a 2D array.</p>
<p>
You can do multiple comparisons with all kinds of operators in a single
line.
</p>
<p>
This snippet can be used to turn a list of strings into a single string
with each element from the list separated by commas.
</p>
<p>This method gets vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) found in a string.</p>
<p>
This method can be used to turn the first letter of the given string into
lowercase.
</p>
<p>
The following methods flatten a potentially deep list using recursion.
</p>
<p>
This method finds the difference between two iterables by keeping only the
values that are in the first one.
</p>
<p>
The following method returns the difference between two lists after
applying a given function to each element of both lists.
</p>
<p>You can call multiple functions inside a single line.</p>
<p>
The following method checks whether a list has duplicate values by using
the fact that <em>set()</em> contains only unique elements.
</p>
<p>The following method can be used to merge two dictionaries.</p>
<p>In Python 3.5 and above, you can also do it like the following:</p>
<p>
The following method can be used to convert two lists into a dictionary.
</p>
<p>
This snippet shows that you can use <em>enumerate</em> to get both the
values and the indexes of lists.
</p>
<p>
This snippet can be used to calculate the time it takes to execute a
particular code.
</p>
<p>
You can have an <em>else</em> clause as part of a
<em>try/except</em> block, which is executed if no exception is thrown.
</p>
<p>This method returns the most frequent element that appears in a list.</p>
<p>This method checks whether a given string is a palindrome.</p>
<p>
The following snippet shows how you can write a simple calculator without
the need to use if-else conditions.
</p>
<p>
This snippet can be used to randomize the order of the elements in a list.
Note that shuffle works in place, and returns <em>None</em>.
</p>
<p>
This method flattens a list similarly like [].concat(…arr) in JavaScript.
</p>
<p>
This is a really quick way for swapping two variables without having to
use an additional one.
</p>
<p>
This snippet shows how you can get a default value in case a key you are
looking for is not included in the dictionary.
</p>
<p>
<a
href="https://towardsdatascience.com/30-helpful-python-snippets-that-you-can-learn-in-30-seconds-or-less-69bb49204172"
>Source</a
>
</p>
</body>
</html>