eval.gif
2.2 MB
🐍 One line calculator
#tips
print( eval(input()) )
is all you need to create a basic calculator.eval()
is a built-in Python function that allows you to evaluate arbitrary Python expressions from string-based input, including simple mathematical expressions like "2 + 2"
, "21 / 3"
or "6 * 6"
.#tips
How to get combinations of n numbers in a list
If you need to get all possible combinations with particular length of a list's elements, simply call
#tips
If you need to get all possible combinations with particular length of a list's elements, simply call
itertools.combinations
on your list. It takes two arguments: a list and an integer value, which should be equal to the length of each combination.#tips
is
vs ==
❓You might have heard somewhere that the Python identity operator (is
) is faster than the equality operator (==
), or you may feel that it looks more Pythonic. But actually there's a subtle difference between this operators.❗️The
==
operator compares the value or equality of two objects, whereas the is
operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators ==
and !=
, except when you’re comparing to None
.#tips
How to Measure Code Execution Time in Python?
The
To measure the execution time of a statement, simply use the
#tips
The
timeit
module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps for measuring execution times.To measure the execution time of a statement, simply use the
timeit.timeit()
function. It accepts the following input parameters:▪️stmt
– String containing the code snippets for your test case.▪️setup
– Initial code that will be executed once.▪️timer
– The timer instance. It is defaulted to time.perf_counter(), which returns a float.▪️number
– Number of executions to be carried out. The default value is 1,000,000.▪️globals
– Specifies a namespace in which to execute the code.#tips
Assert Statements in Python
♦️Python’s
♦️The goal of using assertions is to let developers find the likely root cause of a bug more quickly. An assertion error should never be raised unless there’s a bug in your program. They’re not intended to signal expected error conditions, like “file not found”, where a user can take corrective action or just try again.
#tips
♦️Python’s
assert
statement is a debugging aid that tests a condition. If the condition is true, it does nothing and your program just continues to execute. But if the assert condition evaluates to false, it raises an AssertionError
exception with an optional error message.♦️The goal of using assertions is to let developers find the likely root cause of a bug more quickly. An assertion error should never be raised unless there’s a bug in your program. They’re not intended to signal expected error conditions, like “file not found”, where a user can take corrective action or just try again.
#tips
10 Ways to Speed Up Your Python Code ⚡️
1. List Comprehensions
Many of Python’s built-in functions are written in C, which makes them much faster than a pure python solution.
3. Function Calls Are Expensive
Function calls are expensive in Python. While it is often good practice to separate code into functions, there are times where you should be cautious about calling functions from inside of a loop. It is better to iterate inside a function than to iterate and call a function each iteration.
4. Lazy Module Importing
If you want to use the
5. Take Advantage of Numpy
Numpy is a highly optimized library built with C. It is almost always faster to offload complex math to Numpy rather than relying on the Python interpreter.
6. Try Multiprocessing
Multiprocessing can bring large performance increases to a Python script, but it can be difficult to implement properly compared to other methods mentioned in this post.
7. Be Careful with Bulky Libraries
One of the advantages Python has over other programming languages is the rich selection of third-party libraries available to developers. But, what we may not always consider is the size of the library we are using as a dependency, which could actually decrease the performance of your Python code.
8. Avoid Global Variables
Python is slightly faster at retrieving local variables than global ones. It is simply best to avoid global variables when possible.
9. Try Multiple Solutions
Being able to solve a problem in multiple ways is nice. But, there is often a solution that is faster than the rest and sometimes it comes down to just using a different method or data structure.
10. Think About Your Data Structures
Searching a dictionary or set is insanely fast, but lists take time proportional to the length of the list. However, sets and dictionaries do not maintain order. If you care about the order of your data, you can’t make use of dictionaries or sets.
🔗Source
#tips
1. List Comprehensions
numbers = [x**2 for x in range(100000) if x % 2 == 0]
instead ofnumbers
= []2. Use the Built-In Functions
for x in range(100000):
if x % 2 == 0:
numbers.append(x**2)
Many of Python’s built-in functions are written in C, which makes them much faster than a pure python solution.
3. Function Calls Are Expensive
Function calls are expensive in Python. While it is often good practice to separate code into functions, there are times where you should be cautious about calling functions from inside of a loop. It is better to iterate inside a function than to iterate and call a function each iteration.
4. Lazy Module Importing
If you want to use the
time.sleep()
function in your code, you don't necessarily need to import the entire time
package. Instead, you can just do from time import sleep
and avoid the overhead of loading basically everything.5. Take Advantage of Numpy
Numpy is a highly optimized library built with C. It is almost always faster to offload complex math to Numpy rather than relying on the Python interpreter.
6. Try Multiprocessing
Multiprocessing can bring large performance increases to a Python script, but it can be difficult to implement properly compared to other methods mentioned in this post.
7. Be Careful with Bulky Libraries
One of the advantages Python has over other programming languages is the rich selection of third-party libraries available to developers. But, what we may not always consider is the size of the library we are using as a dependency, which could actually decrease the performance of your Python code.
8. Avoid Global Variables
Python is slightly faster at retrieving local variables than global ones. It is simply best to avoid global variables when possible.
9. Try Multiple Solutions
Being able to solve a problem in multiple ways is nice. But, there is often a solution that is faster than the rest and sometimes it comes down to just using a different method or data structure.
10. Think About Your Data Structures
Searching a dictionary or set is insanely fast, but lists take time proportional to the length of the list. However, sets and dictionaries do not maintain order. If you care about the order of your data, you can’t make use of dictionaries or sets.
🔗Source
#tips
Towards Data Science
10 Ways to Speed Up Your Python Code | Towards Data Science
Python is flexible, but it can be slow. Let's speed it up.