One of the greatest benefits of Python is its ability to snake (pun completely intended) around a lot of conventions found in other languages with little effort on behalf of the programmer, letting you compose incredibly simple little “quips” to get the job done. Here are a few examples!

1. Swap Variables Around

Because you don’t have to deal with tedious things like addresses in memory, swapping your variables for each other can be done in one simple line: Just separate each variable with a comma, and swap them around! This is what the concept would look like in a snippet: Running this in console should output “4 1”.

2. Do a Quick Napkin Factorial

Python’s math tools allow for some very creative code to perform otherwise complex calculations. For example, what’s the quickest way to find the number of ways a number of objects can be arranged? Do a factorial. With the reduce() call, you can quickly come up with the answer! This call will calculate the factorial of any number you previously define in “x.” Don’t forget that reduce() is a component of Python’s functools library. This is what the code looks like in a snippet: Your console should output 479001600 from this particular calculation. Go ahead and make “x” whatever you want!

3. Initialize and Declare Multiple Variables

Python’s syntax rules allow you to do pretty wild things. For instance, initialize and declare as many variables as you want in one single go. This as opposed to doing so line-by-line. Print these out and you’ll end up with “16 78 195.” The neat thing is that you don’t even have to restrict yourself to declaring one type of variable within a line. Replace the “y” declaration with a string like “Hi” and it’ll be just fine!

4. Open and Read a File

Python requires you to iterate through a file line-by-line as you would in many other languages. Even so, it gives you the ability to implement the entirety of the function of both opening and reading the file into one single line of code: Now, if I want to just display the text of my own default bash configuration file, this is what I’d write:

5. Write to a File

Just like with reading a file, the process of writing to one is pretty straightforward in this nifty language. The with statement within Python lets you avoid the hassle of having to close the file handle. Hence it won’t conflict with other applications that would attempt to access it while yours is open. You can now use the one-liner you learned for reading a file to check whether that line was added correctly!

6. Create a Ranged List of Numbers

Similarly to how other scripting languages like LUA work, Python lets you spawn pre-populated lists as long as the operations lead to a predictable result. In this snippet, we create a list of 10 integers ranging from 0 to 9: Printing this list will yield a comma-separated list of numbers with the parameters we discussed earlier.

7. Display All Users (In Linux/Unix/BSD)

Ever wonder how many user names there actually are in your particular Linux installation? Python has a great way of doing this in a single line by opening your “/etc/passwd” file. All we have to do in this case is trim out everything from the first colon (“:”) in each line onwards. If you haven’t sniffed around in that file, you may be surprised to find that there are many more users created by your system than the one you log in with and the root user. User lists are normally this long because the system creates its own forms of authentication based on services you run.

8. Generate a Random Password

Like any self-respecting language, Python lets you randomize things, but it can’t help but take things a step further and let you do so to generate a password in one single line. It is admittedly a very long one.. This particular snippet will generate a 32-character password that allows for spaces. Adjust to your liking. If you don’t want a space in the password for whatever reason, remove the space within the string declaration. Don’t forget to import the “random” library or else your code won’t work!

9. Find Instances of Anything Within Text

If you’re reading a longer file and you’re trying to find how many instances of a particular expression exist within it, there’s a little zinger for that: In this particular example, we’re trying to find how many times the letter “d” appears in the string following it. By printing the output, the console lets us know there are 3 instances of the letter. You can do this with entire words and search within a file.

10. Convert Hexadecimal Expressions to Plaintext

With a little bit of iterative magic, it’s possible to convert hexadecimal code into plain text in one simple expression: The large pile of gibberish within iter() is a hexadecimal expression that this code converts to read out to, “Wow, I’m writing in hexadecimal!” Image credit: Pixabay All screenshots by Miguel Leiva-Gomez

PyCharm – Use this if you are serious about dedicating yourself to Python. This IDE specializes only in Python and contains a significant number of tools and libraries that can help you code fluidly.Visual Studio Code – If you’re looking to branch out eventually and learn other languages, VSCode (known in Linux sometimes as Code – OSS and in Arch Linux as the “code” package) provides a powerhouse of features as well as an integrated compiler for a great variety of languages.Vim – it allows for very rapid complex text editing from inside any terminal emulator. It has a high learning curve but should be in every programmer’s arsenal.