6 Python Easy Tricks for beginners

Hello fellow coders! Today I'm going to tell you 6 python tricks that you can use to make your learning journey fun and easy.

1. Swapping Numbers

Swapping is important concept when it comes to data structure and algorithm. In normal way we need to create a temporary variable to swap the numbers. But in Python it's just easy..

a,b = 10,20
# Before Swapping
print(a,b)  # OUTPUT : 5 10

#Swapping Variables
a, b = b, a
print(a,b)  # OUTPUT : 10 5

2. Opening a Website

Opening website using a python library.This is how we can open a website with just a single line of code.

import webbrowser
webbrowser.open("https://www.google.com")

3. Colored Text

Prints colored output text. But first we need to install termcolor python package using pip install termcolor in cmd or terminal.

from termcolor import colored
print(colored("This is Awesome","red"))
print(colored("Python is easy","blue"))

4. Reverse a String

Trust me, reversing a string can't be easy than this.

string = "pythoniseasy"
print(string[: : -1])

5. Multiple User Input

Single line code for taking multiple user input using .split() method.

a, b = input("Enter A & B : ").split()
print("A :",a)
print("B" :,b)

6. Single String From List

Create a single string from all elements in the list

lstStr = ["coding","is","fun"]
print("".join(lstStr))

That's it for today! Till then KEEP CODING...KEEP HUSTLING

24