<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Tabish Javed</title>
    <description>The latest articles on Forem by Tabish Javed (@tabish0).</description>
    <link>https://forem.com/tabish0</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1116118%2F1788526d-2b5a-4494-8033-a19d28494cad.png</url>
      <title>Forem: Tabish Javed</title>
      <link>https://forem.com/tabish0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tabish0"/>
    <language>en</language>
    <item>
      <title>Pythonic Python: Best Code Practices</title>
      <dc:creator>Tabish Javed</dc:creator>
      <pubDate>Sun, 23 Jul 2023 17:27:41 +0000</pubDate>
      <link>https://forem.com/tabish0/pythonic-python-best-code-practices-5gd7</link>
      <guid>https://forem.com/tabish0/pythonic-python-best-code-practices-5gd7</guid>
      <description>&lt;p&gt;Who doesn't love Python? It's easy to write code in Python and it does a lot of the work for you. However, there are several practices that we should follow to improve code readability and performance. &lt;/p&gt;

&lt;h2&gt;
  
  
  1. Pythonic conditions
&lt;/h2&gt;

&lt;p&gt;Python provides a lot of ease in writing conditions. We are often used to other languages that require long conditions, so we end up doing it here. &lt;br&gt;
Consider the following lines of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Non-Pythonic
if condition:
    x = 10
else:
    x = 5

# Pythonic
x = 10 if condition else 5

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This improves the appearance of your code and makes full use of Python's capabilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Use zip()
&lt;/h2&gt;

&lt;p&gt;The zip function in Python combines multiple iterables (e.g., lists, tuples, or strings) element-wise into a new iterable of tuples. Each tuple contains the corresponding elements from all the input iterables. &lt;br&gt;
It stops when the shortest iterable is exhausted. Here's an example!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;items = ['milk', 'banana', 'yoghurt']
quantities = [1, 12, 2]
prices = [1.5, 0.8, 1.8]

combined = zip(items, quantities, prices)
print(list(combined))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[('milk', 1, 1.5), ('banana', 12, 0.8), ('yogurt', 2, 1.8)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Look for built-in functions
&lt;/h2&gt;

&lt;p&gt;You'll be surprised that there's built-in functionality for almost everything in Python. Whether it's to find min, max, or to reverse and sort an iterable, you don't need to write nested loops! Just do a quick Google search to see if an easy way out exists. &lt;/p&gt;

&lt;h2&gt;
  
  
  4. Use default and keyword arguments
&lt;/h2&gt;

&lt;p&gt;You can define default values for function parameters, so if a user doesn't provide a value for that parameter, the default will be used.&lt;br&gt;
Here's an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def greet(name, greeting="Hello"):
    print(f"{greeting}, {name}!")

greet("Alice")      
greet("Bob", "Hi")  

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, for keyword arguments, instead of relying on their order, you can pass arguments by their names, making the function call more explicit and easier to understand.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def calculate_interest(principal, rate, time):
    return principal * rate * time / 100

# Calling the function using positional arguments
interest1 = calculate_interest(1000, 5, 3)

# Calling the function using keyword arguments
interest2 = calculate_interest(principal=1000, rate=5, time=3)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Use PEP 8
&lt;/h2&gt;

&lt;p&gt;Last but not least, follow pep-8 conventions. PEP 8 is the official style guide for Python programming, offering recommendations and best practices for well-structured Python code. &lt;br&gt;
Key aspects covered by PEP 8 include guidelines on indentation, line length, variable and function naming conventions, import statements, and more.&lt;br&gt;
You can read more about it &lt;a href="https://peps.python.org/pep-0008/"&gt;here&lt;/a&gt;&lt;br&gt;
Certain libraries can help you follow these conventions. Some famous ones are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;isort (sorts imports in required order)&lt;/li&gt;
&lt;li&gt;pylint (checks for errors and suggest good practices)&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>Introduction to Jinja Template</title>
      <dc:creator>Tabish Javed</dc:creator>
      <pubDate>Sun, 23 Jul 2023 17:22:26 +0000</pubDate>
      <link>https://forem.com/tabish0/introduction-to-jinja-template-1k1</link>
      <guid>https://forem.com/tabish0/introduction-to-jinja-template-1k1</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Jinja template is the most popular template engine for Flask and Django frameworks. It is used to make the templates in these frameworks dynamic and separate the backend from the frontend layer. &lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install jinja2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Variables
&lt;/h2&gt;

&lt;p&gt;In case of showing the value of a variable in the HTML code, we can use the following format.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1&amp;gt;Hello, {{ name }}!&amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Expressions
&lt;/h2&gt;

&lt;p&gt;Expressions can be evaluated in the HTML code and displayed using the following approach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;{{ 2 + 2 }}&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;{{ 'Hello ' + 'World' }}&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;{{ 10 &amp;gt; 5 }}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conditional Statements
&lt;/h2&gt;

&lt;p&gt;You can add conditional statements and use them to display HTML code based on conditions being true or false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% if user %}
  &amp;lt;p&amp;gt;Hello, {{ user }}!&amp;lt;/p&amp;gt;
{% else %}
  &amp;lt;p&amp;gt;Hello, Guest!&amp;lt;/p&amp;gt;
{% endif %}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Loops
&lt;/h2&gt;

