avatarHaider Imtiaz

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

7378

Abstract

= endTime - startTime <span class="hljs-attribute">print</span>(totalTime) # <span class="hljs-number">0</span>.<span class="hljs-number">034</span></pre></div><h2 id="a6ec">10. Duplicate Removing from List</h2><p id="0765">This tip and trick will help you to remove duplicates from a list in a fast and easy way. check out the code example below.</p><div id="cf78"><pre><span class="hljs-attr">my_list</span> = [<span class="hljs-number">10</span>, <span class="hljs-number">10</span>, <span class="hljs-number">22</span>, <span class="hljs-number">23</span>, <span class="hljs-number">27</span>, <span class="hljs-number">22</span>, <span class="hljs-number">89</span>, <span class="hljs-number">56</span>, <span class="hljs-number">25</span>, <span class="hljs-number">89</span>]</pre></div><div id="c20a"><pre><span class="hljs-comment">#removing duplicates</span> my_list = list(<span class="hljs-built_in">set</span>(my_list)) <span class="hljs-built_in">print</span>(my_list) # [10, 22, 23, 56, 89, 27, 25]</pre></div><h2 id="6df0">11. Manipulating (append) Tuples</h2><p id="e866">In Python you know that tuples are immutable that means you can’t append them to add new values. But this tip and trick will help you to append tuples and add new values to them.</p><div id="3f7f"><pre><span class="hljs-attr">tuple1</span> = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>) <span class="hljs-attr">lst</span>= list(tuple1)</pre></div><div id="320f"><pre><span class="hljs-attribute">lst</span>.append(<span class="hljs-number">4</span>) <span class="hljs-attribute">lst</span>.append(<span class="hljs-number">5</span>)</pre></div><div id="06cb"><pre><span class="hljs-attribute">tuple1</span> = tuple(lst) <span class="hljs-attribute">print</span>(tuple1) # (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>)</pre></div><h2 id="2973">12. Palindrome checking</h2><p id="15f7">This trick is a one-line code to check the word is palindrome or not. Now you don’t need to write a long loop to check palindrome you can do it in a fast and easy way.</p><div id="c497"><pre><span class="hljs-attribute">w</span> <span class="hljs-operator">=</span> <span class="hljs-string">"mom"</span></pre></div><div id="593e"><pre>palindrome = <span class="hljs-built_in">bool</span>(w<span class="hljs-selector-class">.find</span>(w<span class="hljs-selector-attr">[: : -1]</span>) + <span class="hljs-number">1</span>) <span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(palindrome)</span></span></pre></div><h2 id="3092">13. List Comprehension</h2><p id="d9e9">This tip for list comprehension will help you to understand how it works and how it makes our work more easy and fast.</p><div id="e119"><pre><span class="hljs-comment">#normal way</span> <span class="hljs-attribute">x1</span>=<span class="hljs-meta"> []</span> <span class="hljs-attribute">for</span> x in range(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>): <span class="hljs-attribute">x1</span>.append(x**<span class="hljs-number">2</span>) <span class="hljs-attribute">print</span>(x1) #<span class="hljs-meta"> [1, 4, 9, 16, 25, 36, 49, 64, 81]</span></pre></div><div id="ccbe"><pre>#List comprehension easy <span class="hljs-keyword">and</span> <span class="hljs-keyword">fast</span> <span class="hljs-keyword">x</span><span class="hljs-number">1</span> <span class="hljs-operator">=</span> [<span class="hljs-keyword">x</span>**<span class="hljs-number">2</span> for <span class="hljs-keyword">x</span> in range(<span class="hljs-number">1</span><span class="hljs-punctuation">,</span> <span class="hljs-number">10</span>)] print(<span class="hljs-keyword">x</span><span class="hljs-number">1</span>) # [<span class="hljs-number">1</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4</span><span class="hljs-punctuation">,</span> <span class="hljs-number">9</span><span class="hljs-punctuation">,</span> <span class="hljs-number">16</span><span class="hljs-punctuation">,</span> <span class="hljs-number">25</span><span class="hljs-punctuation">,</span> <span class="hljs-number">36</span><span class="hljs-punctuation">,</span> <span class="hljs-number">49</span><span class="hljs-punctuation">,</span> <span class="hljs-number">64</span><span class="hljs-punctuation">,</span> <span class="hljs-number">81</span>]</pre></div><h2 id="3c97">14. Dictionary Comprehension</h2><p id="dff5">This tip is helpful to understand dictionary comprehension.</p><div id="353e"><pre><span class="hljs-attribute">dict1</span> = {x: x ** <span class="hljs-number">2</span> for x in range(<span class="hljs-number">1</span>, <span class="hljs-number">4</span>)}</pre></div><div id="cc57"><pre><span class="hljs-attribute">print</span>(dict1) #{<span class="hljs-number">1</span>: <span class="hljs-number">1</span>, <span class="hljs-number">2</span>: <span class="hljs-number">4</span>, <span class="hljs-number">3</span>: <span class="hljs-number">9</span>}</pre></div><h2 id="662c">15. Chain Operator Tip</h2><p id="93eb">This tip will help you to know what is chain operator how to use it to make your coding cleaner and faster.</p><div id="f38b"><pre><span class="hljs-attribute">num</span> <span class="hljs-operator">=</span> <span class="hljs-number">50</span></pre></div><div id="a927"><pre><span class="hljs-built_in">result</span> = <span class="hljs-number">25</span> < <span class="hljs-built_in">num</span> < <span class="hljs-number">58</span> print(<span class="hljs-built_in">result</span>) <span class="hljs-comment">#True</span></pre></div><h2 id="5995">16. Breaking String</h2><p id="af88">This trick will help you to break the string on basis of spaces. This is a fast and easy way to do that.</p><div id="288a"><pre><span class="hljs-attribute">string</span> <span class="hljs-operator">=</span> <span class="hljs-string">"Python Programming"</span></pre></div><div id="bb37"><pre><span class="hljs-built_in">print</span>(string.split()) # <span class="hljs-selector-attr">[<span class="hljs-string">'Python'</span>, <span class="hljs-string">'Programming'</span>]</span></pre></div><h2 id="5a54">17. Handle speed of your Code</h2><p id="b7a5">This tip I mostly used to handle my code speed. This tip will help you to slower your code speed.</p><div id="60ab"><pre>import <span class="hljs-built_in">time</span> <span class="hljs-built_in">time</span>.sleep(<span class="hljs-number">2</span>) # wait <span class="hljs-keyword">for</span> <span class="hljs-number">2</span> <span class="hljs-built_in">sec</span> here <span class="hljs-built_in">print</span>(<span class="hljs-string">"Python tips and tricks"</span>)</pre></div><h2 id="1e6d">18. Count Number of Word Appearance in a list</h2><p id="9fe1">This tip will count the number of times the word appears in a list. check the code example below to know how it works.</p><div id="60e3"><pre><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> Counter</pre></div><div id="ef8b"><pre>counter = Counter(['apple', 'mango', 'apple', 'pineapple', 'mango']) print(<span class="hljs-name">counter</span>) # Counter({'apple': <span

