avatarHaider Imtiaz

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

7711

Abstract

=</span> <span class="hljs-number">2</span> <span class="hljs-attribute">c</span> <span class="hljs-operator">=</span> <span class="hljs-number">4</span></pre></div><div id="e085"><pre><span class="hljs-built_in">print</span>(a > 10 > b) # <span class="hljs-literal">false</span> <span class="hljs-built_in">print</span>(a < 10 > b) # <span class="hljs-literal">True</span> <span class="hljs-built_in">print</span>(a < 12 < b > c) # <span class="hljs-literal">False</span></pre></div><h1 id="3d43">👉8# — Multiple Assignments</h1><p id="88ca">Language such as C, Java, C++ allows declaring a single variable with a single value at a time. But in Python you can do multiple assignments. In simple words declaring multiple variables and assign them at the same time.</p><div id="c824"><pre><span class="hljs-attribute">a</span>, b = <span class="hljs-number">4</span>, <span class="hljs-number">5</span></pre></div><div id="6f17"><pre><span class="hljs-attribute">a</span>, b, c, d = <span class="hljs-number">1</span> , <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span></pre></div><div id="b832"><pre><span class="hljs-comment">#multiple list assignment to varible</span></pre></div><div id="2eed"><pre>l = <span class="hljs-selector-attr">[1, 2]</span> <span class="hljs-selector-tag">a</span> , <span class="hljs-selector-tag">b</span> = l</pre></div><h1 id="5484">👉9# — For/Else statement</h1><p id="90fe">You are familiar with Conditional Statements in Python. But do you know about the For else clause ?.</p><div id="a6d4"><pre><span class="hljs-attr">a</span> = [<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><div id="c1e9"><pre><span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> a: <span class="hljs-keyword">if</span> x == <span class="hljs-number">0</span>: <span class="hljs-keyword">break</span> <span class="hljs-keyword">else</span>: <span class="hljs-keyword">print</span>(<span class="hljs-string">"0 is not found in list"</span>)</pre></div><p id="6e00">This code will run the else statement when the for loop is successfully run without any break. If we had 0 in our list that means if the statement is true and then in that case else statement will not execute.</p><h1 id="d4a5">👉10# —Negative Indexing</h1><p id="c541">In Programming language you know we can access data with 0 to any positive number. But Python allows to access the data with a negative index. Negative indexing access data from backward.</p><div id="8c5c"><pre><span class="hljs-comment"># negative index</span> <span class="hljs-attr">lst</span> = [<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><div id="5a0f"><pre><span class="hljs-attribute">print</span>(lst[-<span class="hljs-number">5</span>]) # <span class="hljs-number">1</span></pre></div><div id="95c4"><pre><span class="hljs-attribute">print</span>(lst[-<span class="hljs-number">1</span>]) # <span class="hljs-number">5</span></pre></div><div id="9675"><pre><span class="hljs-attribute">print</span>(lst[-<span class="hljs-number">3</span>]) # <span class="hljs-number">3</span></pre></div><h1 id="1d52">👉11# — Emoji in Python</h1><p id="dbcc">Do you know python allows you to print emoji as a string? You can print specific emojis by Unicode or CLDR names. Check out the below code example.</p><div id="2b80"><pre><span class="hljs-meta">#By Unicodes</span></pre></div><div id="21d8"><pre><span class="hljs-comment"># grinning face</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">"\U0001f600"</span>)

<span class="hljs-comment"># grinning squinting face</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">"\U0001F606"</span>)</pre></div><div id="8cb0"><pre><span class="hljs-meta">#By CLDR Names</span></pre></div><div id="a2e4"><pre><span class="hljs-comment"># grinning face</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">"\N{grinning face}"</span>)

