What us the output of the code given above?
Anonymous Quiz
13%
1 1 1
15%
0 0 0
9%
None None None
22%
1 3 None
28%
1 3 1
3%
0 3 0
10%
Error
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
What is the output of the code given above?
Anonymous Quiz
40%
[1, 2, 3, [4, 5, 6]]
46%
[1, 2, 3, 4, 5, 6]
10%
[1, 2, 3]
1%
None
3%
Error
Speech Recognition with Python ๐ฃ
๐ธHave you ever wondered how to add speech recognition to your Python project? Itโs easier than you might think. SpeechRecognition is a Python library for performing speech recognition, with support for several engines and APIs, online and offline.
Speech recognition engine/API support:
โช๏ธCMU Sphinx (works offline)
โช๏ธGoogle Speech Recognition
โช๏ธGoogle Cloud Speech API
โช๏ธWit.ai
โช๏ธMicrosoft Bing Voice Recognition
โช๏ธHoundify API
โช๏ธIBM Speech to Text
โช๏ธSnowboy Hotword Detection (works offline)
โ๏ธInstallation
๐Examples
๐Speech Recognition in Python- The Complete Beginnerโs Guide
#speechrecognition
๐ธHave you ever wondered how to add speech recognition to your Python project? Itโs easier than you might think. SpeechRecognition is a Python library for performing speech recognition, with support for several engines and APIs, online and offline.
Speech recognition engine/API support:
โช๏ธCMU Sphinx (works offline)
โช๏ธGoogle Speech Recognition
โช๏ธGoogle Cloud Speech API
โช๏ธWit.ai
โช๏ธMicrosoft Bing Voice Recognition
โช๏ธHoundify API
โช๏ธIBM Speech to Text
โช๏ธSnowboy Hotword Detection (works offline)
โ๏ธInstallation
pip install SpeechRecognition
๐GitHub๐Examples
๐Speech Recognition in Python- The Complete Beginnerโs Guide
#speechrecognition
Complex Numbers in Python ๐
Most general-purpose programming languages have either no support or limited support for complex numbers. Python is a rare exception because it comes with complex numbers built in.
The quickest way to define a complex number in Python is by typing its literal directly in the source code:
๐In-depth tutorial on complex numbers by RealPython
#tutorial #complex
Most general-purpose programming languages have either no support or limited support for complex numbers. Python is a rare exception because it comes with complex numbers built in.
The quickest way to define a complex number in Python is by typing its literal directly in the source code:
>>> z1 = 1 + 2j
You can also use a built-in Python function, complex()
. It accepts two numeric parameters. The first one represents the real part, while the second one represents the imaginary part denoted with the letter j in the literal you saw in the example above:>>> z2 = complex(1, 2)To get the real and imaginary parts of a complex number in Python, you can reach for the corresponding
>>> z1 == z2
True
.real
and .imag
attributes:>>> z2.imagNote that both properties are read-only because complex numbers are immutable, so trying to assign a new value to either of them will fail.
2.0
๐In-depth tutorial on complex numbers by RealPython
#tutorial #complex
What is the output of the code given above?
Anonymous Quiz
7%
{3, 4, 5, 6}
15%
{}
25%
{1, 2}
22%
{1, 2, 3, 4, 5, 6}
10%
{1, 2, 3, 4}
5%
{3, 4, 5, 6}
16%
Error
Create Beautiful Diagrams with Python ๐
๐ธDiagrams is a Python library which lets you draw the cloud system architecture in Python code. It was born for prototyping a new system architecture design without any design tools. You can also describe or visualize the existing system architecture as well.
As you can see from the example above, it's extremely simple to create your own customized diagram. See more examples here.
โ๏ธInstallation
๐Diagrams Tutorial
#diagrams
๐ธDiagrams is a Python library which lets you draw the cloud system architecture in Python code. It was born for prototyping a new system architecture design without any design tools. You can also describe or visualize the existing system architecture as well.
As you can see from the example above, it's extremely simple to create your own customized diagram. See more examples here.
โ๏ธInstallation
pip install diagrams๐GitHub
๐Diagrams Tutorial
#diagrams
What's the output of the code given above?
Anonymous Quiz
15%
['mango', 'kiwifruit', 'banana']
24%
['mango', [], 'kiwifruit', 'banana']
25%
['mango', [], 'banana']
10%
['mango', 'carrot', 'kiwifruit', 'banana']
13%
[]
13%
Error
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.
What is the output of the code given above?
Anonymous Quiz
34%
[1, 2, 3, 12]
7%
[12, 1, 2, 3]
5%
[4, 1, 2, 3]
18%
[1, 2, 3, 4]
36%
Error
Working With Zip Files in Python ๐
The ZIP file format is a common archive and compression standard. The in-built zipfile module provides tools to create, read, write, append, and list a ZIP file.
The most common class which is used to work with Zip Files is ZipFile class. It is used to write and read the Zip files and also has some methods which are used to handle the them.
๐Gees For Geeks tutorial
#zipfile
The ZIP file format is a common archive and compression standard. The in-built zipfile module provides tools to create, read, write, append, and list a ZIP file.
The most common class which is used to work with Zip Files is ZipFile class. It is used to write and read the Zip files and also has some methods which are used to handle the them.
๐Gees For Geeks tutorial
#zipfile
What's the output of the code given above?
Anonymous Quiz
17%
1 2 3 4
33%
2 3 4
17%
None 2 3 4
10%
None 2 3
16%
2 4 3
8%
None 1 2 3 4