<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Mohit's Blog]]></title><description><![CDATA[Hello👋, I am Mohit Tanwani, a Full stack engineer 💻. Just like all other coders, even I like to share stuff! So here I am.😉]]></description><link>https://blog.mohit.rocks</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1626120906227/8Lr1HNrms.png</url><title>Mohit&apos;s Blog</title><link>https://blog.mohit.rocks</link></image><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Apr 2026 21:12:12 GMT</lastBuildDate><atom:link href="https://blog.mohit.rocks/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Lazy Imports in Java?🙂‍↔️]]></title><description><![CDATA[I am an experienced Python user, and I love how everything is built-in. One feature I use daily is lazy imports. Recently, I was working with Java and wanted to do something similar. Although I couldn't find any good articles on the topic, I managed ...]]></description><link>https://blog.mohit.rocks/lazy-imports-in-java</link><guid isPermaLink="true">https://blog.mohit.rocks/lazy-imports-in-java</guid><category><![CDATA[Java]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Mohit Tanwani]]></dc:creator><pubDate>Wed, 03 Jul 2024 13:46:33 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1720014277698/223a7218-1fd6-4739-b7f0-cc6b9e2ab4d5.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I am an experienced Python user, and I love how everything is built-in. One feature I use daily is lazy imports. Recently, I was working with Java and wanted to do something similar. Although I couldn't find any good articles on the topic, I managed to figure it out. Here is a sample implementation based on the bcrypt library use case.</p>
<p>Before using lazy import, I would import the function at the top and use it later in the function.</p>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> at.favre.lib.crypto.bcrypt.BCrypt;  

<span class="hljs-comment">// Using it somewhere in the program</span>
String password = “Test@<span class="hljs-number">123</span>”
String storedHashString = <span class="hljs-string">"$2RandomHashString”
isValid = BCrypt.verifyer().verify(password.toCharArray(), storedHashString.toCharArray()).verified</span>
</code></pre>
<p>Above, you can see example code where I am using the Bcrypt library's functions to verify a password. However, I wanted to import Bcrypt lazily. Here is what I did:</p>
<pre><code class="lang-java"><span class="hljs-comment">// Directly wrote this where I wanted to use this function.</span>
Class&lt;?&gt; bCryptClass = Class.forName(<span class="hljs-string">"at.favre.lib.crypto.bcrypt.BCrypt"</span>); <span class="hljs-comment">// Get the instance of the class</span>
Method verifyerMethod = bCryptClass.getMethod(<span class="hljs-string">"verifyer"</span>); <span class="hljs-comment">//Get the method  from class.</span>
Object verifyer = verifyerMethod.invoke(<span class="hljs-keyword">null</span>); <span class="hljs-comment">// Invoke verifyer method and store instance in the variable.</span>
Method verifyMethod = verifyer.getClass().getMethod(<span class="hljs-string">"verify"</span>, <span class="hljs-keyword">char</span>[].class, <span class="hljs-keyword">char</span>[].class);
Object result = verifyMethod.invoke(verifyer, password.toCharArray(), storedHashString.toCharArray()); <span class="hljs-comment">// Final invoke the verify method.</span>
Boolean ans = (Boolean) result.getClass().getField(<span class="hljs-string">"verified"</span>).get(result); <span class="hljs-comment">// get the verified field.</span>
</code></pre>
<p>Few things to notice here:</p>
<ul>
<li><p>I didn’t import the Bcrypt library at the top.</p>
</li>
<li><p>It will be imported at runtime. If the jar file is not present, it will throw a ClassNotFoundException. So, add a try-catch block if needed.</p>
</li>
<li><p>My goal was to achieve this because, in my case, the library might or might not exist in the environment.</p>
</li>
</ul>
<p>Let me know if you have a better way to do this. I hope you find it helpful.<br />P.S. I'm a noob at Java, so don't quote me on this :P</p>
]]></content:encoded></item><item><title><![CDATA[CSS + BEM Methodology = 🤘]]></title><description><![CDATA[We all follow naming conventions in programming languages, don't we? Now, what about CSS? Is there any need of naming convention in that? Of course! Maybe not in a small project, but when you start working on large projects you should organize your c...]]></description><link>https://blog.mohit.rocks/css-bem-methodology</link><guid isPermaLink="true">https://blog.mohit.rocks/css-bem-methodology</guid><category><![CDATA[HTML]]></category><category><![CDATA[CSS]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Tutorial]]></category><dc:creator><![CDATA[Mohit Tanwani]]></dc:creator><pubDate>Tue, 20 Jul 2021 07:53:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1626767486776/IeJkSN7Gq.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>We all follow naming conventions in programming languages, don't we? Now, what about CSS? Is there any need of naming convention in that? Of course! Maybe not in a small project, but when you start working on large projects you should organize your code properly so that anyone can easily get the gist of the functionality by just looking into the code.</p>
<blockquote>
<p>Great codebases don't need comments. </p>
</blockquote>
<h2 id="what-is-bem">What is BEM?</h2>
<p>BEM stands for Block Element Modifier. It is a Methodology that every web developer should follow. This makes development easy and enforces re-usability.
Enough of the theory Let's understand this by an example 😉</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1626767452964/26Z2bvXHM.png" alt="BEM" />
Here We can say,</p>
<ul>
<li>Form = Block(An Independent meaningful entity)</li>
<li>Input, Link, Button = Element(Part of Black, Which has no standalone meaning)</li>
<li>Button types (primary/secondary) = Modifier(Represent state of Block or Element.) </li>
</ul>
<h2 id="bem-methodology">BEM Methodology</h2>
<p>Let's continue with the above example. This is how you will name your CCS classes.</p>
<h3 id="for-block">For Block:</h3>
<pre><code><span class="hljs-selector-class">.form</span> {
   <span class="hljs-comment">// Your CSS</span>
}
</code></pre><h3 id="for-element">For Element:</h3>
<pre><code><span class="hljs-selector-class">.form__input</span> {
   <span class="hljs-comment">// Your CSS</span>
}
<span class="hljs-selector-class">.form__button</span> {
   <span class="hljs-comment">// Your CSS</span>
}
<span class="hljs-selector-class">.form__link</span> {
   <span class="hljs-comment">// Your CSS</span>
}
</code></pre><h3 id="for-modifier">For Modifier:</h3>
<pre><code><span class="hljs-selector-class">.form__button--primary</span> {
   <span class="hljs-attribute">background</span>: <span class="hljs-string">"blue"</span>;
}
<span class="hljs-selector-class">.form__button--secondary</span> {
   <span class="hljs-attribute">background</span>: <span class="hljs-string">"green"</span>;
}
</code></pre><p><strong>NOTE: When using modifiers you will put all the common styles in <code>form__button</code> so that you don't repeat them again</strong>
And here is how your HTML will look like:</p>
<pre><code>&lt;form <span class="hljs-keyword">class</span>="form"&gt;
   &lt;<span class="hljs-keyword">input</span> <span class="hljs-keyword">type</span>="email" <span class="hljs-keyword">class</span>="form__input" placeholder="Email"&gt;
   &lt;<span class="hljs-keyword">input</span> <span class="hljs-keyword">type</span>="email" <span class="hljs-keyword">class</span>="form__input" placeholder="Email"&gt;
   &lt;button <span class="hljs-keyword">class</span>="form__button form__button--primary"&gt;<span class="hljs-keyword">Login</span>&lt;/button&gt;
   &lt;button <span class="hljs-keyword">class</span>="form__button form__button--secondary"&gt;Signup&lt;/button&gt;
&lt;/form&gt;
</code></pre><p>That's it! I know it looks like an overhead but trusts me it will help you a lot in many different ways.</p>
]]></content:encoded></item><item><title><![CDATA[Python Cheatsheet 🔥]]></title><description><![CDATA[Python Cheatsheet
Cheatsheets are good when you want to revise some of the concepts, but not an idle way to start learning 
I would recommend you to learn in depth from this course: Udemy Course
If you can self learn then this Repository: Github
Inde...]]></description><link>https://blog.mohit.rocks/python-cheatsheet</link><guid isPermaLink="true">https://blog.mohit.rocks/python-cheatsheet</guid><category><![CDATA[Python]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[cheatsheet]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Mohit Tanwani]]></dc:creator><pubDate>Mon, 12 Jul 2021 15:13:13 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1626121922009/a0ptvM-EO.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="python-cheatsheet">Python Cheatsheet</h1>
<p>Cheatsheets are good when you want to revise some of the concepts, but not an idle way to start learning <br />
I would recommend you to learn in depth from this course: <a target="_blank" href="https://www.udemy.com/course/complete-python-bootcamp/">Udemy Course</a>
<br />If you can self learn then this Repository: <a target="_blank" href="https://github.com/Pierian-Data/Complete-Python-3-Bootcamp">Github</a></p>
<h2 id="index">Index</h2>
<ul>
<li><a class="post-section-overview" href="#python-cheatsheet">Python Cheatsheet</a><ul>
<li><a class="post-section-overview" href="#index">Index</a></li>
<li><a class="post-section-overview" href="#theory">Theory</a></li>
<li><a class="post-section-overview" href="#vscode-extension">VSCODE extension:</a></li>
<li><a class="post-section-overview" href="#making-a-virtual-env">Making a virtual env:</a><ul>
<li><a class="post-section-overview" href="#why-virtual-env">Why Virtual env?</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#comments-in-python">Comments in python</a></li>
<li><a class="post-section-overview" href="#data-types">Data Types:</a></li>
<li><a class="post-section-overview" href="#naming-conventions">Naming conventions:</a></li>
<li><a class="post-section-overview" href="#printing-in-python">Printing in Python:</a></li>
<li><a class="post-section-overview" href="#numbers-in-python">Numbers in Python:</a></li>
<li><a class="post-section-overview" href="#using-with-variables">Using with variables:</a><ul>
<li><a class="post-section-overview" href="#variables-makes-it-easy-to-understands-the-code">Variables makes it easy to understands the code.</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#strings-in-python">Strings in Python:</a><ul>
<li><a class="post-section-overview" href="#using-directly-with-print">Using directly with print:</a></li>
<li><a class="post-section-overview" href="#taking-input">Taking Input</a></li>
<li><a class="post-section-overview" href="#use--instead-of--this-will-auto-seprate-them-by-spaces">Use <code>,</code> instead of <code>+</code> This will auto seprate them by spaces.</a></li>
<li><a class="post-section-overview" href="#escape-characters-in-python">Escape characters in python:</a></li>
<li><a class="post-section-overview" href="#check-the-length-of-string">Check the length of string:</a></li>
<li><a class="post-section-overview" href="#string-indexing">String indexing</a></li>
<li><a class="post-section-overview" href="#string-slicing">String Slicing</a></li>
<li><a class="post-section-overview" href="#string-methods">String methods:</a></li>
<li><a class="post-section-overview" href="#formatting-strings">Formatting strings:</a></li>
<li><a class="post-section-overview" href="#string-print-alignment">String Print alignment</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#lists-in-python">Lists in Python:</a><ul>
<li><a class="post-section-overview" href="#basic-usage">Basic Usage</a></li>
<li><a class="post-section-overview" href="#concat">Concat</a></li>
<li><a class="post-section-overview" href="#append-the-list">Append the list</a></li>
<li><a class="post-section-overview" href="#poping-objs">Poping objs</a></li>
<li><a class="post-section-overview" href="#sorting">Sorting</a></li>
<li><a class="post-section-overview" href="#reverse">Reverse</a></li>
<li><a class="post-section-overview" href="#nested-list">Nested list</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#dictionaries-in-python">Dictionaries in Python</a><ul>
<li><a class="post-section-overview" href="#basic-usage-1">Basic Usage</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#tuples-in-python">Tuples in Python</a><ul>
<li><a class="post-section-overview" href="#basic-usage-2">Basic Usage</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#sets-in-python">Sets in Python</a><ul>
<li><a class="post-section-overview" href="#convert-list-to-set">Convert list to set</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#file-io-with-python">File IO with Python</a><ul>
<li><a class="post-section-overview" href="#init-file-obj">init file obj</a></li>
<li><a class="post-section-overview" href="#read-contents">read contents</a></li>
<li><a class="post-section-overview" href="#move-the-cursorpointer">move the cursor/pointer</a></li>
<li><a class="post-section-overview" href="#read-line-by-line">read line by line</a></li>
<li><a class="post-section-overview" href="#close-the-file">close the file</a></li>
<li><a class="post-section-overview" href="#using-context-manager">Using context manager</a></li>
<li><a class="post-section-overview" href="#different-modes-for-file">different modes for file</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#chaining-comparison-operators">Chaining comparison operators:</a></li>
<li><a class="post-section-overview" href="#python-statements">Python Statements:</a><ul>
<li><a class="post-section-overview" href="#if-elif-else">if, elif, else</a></li>
<li><a class="post-section-overview" href="#for-loops">for loops</a></li>
<li><a class="post-section-overview" href="#while-loops">while loops</a></li>
<li><a class="post-section-overview" href="#statement-in-python">Statement in python</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#some-useful-operators">Some useful operators</a><ul>
<li><a class="post-section-overview" href="#range">range()</a></li>
<li><a class="post-section-overview" href="#enumerate">enumerate()</a></li>
<li><a class="post-section-overview" href="#zip">zip()</a></li>
<li><a class="post-section-overview" href="#in-operator">in operator:</a></li>
<li><a class="post-section-overview" href="#min-and-max">min and max:</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#list-comprehensions">List Comprehensions</a></li>
<li><a class="post-section-overview" href="#help-function-in-python">help function in python</a></li>
<li><a class="post-section-overview" href="#functions-in-python">Functions in python</a><ul>
<li><a class="post-section-overview" href="#basic-function-with-argument-and-default-value">Basic function with argument and default value</a></li>
<li><a class="post-section-overview" href="#args-and-kwargs"><em>args and *</em>kwargs</a></li>
<li><a class="post-section-overview" href="#lamda-filter-and-map">lamda, filter and map</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#classes-in-python">Classes in python</a><ul>
<li><a class="post-section-overview" href="#basic-implementation">Basic implementation</a></li>
<li><a class="post-section-overview" href="#inheritance">Inheritance</a></li>
<li><a class="post-section-overview" href="#polymorphism">Polymorphism</a></li>
<li><a class="post-section-overview" href="#using-special-methods">Using Special methods</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#exception-handling">Exception Handling</a><ul>
<li><a class="post-section-overview" href="#try-except-finally-and-else">try, except, finally, and else.</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#decorators">Decorators</a></li>
<li><a class="post-section-overview" href="#generators">Generators</a></li>
<li><a class="post-section-overview" href="#useful-python-modules-you-should-look">Useful Python modules you should look.</a></li>
<li><a class="post-section-overview" href="#working-with-csvs-in-python">Working with CSVs in python</a></li>
<li><a class="post-section-overview" href="#working-with-pdfs-in-python">Working with pdfs in python</a></li>
<li><a class="post-section-overview" href="#sending-emails-with-python">Sending Emails with python</a></li>
</ul>
</li>
</ul>
<h2 id="theory">Theory</h2>
<ul>
<li>Python is a scripting language.</li>
<li><strong>Scripting vs Compiled ?</strong> - Language like c++/java's code needs to compiled by its compiler, after compilation it is just machine level code.
Where as in a scripting language its interpreter will be run the code one the spot one line at a time.</li>
</ul>
<h2 id="vscode-extension">VSCODE extension:</h2>
<ul>
<li>Python</li>
<li>Python for vscode</li>
<li>Magic Python</li>
<li>Arepl</li>
</ul>
<h2 id="making-a-virtual-env">Making a virtual env:</h2>
<h3 id="why-virtual-env"><strong>Why Virtual env?</strong></h3>
<p>Because managing python dependencies is a mess, this will install dependencies for that project only instead of globally.</p>
<ul>
<li><code>python -m venv venv</code> this will create a venv folder in your directory.</li>
<li><code>source ./venv/bin/activate</code> this will activate this virtual env.</li>
</ul>
<h2 id="comments-in-python">Comments in python</h2>
<ul>
<li>single line use <code>#</code><pre><code class="lang-python"><span class="hljs-comment"># single line comments</span>
</code></pre>
</li>
<li>multiline use <code>'''</code>. often called as docstring, as it is just to document function/classes. <pre><code class="lang-python"><span class="hljs-string">'''
This is a example of 
Multiline comment.
'''</span>
</code></pre>
</li>
</ul>
<h2 id="data-types">Data Types:</h2>
<ul>
<li>int # Whole Numbers</li>
<li>float # Number with decimals.</li>
<li>str # String</li>
<li>list # ordered sequence of object</li>
<li>dict # unordered key-value pairs.</li>
<li>tup # ordered immutable seq. of objects.</li>
<li>set # Unordered collection of unique objs.</li>
<li>bool # Logical value True / False.</li>
<li>None # no value</li>
</ul>
<h2 id="naming-conventions">Naming conventions:</h2>
<ul>
<li>Use underscore for variables.</li>
<li>variables cannot Start with a number.</li>
<li>Avoid special meaning keywords.</li>
<li>Use snake case for functions.</li>
<li>Use CamelCase for Classes names.</li>
</ul>
<h2 id="printing-in-python">Printing in Python:</h2>
<pre><code class="lang-python">print(<span class="hljs-string">""</span>)
</code></pre>
<h2 id="numbers-in-python">Numbers in Python:</h2>
<pre><code class="lang-python">print(<span class="hljs-number">1</span>+<span class="hljs-number">2</span>) <span class="hljs-comment"># Addition</span>
print(<span class="hljs-number">3</span><span class="hljs-number">-2</span>) <span class="hljs-comment"># Subtraction</span>
print(<span class="hljs-number">3</span>*<span class="hljs-number">2</span>) <span class="hljs-comment"># Multiplication</span>
print(<span class="hljs-number">3</span>/<span class="hljs-number">2</span>) <span class="hljs-comment"># Division</span>
print(<span class="hljs-number">3</span>%<span class="hljs-number">2</span>) <span class="hljs-comment"># Mod.</span>
print(<span class="hljs-number">3</span>**<span class="hljs-number">2</span>) <span class="hljs-comment"># Power</span>
print((<span class="hljs-number">3</span> + <span class="hljs-number">10</span>) * <span class="hljs-number">15</span>) <span class="hljs-comment"># Using Braces.</span>
</code></pre>
<h2 id="using-with-variables">Using with variables:</h2>
<pre><code class="lang-python">a = <span class="hljs-number">10</span>
print(a)
<span class="hljs-comment"># TO check the type:</span>
print(type(a))
</code></pre>
<h3 id="variables-makes-it-easy-to-understands-the-code">Variables makes it easy to understands the code.</h3>
<pre><code class="lang-python">my_income = <span class="hljs-number">100</span>
tax_rate = <span class="hljs-number">0.1</span>
my_taxes = my_income*tax_rate
print(<span class="hljs-string">"My income tax is"</span>,my_taxes)
</code></pre>
<h2 id="strings-in-python">Strings in Python:</h2>
<p>String is nothing but ordered seq. of characters.
Note: Strings are immutable</p>
<h3 id="using-directly-with-print">Using directly with print:</h3>
<pre><code class="lang-python">print(<span class="hljs-string">"Simple String"</span>)
print(<span class="hljs-string">'Add quotes inside the  "string" by using single quote.'</span>)
print(<span class="hljs-string">"concat string "</span>+<span class="hljs-string">"like this"</span>)
</code></pre>
<h3 id="taking-input">Taking Input</h3>
<pre><code class="lang-python">greeting = <span class="hljs-string">"Hello"</span>
name = input(<span class="hljs-string">"Enter your name: "</span>)
print(greeting + <span class="hljs-string">' '</span> + name)
</code></pre>
<h3 id="use-instead-of-this-will-auto-seprate-them-by-spaces">Use <code>,</code> instead of <code>+</code> This will auto seprate them by spaces.</h3>
<pre><code class="lang-python">print(greeting,name)
</code></pre>
<h3 id="escape-characters-in-python">Escape characters in python:</h3>
<pre><code class="lang-python">print(<span class="hljs-string">"Hello\nWorld"</span>)
<span class="hljs-comment"># or</span>
print(<span class="hljs-string">"""
 ---
Hello
world\
Yeahh!!
"Quotes"
'Single quote'
 ---
"""</span>)
</code></pre>
<h3 id="check-the-length-of-string">Check the length of string:</h3>
<pre><code class="lang-python">print(len(<span class="hljs-string">"Hey"</span>))
</code></pre>
<h3 id="string-indexing">String indexing</h3>
<pre><code class="lang-python">a = <span class="hljs-string">"Hello"</span>
a[<span class="hljs-number">0</span>] Will <span class="hljs-keyword">return</span> H
</code></pre>
<h3 id="string-slicing">String Slicing</h3>
<pre><code class="lang-python">a[start:end:step]
a[<span class="hljs-number">0</span>:<span class="hljs-number">2</span>] <span class="hljs-comment"># Start from 0th index till 2(excluding)</span>
a[::<span class="hljs-number">2</span>] <span class="hljs-comment"># Step will be 2.</span>
<span class="hljs-comment"># We can use this to print a string backwards</span>
s[::<span class="hljs-number">-1</span>]
</code></pre>
<h3 id="string-methods">String methods:</h3>
<pre><code class="lang-python"><span class="hljs-comment"># Multiply Strings</span>
a = <span class="hljs-string">'H'</span> * <span class="hljs-number">10</span>
</code></pre>
<pre><code class="lang-python"><span class="hljs-comment"># Upper Case a string</span>
s.upper()
</code></pre>
<pre><code class="lang-python"><span class="hljs-comment"># Lower case</span>
s.lower()
</code></pre>
<pre><code class="lang-python"><span class="hljs-comment"># Splitting</span>
s.split(<span class="hljs-string">'W'</span>)
s.split() <span class="hljs-comment"># split via whitespace.</span>
</code></pre>
<h3 id="formatting-strings">Formatting strings:</h3>
<ul>
<li><code>.format()</code></li>
<li><code>f""</code> F-string<pre><code class="lang-python">print(<span class="hljs-string">'The {2} {1} {0}'</span>.format(<span class="hljs-string">'fox'</span>,<span class="hljs-string">'brown'</span>,<span class="hljs-string">'quick'</span>))
print(<span class="hljs-string">'First Object: {a}, Second Object: {b}, Third Object: {c}'</span>.format(a=<span class="hljs-number">1</span>,b=<span class="hljs-string">'Two'</span>,c=<span class="hljs-number">12.3</span>))
num = <span class="hljs-number">23.45</span>
print(<span class="hljs-string">"My 10 character, four decimal number is:{0:10.4f}"</span>.format(num))
print(<span class="hljs-string">f"My 10 character, four decimal number is:<span class="hljs-subst">{num:<span class="hljs-number">10.4</span>f}</span>"</span>)
</code></pre>
<h3 id="string-print-alignment">String Print alignment</h3>
<pre><code class="lang-python">left_alignment = <span class="hljs-string">"Left Text"</span>
center_alignment = <span class="hljs-string">"Centered Text"</span>
right_alignment = <span class="hljs-string">"Right Text"</span>
print(<span class="hljs-string">f"<span class="hljs-subst">{left_alignment : &lt;<span class="hljs-number">20</span>}</span>|<span class="hljs-subst">{center_alignment : ^<span class="hljs-number">15</span>}</span>|<span class="hljs-subst">{right_alignment : &gt;<span class="hljs-number">20</span>}</span>"</span>)
More about this: https://pyformat.info/
</code></pre>
</li>
</ul>
<h2 id="lists-in-python">Lists in Python:</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="lang-python"><span class="hljs-comment"># Supports dynamic types, as it is python :)</span>
my_list = [<span class="hljs-number">100</span>,<span class="hljs-number">2.5</span>,<span class="hljs-string">"Mohit"</span>]
<span class="hljs-comment"># len(my_list) for length</span>
<span class="hljs-comment"># Change objs:</span>
my_list[<span class="hljs-number">0</span>]=<span class="hljs-number">1000</span>
</code></pre>
<p><strong>Slicing is same as String slicing</strong></p>
<h3 id="concat">Concat</h3>
<pre><code class="lang-python">a = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]
b = [<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
c = a + b
</code></pre>
<h3 id="append-the-list">Append the list</h3>
<pre><code class="lang-python">my_list.append(<span class="hljs-number">10.8</span>)
</code></pre>
<h3 id="poping-objs">Poping objs</h3>
<pre><code class="lang-python">my_list.pop(index) <span class="hljs-comment"># default index is -1, returns popped element.</span>
</code></pre>
<h3 id="sorting">Sorting</h3>
<pre><code class="lang-python">a = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]
a.sort() <span class="hljs-comment"># in-place sort, it will modify the list, returns None</span>
<span class="hljs-comment"># Tip: use sorted(a) it will return the value instead of in-place</span>
</code></pre>
<h3 id="reverse">Reverse</h3>
<pre><code class="lang-python">a = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]
a.reverse() <span class="hljs-comment"># also in-place</span>
</code></pre>
<h3 id="nested-list">Nested list</h3>
<pre><code class="lang-python">a = [<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>,]]
print(a[<span class="hljs-number">3</span>][<span class="hljs-number">1</span>]) <span class="hljs-comment"># Returns 5.</span>
</code></pre>
<h2 id="dictionaries-in-python">Dictionaries in Python</h2>
<p>Unordered key-value mappings, basically you can have custom keys
<br />Think it like, you can use this to make dynamic variables, where key will be the variable name.
<br />Like list value can be any data type.</p>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="lang-python">prices = {<span class="hljs-string">"apple"</span>:<span class="hljs-number">10</span>, <span class="hljs-string">"orange"</span>:<span class="hljs-number">20.5</span>}
print(prices)
print(prices[<span class="hljs-string">"apple"</span>])
print(prices.keys()) <span class="hljs-comment"># get keys</span>
print(prices.values()) <span class="hljs-comment"># get values</span>
print(prices.items()) <span class="hljs-comment"># get items, return tuples with keys and values</span>
print(prices.pop(<span class="hljs-string">"apple"</span>)) <span class="hljs-comment"># Pop the object</span>
print(prices)
print(prices.clear()) <span class="hljs-comment"># Clear All</span>
print(prices)
print(prices.get(<span class="hljs-string">"banana"</span>)) <span class="hljs-comment"># it will check if it is present, return None if not.</span>
print(prices.__contains__(<span class="hljs-string">"apple"</span>)) <span class="hljs-comment"># Returns true/false</span>
</code></pre>
<h2 id="tuples-in-python">Tuples in Python</h2>
<p>Same as list, but immutable.</p>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="lang-python">a = (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>)
print(a)
<span class="hljs-comment"># Interesting Fact: tuple supports only two methods:</span>
a.count(<span class="hljs-number">2</span>) <span class="hljs-comment"># This can be use with list as well.</span>
a.index(<span class="hljs-number">3</span>) <span class="hljs-comment"># This can be use with list as well.</span>
</code></pre>
<h2 id="sets-in-python">Sets in Python</h2>
<p>Sets are an unordered collection of unique elements. </p>
<pre><code class="lang-python">a = set()
a.add(<span class="hljs-number">1</span>)
a.add(<span class="hljs-number">1</span>)
a.add(<span class="hljs-number">1</span>)
print(a) <span class="hljs-comment"># {1}</span>
</code></pre>
<h3 id="convert-list-to-set">Convert list to set</h3>
<pre><code class="lang-python">a = [<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">2</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">3</span>,<span class="hljs-number">3</span>]
a = set(a)
</code></pre>
<h2 id="file-io-with-python">File IO with Python</h2>
<h3 id="init-file-obj">init file obj</h3>
<pre><code class="lang-python">file = open(<span class="hljs-string">"file.txt"</span>)
</code></pre>
<h3 id="read-contents">read contents</h3>
<pre><code class="lang-python">contents = file.read()
print(contents)
</code></pre>
<h3 id="move-the-cursorpointer">move the cursor/pointer</h3>
<pre><code class="lang-python">file.seek(<span class="hljs-number">0</span>)
</code></pre>
<h3 id="read-line-by-line">read line by line</h3>
<pre><code class="lang-python">contents = file.readlines() <span class="hljs-comment"># returns list of lines.</span>
</code></pre>
<h3 id="close-the-file">close the file</h3>
<pre><code class="lang-python">file.close()
</code></pre>
<h3 id="using-context-manager">Using context manager</h3>
<pre><code class="lang-python"><span class="hljs-keyword">with</span> open(<span class="hljs-string">"file.txt"</span>) <span class="hljs-keyword">as</span> file:
    print(file.read())
</code></pre>
<h3 id="different-modes-for-file">different modes for file</h3>
<ul>
<li><code>r</code>: Read</li>
<li><code>r+</code>: Read and Write</li>
<li><code>w</code>: Write (will override the file)</li>
<li><code>w+</code>: Write + Read (will override the file)</li>
<li><code>a</code>: Append the file</li>
</ul>
<h2 id="chaining-comparison-operators">Chaining comparison operators:</h2>
<p>To chain <code>==, != &lt;, &gt;, &gt;=, &lt;= and is</code> these operators, we have these logical operators</p>
<ul>
<li>and</li>
<li>or</li>
<li>not<pre><code class="lang-python"><span class="hljs-keyword">if</span> <span class="hljs-number">2</span> &gt; <span class="hljs-number">3</span> <span class="hljs-keyword">and</span> <span class="hljs-number">2</span> &gt; <span class="hljs-number">5</span>:
  print(<span class="hljs-string">"I am inevitable"</span>)
<span class="hljs-keyword">if</span> <span class="hljs-string">"hello"</span> <span class="hljs-keyword">is</span> <span class="hljs-string">"world"</span> <span class="hljs-keyword">or</span> <span class="hljs-string">"india"</span> <span class="hljs-keyword">is</span> <span class="hljs-string">"country"</span>:
  print(<span class="hljs-string">"Yeah!!"</span>)
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> <span class="hljs-number">10</span> == <span class="hljs-number">10</span>:
  print(<span class="hljs-string">"Tough luck!"</span> )
</code></pre>
</li>
</ul>
<h2 id="python-statements">Python Statements:</h2>
<p>Indentation is <strong>important</strong> in the python.</p>
<h3 id="if-elif-else">if, elif, else</h3>
<pre><code class="lang-python">loc = <span class="hljs-string">'Bank'</span>

<span class="hljs-keyword">if</span> loc == <span class="hljs-string">'Auto Shop'</span>:
    print(<span class="hljs-string">'Welcome to the Auto Shop!'</span>)
<span class="hljs-keyword">elif</span> loc == <span class="hljs-string">'Bank'</span>:
    print(<span class="hljs-string">'Welcome to the bank!'</span>)
<span class="hljs-keyword">else</span>:
    print(<span class="hljs-string">'Where are you?'</span>)
</code></pre>
<h3 id="for-loops">for loops</h3>
<pre><code class="lang-python"><span class="hljs-comment"># iterate list/string/tuple</span>
l = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]
<span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> l:
    print(item)
<span class="hljs-comment"># extraction made easy</span>
list2 = [(<span class="hljs-number">2</span>,<span class="hljs-number">4</span>),(<span class="hljs-number">6</span>,<span class="hljs-number">8</span>),(<span class="hljs-number">10</span>,<span class="hljs-number">12</span>)]
<span class="hljs-keyword">for</span> t1,t2 <span class="hljs-keyword">in</span> list2: <span class="hljs-comment"># Same as dict. t1 will be key, t2 will be value.</span>
    print(t1) <span class="hljs-comment"># will print 2,6,10</span>
<span class="hljs-comment"># Protip about dict: use .value() and .keys() for looping Values/keys.</span>
</code></pre>
<h3 id="while-loops">while loops</h3>
<p>in python we can use python with else statement.</p>
<pre><code class="lang-python">x = <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> x &lt; <span class="hljs-number">3</span>:
    print(<span class="hljs-string">f"x is <span class="hljs-subst">{x}</span>"</span>)
    x = x + <span class="hljs-number">1</span>
<span class="hljs-keyword">else</span>:
    print(<span class="hljs-string">"Uhhoo! x &gt; 3"</span>)
</code></pre>
<h3 id="statement-in-python">Statement in python</h3>
<ul>
<li><strong>break</strong>: Breaks out of the current closest enclosing loop.</li>
<li><strong>continue</strong>: Goes to the top of the closest enclosing loop.</li>
<li><strong>pass</strong>: Does nothing at all, Programmers use this for placeholder.<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">future_method</span>():</span>
  <span class="hljs-comment"># Todo: implement it later.</span>
  <span class="hljs-keyword">pass</span>
<span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:
  <span class="hljs-keyword">break</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>):
  <span class="hljs-keyword">if</span> i == <span class="hljs-number">5</span>:
      <span class="hljs-comment">#omit</span>
      <span class="hljs-keyword">continue</span> 
  print(i)
</code></pre>
<h2 id="some-useful-operators">Some useful operators</h2>
<h3 id="range">range()</h3>
range is a generator.
<br /> <strong>Syntax</strong>: <code>range(start,end,step)</code>
<br /> 
Use directly with loops for iteration.</li>
</ul>
<pre><code class="lang-python">a = list(range(<span class="hljs-number">0</span>,<span class="hljs-number">11</span>,<span class="hljs-number">2</span>)) <span class="hljs-comment"># returns 0,2,4,..10</span>
</code></pre>
<h3 id="enumerate">enumerate()</h3>
<p>with help of this we can keep track of index and value.</p>
<pre><code class="lang-python">a = [<span class="hljs-number">20</span>,<span class="hljs-number">100</span>,<span class="hljs-number">5</span>,<span class="hljs-number">3</span>,<span class="hljs-number">6</span>]
<span class="hljs-keyword">for</span> index,value <span class="hljs-keyword">in</span> enumerate(a):
    print(<span class="hljs-string">f"<span class="hljs-subst">{index}</span>\t<span class="hljs-subst">{value}</span>"</span>)
</code></pre>
<h3 id="zip">zip()</h3>
<pre><code class="lang-python">zip multiple lists.
a = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]
b = [<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>]
<span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> zip(a,b):
    print(item)
</code></pre>
<h3 id="in-operator">in operator:</h3>
<pre><code class="lang-python">a = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]
print(<span class="hljs-number">3</span> <span class="hljs-keyword">in</span> a) <span class="hljs-comment"># True</span>
</code></pre>
<h3 id="min-and-max">min and max:</h3>
<pre><code class="lang-python">a = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]
print(min(a)) <span class="hljs-comment"># 1</span>
print(max(a)) <span class="hljs-comment"># 3</span>
</code></pre>
<h2 id="list-comprehensions">List Comprehensions</h2>
<p>Quicker and unique way to create lists.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Grab every letter in string</span>
lst = [x <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-string">'word'</span>]

<span class="hljs-comment"># Square numbers in range and turn into list</span>
lst = [x**<span class="hljs-number">2</span> <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>,<span class="hljs-number">11</span>)]

<span class="hljs-comment"># Check for even numbers in a range</span>
lst = [x <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">11</span>) <span class="hljs-keyword">if</span> x % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>]
</code></pre>
<h2 id="help-function-in-python">help function in python</h2>
<p>if you are lazy like me, want to learn documentation about specific inbuilt method via terminal, you can use help()</p>
<pre><code class="lang-python">a = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]
help(a.insert) <span class="hljs-comment"># will print info about this method</span>
</code></pre>
<h2 id="functions-in-python">Functions in python</h2>
<h3 id="basic-function-with-argument-and-default-value">Basic function with argument and default value</h3>
<pre><code class="lang-python"><span class="hljs-comment"># def keyword to define functions.</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say_hello</span>(<span class="hljs-params">name=<span class="hljs-string">"world"</span></span>):</span>
    print(<span class="hljs-string">f"Hello <span class="hljs-subst">{name}</span>!"</span>)
    <span class="hljs-comment"># or return f"Hello {name}!" if you want to return it.</span>
</code></pre>
<h3 id="args-and-kwargs"><em>args and *</em>kwargs</h3>
<ul>
<li><code>*args</code>: N number of arguments, returns tuple.</li>
<li><code>**kwargs</code>: N number of keyword arguments, returns dict.<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">total_income</span>(<span class="hljs-params">*args, **kwargs</span>):</span>
  print(<span class="hljs-string">f"Income for month, <span class="hljs-subst">{kwargs[<span class="hljs-string">'month'</span>]}</span> is : <span class="hljs-subst">{sum(args)}</span>"</span>)
total_income(<span class="hljs-number">10</span>,<span class="hljs-number">20</span>,<span class="hljs-number">300</span>,month=<span class="hljs-string">"July"</span>)
</code></pre>
<h3 id="lamda-filter-and-map">lamda, filter and map</h3>
```python
#map
def square(num):
  return num**2
my_nums = [1,2,3,4,5]
map(square,my_nums) # 1, 4, 9, 16, 25</li>
</ul>
<h1 id="filter">filter</h1>
<p>def check_even(num):
    return num % 2 == 0
nums = [0,1,2,3,4,5,6,7,8,9,10]
filter(check_even, nums) # 0, 2, 4, 6, 8, 10</p>
<h1 id="lets-convert-each-of-the-above-function-to-lambda">lets convert each of the above function to lambda.</h1>
<p>map(lambda num:num**2,my_nums)
filter(lambda num:num%2==0, nums) </p>
<pre><code><span class="hljs-comment">## Classes in python</span>
<span class="hljs-comment">### Basic implementation</span>
```python
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Circle</span>:</span>
    pi = <span class="hljs-number">3.14</span>

    <span class="hljs-comment"># Circle gets instantiated with a radius (default is 1)</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, radius=<span class="hljs-number">1</span></span>):</span>
        self.radius = radius 
        self.area = radius * radius * Circle.pi

    <span class="hljs-comment"># Method for resetting Radius</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setRadius</span>(<span class="hljs-params">self, new_radius</span>):</span>
        self.radius = new_radius
        self.area = new_radius * new_radius * self.pi

    <span class="hljs-comment"># Method for getting Circumference</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">getCircumference</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.radius * self.pi * <span class="hljs-number">2</span>


c = Circle()

print(<span class="hljs-string">'Radius is: '</span>,c.radius)
print(<span class="hljs-string">'Area is: '</span>,c.area)
print(<span class="hljs-string">'Circumference is: '</span>,c.getCircumference())
</code></pre><h3 id="inheritance">Inheritance</h3>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Animal created"</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">whoAmI</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Animal"</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">eat</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Eating"</span>)


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self</span>):</span>
        Animal.__init__(self)
        print(<span class="hljs-string">"Dog created"</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">whoAmI</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Dog"</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">bark</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Woof!"</span>)
</code></pre>
<h3 id="polymorphism">Polymorphism</h3>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name</span>):</span>    <span class="hljs-comment"># Constructor of the class</span>
        self.name = name

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>              <span class="hljs-comment"># Abstract method, defined by convention only</span>
        <span class="hljs-keyword">raise</span> NotImplementedError(<span class="hljs-string">"Subclass must implement abstract method"</span>)


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.name+<span class="hljs-string">' says Woof!'</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>(<span class="hljs-params">Animal</span>):</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.name+<span class="hljs-string">' says Meow!'</span>

fido = Dog(<span class="hljs-string">'Fido'</span>)
isis = Cat(<span class="hljs-string">'Isis'</span>)

print(fido.speak())
print(isis.speak())
</code></pre>
<h3 id="using-special-methods">Using Special methods</h3>
<p>Just like <code>__init__</code> we have more special methods.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Book</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, title, author, pages</span>):</span>
        print(<span class="hljs-string">"A book is created"</span>)
        self.title = title
        self.author = author
        self.pages = pages

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__str__</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Title: %s, author: %s, pages: %s"</span> %(self.title, self.author, self.pages)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__len__</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.pages

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__del__</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"A book is destroyed"</span>)


book = Book(<span class="hljs-string">"Python Rocks!"</span>, <span class="hljs-string">"Jose Portilla"</span>, <span class="hljs-number">159</span>)

<span class="hljs-comment">#Special Methods</span>
print(book)
print(len(book))
<span class="hljs-keyword">del</span> book
</code></pre>
<h2 id="exception-handling">Exception Handling</h2>
<h3 id="try-except-finally-and-else">try, except, finally, and else.</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">askint</span>():</span>
    <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:
        <span class="hljs-keyword">try</span>:
            val = int(input(<span class="hljs-string">"Please enter an integer: "</span>))
        <span class="hljs-keyword">except</span>:
            <span class="hljs-comment"># You can also expect specific error like TypeError or generic type Exception</span>
            print(<span class="hljs-string">"Looks like you did not enter an integer!"</span>)
            <span class="hljs-keyword">continue</span>
        <span class="hljs-keyword">else</span>:
            print(<span class="hljs-string">"Yep that's an integer!"</span>)
            <span class="hljs-keyword">break</span>
        <span class="hljs-keyword">finally</span>:
            print(<span class="hljs-string">"Finally, I executed!"</span>)
        print(val)
</code></pre>
<h2 id="decorators">Decorators</h2>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">new_decorator</span>(<span class="hljs-params">func</span>):</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wrap_func</span>():</span>
        print(<span class="hljs-string">"Code would be here, before executing the func"</span>)

        func()

        print(<span class="hljs-string">"Code here will execute after the func()"</span>)

    <span class="hljs-keyword">return</span> wrap_func

<span class="hljs-meta">@new_decorator</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func_needs_decorator</span>():</span>
    print(<span class="hljs-string">"This function is in need of a Decorator"</span>)

func_needs_decorator()
<span class="hljs-comment"># Code would be here, before executing the func</span>
<span class="hljs-comment"># This function is in need of a Decorator</span>
<span class="hljs-comment"># Code here will execute after the func()</span>
</code></pre>
<h2 id="generators">Generators</h2>
<pre><code class="lang-python"><span class="hljs-comment"># Without generator</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_me_cubes</span>(<span class="hljs-params">n</span>):</span>
    output_list = []
    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(n):
        output_list.append(i**<span class="hljs-number">3</span>)
    <span class="hljs-keyword">return</span> output_list

print(get_me_cubes(<span class="hljs-number">10</span>))
<span class="hljs-comment"># With generator</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">generate_cubes</span>(<span class="hljs-params">n</span>):</span>
    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(n):
        <span class="hljs-keyword">yield</span> i**<span class="hljs-number">3</span>

print(generate_cubes(<span class="hljs-number">10</span>))
</code></pre>
<h2 id="useful-python-modules-you-should-look">Useful Python modules you should look.</h2>
<ul>
<li>collections</li>
<li>os</li>
<li>shutil</li>
<li>datetime</li>
<li>math</li>
<li>random</li>
<li>pdb</li>
<li>re</li>
<li>timeit</li>
<li>zipfile</li>
</ul>
<h2 id="working-with-csvs-in-python">Working with CSVs in python</h2>
<pre><code class="lang-python"><span class="hljs-comment"># don't forget to install csv</span>
<span class="hljs-keyword">import</span> csv
data = open(<span class="hljs-string">'example.csv'</span>,encoding=<span class="hljs-string">"utf-8"</span>)
<span class="hljs-comment"># passing encoding is important otherwise you will get the Unicode error.</span>
csv_data = csv.reader(data)
<span class="hljs-comment"># reading</span>
data_lines = list(csv_data)
<span class="hljs-comment"># writing </span>
file_to_output = open(<span class="hljs-string">'to_save_file.csv'</span>,<span class="hljs-string">'w'</span>,newline=<span class="hljs-string">''</span>)
<span class="hljs-comment"># use 'a' for append</span>
csv_writer = csv.writer(file_to_output,delimiter=<span class="hljs-string">','</span>)
csv_writer.writerow([<span class="hljs-string">'a'</span>,<span class="hljs-string">'b'</span>,<span class="hljs-string">'c'</span>])
file_to_output.close()
</code></pre>
<h2 id="working-with-pdfs-in-python">Working with pdfs in python</h2>
<pre><code class="lang-python"><span class="hljs-comment"># don't forget to use PyPDF2</span>
<span class="hljs-keyword">import</span> PyPDF2
f = open(<span class="hljs-string">'Working_Business_Proposal.pdf'</span>,<span class="hljs-string">'rb'</span>)
<span class="hljs-comment"># we need to pass rb for binary files.</span>
pdf_text = []

pdf_reader = PyPDF2.PdfFileReader(f)

<span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> range(pdf_reader.numPages):
    page = pdf_reader.getPage(p)
    pdf_text.append(page.extractText())
</code></pre>
<h2 id="sending-emails-with-python">Sending Emails with python</h2>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> smtplib
smtp_object = smtplib.SMTP(<span class="hljs-string">'smtp.gmail.com'</span>,<span class="hljs-number">587</span>)
email = <span class="hljs-string">"youremail@email.com"</span>
password = <span class="hljs-string">"yourpassword"</span>
<span class="hljs-comment"># Tip: search about how you generate app passwords.</span>
smtp_object.login(email,password)
from_address = <span class="hljs-string">"fromemail@email.com"</span>
to_address = <span class="hljs-string">"toemail@email.com"</span>
subject = <span class="hljs-string">"Subject"</span>
message = <span class="hljs-string">"Message"</span>
msg = <span class="hljs-string">"Subject: "</span> + subject + <span class="hljs-string">'\n'</span> + message
smtp_object.sendmail(from_address,to_address,msg)
smtp_object.quit()
</code></pre>
]]></content:encoded></item></channel></rss>