Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@adeisbright
Last active March 12, 2022 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adeisbright/6e335e005c33303cced03760a0672da3 to your computer and use it in GitHub Desktop.
Save adeisbright/6e335e005c33303cced03760a0672da3 to your computer and use it in GitHub Desktop.
Get started with the basics of python

Bigjara Python : A Beginner Focused Approach

Lesson 1 : Getting Started

The lesson link

Lesson 2 : Comments and Variables

The lesson link

Lesson 3 : Operators

The lesson link

Lesson 4 : Conditionals

The lesson link

Lesson 5 : Repetition

The lesson link

Lesson 6 : A Console Project on FizzBuzz

The lesson link

Assignment on first set of lessons

Write a program that prints the first 100 members of the sequence 2 , -3 , 4 , -5 , 6

Write a program that generates this sequence 0 , 5 , 10 , 15 , 20 , 25, ..., 80

Write a program that grades a student using this grading system :

Score Grade

80 – 100 A

60 – 79 B

50 – 59 C

35 – 49 D

0 – 34 F

Create variables to store the following information about a user with the following detail : Gender : f Age : 35

country of origin : Burundi

Covid Patient : No

Networth : 250,000

Second Set of Lessons on Python

Lesson 7 : Working with List

List Lesson: Module 1

List Lesson: Module 2

Lesson 8 : Tuple

The lesson link

Lesson 9 : Set

The lesson link

Lesson 10 : Dictionary

The lesson link

@adeisbright
Copy link
Author

Practice Assignment on List

Link 1: Basic problems that seeks to reinforce your understanding of python list.

See link

Link 2: Geek for Geeks List Exercises

Attempt the problems here

@adeisbright
Copy link
Author

Practice Assignment on Tuple , Set , and Dictionary

  1. tuple problems

  2. You are given a dictionary of users and their

@adeisbright
Copy link
Author

Building an Ecommerce Using Python Basics

class Product:
"""
A Hypothetical database for storing product information
Attributes
----------
products : a list of products
Methods:
-------
add_product
show_products
"""
products = []

def add_product(self , title , quantity , price):
    if (len(Product.products) > 0 ):
        for product in Product.products:
            if(product["title"] == title):
                product["quantity"] += quantity 
                if (price > product["price"]) :
                    product["price"] = price 
                return 

        product = {
            "title" : title , 
            "quantity" : quantity , 
            "price" : price
        }

        Product.products.append(product)
    else : 
        product = {
            "title" : title , 
            "quantity" : quantity , 
            "price" : price
        }
        Product.products.append(product)

    


def show_products(self):
    if(len(Product.products) == 0):
        print("No Product to display")
        return "No Product to display" 
    else :
        for product in Product.products:
            print(product)

if name == "main":
catalog = Product()
catalog.show_products()
catalog.add_product("Book" , 45 , 100)
catalog.add_product("Pen" , 10 , 50)

catalog.add_product("Book" , 300 , 190)

catalog.show_products()

@adeisbright
Copy link
Author

Catalog Management for a Fictional Ecommerce Portal

You can now :

  1. Find a product by id
  2. Remove a product by its id
  3. Update a product
    """
        A Hypothetical database for storing product information 
        Attributes 
        ---------- 
            products : a list of products 
        Methods:
        ------- 
            add_product 
            show_products
    """
    products = [] 
    count = 0 

    def add_product(self , title , quantity , price):
        if (len(Product.products) > 0 ):
            for product in Product.products:
                if(product["title"] == title):
                    product["quantity"] += quantity 
                    if (price > product["price"]) :
                        product["price"] = price 
                    return 

            Product.count += 1
            product = {
                "title" : title , 
                "quantity" : quantity , 
                "price" : price ,
                "id" : Product.count 
            }

            Product.products.append(product)
        else : 
            Product.count += 1
            product = {
                "title" : title , 
                "quantity" : quantity , 
                "price" : price  , 
                "id" : Product.count 
            }
            Product.products.append(product)

        
    

    def show_products(self):
        if(len(Product.products) == 0):
            print("No Product to display")
            return "No Product to display" 
        else :
            for product in Product.products:
                print(product)

    def find_product(self , id):
        """ Searches the catalog for a product whose id is id"""
        count = 0
        for product in Product.products:
            if (product["id"] == id):
                result =  {
                    "product" : product , 
                    "index" : count
                }
                return result
            count += 1
        return False
   
    def update_product(self , id , body):
        """
            Upates a particular product with the body using 
            the id 

            Parameters
            ---------- 
            id : Number : The id of the product 
            body : Dictionary : New content for the product
        """
        is_product = self.find_product(id) 
        if(is_product):
            return is_product["product"].update(body)
        else:
            return "Invalid Product ID"
        
    def remove_product(self , id) : 
        """ Removes a product whose id is id"""
        is_product = self.find_product(id) 
        if (is_product) :
            Product.products.pop(is_product["index"])
        else:
            return "Invalid Product ID"

if __name__ == "__main__":
    catalog = Product() 
    catalog.show_products()
    catalog.add_product("Book" , 45 , 100)
    catalog.add_product("Pen" , 10 , 50) 

    catalog.add_product("Book" , 300 , 190) 
    catalog.add_product("Car" , 200 , 500000)

    #catalog.show_products()

    product = catalog.find_product(7) 
    print("---Searching for a Product by Id--")
    print("The product is:\n\n" , product , "\n\n")
    catalog.update_product(3, 
    {"title" : "Example" , "quantity" : 40 , "price" : 4})
    catalog.remove_product(2)

    print("After Deleting removing a particular item")

    catalog.show_products()
    ```

@adeisbright
Copy link
Author

from http.server import BaseHTTPRequestHandler, HTTPServer


hostName = "localhost"
serverPort = 8080

class Server(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(bytes("""
        <html><head><title>Getting Started</title></head>
        <body>
            <p>Hello , World</p>
        </body>
        </html>
        """, "utf-8"))

if __name__ == "__main__":        
    webServer = HTTPServer((hostName, serverPort), Server)
    print("Server started http://{}:{}".format(hostName, serverPort))

    try:
        webServer.serve_forever()
    except KeyboardInterrupt:
        webServer.server_close()
        print("Server stopped.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment