<?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>DEV Community: Zubair Ahmad</title>
    <description>The latest articles on DEV Community by Zubair Ahmad (@zubairwazir).</description>
    <link>https://dev.to/zubairwazir</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%2F1046908%2F1c53d23a-a4d2-4f33-a5d8-154490dac729.jpeg</url>
      <title>DEV Community: Zubair Ahmad</title>
      <link>https://dev.to/zubairwazir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zubairwazir"/>
    <language>en</language>
    <item>
      <title>Time Complexity and Big O Notation</title>
      <dc:creator>Zubair Ahmad</dc:creator>
      <pubDate>Tue, 04 Apr 2023 20:47:55 +0000</pubDate>
      <link>https://dev.to/zubairwazir/time-complexity-and-big-o-notation-3jjp</link>
      <guid>https://dev.to/zubairwazir/time-complexity-and-big-o-notation-3jjp</guid>
      <description>&lt;p&gt;Time complexity is an important concept in computer science that refers to the amount of time it takes for an algorithm or program to run as a function of its input size. In this tutorial, I will explore the time complexity of loops, nested loops, if-else statements, and different Python data structures, and discuss which ones are optimized for various use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time Complexity of Loops
&lt;/h2&gt;

&lt;p&gt;Loops are a common programming construct used to repeat a block of code a certain number of times. The time complexity of a loop depends on the number of times the loop runs as a function of its input size.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Simple for loop
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(n):
    print(i)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The time complexity of this loop is O(n), where n is the input size. This is because the loop runs exactly n times, printing the value of i each time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Nested for loops
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(n):
    for j in range(n):
        print(i, j)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The time complexity of this loop is O(n^2), because the loop runs n^2 times. This is because for each value of i, the inner loop runs n times, so the total number of iterations is n * n = n^2.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time Complexity of If-Else Statements
&lt;/h2&gt;

&lt;p&gt;If-else statements are used to conditionally execute code based on a given condition. The time complexity of an if-else statement is typically O(1), as the code will only execute once regardless of the input size.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Simple if-else statement
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if x &amp;gt; 0:
    print("x is positive")
else:
    print("x is non-positive")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The time complexity of this if-else statement is O(1), as the code inside the if or else block will only execute once.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Nested if-else statements
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if x &amp;gt; 0:
    if y &amp;gt; 0:
        print("x and y are positive")
    else:
        print("x is positive, y is non-positive")
else:
    print("x is non-positive")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The time complexity of this nested if-else statement is still O(1), as the code inside the if or else blocks will only execute once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time Complexity of Python Data Structures
&lt;/h2&gt;

&lt;p&gt;Different Python data structures have different time complexities for common operations such as accessing, inserting, and deleting elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lists
&lt;/h3&gt;

&lt;p&gt;Lists are a common data structure in Python that can hold a sequence of elements. Lists are implemented as an array, and have the following time complexities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Accessing an element by index: O(1)&lt;/li&gt;
&lt;li&gt;  Appending an element: O(1)&lt;/li&gt;
&lt;li&gt;  Inserting an element at a specific index: O(n)&lt;/li&gt;
&lt;li&gt;  Removing an element: O(n)&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Sorting the list: O(n log n)&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_list = [1, 2, 3, 4, 5]

print(my_list[0])     # O(1)

my_list.append(6)     # O(1)

my_list.insert(2, 7)  # O(n)

my_list.remove(3)     # O(n)

my_list.sort()        # O(n log n)
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tuples
&lt;/h3&gt;

&lt;p&gt;Tuples are similar to lists, but are immutable (i.e. cannot be changed after creation). Tuples have the following time complexities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Accessing an element by index: O(1)&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[0])  # O(1)
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Dictionaries
&lt;/h3&gt;

&lt;p&gt;Dictionaries are a data structure that maps keys to values. Dictionaries are implemented as a hash table, and have the following time complexities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Accessing a value by key: O(1)&lt;/li&gt;
&lt;li&gt;  Inserting a new key-value pair: O(1)&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Deleting a key-value pair: O(1)&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_dict = {"apple": 1, "banana": 2, "orange": 3} 

print(my_dict["banana"])  # O(1)
my_dict["grape"] = 4      # O(1)
del my_dict["orange"]     # O(1)
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Sets
&lt;/h3&gt;

&lt;p&gt;Sets are a data structure that holds a collection of unique elements. Sets are implemented as a hash table, and have the following time complexities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Adding an element: O(1)&lt;/li&gt;
&lt;li&gt;  Checking if an element is in the set: O(1)&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Removing an element: O(1)&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_set = {1, 2, 3, 4, 5}