<span class="hljs-comment"># slightly smiling face</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">"\N{slightly smiling face}"</span>)</pre></div><h1 id="e535">👉12# — Sequence Multiplication</h1><p id="4a4d">You can double the data in Python by multiplying with any positive number. Take a look at the below code to know how it works.</p><div id="0bfa"><pre>d = <span class="hljs-string">"abc"</span> * <span class="hljs-number">3</span> <span class="hljs-keyword">print</span>(d) <span class="hljs-meta"># abcabcabc</span></pre></div><div id="9e64"><pre>d = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>] * <span class="hljs-number">2</span> <span class="hljs-keyword">print</span>(d) <span class="hljs-meta"># [1, 2, 3, 1, 2, 3]</span></pre></div><h1 id="cd3e">👉13# — Zen of Python</h1><p id="b287">Did you know Python had a secrete module name “this” which shows the Zen of Python? According to <a href="https://en.wikipedia.org/wiki/Zen_of_Python"><b>Wikipedia</b></a>, it is a collection of 19 “guiding principles” for writing computer programs that influence the design of the Python programming language.</p><div id="ab58"><pre>The Zen <span class="hljs-keyword">of</span> Python, <span class="hljs-keyword">by</span> Tim Peters</pre></div><div id="43e3"><pre>Beautiful <span class="hljs-keyword">is</span> better than ugly. Explicit <span class="hljs-keyword">is</span> better than implicit. Simple <span class="hljs-keyword">is</span> better than complex. Complex <span class="hljs-keyword">is</span> better than complicated. Flat <span class="hljs-keyword">is</span> better than nested. Sparse <span class="hljs-keyword">is</span> better than dense. Readability counts. Special cases aren't special enough <span class="hljs-keyword">to</span> break <span class="hljs-keyword">the</span> rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In <span class="hljs-keyword">the</span> face <span class="hljs-keyword">of</span> ambiguity, refuse <span class="hljs-keyword">the</span> temptation <span class="hljs-keyword">to</span> guess. There should be one<span class="hljs-comment">-- and preferably only one --obvious way to do it.</span> Although <span class="hljs-keyword">that</span> way may <span class="hljs-keyword">not</span> be obvious <span class="hljs-keyword">at</span> <span class="hljs-keyword">first</span> unless you're Dutch. Now <span class="hljs-keyword">is</span> better than never. Although never <span class="hljs-keyword">is</span> often better than right now. If <span class="hljs-keyword">the</span> implementation <span class="hljs-keyword">is</span> hard <span class="hljs-keyword">to</span> explain, <span class="hljs-keyword">it</span>'s a bad idea. If <span class="hljs-keyword">the</span> implementation <span class="hljs-keyword">is</span> easy <span class="hljs-keyword">to</span> explain, <span class="hljs-keyword">it</span> may be a good idea. Namespaces are one honking great idea <span class="hljs-comment">-- let's do more of those!</span></pre></div><h1 id="cac4">👉14# — Flatten List with Sum</h1><p id="963a">You Should know about flattening a list with a map() built-in function. But Python had a feature to do the same work with the sum() built-in function.</p><div id="410d"><pre>lst = <span class="hljs-string">[[1,

Options

