25
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.
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
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")
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"))
Trust me, reversing a string can't be easy than this.
string = "pythoniseasy"
print(string[: : -1])
Single line code for taking multiple user input using .split()
method.
a, b = input("Enter A & B : ").split()
print("A :",a)
print("B" :,b)
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
25