my_set.add(6)       # O(1)
print(2 in my_set)  # O(1)
my_set.remove(3)    # O(1)
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Which Data Structure to Use
&lt;/h3&gt;

&lt;p&gt;Choosing the right data structure for a given problem depends on the specific requirements of the problem and the expected input size. Generally, if quick access or modification of elements is required, lists and dictionaries are good choices. If uniqueness of elements is required, sets should be used. If the elements cannot be changed after creation, tuples should be used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding time complexity is important for designing efficient algorithms and choosing appropriate data structures for different use cases. By knowing the time complexity of loops, if-else statements, and Python data structures, we can make informed decisions about which algorithms and data structures to use to optimize performance.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>python</category>
      <category>tutorial</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Network Communication in Python: Sockets, Requests, and Urllib</title>
      <dc:creator>Zubair Ahmad</dc:creator>
      <pubDate>Sun, 02 Apr 2023 08:54:51 +0000</pubDate>
      <link>https://dev.to/zubairwazir/network-communication-in-python-sockets-requests-and-urllib-4545</link>
      <guid>https://dev.to/zubairwazir/network-communication-in-python-sockets-requests-and-urllib-4545</guid>
      <description>&lt;p&gt;Python provides several libraries for network communication, including sockets, requests, and urllib. These libraries can be used to send and receive data over the internet, make HTTP requests, and more. In this tutorial, we'll explore these libraries and provide examples of how to retrieve the content of a web page using each one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sockets
&lt;/h2&gt;

&lt;p&gt;Sockets are a low-level interface to network communication. They allow you to directly send and receive data over a network connection. Sockets can be used to implement protocols like TCP, UDP, and more.&lt;/p&gt;

&lt;p&gt;Here's how to use sockets to retrieve the content of a web page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import socket

HOST = 'www.example.com'
PORT = 80

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))

sock.sendall(b'GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n')

response = b''
while True:
    data = sock.recv(1024)
    if not data:
        break
    response += data

print(response.decode())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we create a socket object using the &lt;code&gt;socket.socket()&lt;/code&gt; method and connect to the web server at port 80 using the &lt;code&gt;sock.connect()&lt;/code&gt; method. We then send a GET request to the server using the &lt;code&gt;sock.sendall()&lt;/code&gt; method and receive the response in chunks using a while loop that reads data from the socket using the &lt;code&gt;sock.recv()&lt;/code&gt; method. Finally, we print the response content as a string using the &lt;code&gt;response.decode()&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requests
&lt;/h2&gt;

&lt;p&gt;Requests is a high-level library that provides an easier-to-use interface for making HTTP requests. Requests can be used to send GET, POST, PUT, DELETE, and other types of HTTP requests.&lt;/p&gt;

&lt;p&gt;Here's how to use requests to retrieve the content of a web page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import requests

url = 'http://www.example.com'
response = requests.get(url)