3, 4], [3, 34, 9, 0, 1], [94 ,5]]</span></pre></div><div id="e5f5"><pre><span class="hljs-keyword">print</span>(<span class="hljs-keyword">sum</span>(lst,[])) <span class="hljs-meta"># [1, 3, 4, 3, 34, 9, 0, 1, 94, 5]</span></pre></div><div id="513c"><pre><span class="hljs-comment">#Note this feature not work on highly unordered list</span></pre></div><h1 id="1beb">👉15# — Anti-Gravity</h1><p id="d931">Do you know Python had a hidden module name antigravity. When you import that library and execute the code. Python will open your browser and redirect you to a website. Want to know about the website, Try out the following code now.</p><div id="be3b"><pre><span class="hljs-keyword">import</span> antigravity</pre></div><h1 id="9586">Final Thoughts</h1><p id="c321">Well Python is no doubt is an awesome programming language and it’s fun to try some crazy fun stuff on it. I hope you find those features interesting and fun to learn. Feel free to share your response or a snippet that you believe is useful for Python Programmers.</p><p id="a7a6">If you found this article helpful, click the ❤️ button below and share the article on Facebook or Twitter, or any of your social media so your friends can also benefit from it too.</p><h1 id="7c7b">Learn More</h1><p id="5aef">If you had enjoyed this article, then you should take a look at my other programming articles.</p><div id="8e60" class="link-block"> <a href="https://levelup.gitconnected.com/25-useful-python-snippets-for-everyday-problems-4e1a74d1abae"> <div> <div> <h2>25 Useful Python Snippets for Everyday Problems</h2> <div><h3>Here is my 25 useful and time-saving snippets for your everyday Python Problems</h3></div> <div><p>levelup.gitconnected.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*45obi6lIPEGUefPLw3Pk-g.png)"></div> </div> </div> </a> </div><div id="43b4" class="link-block"> <a href="https://levelup.gitconnected.com/20-essential-snippets-to-code-like-a-pro-in-javascript-c7a6ef4dbddc"> <div> <div> <h2>20 Essential Snippets to Code like a Pro in JavaScript</h2> <div><h3>20 JavaScript Snippets You Can Learn in 30 Seconds or Less</h3></div> <div><p>levelup.gitconnected.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*mXw6ODgnWvTM12RwNgimHg.jpeg)"></div> </div> </div> </a> </div><div id="7fe9" class="link-block"> <a href="https://levelup.gitconnected.com/20-ways-to-make-money-online-while-learning-to-code-9aec753b742d"> <div> <div> <h2>20 Ways to Make Money Online While Learning to Code</h2> <div><h3>If you are a programmer and not making any money online then you missing a big opportunity</h3></div> <div><p>levelup.gitconnected.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*BP_A-Daqvni1nCIe2JRC7w.jpeg)"></div> </div> </div> </a> </div><div id="4eb7" class="link-block"> <a href="https://levelup.gitconnected.com/a-beginners-guide-to-natural-language-processing-in-python-using-nltk-6e4692b825d4"> <div> <div> <h2>A Beginners Guide to Natural Language Processing in Python using NLTK</h2> <div><h3>Natural Language Processing is a subfield of Artificial Intelligence (AI), Which helps the computer to understand the…</h3></div> <div><p>levelup.gitconnected.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*lr-drsYoCmD4auCvr5MHCw.jpeg)"></div> </div> </div> </a> </div><div id="51e4" class="link-block"> <a href="https://levelup.gitconnected.com/a-beginners-guide-to-tesseract-ocr-using-pytesseract-23036f5b2211"> <div> <div> <h2>A Beginners Guide To Tesseract OCR Using Pytesseract</h2> <div><h3>Optical character recognition or optical character reader (OCR) is the electronic or mechanical conversion of images of…</h3></div> <div><p>levelup.gitconnected.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*fp_lg2Tb-8OwvIcT)"></div> </div> </div> </a> </div><div id="bd8e" class="link-block"> <a href="https://levelup.gitconnected.com/master-object-oriented-programming-oop-in-python-3-c69a1e8a6d3d"> <div> <div> <h2>Master Object Oriented Programming (OOP) in Python</h2> <div><h3>Learn to write cleaner, more modular code in Python by gaining a master of Object Oriented Programming (OOP).</h3></div> <div><p>levelup.gitconnected.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*LZgI_tQFyE9rCh1B)"></div> </div> </div> </a> </div><div id="c44c" class="link-block"> <a href="https://levelup.gitconnected.com/how-to-make-your-python-code-run-10x-times-faster-5690f5d4d7aa"> <div> <div> <h2>How to Make your python code run 10x times faster</h2> <div><h3>A simple tips and guide to making your python code run 10x faster</h3></div> <div><p>levelup.gitconnected.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*O2JZoK7i_58cfv9hKXaPZw.jpeg)"></div> </div> </div> </a> </div><div id="b7c7" class="link-block"> <a href="https://levelup.gitconnected.com/build-a-desktop-app-with-python-4a847e3b596c"> <div> <div> <h2>Building Desktop Applications with Tkinter and Python</h2> <div><h3>In this article, we will learn how we can develop a modern desktop application using python and Tkinter modules. A…</h3></div> <div><p>levelup.gitconnected.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*jbcwR3BOrRVUV9MI)"></div> </div> </div> </a> </div><div id="04ad" class="link-block"> <a href="https://levelup.gitconnected.com/how-to-work-with-a-pdf-in-python-a1e0c1d127a4"> <div> <div> <h2>Reading and Editing PDF’s Documents Using Python</h2> <div><h3>In this article, we will learn about how we can use python pdf modules to read and write the pdf files. PyPDF2 is an…</h3></div> <div><p>levelup.gitconnected.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*P5XRqcTZR9QEwNuyPYVb3g.jpeg)"></div> </div> </div> </a> </div></article></body>

15 Python Features You’ve Probably Never Used

Most people don’t know these incredible features of Python.

Photo by krakenimages on Unsplash

Today in this article I will show you the 15+ hidden features of Python you probably don’t know. These features I discovered from many websites and read many answers from StackOverflow and get the most useful features for you.

👉1# — Function Argument Unpacking

Do you know you can unpack a list or dictionary as a function argument using * and ** . This is very useful when passing a tuple, list, and dictionary as arguments containers in function.

def fun(x, y):
    # do something
    print(x, y)
foo1 = [2, 5]
foo2 = {'x':2, 'y':5}
fun(*foo1)
fun(*foo2)

👉2# —String Style Formating

You can do String style formating using format method and the keyword f. In the format method, you pass a dictionary which key value will replace in the String by match key in the curly braces {}.

#method 1
print("I'm a {foo} and {bar} Developer".format(foo="Programmer", bar="Android"))
#method 2
foo = "Programmer"
bar = "Android"
print(f"I'm a {foo} and {bar} Developer")

👉3# — In place Value Swapping

Normally you swap values with a help of a third-party variable temp. But you can do this in an easy and fast way without using any temp variable.