&lt;p&gt;Loops can be used in Jinja template to iterate through collections/lists of values and dynamically generate the elements as many times as required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ul&amp;gt;
  {% for fruit in fruits %}
    &amp;lt;li&amp;gt;{{ fruit }}&amp;lt;/li&amp;gt;
  {% endfor %}
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;It is also possible to inherit the code from other templates and add additional code to it removing redundancy.&lt;/p&gt;

&lt;p&gt;The following is a code which is base HTML code which can be inherited by all the files. We have created two blocks title and content and these will be replaced by HTML code of the files inheriting this base HTML.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Base Template: templates/base.html
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;title&amp;gt;{% block title %}{% endblock %}&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;main&amp;gt;
    {% block content %}{% endblock %}
  &amp;lt;/main&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the following code, the title block gets text Home Page and the content block gets an HTML code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% extends "base.html" %}

{% block title %}Home Page{% endblock %}

{% block content %}
  &amp;lt;h1&amp;gt;Welcome to our website!&amp;lt;/h1&amp;gt;
  &amp;lt;p&amp;gt;Content goes here...&amp;lt;/p&amp;gt;
{% endblock %}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>django</category>
      <category>html</category>
    </item>
    <item>
      <title>Handle form input in Flask</title>
      <dc:creator>Tabish Javed</dc:creator>
      <pubDate>Sun, 16 Jul 2023 05:35:32 +0000</pubDate>
      <link>https://forem.com/tabish0/handle-form-input-in-flask-1faa</link>
      <guid>https://forem.com/tabish0/handle-form-input-in-flask-1faa</guid>
      <description>&lt;p&gt;Flask is an easy-to-use web framework that follows an MVC architecture to develop web applications. It is popular because it is very easy to use and flexible allowing rapid development.&lt;/p&gt;

&lt;p&gt;This is a guide to making a basic frontend and sending the response to the Flask API ultimately saving it in a database.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTML Frontend
&lt;/h2&gt;

&lt;p&gt;Let's begin by creating a simple frontend where our users can enter their name, age, and hobby.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;title&amp;gt;User Form&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;h2&amp;gt;User Input Form&amp;lt;/h2&amp;gt;
  &amp;lt;form action="/submit" method="post"&amp;gt;
    &amp;lt;label for="name"&amp;gt;Name:&amp;lt;/label&amp;gt;
    &amp;lt;input type="text" id="name" name="name" required&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

    &amp;lt;label for="age"&amp;gt;Age:&amp;lt;/label&amp;gt;
    &amp;lt;input type="number" id="age" name="age" required&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

    &amp;lt;label for="hobby"&amp;gt;Hobby:&amp;lt;/label&amp;gt;
    &amp;lt;input type="text" id="hobby" name="hobby" required&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

    &amp;lt;input type="submit" value="Submit"&amp;gt;
  &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is a simple HTML form that submits to a route '/submit' and it contains three input tags in HTML that will help get the input from the user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Flask Backend
&lt;/h2&gt;

&lt;p&gt;Now we shall begin writing our flask API where we will handle this request from frontend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/submit', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        name = request.form['name']
        age = request.form['age']
        hobby = request.form['hobby']

        return 'User data saved successfully!'
    return render_template('index.html')

if __name__ == '__main__':
    db.create_all()
    app.run(debug=True)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this flask API, we have set the route information at the top of the method to '/submit' where the form is submitted and allowed the method to have to get and post requests both.&lt;/p&gt;

&lt;p&gt;In the API, request.form can be used to access the values of the input tags in the form. The values are accessed by the value of the name attribute assigned in the input tag.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connect database
&lt;/h2&gt;

