DEV Community 👩‍💻👨‍💻

Rohan Kiratsata
Rohan Kiratsata

Posted on

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
Enter fullscreen mode Exit fullscreen mode

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"")
Enter fullscreen mode Exit fullscreen mode

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"))
Enter fullscreen mode Exit fullscreen mode

4. Reverse a String

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

string = "pythoniseasy"
print(string[: : -1])
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

6. Single String From List

Create a single string from all elements in the list

lstStr = ["coding","is","fun"]
Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

xanderyzwich profile image
Corey McCarty

That technique for reversing a string works for lists too. It's the slice operator where list_name[start:end:step] gives you the elements from start to end - 1 and step can allow you to grab every second or third element. -1 in this context tells it to start at the end because it's negative and use step size of one.

Want to Create an Account?
Now it's your turn!
🗒 Share a tutorial
🤔 Reflect on your coding journey
❓ Ask a question

Create an account to join hundreds of thousands of DEV members on their journey.