# old method
a = 5
b = 6
temp = a
a = b
b = a
print(a, b) # 6  5
# new method
a = 5
b = 6
a, b = b, a
print(a, b) # 6  5

👉4# — Dictionary Get() Method

Dictionary has a get() method which had the same functionality as same we access the data of the dictionary. But in benefit, unlike dictionary bracket value access method which give exception when a key is not available, Get method returns None.

dic={1 :2, 3: 5, 6:8}
print(dic[8]) #IT will throw an Error
print(dic.get(8)) # None

👉5# — Step Argument

The step argument is used as a slice operator to slice a list. Take a look at the below to know how it works.

a = [1, 2, 3, 4, 5, 6, 7, 8]
print(a[::2]) # [1, 3, 5, 7]
print(a[::3]) # [1, 4, 7]
#reverse a string
print(a[::-1]) # [8, 7, 6, 5, 4, 3, 2, 1]

👉6# — List Comprehension

List comprehension in my opinion is the best and fastest way to iterate and putting conditions on the loop. Within the single line code, you can do multiple things.

# finding even numbers from a list
a  = [1, 2, 3, 4, 5, 6]
result = [even for even in a if even % 2 == 0]
print(result) # [2, 4, 6]

👉7# — Chain Comparison

Chain comparison is an interesting feature of Python to do multiple comparisons in a chain. Let say you do x < 10 and next, you do 10 > y but with chain comparison, you can do this in a single line.

a = 5
b = 2
c = 4
print(a > 10 > b) # false
print(a < 10 > b) # True
print(a < 12 < b > c) # False

👉8# — Multiple Assignments

Language such as C, Java, C++ allows declaring a single variable with a single value at a time. But in Python you can do multiple assignments. In simple words declaring multiple variables and assign them at the same time.

a, b = 4, 5
a, b, c, d = 1 , 2, 3, 4
#multiple list assignment to varible
l = [1, 2]
a , b = l

👉9# — For/Else statement

You are familiar with Conditional Statements in Python. But do you know about the For else clause ?.

a = [1, 2, 3, 4, 5]
for x in a:
    if x == 0:
        break
else:
    print("0 is not found in list")

This code will run the else statement when the for loop is successfully run without any break. If we had 0 in our list that means if the statement is true and then in that case else statement will not execute.

👉10# —Negative Indexing

In Programming language you know we can access data with 0 to any positive number. But Python allows to access the data with a negative index. Negative indexing access data from backward.

# negative index
lst = [1, 2, 3, 4, 5]
print(lst[-5]) # 1
print(lst[-1]) # 5
print(lst[-3]) # 3

👉11# — Emoji in Python

Do you know python allows you to print emoji as a string? You can print specific emojis by Unicode or CLDR names. Check out the below code example.

#By Unicodes
# grinning face
print("\U0001f600")
  
# grinning squinting face
print("\U0001F606")
#By CLDR Names
# grinning face
print("\N{grinning face}")
  
# slightly smiling face
print("\N{slightly smiling face}")

👉12# — Sequence Multiplication

You can double the data in Python by multiplying with any positive number. Take a look at the below code to know how it works.

d =  "abc" * 3
print(d) # abcabcabc
d = [1,2,3] * 2
print(d) # [1, 2, 3, 1, 2, 3]

👉13# — Zen of Python

Did you know Python had a secrete module name “this” which shows the Zen of Python? According to Wikipedia, it is a collection of 19 “guiding principles” for writing computer programs that influence the design of the Python programming language.

The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

👉14# — Flatten List with Sum

You Should know about flattening a list with a map() built-in function. But Python had a feature to do the same work with the sum() built-in function.

lst = [[1, 3, 4], [3, 34, 9, 0, 1], [94 ,5]]
print(sum(lst,[])) # [1, 3, 4, 3, 34, 9, 0, 1, 94, 5]
#Note this feature not work on highly unordered list

👉15# — Anti-Gravity

Do you know Python had a hidden module name antigravity. When you import that library and execute the code. Python will open your browser and redirect you to a website. Want to know about the website, Try out the following code now.

import antigravity

Final Thoughts

Well Python is no doubt is an awesome programming language and it’s fun to try some crazy fun stuff on it. I hope you find those features interesting and fun to learn. Feel free to share your response or a snippet that you believe is useful for Python Programmers.

If you found this article helpful, click the ❤️ button below and share the article on Facebook or Twitter, or any of your social media so your friends can also benefit from it too.

Learn More

If you had enjoyed this article, then you should take a look at my other programming articles.

Python
Programming
Coding
Software Development
Education
Recommended from ReadMedium