Python: Tips and Tricks

Looking to improve your Python skills? Check out Webucator’s Python classes.
None
  1. This is a short video explaining how scientific notation works.

    Read Article

  2. This a brief article and video explaing Python magic __main__ variable.

    Read Article

  3. In this brief tutorial, we’ll show you how to create a virtual environment with Python’s venv module. We will do this using Visual Studio Code.

    Read Article

  4. When you install Python 3 on a Mac, it does not update the python command to use Python 3 instead of Python 2. Instead, to run Python 3, you have to use the python3 command. In this brief article, we show you how to fix this.

    Read Article

  5. Want to be able to open files in IDLE on Windows by double-clicking on them? You need to make IDLE the default editor for Python files. In this short article, you'll learn how.

    Read Article

  6. The syntax for the ternary operator in Python is:

    [on_true] if [expression] else [on_false]

    Many programming languages, including Python, have a ternary conditional operator, which is most often used for conditional variable assignment. To illustrate, consider this code:

    Read Article

  7. The time() method of Python's time module returns the seconds since the epoch (1/1/1970 at midnight). To convert the number of seconds to years with Python, divide by seconds in a minute, minutes in an hour, hours in a day, and days in a year.

    Read Article

  8. Python packages are very easy to create, but not so easy to design. A Python package is just a group of files (and possibly subfolders) stored in a directory that includes a file named __init__.py.

    Read Article

  9. In Python, you open a file using the built-in open() function and passing it the path to the file you want to open. By default, files are opened as read-only.

    Read Article

  10. Python is cross-platform and generally runs the same all operating systems, but if you're writing code that is operating-system dependent, you may need to check the OS. Here's a function for doing that.

    Read Article

  11. You have a list of items and want to print it out as a numbered list using Python.

    Read Article

  12. In Python, if you need to repeatedly append to a string, you should convert it to a list, append your items to that list, and then join the list back into a string after you've made all the additions.

    Read Article

  13. In 2016, we launched a new website, and I wrote a Python script that checked our old sitemap to make sure that we had all our 301 redirects properly in place.

    Five years later, we are again launching a new site, so I revisited that script.

    Read Article

  14. A very cool feature of Python is that it allows for simultaneous assignment. The syntax is as follows:

    var_name1, var_name2 = value1, value2

    This can be useful as a shortcut for assigning several values at once, like this:

    smart_1, cute_1, funny_1, quiet_1 = "John","Paul","Ringo","George"

    But simultaneous assignment is really useful in a scenario like the one shown below.

    Read Article

  15. When running Python files using Run Python File in Terminal, Visual Studio Code runs the Pythn file using an absolute path. For example:

    Read Article

  16. In Python, when you need to get a sequence of characters from a string (i.e., a substring), you get a slice of the string using the following syntax:

    Read Article

  17. The Python interpreter must locate imported modules. Where does it look for them?

    Read Article

  18. In Python 3.5, you can merge two or more dictionaries in a single statement by unpacking the new dictionaries into a new dictionary.

    Read Article

  19. Indexing is the process of finding a specific element within a sequence of elements through the element's position. Remember that strings are basically sequences of characters. We can use indexing to find a specific character within the string.

    Read Article

  20. In this brief article, we explain why you should always use parentheses when you create tuples in Python.

    Read Article