Options

class="hljs-number">2</span>, 'mango': <span class="hljs-number">2</span>, 'pineapple': <span class="hljs-number">1</span>})</pre></div><h2 id="d448">19. For loop with Else</h2><p id="908d">You had usually use for loop alone but this tip will show you how you can use for loop with else statement.</p><div id="87e2"><pre><span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(1, 4): <span class="hljs-built_in">print</span>(x) <span class="hljs-keyword">else</span>: <span class="hljs-built_in">print</span>(<span class="hljs-string">"Python"</span>)</pre></div><div id="e4e1"><pre><span class="hljs-meta">#output</span> <span class="hljs-meta"># 1</span> <span class="hljs-meta"># 2</span> <span class="hljs-meta"># 3</span> <span class="hljs-meta"># python</span></pre></div><h2 id="13c7">20. Lambda</h2><p id="bdd0">This lambda tip will help you to understand how lambda works. Lambda in Python is the user-defined one-line mini function.</p><div id="f2b4"><pre><span class="hljs-built_in">num</span> = <span class="hljs-built_in">lambda</span> x: x**<span class="hljs-number">2</span> <span class="hljs-built_in">print</span>(<span class="hljs-built_in">num</span>(<span class="hljs-number">5</span>))</pre></div><h2 id="c44f">21. Return multiple results from a function</h2><p id="2ebe">This trick will show you how you can return multiple results from the function back to the function call.</p><div id="3eee"><pre><span class="hljs-attribute">def</span> values(): <span class="hljs-attribute">return</span> <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span></pre></div><div id="c85f"><pre>print(values()) <span class="hljs-punctuation">#</span><span class="hljs-params">(1, 2, 3)</span></pre></div><h2 id="c028">22. Deep Copy</h2><p id="b106">You can do a Shallow copy in python but how you can do a deep copy ?. Well, this tip will help you do a deep copy in Python in an easy way.</p><div id="a86f"><pre><span class="hljs-keyword">import</span> copy</pre></div><div id="cf1c"><pre><span class="hljs-attr">list1</span> = [<span class="hljs-string">"ferb"</span>, <span class="hljs-string">"haider"</span>, <span class="hljs-string">"Jenny"</span>] <span class="hljs-attr">list2</span> = copy.deepcopy(list1)</pre></div><h2 id="9eb0">Final Thoughts</h2><p id="b6fe">I hope you enjoy and found these tips and tricks helpful and interesting. If I miss any important tips let me know in response. Moreover, python is a famous language and you can find the solution to the problem in a different way and it depends on which solution is suitable for you. You can leave your feedback and <b>Happy Codding!</b>.</p><p id="100e">Check out my other interesting articles on Python Programming. I hope you find them helpful also.</p><div id="2313" class="link-block"> <a href="https://python.plainenglish.io/22-useful-snippets-to-code-like-a-pro-in-python-1d0dcaacac69"> <div> <div> <h2>22 Useful Snippets to Code like a Pro in Python</h2> <div><h3>Get vowels, finding anagrams, sorting dictionary, n times string, byte sizing, filtering, etc. You will learn much more…</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*8lhZBBja11Pf4_K1IYCj-Q.png)"></div> </div> </div> </a> </div><div id="66f5" class="link-block"> <a href="https://python.plainenglish.io/best-ways-to-earn-1000-in-a-month-with-programming-fbb70cf553da"> <div> <div> <h2>7 Ways to Earn 1000 in a Month with Programming</h2> <div><h3>In this article, we will see the best ways to earn 1000 online in a month if you are a programmer. Well, you had…</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*4rfHyoDrhAlptiYq)"></div> </div> </div> </a> </div><p id="9933"><a href="https://python.plainenglish.io/build-your-first-simple-django-web-app-with-python-9a4452157089">https://python.plainenglish.io/build-your-first-simple-django-web-app-with-python-9a4452157089</a></p><div id="2c3c" class="link-block"> <a href="https://python.plainenglish.io/step-by-step-guide-to-converting-your-py-file-to-an-exe-file-f86eb0b5f253"> <div> <div> <h2>Step by Step Guide to Converting Your .py File to an .exe File</h2> <div><h3>Looking for a guide for converting your python file to an executable ( .exe ) file. In this article, we will learn…</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*GvPP9cBdy2M7VqYgge9SQQ.png)"></div> </div> </div> </a> </div><div id="90be" class="link-block"> <a href="https://python.plainenglish.io/how-to-make-a-discord-bot-using-python-4fee60648d3d"> <div> <div> <h2>How to Make your own Discord Bot in Python</h2> <div><h3>In this article, we will learn how we can make a Discord Bot step-by-step using a python programming language.</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*D8_QtneeSoS7fU8wCHq4Fw.png)"></div> </div> </div> </a> </div><div id="efc6" class="link-block"> <a href="https://python.plainenglish.io/how-to-read-and-write-to-json-file-in-python-ef35460aaeb5"> <div> <div> <h2>How to Read and Write to JSON File in Python</h2> <div><h3>JSON stands for JavaScript Object Notation. JSON is a lightweight format for storing and transferring data, even it is…</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*7egri62bf4B8ebEmK17RfA.jpeg)"></div> </div> </div> </a> </div><div id="e07b" class="link-block"> <a href="https://python.plainenglish.io/how-to-read-and-write-excel-files-in-python-3da9825e4955"> <div> <div> <h2>How to Read and Write Excel Files in Python</h2> <div><h3>Reading and Writing Excel Files in Python using the Openpyxl module.</h3></div> <div><p>python.plainenglish.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*jSZ_WhLMy0J5_Ftr8zH1og.png)"></div> </div> </div> </a> </div><p id="8ab1"><i>More content at <a href="http://plainenglish.io/">plainenglish.io</a></i></p></article></body>