&lt;p&gt;The last step is to save these values in a database table. Lets connect to a database and perform the insert operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    age = db.Column(db.Integer)
    hobby = db.Column(db.String(100))

    def __init__(self, name, age, hobby):
        self.name = name
        self.age = age
        self.hobby = hobby

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        name = request.form['name']
        age = request.form['age']
        hobby = request.form['hobby']
        user = User(name=name, age=age, hobby=hobby)
        db.session.add(user)
        db.session.commit()
        return 'User data saved successfully!'
    return render_template('index.html')

if __name__ == '__main__':
    db.create_all()
    app.run(debug=True)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The User class has modeled as a table for our input request and our data is going to get saved in the SQL lite database. By this, we are done with making a full-stack application.&lt;/p&gt;

</description>
      <category>python</category>
      <category>flask</category>
      <category>webdev</category>
    </item>
    <item>
      <title>AsyncIO basics in Python</title>
      <dc:creator>Tabish Javed</dc:creator>
      <pubDate>Sun, 09 Jul 2023 16:15:23 +0000</pubDate>
      <link>https://forem.com/tabish0/asyncio-basics-in-python-i4o</link>
      <guid>https://forem.com/tabish0/asyncio-basics-in-python-i4o</guid>
      <description>&lt;p&gt;While there are methods that facilitate parallel programming in Python, it is a single-threaded language by default. To avail the full advantage of single-threaded execution, we can make use of the AsyncIO library. &lt;/p&gt;

&lt;h2&gt;
  
  
  Asynchronous code
&lt;/h2&gt;

&lt;p&gt;As its name suggests, AsyncIO provides asynchronous execution in Python. Asynchronous code runs in a non-blocking way. If a function has a longer wait time, another task can be performed during the waiting period, instead of all the tasks being put on hold.&lt;/p&gt;

&lt;p&gt;The following image further explains synchronous vs asynchronous code execution. While waiting for the first response, we can make the second request to better utilize the processing capabilities. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uB6vAQ7I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twcagkddmee8yzjc5w2p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uB6vAQ7I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twcagkddmee8yzjc5w2p.png" alt="Asynchronous vs synchronous code" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although it does not make use of multi-threading, it does perform concurrent execution of code and reduces wait time considerably in many cases. &lt;/p&gt;

&lt;h2&gt;
  
  
  Using AsyncIO
&lt;/h2&gt;

&lt;p&gt;AsyncIO library has two major keywords:&lt;br&gt;
&lt;strong&gt;1. async&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. await&lt;/strong&gt;&lt;br&gt;
We can use the keywords &lt;strong&gt;async def&lt;/strong&gt; to define a coroutine. &lt;br&gt;
The &lt;strong&gt;await&lt;/strong&gt; keyword is used inside an async coroutine to suggest that the program should wait here and execute something else in the meantime. &lt;/p&gt;

&lt;p&gt;Take a look at the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;asyncio&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;co_routine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Entered coroutine number : '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Exiting coroutine number : '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;co_routine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
                         &lt;span class="n"&gt;co_routine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
                         &lt;span class="n"&gt;co_routine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;


&lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the output!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Entered coroutine number :  1
Entered coroutine number :  2
Entered coroutine number :  3
Exiting coroutine number :  1
Exiting coroutine number :  2
Exiting coroutine number :  3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we did not have to wait for coroutine 1 to finish executing before moving on to coroutines 2 and 3. Let's see the code step by step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;asyncio&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This imports the asyncio library to your python code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;co_routine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Entered coroutine number : '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Exiting coroutine number : '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is our asynchronous coroutine. We define it using the &lt;strong&gt;async def&lt;/strong&gt; keywords. We print a message on entering and then the coroutine sleeps for 2 seconds. This is where the magic happens. The &lt;strong&gt;await&lt;/strong&gt; keywords signals that we have to wait, so the program can execute something else. &lt;br&gt;
&lt;strong&gt;An await keyword can only be used inside an async coroutine!&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;co_routine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
                         &lt;span class="n"&gt;co_routine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
                         &lt;span class="n"&gt;co_routine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This defines our asynchronous main. &lt;strong&gt;asyncio.gather&lt;/strong&gt; takes multiple coroutine calls and waits for them to execute concurrently before returning. Here, we run our coroutine for values 1, 2, and 3. Since we had to use the await keywords, our main is also defined as async. &lt;/p&gt;

&lt;p&gt;However, for our final call, we can use &lt;strong&gt;asyncio.run&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This executes our main without the need to use await. &lt;/p&gt;

&lt;p&gt;Now, you can use async/await in the programs you wish to run concurrently. &lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
