Python tips

Return multiple results from a function

This trick will show you how you can return multiple results from the function back to the function call.

def values():    return 1, 2, 3print(values()) #(1, 2, 3)

Read Passwords as User input

This tip and trick are useful when you want to input a password from a user but you know the input function in python did not convert your user input in “*” asterisk form.

from getpass import getpassusern = input('Enter username : ')passw = getpass('Enter password : ' )

Shutting down your PC/Laptop

This tip and trick will help you to shut down your computer using operating systems and modules and python with only one line of code.

import os os.system("window -s")

Walrus (:=) Operator

This tip will help you to understand the walrus (:=) operator. This operator is recently added in the new version of python 3.8 and its Assignment expressions allow you to assign and return a value in the same expression. Take a look at the example below to understand this operator.

Multiple User input

These tips will help you to take multiple user inputs. if you are thinking that is easy if we add two input function then you are right it’s easy but I had a fast and easy way to do that.

#normal waya = input("Enter name: ")b = input("Enter age: ")print(a, b) # haider 22#Fast and Easy Waya, b = input("Enter name and age: ").split()print(a, b) # haider 22

Converting a list to Dictionary

This tip and trick are useful when you are required to convert a list into the dictionary. I will guide you in an easy and fast method of how to do that?.

names=["ferb", "Jenny", "John"]salary=["45000", "65000", "75000"]employee=dict(zip(names,salary))print(employee) #{'ferb': '45000', 'Jenny': '65000', 'John': '75000'}

Reverse a String

These tips will help you to reverse a string in an easy and fast way.

string1 = "python"string2 = "programming"print(string1[: : -1]) #nohtypprint(string2[: : -1]) #gnimmargorp

Swapping Numbers in Python

Swapping numbers in programming is usually done with an extra variable temp. But we had a trick for you for fast numbers swapping.

a = 5b = 8#Normal waytemp = aa = bb = temp#Tip and Trick waya, b = b, a


To keep your programs manageable as they grow, you may want to break them up into several files. Python allows you to put multiple function definitions into a file and use them as a module. You can import these modules into other scripts and programs. These files must have a .py extension.

# 1- Module definition => save file as
def minmax(a,b):
    if a <= b:
        min, max = a, b
        min, max = b, a
    return min, max

# 2- Module Usage
import my_function
x,y = my_function.minmax(25, 6.3)


The __init__ method.

The init method is invoked soon after the object of a class is instantiated. The method is useful to perform any initialization you plan. The init method is analogous to a constructor in C++, C# or Java.

# Implementing a Python class as

class Employee(object):

    def __init__(self, role, salary):
        self.role = role
        self.salary = salary

    def is_contract_emp(self):
        return self.salary <= 1250

    def is_regular_emp(self):
        return self.salary > 1250
emp = Employee('Tester', 2000)

if emp.is_contract_emp():
    print("I'm a contract employee.")
elif emp.is_regular_emp():
    print("I'm a regular employee.")

print("Happy reading Python coding tips!")

The output of the above code would look like as given below.

[~/src/python $:] python

I'm a regular employee.
Happy reading Python coding tips!