Useful Tips and Tricks for Python Programmers

Python is no doubt a cool and famous language and every new developer wants to learn it. But to help you learn faster and code like a pro, I made some helpful tips and tricks for beginner programmers. That will help you to code faster, cleaner, and at a pro-level.

There are two types of developers — smart ones and good ones. These tips will make you a smart developer and a quick problem solver. This is more like a Python Tricks Course that you can get benefit from at various levels of difficulty and creativity. Following I had started to discuss our 22 helpful Python Tips and Tricks.

1. 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 = 5
b = 8
#Normal waytemp = a
a = b
b = temp#Tip and Trick waya, b = b, a

2. 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]) #nohtyp
print(string2[: : -1]) #gnimmargorp

3. 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'}

4. 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 way
a = input("Enter name: ")
b = input("Enter age: ")
print(a, b) # haider 22#Fast and Easy Way
a, b = input("Enter name and age: ").split()
print(a, b) # haider 22

5. 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.

6. 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")

7. Multiple Arguments function

This is an awesome tip and trick, let say you are creating a function and don’t know how many arguments are coming from the user so that trick will help you to accept unlimited arguments from the user.

def number(*num):
    for n in num:
        print(n)number(2,3,5,6)
number(4,5,6,2,6,7,9,10)

8. 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 getpass
usern = input('Enter username : ')
passw = getpass('Enter password : ' )

