Python is one of the most popular programming languages out there today. Its simplicity and readability make it a favorite among many programmers.

So in this tutorial, I'm sharing a few pointers and strategies to help you improve your Python programming skills.

How to Reverse a String in Python

In Python, there is no built-in method for reversing a string. The quickest (and perhaps easiest?) method is to employ a slice that moves backward, -1.

For instance, suppose we have a string "Freecodecamp" that we wish to reverse. Make a slice that goes backward from the end of the string.

In this case, the slice expression [::-1] indicates to begin at the end of the string and terminate at position 0, then move with step -1, negative one, which implies one step backward.


# Create a variable name "a"
a = "Freecodecamp"
# Then you assign freecodecamp to variable 'a'
print("Reverse is", a[::-1])
Reversing a string in Python

The below output shows the backward version of the string "freecodecamp":

fcc
Reversing a string in Python

How to Print the File Path of Imported Modules

Python offers a very simple method for getting the file paths of an imported module. This is useful if you need to quickly identify a file path while working on a project with several subdirectories, or if you're using scripts or applications that are typically accessible via the command line.

If you're in a similar scenario, you may use the following approach to get your module's precise file path:

import os
import socket

print(os)
print(socket)

That's all. Use this approach to determine the precise location of module files in your code. The module's exact file location should then be printed for you. It will most likely look like this:

import-os
output for printing file path

How to Use Enums in Python

An Enum is a collection of symbolic names that are linked to distinct values. They are comparable to global variables, but they have additional helpful capabilities such as repr(), grouping, type-safety, and a few more. As you can see, making an Enum is as easy as building a class that inherits from Enum.

Enum Properties:

  • Enums can be shown as a string or as a representation.
  • You can use type to determine the type of an Enum ().
  • You use the "name" keyword to display the Enum member's name.
class MyName:
	Python, By, Davidking = range(3)

print(MyName.Python)
print(MyName.By)
print(MyName.Davidking)

The output will print out the result according to their number as arranged on the list:

enum
Enum in Python

How to Swap Variable Values in Python

Swapping two variables means switching the values of the variables. In most cases, you do this using the data in memory.

Swapping is as simple as it gets. You can use this step to swap two objects in Python:

a = 1
b = 2

print('Before Swapping')
print(a, b)

a, b = b, a
print('After Swapping')
print(a, b)

The result will return the new interchanged position/number for the two variables "a, b":

swap
swapping in Python Fcc

How to Find the Most Frequent Value in a List in Python

You can use this process to sort out the most repeated value from a list. Imagine a game where numbers were given to people randomly and the group with the most frequent value wins the game. You can use this method to find out the game-winner without the need to count manually.

Now let's see how it goes:

test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4, 3, 1, 2, 2, 2]
print(max(set(test), key = test.count))

Now the output here will tell us the winner of the game without having to count manually. The output here is "2" when you run the code above.

How to Check the Memory Usage of an Object in Python

With this simple yet powerful Python method, you can calculate how much memory your Python objects consume.

Here is an example below:

import sys
x = 1
print(sys.getsizeof(x))

The result of variable X here is "28".

How to Create a File Server in Python

Servers are pieces of computer software or hardware that handle requests and provide data to clients across a network. There are several sorts of servers, the most popular of which are web servers, database servers, application servers, and transaction servers.

Python's SimpleHTTPServer module is a handy and simple utility that developers may use for a variety of purposes, the most common of which is to serve files from a directory.

It removes the time-consuming procedure of installing and configuring available cross-platform web servers.

Did you want to build a file server in Python? You can accomplish this by using the simple line of code below:

python -m SimpleHTTPServer 
# The default port is 8080

How to Check Your Python Version from IDLE

You might be curious about the Python version you're using. Well, you can check the installed Python version on your PC just by writing a few lines of code:

import sys
print("My Python version Number: {}".format(sys.version))

Output:

My Python version Number: 3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)]

Here you go, it will print out the version you are using.

Conclusion

Hopefully, after going through this set of python tips and tricks you find it useful and interesting, Thanks for reading and I wish you good luck in your coding career.