python-snippets
python-snippets
About the Book
Table of Contents
- “Python Tips and Tricks: A guide to making the most of the python language”
- How to iterate through 2 lists in Python:
- How to Split a String on the First N Occurrences in Python
- How to Quickly Find the Longest String in a List in Python
- You can round numbers in Python using negative rounding numbers
- Get Rid of Duplicates without using sets
- Generate Random Numbers using module random()
- 3 Ways to Check Whether a List is Empty
- How to replace a text with some other text
- How to use Lambda functions
- How to Quickly Simplify if statements
- How to find the first day of the month
- How to put days before months
- How to find the difference between 2 dates in months
- How to subtract weeks from a date
- How to subtract days from a date
- How to add weeks to a date
- How to add days to a date
- How to find the number of days between 2 dates
- How to convert from one timezone to another one
- How to convert a string into a date
- Date formatting
- Math operation inside strings
- Get default value for missing keys
-
Swap values
- Shuffle elements using shuffle()
- How to Quickly Check Whether a String is a Palindrome
- Try else
- Time spent
- Chained function call
- Decapitalize
- Get vowels
- Comma-separated
- Compact
- Capitalize first letters
- Chek whether 2 sets have the same elements
- Find the largest element in a dictionary using max
- Find the index of a substring in a string
- How to generate fake data using Faker
- How to Check Whether a Variable is a Number
- How to Convert two lists into a dictionary
- How to Display a Progress bar in Python using tqdm
- How to Quickly Find the Symmetric Difference of 2 Sets in Python
- How to Quickly Find the Union of 2 Sets in Python
- How to Quickly Find the Intersection of 2 Sets in Python
- How to Quickly Find the Difference Between 2 Sets in Python
- How to Quickly Multiply Elements in a List in Python
- How to Reverse a List Using Negative Slicing in Python
- How to Repeat a List in Python
- How to start implementing a method and finish it later on using Pass statement
- How to generate unique IDs using the uuid module
- Difference between sort() and sorted()
- You can check whether a value is not part of a list using “not in”
- Print the place where a module is located
- You can remove duplicates from a list in a single line
- You can avoid throwing errors when trying to access a non-existent key in a dictionary
- You can build your own iterator
- Difference between copy() and deepcopy()
- Find the most frequent element in a list in just one line
- Sort elements of a string or list based on their frequency
- Count the number of elements using “count” from “itertools”
- Check if 2 strings are anagrams using Counter
- Count the number of elements in a string or list using Counter from “collections”
- You cannot change the value of a global variable just by mentioning it inside a function
- Other values that evaluate to False are None, “False” and the number 0
- Any non-empty list, tuple, or dictionary is evaluated to True
- Any non-empty string is evaluated to True
- At least one condition is met out of many in a single if-statement
- Multiple conditions at a single if-statement
- Condition inside the print function
- Join 2 sets
- Remove all elements from a list, set, or dictionary
- Remove all elements from a list, set, or dictionary
- Get sub-tuples starting from an index
- Get sub-tuples by making jumps
- Get the index of an element in a tuple
- Count the number of times an element appears in a tuple
- You can easily get the last n elements using slice()
- Quickly count the number of times an element appears in a list that satisfies a condition
- Nest a list and a tuple inside a tuple
- We can use negative indexes in tuples too
- Check if a string has all its words starting with an uppercase character
- Check if a string represents a Chinese number
- Check if a string represents a number
- Remove characters from the right based on the argument
- Check if all characters in a string are alphabets
- Check if all characters in a string are either alphabets or numbers
- Check if all characters are white spaces in a string
- Swap cases of characters in a string
- You can define a custom printable version for an object of a class
- You can also redefine the “<” and “==” operators inside your own classes
- You can redefine the “+” operator inside your own classes
- You can call the parent class’s initializer using super() or parent class’s name
- You can define a method that can be called with as many parameters as you want
- Check the memory usage of an object
- You can access private properties even outside their intended scope
- You can define the same method multiple times inside the same scope
- You don’t need to compare the length with 0
- range() starts by default at 0
- range() includes a step parameter that may not be known that much
- map() returns a new object
- filter() returns a new object
- Conditionals statements in lambda should always include the “else” part
- Lambda functions can only be in one line
- Add a value in the first position in a list
- Convert a value into a complex number
- You can convert any data type into a boolean value
- You can convert a boolean value into a number
- You can use boolean values in arithmetic operations
- Swap keys and values of a dictionary
- Get values of a dictionary in a single line
- Get keys of a dictionary in a single line
- Get the value of a character in Unicode
- Check whether 2 strings are anagrams using sorted()
- An “if-elif” block can exist without the else block at the end
- You can turn a set into an immutable set
- Lists, sets, and dictionaries are mutable
- Strings and tuples are immutable
- Integers, floats, strings, booleans, and tuples are immutable
- Find the unique id of a variable using id()
- Check whether a string starts with a particular character without using the index 0
- Check whether a string is larger than another string
- Merge 2 dictionaries quickly
- Difference between == and “is”
- Float division
- Partial Slicing with just the beginning or ending index
- Reverse slicing
- Slice a string using a step function
- Reverse the ordering of a list
- You can separate big numbers with the underscore
- You can use the underscore character anywhere in the name of a variable
- You cannot have 0 as the first digit in a number
- You cannot use an operator at the beginning of the name of a variable
- You cannot use a number at the beginning of the name of a variable
- Print multiple values with a custom separator in between each value
- Print multiple elements in the same line using multiple prints
- Print multiple elements with a single print() statement
- Avoid “trivial” mistakes by using named parameters
- Convert a string into a list of strings
- Find the index of an element in a list or tuple
- Merge dictionaries in a single readable line
- Compare 3 numbers just like in Math
- Repeat strings without looping
- Enumerate related items of the same concept via Enum
- Navigate in a List without using for loops
-
Assign multiple variables in just one line
- “Else” condition inside a “for” loop
- Extract elements from lists
- Get n largest or n smallest elements in a list using the module heapq
- Pass lists as method arguments
- Get all the elements in the middle of the list
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $14 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them