9. Calculate Execution Time

This tip will help you to measure your program running time. In simple words how much your program takes time to complete its execution.

import time
def add(a, b):
    a = a + 3
    b = b + 2
    c = a + b
    return c
startTime=time.time()
add(5,4)
endTime = time.time()
totalTime = endTime - startTime 
print(totalTime) # 0.034

10. Duplicate Removing from List

This tip and trick will help you to remove duplicates from a list in a fast and easy way. check out the code example below.

my_list = [10, 10, 22, 23, 27, 22, 89, 56, 25, 89]
#removing duplicates
my_list = list(set(my_list))
print(my_list) # [10, 22, 23, 56, 89, 27, 25]

11. Manipulating (append) Tuples

In Python you know that tuples are immutable that means you can’t append them to add new values. But this tip and trick will help you to append tuples and add new values to them.

tuple1 = (1, 2, 3)
lst= list(tuple1)
lst.append(4)
lst.append(5)
tuple1 = tuple(lst)
print(tuple1) # (1, 2, 3, 4, 5)

12. Palindrome checking

This trick is a one-line code to check the word is palindrome or not. Now you don’t need to write a long loop to check palindrome you can do it in a fast and easy way.

w = "mom"
palindrome = bool(w.find(w[: : -1]) + 1)
print(palindrome)

13. List Comprehension

This tip for list comprehension will help you to understand how it works and how it makes our work more easy and fast.

#normal way
x1= []
for x in range(1, 10):
    x1.append(x**2)
print(x1) # [1, 4, 9, 16, 25, 36, 49, 64, 81]
#List comprehension easy and fast
x1 = [x**2 for x in range(1, 10)]
print(x1) # [1, 4, 9, 16, 25, 36, 49, 64, 81]

14. Dictionary Comprehension

This tip is helpful to understand dictionary comprehension.

dict1 = {x: x ** 2 for x in range(1, 4)}
print(dict1) #{1: 1, 2: 4, 3: 9}

15. Chain Operator Tip

This tip will help you to know what is chain operator how to use it to make your coding cleaner and faster.

num = 50
result =  25 < num < 58
print(result) #True

16. Breaking String

This trick will help you to break the string on basis of spaces. This is a fast and easy way to do that.

string = "Python Programming"
print(string.split()) # ['Python', 'Programming']

17. Handle speed of your Code

This tip I mostly used to handle my code speed. This tip will help you to slower your code speed.

import time
time.sleep(2) # wait for 2 sec here 
print("Python tips and tricks")

18. Count Number of Word Appearance in a list

This tip will count the number of times the word appears in a list. check the code example below to know how it works.

from collections import Counter
counter = Counter(['apple', 'mango', 'apple', 'pineapple', 'mango'])
print(counter) # Counter({'apple': 2, 'mango': 2, 'pineapple': 1})

19. For loop with Else

You had usually use for loop alone but this tip will show you how you can use for loop with else statement.

for x in range(1, 4):
    print(x)
else:
    print("Python")
#output
# 1
# 2
# 3
# python

20. Lambda

This lambda tip will help you to understand how lambda works. Lambda in Python is the user-defined one-line mini function.

num = lambda x: x**2
print(num(5))

21. 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, 3
print(values()) #(1, 2, 3)

22. Deep Copy

You can do a Shallow copy in python but how you can do a deep copy ?. Well, this tip will help you do a deep copy in Python in an easy way.

import copy
list1 = ["ferb", "haider", "Jenny"]
list2 = copy.deepcopy(list1)

Final Thoughts

I hope you enjoy and found these tips and tricks helpful and interesting. If I miss any important tips let me know in response. Moreover, python is a famous language and you can find the solution to the problem in a different way and it depends on which solution is suitable for you. You can leave your feedback and Happy Codding!.

Check out my other interesting articles on Python Programming. I hope you find them helpful also.

https://python.plainenglish.io/build-your-first-simple-django-web-app-with-python-9a4452157089

More content at plainenglish.io

Python
Programming
Coding
Software Development
Data Science
Recommended from ReadMedium