print(response.text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we use the &lt;code&gt;requests.get()&lt;/code&gt; method to send a GET request to the specified URL and receive the response as a &lt;code&gt;Response&lt;/code&gt; object. We can then access the content of the response using the &lt;code&gt;text&lt;/code&gt; attribute, which returns the response content as a string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Urllib
&lt;/h2&gt;

&lt;p&gt;Urllib is a library that provides several modules for working with URLs and making HTTP requests. It can be used to send GET, POST, PUT, DELETE, and other types of HTTP requests.&lt;/p&gt;

&lt;p&gt;Here's how to use urllib to retrieve the content of a web page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import urllib.request

url = 'http://www.example.com'
response = urllib.request.urlopen(url)

print(response.read().decode())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we use the &lt;code&gt;urllib.request.urlopen()&lt;/code&gt; method to send a GET request to the specified URL and receive the response as a file-like object. We can then read the content of the response using the &lt;code&gt;read()&lt;/code&gt; method and decode it into a string using the &lt;code&gt;decode()&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we explained how to use Python's network communication libraries, including sockets, requests, and urllib. We provided examples of how to retrieve the content of a web page using each library. Sockets are a low-level interface to network communication, requests are a high-level library for making HTTP requests, and urllib is a library for working with URLs and making HTTP requests. The choice of which library to use depends on the specific requirements of your application.&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Flask Islamic Quiz Game</title>
      <dc:creator>Zubair Ahmad</dc:creator>
      <pubDate>Thu, 30 Mar 2023 06:32:51 +0000</pubDate>
      <link>https://dev.to/zubairwazir/flask-islamic-quiz-game-2kjn</link>
      <guid>https://dev.to/zubairwazir/flask-islamic-quiz-game-2kjn</guid>
      <description>&lt;p&gt;Here is a step-by-step guide on how to create an Islamic Quiz Game using Python and Flask:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a database of Islamic quiz questions and answers in a CSV file or any other database format. For example:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question,Answer
What is the first pillar of Islam?,Shahada
What is the Islamic holy book?,Quran
What is the name of the pilgrimage to Mecca?,Hajj
What is the name of the month of fasting in Islam?,Ramadan
Who was the first Caliph of Islam?,Abu Bakr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Install the Flask library if it's not already installed in your Python environment. You can do this by running the following command in your terminal or command prompt:
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;pip install flask&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a Flask app in your Python script &lt;strong&gt;app.py&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&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
import random
import csv

app = Flask(__name__)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Load the quiz questions and answers from the CSV file into a list of dictionaries:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;quiz_data = []

with open('data.csv', 'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
        quiz_data.append(row)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a route for the quiz page that displays a random question from the quiz data:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route('/')
@app.route('/quiz')
def quiz():
    question = random.choice(quiz_data)
    return render_template('quiz.html', question=question)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a HTML template for the quiz page that displays the question and allows the user to submit their answer:
&lt;/li&gt;
&lt;/ol&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;Islamic Quiz Game&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Islamic Quiz Game&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;{{ question['Question'] }}&amp;lt;/p&amp;gt;
    &amp;lt;form action="{{ url_for('answer') }}" method="POST"&amp;gt;
        &amp;lt;input type="hidden" name="question" value="{{ question['Question'] }}"&amp;gt;
        &amp;lt;input type="text" name="answer" placeholder="Enter your answer"&amp;gt;
        &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&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;ol&gt;
&lt;li&gt;Create a route for the answer page that checks the user's answer and displays the result:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route('/answer', methods=['POST'])
def answer():
    question = request.form['question']
    user_answer = request.form['answer']
    correct_answer = next((q['Answer'] for q in quiz_data if q['Question'] == question), None)
    if user_answer.lower() == correct_answer.lower():
        result = "Correct!"
    else:
        result = f"Sorry, the correct answer is {correct_answer}."
    return render_template('answer.html', result=result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a HTML template for the answer page that displays whether the user's answer was correct or not:
&lt;/li&gt;
&lt;/ol&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;Islamic Quiz Game&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Islamic Quiz Game&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;{{ result }}&amp;lt;/p&amp;gt;
    &amp;lt;a href="{{ url_for('quiz') }}"&amp;gt;Next Question&amp;lt;/a&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Here is complete &lt;strong&gt;app.py&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&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
import random
import csv

app = Flask(__name__)

quiz_data = []
with open('data.csv', 'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
        quiz_data.append(row)


@app.route('/')
@app.route('/quiz')
def quiz():
    question = random.choice(quiz_data)
    return render_template('quiz.html', question=question)


@app.route('/answer', methods=['POST'])
def answer():
    question = request.form['question']
    user_answer = request.form['answer']
    correct_answer = next((q['Answer'] for q in quiz_data if q['Question'] == question), None)
    if user_answer.lower() == correct_answer.lower():
        result = "Correct!"
    else:
        result = f"Sorry, the correct answer is {correct_answer}."
    return render_template('answer.html', result=result)


if __name__ == "__main__":
    app.run(debug=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Run the Flask app in terminal:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flask run
or
python app.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Author:&lt;/strong&gt; &lt;a href="https://zubairwazir.github.io/"&gt;Zubair Ahmad&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Contact:&lt;/strong&gt; Follow me on &lt;a href="https://www.linkedin.com/in/zubairwazir/"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/zubairwazir777"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>flask</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>FastAPI Crash Course</title>
      <dc:creator>Zubair Ahmad</dc:creator>
      <pubDate>Fri, 17 Mar 2023 07:06:21 +0000</pubDate>
      <link>https://dev.to/zubairwazir/fastapi-crash-course-4ic3</link>
      <guid>https://dev.to/zubairwazir/fastapi-crash-course-4ic3</guid>
      <description>&lt;p&gt;FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.6+ based on standard Python type hints. It provides a simple and intuitive way to create robust, scalable, and maintainable web APIs. FastAPI leverages the latest Python features and the powerful data validation and serialization capabilities of the Pydantic library to provide a more efficient development experience with fewer bugs. FastAPI is also designed to be easy to learn and use, with excellent documentation and an active community of developers contributing to its development and support.Some of its notable features include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;High Performance: FastAPI is designed for high performance, thanks to its use of asynchronous programming and modern Python features like type hints.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easy to Use: FastAPI is easy to learn and use, with a simple and intuitive API that makes it quick to get up and running.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data Validation: FastAPI leverages the powerful data validation and serialization capabilities of the Pydantic library to ensure that data is well-formed and properly typed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Integration with Popular Databases and ORMs: FastAPI seamlessly integrates with popular databases and ORMs such as PostgreSQL, MySQL, SQLite, MongoDB, SQLAlchemy, and Tortoise ORM, providing flexibility to developers to utilize their preferred database technology and ORM to manage their data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Automatic API Documentation: FastAPI automatically generates comprehensive and interactive API documentation based on the code itself, making it easy to explore and understand the API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;OpenAPI and JSON Schema: FastAPI fully supports the OpenAPI and JSON Schema standards, providing a standardized way to describe APIs and their data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fast Development: FastAPI provides a smooth development experience with features like live reload, interactive API documentation, and an easy-to-use development server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Testability: FastAPI is highly testable, with built-in support for testing and debugging APIs.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Terms related to FastAPI:
&lt;/h2&gt;

&lt;p&gt;Here's a brief explanation of some of the terms related to FastAPI:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Path operation functions: In FastAPI, a path operation function is a Python function that handles a specific HTTP request method (such as GET, POST, PUT, DELETE, etc.) and URL path.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Path parameters: Path parameters are part of the URL path that are used to capture specific values from the client's request. They are defined in the path of a route using braces {}.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Query parameters: Query parameters are additional parameters that can be added to the end of a URL path to modify the behavior of an API endpoint. They are defined after a ? in the URL and can be accessed using the request object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Request body: The request body is the data sent by the client in the request payload. FastAPI can automatically parse the request body and generate appropriate type hints for the data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Response model: In FastAPI, you can specify the structure of the response data using Pydantic models. This allows for automatic serialization of response data into the appropriate format (such as JSON).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pydantic: Pydantic is a data validation and serialization library that is used heavily in FastAPI. It provides a simple and intuitive way to validate and convert data between Python objects and JSON.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;OpenAPI: OpenAPI is a specification for building APIs, including a standardized way to describe APIs and their data. FastAPI fully supports the OpenAPI standard and provides tools to generate OpenAPI documentation automatically.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Swagger UI: Swagger UI is a web-based tool for exploring and testing APIs. FastAPI automatically generates a Swagger UI page that allows developers to test and explore the API endpoints.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JSON Schema: JSON Schema is a vocabulary for describing JSON data, including its structure, data types, and validation constraints. FastAPI uses JSON Schema extensively for data validation and serialization.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ASGI: ASGI (Asynchronous Server Gateway Interface) is a protocol for building asynchronous web applications in Python. FastAPI is built on top of ASGI and provides a high-performance web server that is optimized for handling large volumes of traffic.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Overall, FastAPI is a powerful and flexible web framework that makes it easy to build high-performance APIs in Python. Its support for asynchronous code, automatic API documentation generation, and Pydantic data validation make it a great choice for building modern web applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exploring Key Features and Code Examples
&lt;/h2&gt;

&lt;p&gt;In FastAPI, path parameters and query parameters are two ways to accept data from a client.&lt;/p&gt;

&lt;h2&gt;
  
  
  Path Parameter
&lt;/h2&gt;

&lt;p&gt;Path parameters are used to capture parts of the URL path, and are declared by wrapping them in curly braces {} in the path. For example, in the following path "/items/{item_id}", {item_id} is a path parameter. The value of the path parameter is extracted from the URL and passed as an argument to the corresponding function in the application.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Here's an example of a FastAPI endpoint that accepts a path parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI

app = FastAPI()

items = {
  1: {
    "name": "apple",
    "price": 0.5,
    "description": "A sweet fruit with a red or green skin and a core containing seeds."
  },
  2: {
    "name": "banana",
    "price": 0.25,
    "description": "A long curved fruit with a yellow skin and soft sweet flesh."
  },
  3: {
    "name": "orange",
    "price": 0.35,
    "description": "A round citrus fruit with a tough bright reddish-yellow rind and juicy acid pulp."
  }
}

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the read_item function accepts a path parameter item_id of type int. When the client requests a URL like /items/42, the value 42 will be passed as the item_id argument to the function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Query Parameter
&lt;/h2&gt;

&lt;p&gt;Query parameters are used to pass additional data in the URL query string. They are declared as function parameters with default values, and FastAPI will automatically parse the query string and populate the parameter with the corresponding value. For example, in the following URL /items?skip=0&amp;amp;limit=10, skip and limit are query parameters.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Here's an example of a FastAPI endpoint that accepts query parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI

app = FastAPI()

items = {
  1: {
    "name": "apple",
    "price": 0.5,
    "description": "A sweet fruit with a red or green skin and a core containing seeds."
  },
  2: {
    "name": "banana",
    "price": 0.25,
    "description": "A long curved fruit with a yellow skin and soft sweet flesh."
  },
  3: {
    "name": "orange",
    "price": 0.35,
    "description": "A round citrus fruit with a tough bright reddish-yellow rind and juicy acid pulp."
  }
}

@app.get("/items/")
async def read_items(skip: int = 0, limit: int = 10):
    return {"skip": skip, "limit": limit}

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

&lt;/div&gt;



&lt;p&gt;In this example, the read_items function accepts two query parameters skip and limit, both of type int with default values of 0 and 10 respectively. When the client requests a URL like /items?skip=20&amp;amp;limit=5, the function will receive skip=20 and limit=5 as the values for the corresponding parameters. If no values are provided in the URL, the function will use the default values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Combining Path and Query Parameters
&lt;/h2&gt;

&lt;p&gt;We can combine path and query parameters in FastAPI.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Here's an example of how to define an endpoint that uses both path and query parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI

app = FastAPI()

items = {
  1: {
    "name": "apple",
    "price": 0.5,
    "description": "A sweet fruit with a red or green skin and a core containing seeds."
  },
  2: {
    "name": "banana",
    "price": 0.25,
    "description": "A long curved fruit with a yellow skin and soft sweet flesh."
  },
  3: {
    "name": "orange",
    "price": 0.35,
    "description": "A round citrus fruit with a tough bright reddish-yellow rind and juicy acid pulp."
  }
}

@app.get("/items/{item_id}")
def get_item(item_id: int, q: str = None):
    item = items.get(item_id)
    if not item:
        return {"error": "Item not found"}
    if q:
        item.update({"q": q})
    return item
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the endpoint is defined using the @app.get decorator, which specifies that it responds to HTTP GET requests. The endpoint path includes a path parameter, item_id, which is defined by enclosing it in curly braces {}. The endpoint also includes a query parameter, q, which is optional and has a default value of None.&lt;/p&gt;

&lt;p&gt;When the endpoint is called, the value of item_id is taken from the path parameter in the URL, and the value of q is taken from the query string, if it is present. The endpoint returns a JSON response containing both parameter values. &lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP Methods
&lt;/h2&gt;

&lt;p&gt;FastAPI supports several HTTP methods that can be used to define different types of endpoints in an application. Here's a short description of each HTTP method in FastAPI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GET&lt;/strong&gt;: Used to retrieve a resource or a list of resources. This method should be safe and idempotent, meaning it should not modify any data on the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;POST&lt;/strong&gt;: Used to create a new resource. This method is not idempotent, meaning multiple requests can result in multiple resource creations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;PUT&lt;/strong&gt;: Used to update an existing resource. This method should be idempotent, meaning multiple requests should result in the same resource state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;PATCH&lt;/strong&gt;: Similar to PUT, but used to partially update an existing resource.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DELETE&lt;/strong&gt;: Used to delete an existing resource. This method should be idempotent, meaning multiple requests should result in the same resource state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;OPTIONS&lt;/strong&gt;: Used to retrieve information about the communication options available for a resource.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;HEAD&lt;/strong&gt;: Similar to GET, but only retrieves the HTTP headers for a resource, without the actual content.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In FastAPI, each of these &lt;strong&gt;HTTP methods can be mapped to a Python function that defines the behavior of the corresponding endpoint&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;@app.get&lt;/strong&gt;, &lt;strong&gt;@app.post&lt;/strong&gt;, &lt;strong&gt;@app.put&lt;/strong&gt;, &lt;strong&gt;@app.patch&lt;/strong&gt;, &lt;strong&gt;@app.delete&lt;/strong&gt;, &lt;strong&gt;@app.options&lt;/strong&gt;, and &lt;strong&gt;@app.head decorators&lt;/strong&gt; are used to create endpoints that respond to the corresponding HTTP methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Request Body and Post Method
&lt;/h2&gt;

&lt;p&gt;In FastAPI, you can define a request body using the BaseModel class from the pydantic module. A request body is the data that the client sends to the server as part of a POST request. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;Here's an example of how to define a request body for adding a new item to the items dictionary using the POST method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float
    description: Optional[str] = None

items = {}

@app.post("/create_item")
def create_item(item_id: int, item: Item):
    if item_id in items:
        return {"error": "Item already exists!"}
    items[item_id] = item
    return items[item_id]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we first define a new Item class that inherits from the BaseModel class in FastAPI's pydantic module. The Item class has three fields: name, price, and description.&lt;/p&gt;

&lt;p&gt;Next, we define the create_item endpoint with the @app.post decorator. In the create_item function, we define a parameter called item with the type Item, which is the class we defined earlier. This means that the request body should have JSON data with the same keys as the attributes of the Item class. This endpoint takes an Item object as the request body, which is automatically parsed and validated by FastAPI based on the field types and validation rules specified in the Item class.&lt;/p&gt;

&lt;p&gt;Inside the endpoint, we generate a new item_id based on the current length of the items dictionary, add the new item to the dictionary with the item_id as the key, and return a response containing the new item_id.&lt;/p&gt;

&lt;h2&gt;
  
  
  PUT Method
&lt;/h2&gt;

&lt;p&gt;Here's an example of using the PUT method in FastAPI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class UpdateItem(BaseModel):
    name: Optional[str] = None
    price: Optional[float] = None
    description: Optional[str] = None

items = {
  1: {
    "name": "apple",
    "price": 0.5,
    "description": "A sweet fruit with a red or green skin and a core containing seeds."
  }
}

@app.put("/items/{item_id}")
async def update_item(item_id: int, item: UpdateItem):
    if item_id not in items:
        return {"error": "Item not found"}

    if item.name != None:
        items[item_id]["name"] = item.name
    if item.price != None:
        items[item_id]["price"] = item.price
    if item.description != None:
        items[item_id]["description"] = item.description

    return {"message": "Item updated successfully"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have defined a PUT endpoint that updates an existing item by ID. We have used the Item class that we defined earlier, and we have assumed that we already have a dictionary called items that contains the existing items.&lt;/p&gt;

&lt;p&gt;The endpoint takes two parameters - item_id and item. The item_id parameter is a path parameter that specifies the ID of the item that needs to be updated. The item parameter is a request body parameter that contains the updated information for the item.&lt;/p&gt;

&lt;p&gt;We first check if the specified item_id exists in the items dictionary. If it doesn't, we return an error message. If it does, we update the corresponding item with the data from the item parameter. We use the dict() method of the Item object to convert it to a dictionary before updating the items dictionary.&lt;/p&gt;

&lt;p&gt;Finally, we return a message indicating that the item was updated successfully.&lt;/p&gt;

&lt;h2&gt;
  
  
  DELETE Method
&lt;/h2&gt;

&lt;p&gt;In FastAPI, you can define a DELETE method to handle HTTP DELETE requests. The DELETE method is typically used to delete a specific resource identified by a unique identifier.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Here's an example of how you can implement a DELETE method to delete an item from the items dictionary based on its ID:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI

app = FastAPI()

items = {
  1: {
    "name": "apple",
    "price": 0.5,
    "description": "A sweet fruit with a red or green skin and a core containing seeds."
  }
}

@app.delete("/items/{item_id}")
async def delete_item(item_id: int):
    if item_id not in items:
        return {"error": "Item not found"}
    del items[item_id]
    return {"message": "Item deleted successfully"}

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

&lt;/div&gt;



&lt;p&gt;In this example, the delete_item function takes a item_id parameter which is passed in through the path parameter of the URL. If the item with the specified ID does not exist in the items dictionary, the function returns an error message. Otherwise, it deletes the item from the dictionary using the del keyword, and returns a success message.&lt;/p&gt;

&lt;p&gt;Note that it's important to handle errors gracefully in your application to provide informative error messages to clients. In this example, if the item is not found, we return an error message with an appropriate HTTP status code (e.g. 404 Not Found).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Author:&lt;/strong&gt; &lt;a href="https://zubairwazir.github.io/"&gt;Zubair Ahmad&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Contact:&lt;/strong&gt; Follow me on &lt;a href="https://www.linkedin.com/in/zubairwazir/"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/zubairwazir777"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>fastapi</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
