<?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: Marriane Akeyo</title>
    <description>The latest articles on DEV Community by Marriane Akeyo (@marrie).</description>
    <link>https://dev.to/marrie</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%2F809894%2F4378c250-4993-45c8-9acb-d6cd5877fbe9.jpeg</url>
      <title>DEV Community: Marriane Akeyo</title>
      <link>https://dev.to/marrie</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marrie"/>
    <language>en</language>
    <item>
      <title>Docker from zero to pro</title>
      <dc:creator>Marriane Akeyo</dc:creator>
      <pubDate>Tue, 01 Aug 2023 19:30:51 +0000</pubDate>
      <link>https://dev.to/marrie/docker-from-zero-to-pro-2of7</link>
      <guid>https://dev.to/marrie/docker-from-zero-to-pro-2of7</guid>
      <description>&lt;p&gt;In this article, we embark on an insightful journey into the realm of Docker essentials, unraveling all the knowledge needed to grasp this groundbreaking technology. This involves delving deep into docker commands with examples and  docker compose as well. Lets dig in!!&lt;/p&gt;

&lt;h2&gt;
  
  
  Docker definition
&lt;/h2&gt;

&lt;p&gt;Docker can be defined as an application used to automate the deployment, management and scaling of applications. Its runs applications in an isolated environment very similar to virtual machines but slightly better. Docker works with "boxes" called &lt;em&gt;containers&lt;/em&gt; which as the name suggests, holds everything that is required to run the application, from the code to its dependencies. Multiple containers can run on the same machine without affecting each other, since each container has its own dependencies. They all however share the same kernel(acts as an intermediary between the hardware and the software, providing essential services and functionalities to manage system resources, facilitate communication between hardware and software, and ensure overall system stability and security.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages of docker
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;It uses less memory space, since the application are stored in the docker container and not locally.&lt;/li&gt;
&lt;li&gt;They are first to boot with just one command once the container is set up.&lt;/li&gt;
&lt;li&gt;They are easy to set up compared to locally running instances of the same application which are limited to the running computer's capabilities and easy to scale when needed.&lt;/li&gt;
&lt;li&gt;Easy to share with others, deploy and test applications.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The installation of docker and docker-compose(discussed later in the article) are limited to the type of OS a user is running. You can visit &lt;a href="https://docs.docker.com/engine/install/" rel="noopener noreferrer"&gt;this link&lt;/a&gt; for installation guidance.&lt;br&gt;
To verify if docker is correctly installed use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker --version

Output example depending on the version installed
Docker version 24.0.4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Docker Image
&lt;/h2&gt;

&lt;p&gt;This is a package template used to create one or more containers as we shall see in the examples  bellow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Docker hub
&lt;/h2&gt;

&lt;p&gt;There are some common tool  used in day to day programming like languages eg &lt;em&gt;python&lt;/em&gt;, web servers eg &lt;em&gt;nginx&lt;/em&gt; and databases eg &lt;em&gt;postgresql&lt;/em&gt; , which are used when creating various applications. These tools are stored in  a registry called &lt;strong&gt;docker hub&lt;/strong&gt; in form of &lt;strong&gt;images&lt;/strong&gt;. You need to create an account in order to access &lt;a href="https://hub.docker.com/" rel="noopener noreferrer"&gt;dockerhub&lt;/a&gt; and utilize its images.&lt;br&gt;
Once the account is created, we can easily pull images from docker hub.&lt;br&gt;
The images you create can also be pushed into docker hub and made public or private.&lt;br&gt;
The following command pulls a python version 3.10 image from docker hub&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker pull python:3.10-alpine3.18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;alpine3.18&lt;/strong&gt; keyword pulls the smallest in size of python3.10 available , hence saving on storage space. It is advisable to use "alpine" when pulling or defining images.&lt;/p&gt;

&lt;p&gt;To view all the images we can run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker images
Output:
REPOSITORY       TAG               IMAGE ID       CREATED       SIZE
python           3.10-alpine3.18   7d34b8225fda   7 weeks ago   49.7MB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running a normal installation of python3 using &lt;code&gt;docker pull python:3.10&lt;/code&gt; then viewing installed images, displays the difference in size between the normal one and the alpine version as shown:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REPOSITORY       TAG               IMAGE ID       CREATED       SIZE
python           3.10              d9122363988f   6 weeks ago   1GB
python           3.10-alpine3.18   7d34b8225fda   7 weeks ago   49.7MB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove an image run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker image rm &amp;lt;image_name/image_id&amp;gt;

Example:
docker image rm python:3.10

Output:
d9122363988f
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Docker Container
&lt;/h2&gt;

&lt;p&gt;Once the image is defined, it can be used to create a container, since a container runs an instance of an image.&lt;br&gt;
For example using the python image above:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run --name pytry -it python:3.10-alpine3.18&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;--name&lt;/em&gt; flag is used to define the name that will be used to reference the container, else a random name will be given to your container. &lt;br&gt;
The &lt;em&gt;-it&lt;/em&gt; command is used to run the command in an interactive mode.&lt;br&gt;
The above command produces a python shell which we can use to run various commands as shown.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Python 3.10.12 (main, Jun 15 2023, 03:17:49) [GCC 12.2.1 20220924] on linux
Type "help", "copyright", "credits" or "license" for more information.
&amp;gt;&amp;gt;&amp;gt; print("I am a python container")
I am a python container
&amp;gt;&amp;gt;&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To view all the flags that can be used with the docker run command use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run --help&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now that the container is defined, run the command bellow on another terminal window to view running containers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Command:
docker ps

Output:
CONTAINER ID   IMAGE                    COMMAND     CREATED          STATUS          PORTS     NAMES
5bcde232650b   python:3.10-alpine3.18   "python3"   32 seconds ago   Up 31 seconds             pytry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To view all the container which are present in docker use:&lt;br&gt;&lt;br&gt;
&lt;code&gt;docker ps -a&lt;/code&gt;&lt;br&gt;
Move to the terminal window running python container and run the command &lt;strong&gt;exit()&lt;/strong&gt; to return to the command line.&lt;br&gt;
Notice running &lt;code&gt;docker ps&lt;/code&gt; to view running containers produces no results since our container is closed.&lt;/p&gt;

&lt;p&gt;To make the code above more complex we can create a folder called hello.py in the current directory and write some code in it. Then run the command again but with a different container name to avoid conflicts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# file in the current directory
hello.py
from datetime import datetime
time = datetime.now()
print(f'I am a python file in a docker container running at {time}')

Command:
docker run --name pytryFile -w /app -v "$(pwd):/app" python:3.10-alpine3.18 python hello.py

Output:
I am a python file in a docker container running at &amp;lt;a_timestamp_of _the_time_right_now&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break down this command:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;-v "$(pwd):/app" mounts the current directory (pwd) into the container at the /app directory, allowing access to your Python script.&lt;/li&gt;
&lt;li&gt;-w /app sets the working directory inside the container to /app.&lt;/li&gt;
&lt;li&gt;python:3.10-alpine3.18 is the image name&lt;/li&gt;
&lt;li&gt;python hello.py is the command that runs your Python script inside the container.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Managing Containers
&lt;/h2&gt;

&lt;p&gt;To stop a running docker container use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker stop &amp;lt;container_name/container_id&amp;gt;
example:
docker stop pytry

Output:
pytry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To start a stopped container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker start &amp;lt;container_name/container_id&amp;gt;
example:
docker start pytry
Output:
pytry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove  a docker container :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker rm &amp;lt;container_name/container_id&amp;gt;
example:
docker rm pytry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To list all container's id&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker ps -aq
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get rid of all of the containers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker rm $(docker ps -aq)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To view other interesting flags you can use to manipulate containers:&lt;br&gt;
&lt;code&gt;docker ps --help&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Docker Volumes
&lt;/h2&gt;

&lt;p&gt;They enable easy sharing of information like files and folders between host and container and between containers themselves, just as the hello.py example above demonstrated.&lt;br&gt;
Another example, we create a new folder and inside it , a file called hello.html that displays:&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;h2&amp;gt; Hello World &amp;lt;/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To display this content on a web browser using the nginx web server, run the nginx image as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --name helloWorld -v $(pwd)/hello.html:/usr/share/nginx/html/hello.html:ro -p 8080:80 -d nginx

# View the content on the browser as shown:
http://localhost:8080/hello.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;/usr/share/nginx/html&lt;/em&gt;: is the nginx folder used to display content stored in nginx web server&lt;br&gt;
&lt;em&gt;ro&lt;/em&gt;: includes permissions on the shared content, in our case read and write permission.&lt;br&gt;
&lt;em&gt;-p&lt;/em&gt;: this flag illustrates  the port mapping in which the contents will be viewed from. In our case all the contents on the container(80) will be viewed on host by accessing port(8080)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note&lt;/em&gt;: &lt;em&gt;we did not pull the nginx image before using it , but running "docker images" after the above command is successful returns an nginx image as one of the images present. This is because when executing docker run ..., docker checks for the presence of the image locally and once it can't find it, it pulls it from docker hub and runs it&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker images
Output:
REPOSITORY       TAG               IMAGE ID       CREATED       SIZE
python           3.10-alpine3.18   7d34b8225fda   7 weeks ago   49.7MB
nginx            latest            021283c8eb95   3 weeks ago   187MB

# running containers
docker ps

Output:
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
eefaa7cd973c   nginx     "/docker-entrypoint.…"   11 minutes ago   Up 11 minutes   0.0.0.0:8080-&amp;gt;80/tcp, :::8080-&amp;gt;80/tcp   helloWorld
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Dockerfile
&lt;/h2&gt;

&lt;p&gt;This is a file given the name "Dockerfile" in running applications and is usually used to build applications which contain multiple parts and share them. &lt;br&gt;
In simple terms a docker file is used to make our own personal images.&lt;br&gt;
It contains a list of steps used to bring the app to life.&lt;br&gt;
A docker file has its own structure, always starting with a FROM clause and the rest of the flags can come interchangeably depending on the requirements of the application.&lt;br&gt;
The FROM clause can also appear multiple times to create multiple images.&lt;br&gt;
Example of commands that can come after the FROM clause include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM - statement defining the image to pull
RUN - to execute commands
WORKDIR - define the directory to work from
COPY - create a duplicate o the files or folders present in the host to the container.
ADD - copies new files, folders to the remote directory.
RUN - execute a command
CMD - run a file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;More commands like this one can be found in the &lt;a href="https://docs.docker.com/engine/reference/builder/" rel="noopener noreferrer"&gt;docker reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An example of a dockerfile includes:&lt;br&gt;
Assuming we have a folder called Example,with a hello.js file&lt;br&gt;
containing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('hello, world');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create another file and name it "dockerfile" with the content below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:alpine
WORKDIR /app
COPY . .
CMD node hello.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Docker Build
&lt;/h2&gt;

&lt;p&gt;Once  a docker file is defined, the "build" command is used to create the image. View &lt;code&gt;docker build --help&lt;/code&gt; to view the flags that can be used with docker build.&lt;br&gt;
The syntax is as shown:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker build --tag &amp;lt;name_of_image&amp;gt;:&amp;lt;tag&amp;gt; &amp;lt;directory_with_dockerfile&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;tag&lt;/strong&gt; represents the image versioning which aids in image naming. If none is given, then latest is assumed.It provides more control of the image you create.&lt;/p&gt;

&lt;p&gt;Creating the nodejs image above&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker build --tag greetings:latest .&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Viewing the images as shown above, we notice a new image with a tag &lt;em&gt;greetings:latest&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REPOSITORY       TAG               IMAGE ID       CREATED       SIZE
python           3.10-alpine3.18   7d34b8225fda   7 weeks ago   49.7MB
nginx            latest            021283c8eb95   3 weeks ago   187MB
greetings           latest            531620bb45c5   17 seconds ago   181MB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running the image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run greetings:latest

Output:
hello, world

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  .dockerignore
&lt;/h2&gt;

&lt;p&gt;It is used to ignore files that the application does not require to run like node_modules, requirement.txt and .git.&lt;br&gt;
It is created as file in the current directory and the files to be ignored placed inside it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.dockerignore
requirement.txt
node_modules
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Docker Registry
&lt;/h2&gt;

&lt;p&gt;As discussed before images are pulled and pushed from docker hub. These images are stored in a repository called &lt;strong&gt;docker registry&lt;/strong&gt;. &lt;br&gt;
That said it is possible to push personal images to docker hub.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First create an account in &lt;a href="https://hub.docker.com/" rel="noopener noreferrer"&gt;docker hub&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Run the following command and provide your Docker Hub credentials (username and password) when prompted: &lt;code&gt;docker login&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Tag the previously built Docker image with your Docker Hub username and the desired repository name.
&lt;code&gt;docker tag greetings your_username/greetings:latest&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Finally, push the tagged image to Docker Hub using the following command: &lt;code&gt;docker push your_username/greetings:latest&lt;/code&gt;
This will push the Docker image to your Docker Hub repository named "greetings" with the "latest" tag. &lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Docker Inspect
&lt;/h2&gt;

&lt;p&gt;This command allows you to retrieve detailed information about Docker objects, such as containers, images, volumes, and networks. It provides a JSON representation of the specified Docker object, which includes various metadata and configuration details.&lt;br&gt;
The syntax is as shown:&lt;br&gt;
&lt;code&gt;docker inspect &amp;lt;container_name /id &amp;gt;&lt;/code&gt;&lt;br&gt;
Pick a container and try it out.&lt;/p&gt;
&lt;h2&gt;
  
  
  Docker Logs
&lt;/h2&gt;

&lt;p&gt;This command is used to monitor traffic of a running container.&lt;br&gt;
The syntax is as shown:&lt;br&gt;
&lt;code&gt;docker logs &amp;lt;container_name /id &amp;gt;&lt;/code&gt;&lt;br&gt;
To follow the logs of the container as they come in add the &lt;strong&gt;-f&lt;/strong&gt; flag before the container name/id.&lt;br&gt;
To add a timestamp to each log when following add the the flags &lt;strong&gt;-ft&lt;/strong&gt; before the container name/id.&lt;br&gt;
For more important flags view &lt;br&gt;
&lt;code&gt;docker logs --help&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Docker Network
&lt;/h2&gt;

&lt;p&gt;This command is used to place two or more networks on the same network hence making it easy for them to communicate with each other. For example a postgres container and a pgadmin container that provides a graphical user interface of the data in a postgres database can easily communicate if they are placed on the same  network.&lt;br&gt;
The syntax is as shown:&lt;br&gt;
&lt;code&gt;docker network create &amp;lt;network_name&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The two images will have to now contain the flag &lt;strong&gt;--network&lt;/strong&gt; when run in order to communicate eg&lt;br&gt;
&lt;code&gt;docker -e POSTGRES_PASSWORD=pass -p 5432:5432 --network &amp;lt;network_name&amp;gt; -d postgres:latest&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Docker Compose
&lt;/h2&gt;

&lt;p&gt;It allows you to define and manage multiple docker containers as one instead of linking them using a network.&lt;br&gt;
This command is used to bring multiple parts of an application,eg &lt;em&gt;frontend, backend and storage&lt;/em&gt; to life with just one command. &lt;br&gt;
This is especially useful for development and testing environments, as well as for deploying applications to production.&lt;br&gt;
It is mainly done using a &lt;strong&gt;docker-compose.yaml&lt;/strong&gt; file containing services, networks, and volumes. Each service represents a container, and you can specify their images, ports, volumes, environment variables, etc. &lt;br&gt;
The YAML file should be placed in the root directory of your project.&lt;br&gt;
Ensure you install docker-compose according to your os type before usage. The docker-compose version should also be compatible with your docker version.&lt;br&gt;
An example of a docker-compose file includes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;services:
  db:
    image: postgres:15.3-alpine3.18
    environment:
      - POSTGRES_PASSWORD=&amp;lt;password&amp;gt;
      - POSTGRES_DB=&amp;lt;db_name&amp;gt;
    volumes:
      - "./green:/var/lib/postgresql/data:rw"
    ports:
      - "5432:5432"
  admin:
     image: dpage/pgadmin4
     environment:
        - PGADMIN_DEFAULT_EMAIL=&amp;lt;any@email.com&amp;gt;
        - PGADMIN_DEFAULT_PASSWORD=&amp;lt;pgadmin_password&amp;gt;
     ports:
        - "8080:80"
     volumes:
        - "./pgadmindata:/var/lib/pgadmin:rw"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can include the version of docker compose you are using eg &lt;code&gt;version:3.8&lt;/code&gt;, just above everything else but its not a requirement by default the installed version will be used.&lt;/p&gt;

&lt;p&gt;Let's unpack the file above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A service&lt;/strong&gt;: contains various parts of your application. It also directs docker on how to build the image required by that service. Each service has to have a unique name that differentiates it from another. We use &lt;strong&gt;db&lt;/strong&gt; as the service title for our postgres database image and &lt;strong&gt;admin&lt;/strong&gt; to reffere to the pgadmin image and the flags it will use to run
successfully.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To run the file :&lt;br&gt;
&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Use docker ps to view if the containers were created successfully and are up and running.&lt;br&gt;
So generally our docker-compose file runs both postgres and pgadmin which is postgres gui in the same network.&lt;/p&gt;

&lt;p&gt;Running &lt;code&gt;https://localhost 8080&lt;/code&gt; provide a pgadmin interface where you can log in using pgadmin credentials and create a server to view the postgres database with on the browser as shown.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffzryd2j2ifjpmw0xeb8w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffzryd2j2ifjpmw0xeb8w.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Create a server name&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7jkxt56icsg1ip4hgg14.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7jkxt56icsg1ip4hgg14.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Move to connection and use the name of the postgres service as the hostname  and include the postgres user and password in the columns indicated&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ferz6qjk2j9pxggaptlgt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ferz6qjk2j9pxggaptlgt.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Now you can run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker exec -it postgres:15.3-alpine3.18
psql -U &amp;lt;postgres_user&amp;gt;
\c &amp;lt;db_name&amp;gt;
CREATE TABLE hello_pdadmin;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Refresh pgadmin to see the changes.&lt;br&gt;
To build the images and not run them, use &lt;br&gt;
&lt;code&gt;docker-compose build&lt;/code&gt;&lt;br&gt;
To stop the running containers use &lt;br&gt;
&lt;code&gt;docker compose down&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;In conclusion, Docker is not just a technology but a mindset that encourages continuous integration and continuous deployment. Whether you're an individual developer, part of a team, or managing large-scale infrastructures, Docker has something to offer for everyone.Embracing Docker's capabilities will undoubtedly enhance your development workflow and bring your projects to new heights of efficiency and reliability. Thank you for staying curious and happy hacking!!&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>dataenineering</category>
      <category>dockercompose</category>
    </item>
    <item>
      <title>Functions for manipulating data in PostgreSQL</title>
      <dc:creator>Marriane Akeyo</dc:creator>
      <pubDate>Thu, 06 Jul 2023 11:58:15 +0000</pubDate>
      <link>https://dev.to/marrie/functions-for-manipulating-data-in-postgresql-4cg2</link>
      <guid>https://dev.to/marrie/functions-for-manipulating-data-in-postgresql-4cg2</guid>
      <description>&lt;p&gt;PostgreSQL is an open source, object-relational database management system (DBMS) that provides robust features and scalability for handling large and complex datasets. It follows the SQL (Structured Query Language) standard and extends it with additional capabilities.Some common data types it supports includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Text eg CHAR, VARCHAR and TEXT&lt;/li&gt;
&lt;li&gt;Numeric eg INT and DECIMAL&lt;/li&gt;
&lt;li&gt;Date/time eg DATE, TIME, TIMESTAMP, INTERVAL&lt;/li&gt;
&lt;li&gt;Arrays
It also supports JSON, XML, hstore (key-value store), and other specialized data types to handle diverse data requirements.
To determine dataypes from existing tables you can query the INFORMATION SCHEMA.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
     column_name,
     data_type,
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name IN ('title', 'description',                                                     'special_features)
AND table_name = 'name_of_your_table';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Array manipulation
&lt;/h2&gt;

&lt;p&gt;It allows you to store and manipulate arrays of values.&lt;br&gt;
Arrays can contain elements of any data type, including built-in types, user-defined types, or even arrays of arrays.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Array Declaration Syntax:&lt;/strong&gt;&lt;br&gt;
To declare a variable of array type in PL/pgSQL (PostgreSQL's procedural language), you use the same syntax. As shown, &lt;br&gt;
&lt;code&gt;array_name data_type [];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE grades (
  email text[][],
  test_scores int[]
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Array Initialization:&lt;/strong&gt;&lt;br&gt;
Array literals can be created using the curly braces "{ }" notation. For example, initializing the arrays declared above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO grades (email, test_scores)
VALUES (
     {{"work","work@gmail.com},{"other","other@gmail.com"}},
     {92,85,56,88}
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Accessing Array Elements:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
     email [1][1] AS type,
     email [1][2] AS email,
     test_scores [1] AS scores
FROM grades;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; : &lt;em&gt;Postgres arrays start with 1 and not 0&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The ANY() function
&lt;/h3&gt;

&lt;p&gt;It is an array operator in PostgreSQL that allows you to compare a value with an array and returns true if the value matches any element of the array. It is commonly used in combination with other comparison operators, such as &lt;strong&gt;=, &amp;lt;&amp;gt;, &amp;lt;, &amp;gt;, &amp;lt;=, &amp;gt;=&lt;/strong&gt; and many more.&lt;/p&gt;

&lt;p&gt;The syntax is as follows:&lt;br&gt;
&lt;code&gt;ANY(array_expression)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can use the ANY() function to manipulate the data selected from the grades table as shown:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
     email [1][1] AS type,
     email [1][2] AS email,
     test_scores [1] AS scores
FROM grades
WHERE 'other' = ANY(email);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The array containment operator (@&amp;gt;)
&lt;/h3&gt;

&lt;p&gt;It is used in PostgreSQL to check if an array contains all the elements of another array, verifying if an array is a subset of another array, or filtering rows based on array containment.It evaluates to true if the left-hand array contains all the values in the right-hand array.&lt;/p&gt;

&lt;p&gt;The syntax is as shown:&lt;br&gt;
&lt;code&gt;array_expression @&amp;gt; array_to_check&lt;/code&gt;&lt;br&gt;
It thus checks if the array represented by array_expression contains all the elements in the array_to_check.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;em&gt;The @&amp;gt; operator checks for containment, meaning it requires the left-hand array to contain all the elements without any duplicates. If you need to check for equality (exact match), you can use the = operator instead.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We can thus obtain the same results above when we use the @&amp;gt; operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
     email[1][1] AS type,
     email[1][2] AS email,
     test_scores[1] AS scores
FROM grades
WHERE email @&amp;gt; ARRAY['other'];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both queries will return the same result set, consisting of rows where the "email" array contains the value 'other'. The choice between using ANY() or the @&amp;gt; operator depends on your preference and the specific requirements of your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Date/Time manipulation
&lt;/h2&gt;

&lt;p&gt;The date/time data types are used to store and manipulate temporal data.They include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;date&lt;/strong&gt;: Stores a calendar date (year, month, day). Example:'2023-07-06'&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;time&lt;/strong&gt;: Stores the time of day (hour, minute, second).
Example: '14:30:45'&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;time with time zone&lt;/strong&gt;: Similar to time, but includes time zone information.Example: '14:30:45+05:30'&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;timestamp&lt;/strong&gt;: Stores both date and time (year, month, day, hour, minute, second). Example: '2023-07-06 14:30:45'&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;timestamp with time zone&lt;/strong&gt;: Similar to timestamp, but includes time zone information.Example: '2023-07-06 14:30:45+05:30'&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;interval&lt;/strong&gt;: Represents a duration of time. Example: '1 day 3 hours'&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PostgreSQL also provides a rich set of functions and operators for manipulating and performing operations on date/time data. These include functions to extract specific components (e.g., year, month, day) from a date/time value, perform arithmetic operations on dates and intervals, and format dates/times as strings. As we shall see bellow.&lt;/p&gt;

&lt;p&gt;We can start with simple calculations such as :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you subtract two date time values you get an integer datatype.&lt;/li&gt;
&lt;li&gt;Subtracting two timestamps gives us an interval.&lt;/li&gt;
&lt;li&gt;Adding an integer to a date we get a date value equivalent to the sum of the two.
&lt;code&gt;SELECT date '2020-09-11' + integer '3';&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You can also manipulate date time data using other arithmetic operations. 
&lt;code&gt;SELECT timestamp '2023-07-06 14:30:45' + 21 * interval '1 day 3 hours'&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  AGE()
&lt;/h3&gt;

&lt;p&gt;This function allows us to calculate the difference between two timestamps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- table creation
CREATE TABLE sample (
     start_date timestamp,
     end_date timestamp
     );

-- insert into table
INSERT INTO sample (start_date, end_date)
VALUES ('2023-07-01 12:00:00', '2023-07-06 14:30:00');

-- timestamp difference
SELECT AGE(start_date, end_date) AS diff
FROM sample;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Retrieving the current timestamp
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;CURRENT_TIMESTAMP&lt;/strong&gt; and the &lt;strong&gt;NOW()&lt;/strong&gt; functions are used to retrieve the current timestamp in the format &lt;strong&gt;'YYYY-MM-DD HH:MI:SS+TZ'&lt;/strong&gt;. Both functions provide the current date and time information. You can use these functions in SELECT queries, INSERT statements, or anywhere you need to retrieve the current timestamp in your SQL statements.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;CURRENT_TIMESTAMP Function
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT CURRENT_TIMESTAMP;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example Output: '2023-07-06 14:30:45+05:30'&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;NOW() Function
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT NOW();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example Output: '2023-07-06 14:30:45+05:30'&lt;/p&gt;

&lt;p&gt;Casting enables the conversion from one timestamp to another. We can thus get the now value without a timestamp as shown&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT NOW() :: timestamp;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can also use the CAST() function to get the same results.&lt;/p&gt;

&lt;p&gt;`SELECT CAST(NOW() as timestamp);&lt;/p&gt;

&lt;p&gt;To make things much easier you can use &lt;strong&gt;CURRENT_DATE&lt;/strong&gt; as shown:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT CURRENT_DATE;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To select just the time without date value:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT CURRENT_TIME;&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Extracting and Transforming date and datetime data
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The EXTRACT() function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Used to retrieve specific components or fields from date or datetime data.&lt;br&gt;
The syntax is as follows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EXTRACT(field FROM source)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Some examples include:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
SELECT EXTRACT(MINUTE FROM INTERVAL '3 hours 30 minutes') AS extracted_minute;&lt;/p&gt;

&lt;p&gt;-- example two&lt;br&gt;
SELECT EXTRACT(YEAR FROM '2023-07-06'::DATE) AS extracted_year;&lt;/p&gt;

&lt;p&gt;-- example three&lt;br&gt;
SELECT EXTRACT(quarter FROM TIMESTAMP '2023-07-06 14:30:00') AS quarter;&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The DATE_PART() function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It performs the same function as extract but their syntax differ a bit as shown.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;DATE_PART(field,source)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lets redefine the examples above&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
SELECT DATE_PART(MINUTE, INTERVAL '3 hours 30 minutes') AS extracted_minute;&lt;/p&gt;

&lt;p&gt;-- example two&lt;br&gt;
SELECT DATE_PART(YEAR, '2023-07-06'::DATE) AS extracted_year;&lt;/p&gt;

&lt;p&gt;-- example three&lt;br&gt;
SELECT DATE_PART(quarter, TIMESTAMP '2023-07-06 14:30:00') AS quarter;&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;DATE_ADD&lt;/strong&gt;&lt;br&gt;
Used to retrieve the next day's date.For example:&lt;br&gt;
Given the table :&lt;br&gt;
------------+-----------+-------------+&lt;br&gt;
| product_id | new_price | date |&lt;br&gt;
+------------+-----------+-------------+&lt;br&gt;
| 1          | 20        | 2019-08-14  |&lt;br&gt;
| 2          | 50        | 2019-08-12  |&lt;br&gt;
| 3          | 35        | 2019-08-16  |&lt;br&gt;
| 4          | 65        | 2019-08-17  |&lt;br&gt;
| 5          | 20        | 2019-08-18  |&lt;br&gt;
+------------+-----------+-------------+&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
SELECT &lt;br&gt;
    curr_date,&lt;br&gt;
    DATE_ADD(change_date, INTERVAL 1 DAY) AS day_after&lt;br&gt;
FROM your_table_name;&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The DATE_TRUNC function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is used to round off timestamps or interval values to a specified precision. It allows you to remove the smaller units of time from a given date or timestamp while preserving the desired level of precision.&lt;br&gt;
The syntax is as shown:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;DATE_TRUNC(unit, source)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lets look at some examples:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
-- returns the year in the formart '2023-01-01'&lt;br&gt;
SELECT DATE_TRUNC('year', DATE '2023-07-06') AS truncated_date;&lt;/p&gt;

&lt;p&gt;-- returns '2023-07-06 14:00:00'&lt;br&gt;
SELECT DATE_TRUNC('hour', TIMESTAMP '2023-07-06 14:30:45') AS truncated_timestamp;&lt;/p&gt;

&lt;p&gt;-- returns '2005-05-01'&lt;br&gt;
SELECT DATE_TRUNC( 'month', TIMESTAMP '2005-05-21 15:30:30'); &lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Text manipulation
&lt;/h2&gt;

&lt;p&gt;It is made up of VARCHAR, CHAR and Text data.&lt;br&gt;
Text datatype is considered to be mostly used in any database system and can be manipulated in various ways.&lt;/p&gt;

&lt;h3&gt;
  
  
  String concatenation
&lt;/h3&gt;

&lt;p&gt;This includes merging of two or more strings together or a non-string data with  a string data.&lt;br&gt;
It can be done using pipes(||) or the CONCAT() function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT first_name,&lt;br&gt;
       last_name&lt;br&gt;
       first_name || ' ' || last_name AS full_name&lt;br&gt;
FROM customers &lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The full name column will now contain the last_name and first_name of a customer separated by a space.&lt;br&gt;
The same results can be achieved using  the CONCAT() function as shown&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT first_name,&lt;br&gt;
       last_name,&lt;br&gt;
       CONCAT(first_name ' ' last_name) AS full_name&lt;br&gt;
FROM customers;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Concatenating multiple rows
&lt;/h3&gt;

&lt;p&gt;GROUP_CONCAT() is  used to concatenate values from multiple rows into a single string. It is often used in conjunction with the GROUP BY clause to aggregate data in a way that combines multiple rows into one, typically for reporting or display purposes.&lt;br&gt;
Consider a simplified example with a table called orders:&lt;/p&gt;

&lt;p&gt;sql&lt;/p&gt;

&lt;p&gt;+---------+-----------+&lt;br&gt;
| order_id| product   |&lt;br&gt;
+---------+-----------+&lt;br&gt;
| 1       | Apple     |&lt;br&gt;
| 2       | Banana    |&lt;br&gt;
| 3       | Orange    |&lt;br&gt;
| 1       | Banana    |&lt;br&gt;
| 2       | Apple     |&lt;br&gt;
+---------+-----------+&lt;/p&gt;

&lt;p&gt;If you want to concatenate the distinct product names for each order, sorted lexicographically and separated by a comma, you can use:&lt;/p&gt;

&lt;p&gt;SELECT&lt;br&gt;
    order_id,&lt;br&gt;
    GROUP_CONCAT(DISTINCT product ORDER BY product ASC SEPARATOR ', ') AS products&lt;br&gt;
FROM orders&lt;br&gt;
GROUP BY order_id;&lt;/p&gt;

&lt;p&gt;The result would be:&lt;/p&gt;

&lt;p&gt;+---------+-----------------+&lt;br&gt;
| order_id| products        |&lt;br&gt;
+---------+-----------------+&lt;br&gt;
| 1       | Apple, Banana   |&lt;br&gt;
| 2       | Apple, Banana   |&lt;br&gt;
| 3       | Orange          |&lt;br&gt;
+---------+-----------------+&lt;/p&gt;

&lt;h3&gt;
  
  
  Changing the case of a string
&lt;/h3&gt;

&lt;p&gt;Some commonly used functions to change the case of strings include:&lt;br&gt;
-** UPPER()**: converts a string to uppercase. For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT UPPER('Hello World') AS upper_case;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: 'HELLO WORLD'&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;LOWER()&lt;/strong&gt;: converts a string to lowercase. For example:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;SELECT LOWER('Hello World') AS lower_case;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: 'hello world'&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;INITCAP()&lt;/strong&gt;: converts a string to camel case. For Example:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;SELECT INITCAP('hello world') AS init_cap;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: 'Hello World'&lt;/p&gt;

&lt;h3&gt;
  
  
  Replacing a string
&lt;/h3&gt;

&lt;p&gt;The REPLACE() function locates a string and replaces it with another string.&lt;br&gt;
For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT &lt;br&gt;
     REPLACE (currency, 'dollars', 'shillings) AS new_currency&lt;br&gt;
FROM salary&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above query replaces all the dollar strings to shillings in the currency column and selects them as new_currency.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parsing string and character data
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The CHAR_LENGTH() function&lt;/strong&gt;&lt;br&gt;
This function is used to determine the number of characters in a string.&lt;br&gt;
Syntax: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;CHAR_LENGTH(string)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
Assuming we have a table called film with film titles and descriptions, we can find the title length as shown:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT title,&lt;br&gt;
       CHAR_LENGTH(title) AS len&lt;br&gt;
FROM films;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;LENGTH()&lt;/strong&gt; function can be used interchangeably with CHAR_LENGTH() function to produce similar results, and is mostly preferred.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT title,&lt;br&gt;
       LENGTH(title) AS len&lt;br&gt;
FROM films;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The POSITION() function&lt;/strong&gt;&lt;br&gt;
It is used to return the position of a string from left to right.&lt;br&gt;
Syntax: POSITION('string' IN source)&lt;br&gt;
Example:&lt;br&gt;
Selecting from the table created in Array manipulation above&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT email&lt;br&gt;
     POSITION('@' IN email) &lt;br&gt;
FROM grades;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;STRPOS()&lt;/strong&gt; function is an alias of POSITION() with a slightly different syntax as shown:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;STRPOS(source, 'string')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT email&lt;br&gt;
     STRPOS(email, '@') &lt;br&gt;
FROM grades;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The LEFT() function&lt;/strong&gt;&lt;br&gt;
Allows us to select a set of characters from the left of a string.&lt;br&gt;
Syntax: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;LEFT(column, number_limit)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
Considering a film table with title and description columns&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT &lt;br&gt;
     LEFT(description, 50)&lt;br&gt;
FROM film;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The RIGHT() function&lt;/strong&gt;&lt;br&gt;
Allows us to select a set of characters from the right side of a string.&lt;br&gt;
Syntax: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;RIGHT(column, number_limit)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
Considering a film table with title and description columns&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT &lt;br&gt;
     RIGHT(description, 50)&lt;br&gt;
FROM film;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The SUBSTRING() function&lt;/strong&gt;&lt;br&gt;
This function selects characters in between a string.&lt;br&gt;
Syntax: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;SUBSTRING(column, start, end)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT &lt;br&gt;
     SUBSTRING(description, 10, 50)&lt;br&gt;
FROM film;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The substring function can be combined with other functions to provide more context.For example selecting the name before @ in an email&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
-- before @&lt;br&gt;
SELECT&lt;br&gt;
     SUBSTRING(email, 0, POSITION('@' IN email)) AS name&lt;br&gt;
FROM grades;&lt;/p&gt;

&lt;p&gt;-- after @&lt;br&gt;
SELECT&lt;br&gt;
     SUBSTRING(email, POSITION('@' IN email)+1 FOR LENGTH   (email) ) AS domain&lt;br&gt;
FROM grades;&lt;br&gt;
or&lt;br&gt;
SELECT&lt;br&gt;
     SUBSTRING(email, FROM POSITION('@' IN email)+1) AS domain&lt;br&gt;
FROM grades;&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;SUBSTR()&lt;/strong&gt; is another alternative of SUBSTRING but does not allow other functions in it or the FOR keyword.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT &lt;br&gt;
     SUBSTR(description, 10, 50)&lt;br&gt;
FROM film;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The TRIM() function&lt;/strong&gt;&lt;br&gt;
It is used to remove whitespaces from strings.&lt;br&gt;
It follows the syntax&lt;/p&gt;

&lt;p&gt;&lt;code&gt;TRIM([leading|trailing|both] [characters] from string)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the first parameter is not given, the default value is both.&lt;br&gt;
If the second parameter is not given the default value is blank lines.&lt;br&gt;
Most of the times only the string is passed.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT TRIM(' john doe ')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: 'john doe'&lt;/p&gt;

&lt;p&gt;Other trim like functions include:&lt;br&gt;
LTRIM() - which removes whitespaces only from the beginning of the string.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT LTRIM(' john doe ')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: 'john doe '&lt;/p&gt;

&lt;p&gt;RTRIM() - which removes whitespaces at the end of the string&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT TRIM(' john doe ')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: ' john doe'&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The LPAD and RPAD functions&lt;/strong&gt;&lt;br&gt;
They append a spesified number of characters to a string with LPAD() appending to the left of a string and RPAD() to the right of a string.&lt;br&gt;
Syntax: LPAD/RPAD(string, no_of_times, string_to_be_added)&lt;br&gt;
Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;LPAD('food', 10, '*');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If no 'string_to_be_added' is provided then the string is padded by white spaces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Text searching&lt;/strong&gt;&lt;br&gt;
The LIKE character as learnt in &lt;a href="https://dev.to/marrie/sql101-introduction-to-sql-for-data-analysis-4i1b"&gt;introduction to sql&lt;/a&gt; is used to match characters and strings.&lt;br&gt;
We can use &lt;strong&gt;_&lt;/strong&gt; to match exactly one character or &lt;strong&gt;%&lt;/strong&gt; to match more than one character as shown.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT &lt;br&gt;
     title&lt;br&gt;
FROM film&lt;br&gt;
WHERE title LIKE 'Queen%';&lt;br&gt;
or &lt;br&gt;
WHERE title LIKE '%Charlotte';&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Either of the where clauses will return the title 'Queen Charlotte'&lt;br&gt;
However, LIKE is case sensitive and might not give us accurate results if we are performing a full text search and is also quite expensive in terms of performance.&lt;br&gt;
We can therefore utilize the &lt;strong&gt;to_tsvector()&lt;/strong&gt; and &lt;strong&gt;to_tsquery()&lt;/strong&gt; in our where clause which perform natural language queries of text and data in the database, as shown:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT &lt;br&gt;
     title&lt;br&gt;
FROM film&lt;br&gt;
WHERE to_tsvector(title)@@to_tsquery('Queen');&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;tsvector vectorises the title and tsquery searches for the subquery within the vectors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extending the Capabilities of PostgreSQL
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The CREATE_TYPE command&lt;/strong&gt;&lt;br&gt;
It is used to create user defined datatypes with specific attributes and behaviors.&lt;br&gt;
It registers the type in a postgres db and can be used where postgres expects a type name.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating a Composite Type
They are user-defined types that can contain multiple fields or attributes.
Syntax:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
CREATE TYPE type_name AS (&lt;br&gt;
  attribute1 datatype1,&lt;br&gt;
  attribute2 datatype2,&lt;br&gt;
  ...&lt;br&gt;
);&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
CREATE TYPE person_type AS (&lt;br&gt;
  name text,&lt;br&gt;
  age integer,&lt;br&gt;
  email text&lt;br&gt;
);&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Usage:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
-- create a table using the composite type&lt;br&gt;
CREATE TABLE persons (&lt;br&gt;
  id serial primary key,&lt;br&gt;
  person person_type&lt;br&gt;
);&lt;/p&gt;

&lt;p&gt;-- insert into the table&lt;br&gt;
INSERT INTO persons (person)&lt;br&gt;
VALUES (('John Doe', 30, '&lt;a href="mailto:john@example.com"&gt;john@example.com&lt;/a&gt;'));&lt;/p&gt;

&lt;p&gt;-- query the table&lt;br&gt;
SELECT person.name, person.age, person.email&lt;br&gt;
FROM persons;&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating an Enumerated Type:
It  allows you to define a fixed set of values that a column    or variable can take.
Syntax:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;CREATE TYPE type_name AS ENUM ('value1', 'value2', ...);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CREATE TYPE day_of_week AS ENUM ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once created, these custom types can be used in table definitions, column definitions, function parameters, and more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;User defined functions&lt;/strong&gt;&lt;br&gt;
They allow you to create custom functions tailored to your specific requirements. You can define functions that encapsulate complex logic, perform calculations, manipulate data, or provide any other functionality you need.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
CREATE FUNCTION function_name (parameter1 data_type1, parameter2 data_type2, ...)&lt;br&gt;
RETURNS return_type&lt;br&gt;
AS $$&lt;br&gt;
-- Function body goes here&lt;br&gt;
BEGIN&lt;br&gt;
  -- SQL statements or other logic&lt;br&gt;
  RETURN result_value;&lt;br&gt;
END;&lt;br&gt;
$$&lt;br&gt;
LANGUAGE plpgsql; -- or other supported language like SQL, Python, etc.&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
CREATE FUNCTION square (i integer)&lt;br&gt;
RETURNS integer&lt;br&gt;
AS $$&lt;br&gt;
BEGIN&lt;br&gt;
  RETURN i * i;&lt;br&gt;
END;&lt;br&gt;
$$&lt;br&gt;
LANGUAGE plpgsql; &lt;/p&gt;

&lt;p&gt;-- you then can use it in SQL queries just like any built-in function&lt;br&gt;
SELECT square(5) AS square_result;&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LEVEINSTEIN()&lt;/strong&gt; - claculates the distance required to make 2 strings simmilar in values between 0 and 1 , 1 being most accurate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SIMILARIRY()&lt;/strong&gt;- claclulates how simmilar or different two strings are from each other&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Postgres Extensions&lt;/strong&gt;&lt;br&gt;
PostgreSQL provides a command called &lt;strong&gt;CREATE EXTENSION&lt;/strong&gt; to install an extension.&lt;br&gt;
Syntax: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;CREATE EXTENSION extension_name;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;CREATE EXTENSION hstore;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Extensions can also be installed using package managers, such as apt, yum, or brew, depending on the operating system.&lt;br&gt;
Some popular extensions include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PostGIS&lt;/strong&gt;: Adds support for geographic objects and spatial indexing, enabling the use of PostgreSQL as a geospatial database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pgcrypto&lt;/strong&gt;: Provides cryptographic functions for secure data storage and retrieval.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pg_trgm&lt;/strong&gt;: Offers fuzzy string matching and indexing capabilities.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;hstore&lt;/strong&gt;: Allows storing key-value pairs within a single column, providing a basic NoSQL-like functionality.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;uuid-ossp&lt;/strong&gt;: Generates universally unique identifiers (UUIDs) using various algorithms.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pg_stat_statements&lt;/strong&gt;: Collects statistics about SQL statements executed in a database, aiding in query optimization.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PostPic&lt;/strong&gt;: allows for image processing within the db&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To view extensions available for install query&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT name&lt;br&gt;
FROM pg_available_extension;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To view already installed extensions&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
SELECT extname&lt;br&gt;
FROM pg_extension;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;br&gt;
You can read more about the usage of postgres extensions in the &lt;a href="https://www.postgresql.org/search/?u=%2Fdocs%2F15%2F&amp;amp;q=extensions"&gt;postgreSQL documentation&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Thank you for reading, I hope this helps you dive deeper into postgres. Stay curious!!!  and Happy hacking.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>sql</category>
    </item>
    <item>
      <title>SQL 102:Intermediate SQL</title>
      <dc:creator>Marriane Akeyo</dc:creator>
      <pubDate>Mon, 17 Apr 2023 08:50:37 +0000</pubDate>
      <link>https://dev.to/marrie/sql-102intermediate-sql-584p</link>
      <guid>https://dev.to/marrie/sql-102intermediate-sql-584p</guid>
      <description>&lt;p&gt;This post is a continuation of the &lt;a href="https://dev.to/marrie/sql101-introduction-to-sql-for-data-analysis-4i1b"&gt;Introduction to SQL&lt;/a&gt;. However, if you are familiar with basic SQL concepts you can take a read through this article for more advance concepts.&lt;/p&gt;

&lt;h2&gt;
  
  
  CASE WHEN AND THEN
&lt;/h2&gt;

&lt;p&gt;Evaluates a set of conditions just like an if else statement and return a corresponding result when the condition is true.&lt;br&gt;
The corresponding syntax is as shown:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT column1,
       column2,
       CASE WHEN condition1 THEN result1
            WHEN condition2 THEN result2
           ...(other when conditions)
           ELSE default_result
       END AS new_column_name
FROM table_name;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets look at a more concrete example. Assuming we have a matches table with different teams and how they played and we want to determine if the won when they played at home, we shall have the following query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT id,
       name,
       CASE WHEN home_goal &amp;gt; away_goal THEN 'Its a home win'
            WHEN home_goal &amp;lt; away_goal THEN 'Its a home loss'
            ELSE 'Its a tie'
       END AS home_scores
INTO home_analysis
FROM matches;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the above code is three columns &lt;em&gt;id, name&lt;/em&gt; and &lt;em&gt;home_scores&lt;/em&gt; which are stored inside the home_analysis table.&lt;br&gt;
We used the CASE statement to filter the results of the home_scores column.&lt;/p&gt;

&lt;p&gt;You can also use a CASE statement to aggregate data based on the results of a WHERE clause, since aggregate functions cannot be used directly in a WHERE clause.&lt;/p&gt;

&lt;p&gt;Let's say you have a table called "employees" with columns "name", "salary", and "department". You want to create a report that shows the total salary for each department, and categorize each department as either "High Paying" or "Low Paying" based on the total salary. The results should include only employees hired on or after January 1, 2023&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT department,
       SUM(salary) as total_salary
       CASE WHEN SUM(salary) &amp;gt;= 1000000 THEN 'High paying dpt'
            ELSE 'Low paying dpt'
       END AS dpt_salary
FROM employees
WHERE hire_date &amp;gt;= '2023-01-01'
GROUP BY department;   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sum of the salaries will only be placed in any category if they satisfy the where clause first.&lt;/p&gt;

&lt;h2&gt;
  
  
  SUBQUERIES
&lt;/h2&gt;

&lt;p&gt;They are queries found inside other queries hence also known as nested queries.The subquery is executed first, and the results are then used in the main query. This allows you to perform more complex queries that involve multiple tables and conditions.&lt;br&gt;
The syntax is as shown:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT column
FROM (SELECT column
      FROM table) AS subquery
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A subquery can be placed at any part of your query, such as the SELECT, FROM, WHERE or GROUP BY&lt;br&gt;
The allow you to compare summarized values  to detailed data and also reshape your data as desired.&lt;br&gt;
A subquery is also used to combine data that cannot be joined.&lt;/p&gt;
&lt;h3&gt;
  
  
  A subquery in the where clause
&lt;/h3&gt;

&lt;p&gt;It is used to filter data based on the results of another table.For example, if you have a table of customers and a table of orders, you can use a subquery to find all customers who have made at least one order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM customers
WHERE id IN (SELECT c_id FROM orders); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  A subquery in the FROM clause
&lt;/h3&gt;

&lt;p&gt;They are used to restructure and transform the data to be selected. Ensure you alias the subquery for easy reference. &lt;br&gt;
Lets use the customer and order table for reference and see the orders for each customer in the 2022/2023 season.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT name,
       order
FROM (SELECT c.name AS name 
             o.id AS order
      FROM customers AS c
      INNER JOIN orders AS o
      ON c.order = o.id)
      AS customer_order
WHERE season = '2022/2023';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also create more than one subquery inside a from statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT customers.customer_id,
      customers.first_name, '
      customers.last_name, 
      orders.total
FROM (
   SELECT customer_id, 
          SUM(price * quantity) AS total
   FROM order_items
   GROUP BY customer_id
   ) AS orders
INNER JOIN customers 
ON orders.customer_id = customers.customer_id
WHERE orders.total &amp;gt; (
   SELECT AVG(total)
       FROM (
          SELECT customer_id, 
                 SUM(price * quantity) AS total
          FROM order_items
          GROUP BY customer_id
         ) AS order_totals
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're using two subqueries in the FROM clause. The first subquery calculates the total amount spent by each customer on all of their orders. The second subquery calculates the average total amount spent by all customers.&lt;/p&gt;

&lt;p&gt;The main query then joins the orders subquery with the customers table to retrieve the customer's ID, first name, and last name, as well as their total amount spent. Finally, the WHERE clause filters the results to only include customers whose total amount spent is greater than the average total amount spent by all customers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Subquries in the select statement
&lt;/h2&gt;

&lt;p&gt;They  can be used to perform calculations based on data from another table. For example, if you have a table of orders and a table of products, you can use a subquery to calculate the total price of each order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT id, 
       (SELECT SUM(price * quantity)
        FROM products 
         WHERE order.id = products.order_id) AS total_price
FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can also be used to retrieve information from a table as we saw above we got the total_price of our commodities from products table. &lt;br&gt;
The big question however is, when is it advisable to use subqueries considering that each subquery requires additional computing power. There are certain factors to consider when selecting  a subquery:&lt;br&gt;
1.Data complexity&lt;br&gt;
Subqueries are useful when working with complex data queries that require multiple tables and conditions. If the query involves multiple subqueries or multiple nested subqueries, it can become very difficult to read and maintain. In this case, it may be better to break the query into smaller parts or consider using a different approach.&lt;/p&gt;

&lt;p&gt;2.Data volume&lt;br&gt;
Subqueries can be slow to execute when dealing with large datasets. If the query involves a large amount of data, it may be better to use a join or a different type of query.&lt;/p&gt;

&lt;p&gt;3.Performance&lt;br&gt;
The performance of a subquery can depend on the database engine being used. Some database engines can optimize subqueries for better performance. It's important to consider the performance implications when deciding whether to use a subquery.&lt;/p&gt;

&lt;p&gt;4.Maintainability&lt;br&gt;
Subqueries can make queries more difficult to read and maintain. It's important to consider whether the use of a subquery will make the query more or less maintainable in the long run.&lt;br&gt;
5.Query reuse&lt;br&gt;
If the same subquery is going to be used multiple times in different parts of a query or across multiple queries, it may be more efficient to create a view or a temporary table instead of using a subquery.&lt;/p&gt;
&lt;h2&gt;
  
  
  Corelated Subqueries
&lt;/h2&gt;

&lt;p&gt;It is a type of subquery which uses the values from the outer query to generate results, by referencing one or more columns in the main query. It is always re-run for every new row generated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM employees AS e
WHERE e.salary &amp;gt; (
     SELECT AVG(salary)
     FROM employees
     WHERE dpt_id = e.dpt_id
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is also very possible to have a nested subquery. Lets select an employee's name and the average salary he received in the first quarter of the year.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
     e.name AS employee,
     (SELECT AVG(jan_pay + feb_pay + mar_pay),
      FROM salary AS s
      WHERE s.empl_id = e.id
          AND id IN (
               SELECT id 
               FROM salary 
               WHERE year == 2023
               ) AS avg_salary
FROM employees AS e
GROUP BY employee;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The subquery in the SELECT clause is dependent on the equality of the salary and the employees id, which is also  dependent on the year column in the salary table.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Table Expressions(CTEs)
&lt;/h2&gt;

&lt;p&gt;Subquries are a good way of bringing multiple unrelated tables together and also simplifying calculations. However, having multiple nested subqueries can be hard to read and understand as discussed above. That is why a solution to this can be using CTEs.&lt;br&gt;
CTEs are declared ahead of the main query using a WITH statement and referenced later in the FROM statement.&lt;br&gt;
It  can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement in SQL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH s AS (
   SELECT emp_id, id,
         AVG(jan_pay + feb_pay + mar_pay) AS q1
   FROM salary
   WHERE year == 2023
   GROUP BY id
)
SELECT e.name AS employee,
       s.q1
FROM employees AS e
INNER JOIN s
ON e.id = s.emp_id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CTEs are better than subqueries because they are run only once and then stored in memory, hence reducing the amount of time required to run the query.&lt;br&gt;
You can also have multiple CTEs, separated by a comma but make sure you join them in their respective order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH max_salary AS (
  SELECT department_id, MAX(salary) AS max_salary
  FROM salary
  GROUP BY department_id
), 
avg_salary AS (
  SELECT department_id, AVG(salary) AS avg_salary
  FROM salary
  GROUP BY department_id
)
SELECT employees.*, max_salary.max_salary, avg_salary.avg_salary
FROM employees
JOIN max_salary ON employees.department_id = max_salary.department_id
JOIN avg_salary ON employees.department_id = avg_salary.department_id
WHERE employees.salary = max_salary.max_salary

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Window Functions
&lt;/h2&gt;

&lt;p&gt;Perform a set of operations that are somehow related to the current row, similar to group by but rows are not groupd into a single row.&lt;br&gt;
They are called "window" functions because they operate on a "window" of rows, defined by an OVER clause, rather than on individual rows. &lt;br&gt;
Some use cases of window functions include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fetching values before and after the current row.&lt;/li&gt;
&lt;li&gt;Ranking row values.&lt;/li&gt;
&lt;li&gt;Calculating averages
See the example below:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT transaction_id, 
       sales_amount, 
       SUM(sales_amount) OVER() AS total_sales
FROM sales_transactions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This sums the sales_amount per amount and displays it as total_sales.The first row will contain its own value in the total sales column, the second row will contain a sum of the first and second row and so on.&lt;br&gt;
At any given point we might want to give each of our rows a value in order to easily access it. This can be done using the &lt;br&gt;
ROW_NUMBER() function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT transaction_id, 
       sales_amount, 
       ROW_NUMBER() OVER() AS row_n
FROM sales_transactions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the above query will contain an extra row called &lt;em&gt;row_n&lt;/em&gt; with row numbers from 1 and so on.&lt;br&gt;
It is possible that we want the values in the row_n columns to be ordered in a certain way. We can thus use the &lt;em&gt;ORDER BY&lt;/em&gt; clause in OVER() function as shown:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT transaction_id, 
       sales_amount, 
       ROW_NUMBER() OVER(ORDER BY sales_amount DESC) AS row_n
FROM sales_transactions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The row_n values will now be sorted starting from the one with the highest sales order to the lowest.&lt;/p&gt;

&lt;p&gt;We can generate ranks instead of ordering as well  using the RANK() window function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT transaction_id, 
       sales_amount, 
       SUM(sales_amount) RANK() OVER() AS total_sales
FROM sales_transactions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that window functions are produced after other queries have been processed. It uses the generated table to find its results.&lt;br&gt;
As stated earlier we can also use window functions to compare the current row and the one before it. This is made possible using the LAG() function. Example:&lt;/p&gt;

&lt;p&gt;Assuming we have a games table with &lt;strong&gt;year, champion,medals,match and other columns in it&lt;/strong&gt;. We can find out which champion won recurrently.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
      year,match, champion
      LAG(champion) OVER(
      ORDER BY year ASC) AS last_champion
FROM games
ORDER BY year ASC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now compare the champion and last_champion columns and find recurrent champions.&lt;br&gt;
The above query will produce the values for last_champion regardless of weather the matches are similar or not. For example if we had matches for hockey and rollball within the same year it will not provide the last_champion for each separate match but just generalize them.&lt;br&gt;
To solve this , we use a &lt;strong&gt;PARTITION BY&lt;/strong&gt; clause as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
      year,match, champion
      LAG(champion) OVER(
      PARTITION BY match
      ORDER BY year ASC) AS last_champion
FROM games
ORDER BY year ASC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Window partitions&lt;/strong&gt; also group the result set of a row into a smaller group so that window functions can be applied to those subsets separately.&lt;br&gt;
Lets look at another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT department_id, 
       employee_id, 
       salary, 
       AVG(salary) OVER (PARTITION BY department_id
                              ) AS avg_salary
FROM employees
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The results will be first partitioned then average salary calculated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sliding Windows
&lt;/h2&gt;

&lt;p&gt;They enable us to perform calculations relative to the current window of the dataset.&lt;br&gt;
The window is defined by a rage of rows that "slide " or move through the partition based on  a specified ordering.&lt;br&gt;
The syntax is as shown:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ROWS BETWEEN &amp;lt;start&amp;gt; AND &amp;lt;finish&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The start and finish can be replaced with PRECEDING(comes before), FOLLOWING(comes after), UNBOUNDED PRECEDING(all rows in the partition up to and including the current row), UNBOUNDED FOLLOWING, CURRENT ROW&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT date,
       jan_pay,
       SUM (jan_pay)
       OVER(ORDER BY date ROWS BETWEEN
            UNBOUNDED PRECEDING AND CURRENT ROW)
       AS jan_total
FROM salary
WHERE year = 2023;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Fetching
&lt;/h2&gt;

&lt;p&gt;They include functions that enable us to get certain values from different parts of the table or partition into one row.&lt;br&gt;
The &lt;strong&gt;LAG()&lt;/strong&gt; function discussed above is also one of them.&lt;br&gt;
The &lt;strong&gt;LEAD()&lt;/strong&gt;  function is used return the values after the current row. Lets use the LAG() example shown above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
      year,match, champion
      LEAD(champion) OVER(
      ORDER BY year ASC) AS following_champion
FROM games
ORDER BY year ASC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above query compares the current champion and the champion after him.&lt;br&gt;
&lt;strong&gt;FIRST_VALUE&lt;/strong&gt; returns the first value in the table or partition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
      year,match, champion
      FIRST_VALUE(champion) OVER(
      ORDER BY year ASC) AS first
FROM games
ORDER BY year ASC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;LAST_VALUE&lt;/strong&gt; returns the last value in the table or partition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
      year,match, champion
      LAST_VALUE(champion) OVER(
      ORDER BY year ASC) AS last
FROM games
ORDER BY year ASC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  NOTE
&lt;/h3&gt;

&lt;p&gt;A sliding window can also be defined as a frame. Without  frame the &lt;strong&gt;LAST_VALUE()&lt;/strong&gt; function would return the current row. Efficient example of the &lt;strong&gt;LAST_VALUE()&lt;/strong&gt; would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
      year,match, champion
      LAST_VALUE(champion) OVER(
      ORDER BY year ASC
      ROWS BETWEEN UNBOUNDED PRECEDING
      AND CURRENT ROW)) AS last
FROM games
ORDER BY year ASC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are two types of frames, &lt;strong&gt;RANGE BETWEEN&lt;/strong&gt; and &lt;strong&gt;ROWS BETWEEN&lt;/strong&gt;. RANGE BETWEEN however lists duplicate values when used with the OVER's ORDER BY subclause as a single entity. Hence ROWS BETTWEEN is mostly used over RANGE BETWEEN.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ranking
&lt;/h2&gt;

&lt;p&gt;It involves arranging the data in a partition or table in a particular order.&lt;br&gt;
The ranking functions include:&lt;br&gt;
&lt;strong&gt;ROW_NUMBER()&lt;/strong&gt;: It assigns each row a unique number even if the row values are the same. Check the window functions example for more clarification.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RANK()&lt;/strong&gt;: It orders the values of a table or partition giving rows with identical values the same number and skipping one number over to the next value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DENSE_RANK()&lt;/strong&gt;: It ranks values in a particular table or partition giving the same number for identical values but does not skip a number to the next value, hence mostly preferred.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT transaction_id, 
       sales_amount, 
       SUM(sales_amount) DENSE_RANK() OVER() AS rank_sales
FROM sales_transactions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will rank the total sales  giving identical values the same rank then move to the next total_sales value without skipping a number.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;transaction_id&lt;/th&gt;
&lt;th&gt;sales_amount&lt;/th&gt;
&lt;th&gt;rank_sales&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;iutio&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ghjkl&lt;/td&gt;
&lt;td&gt;48&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;frtyu&lt;/td&gt;
&lt;td&gt;48&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;lkjhg&lt;/td&gt;
&lt;td&gt;36&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Paging
&lt;/h2&gt;

&lt;p&gt;This includes splitting data into approximately equal chunks which enables us to easily analyze data piece by piece.&lt;br&gt;
Paging in sql is done using  the &lt;strong&gt;NTILE(n)&lt;/strong&gt; which splits data into approximately n equal pages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT transaction_id, 
       sales_amount, 
       NTILE(15) OVER() AS page1
FROM sales_transactions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above query the rows are divided into 15 buckets when called.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pivoting
&lt;/h2&gt;

&lt;p&gt;It makes it easier to understand and analyze data by converting columns into rows. It is very valuable when preparing data for visualization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE EXTENSION IF NOT EXISTS tablefunc;
SELECT * FROM CROSSTAB ( $$
     SELECT country,
            year,
            COUNT(*) :: INTEGER AS awards
     FROM medals
     WHERE year IN (2022, 2023)
     GROUP BY country, year
     ORDER BY country ASC;
      $$) AS ct ( country VARCHAR, 
                 "2022" INTEGER,
                  "2023" INTEGER)
 ORDER BY country ASC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above query converts the country and year column in the medal table into rows and splits the year into 2022 and 2023.&lt;br&gt;
Each row is also given their respective datatype.&lt;/p&gt;

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

&lt;p&gt;In conclusion, this article has covered several intermediate SQL topics that can help you gain a deeper understanding of SQL and data manipulation. By mastering these concepts, you will be able to write more complex SQL queries and work with large datasets more efficiently. I hope you found this article helpful in your SQL journey and that it has provided you with valuable insights and knowledge. Keep practicing and exploring the world of SQL to become a proficient data analyst or developer. Stay curious and stay golden!  &lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>postgres</category>
      <category>dataengineering</category>
    </item>
    <item>
      <title>SQL101: Introduction to SQL</title>
      <dc:creator>Marriane Akeyo</dc:creator>
      <pubDate>Fri, 17 Feb 2023 06:27:12 +0000</pubDate>
      <link>https://dev.to/marrie/sql101-introduction-to-sql-for-data-analysis-4i1b</link>
      <guid>https://dev.to/marrie/sql101-introduction-to-sql-for-data-analysis-4i1b</guid>
      <description>&lt;p&gt;Structured Query Language(SQL) is a database language that is used to manage and retrieve data stored in a database, such that it can be easily accessed and manipulated. It is a common language recognized by Database Management Systems(DBMS) like MYSQL. We are going to make reference to &lt;strong&gt;relational databases&lt;/strong&gt; since they are mostly used. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Relational database&lt;/strong&gt;:Store data in form of tables. As the name suggests, a relation ties two records or tables together.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Table&lt;/strong&gt;: made up of rows and columns.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Rows&lt;/strong&gt;: A single record in a table.eg one person's details in a User's table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Column&lt;/strong&gt;: value of a particular type eg First_Name column in table person.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Statement&lt;/strong&gt;: A text that is uniformly recognized as a valid command by the database.They usually end with a semicolon.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE users(
id INTEGER,
first_name VARCHAR(60),
last_name VARCHAR(60)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;:SQL clauses like CREATE and TABLE are usually written in capital letters.&lt;br&gt;
SQL syntax are divided into &lt;em&gt;data definition language&lt;/em&gt; and &lt;em&gt;data manipulation language&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Definition Language
&lt;/h2&gt;

&lt;p&gt;They are used to build and modify the structure of your tables,views, indexes and other objects in the database. Examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CREATE TABLE statement: creates a new table with the specified columns and data types.&lt;/li&gt;
&lt;li&gt;CREATE DATABASE statement: creates a new database.&lt;/li&gt;
&lt;li&gt;ALTER TABLE statement: used to add, modify or delete columns in an existing table.&lt;/li&gt;
&lt;li&gt;ALTER DATABASE statement: modifies an existing database.&lt;/li&gt;
&lt;li&gt;DROP TABLE statement:  used to delete an existing table and all of its data.&lt;/li&gt;
&lt;li&gt;CREATE INDEX statement: creates an index (search key)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Data Manipulation Language
&lt;/h2&gt;

&lt;p&gt;They include statements used to work with data in the database.Some examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;INSERT Statement:used to insert new data into a table.&lt;/li&gt;
&lt;li&gt;UPDATE Statement:used to modify the existing data in a table.&lt;/li&gt;
&lt;li&gt;DELETE Statement:used to delete data from a table.&lt;/li&gt;
&lt;li&gt;SELECT Statement:used to retrieve data from one or more tables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We shall look at examples of these statements in details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a database
&lt;/h2&gt;

&lt;p&gt;Lets now create a database named &lt;em&gt;customers&lt;/em&gt; that we shall be working from.The syntax is as shown:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CREATE DATABASE database_name;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now to our database&lt;br&gt;
&lt;code&gt;CREATE DATABASE customers;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Some databases might require you to use (``) when defining database schemas eg creating tables and databases.As shown below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
CREATE DATABASE `customers`;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can also be able to view all the databases we have created using the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SHOW DATABASES;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Assign a user to our database
&lt;/h2&gt;

&lt;p&gt;Once you are able to access your database either through MySQL prompt or any other way, it is advisable to create a new user in order to avoid using the root user and causing damage to the whole database in general.&lt;br&gt;
Syntax:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CREATE USER 'username'@'host' IDENTIFIED BY 'password';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lets create a user named marrie, at localhost, with a password pass_123.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CREATE USER 'marrie'@'localhost' IDENTIFIED BY 'Pass_123';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can now grant our new user some privileges. The syntax is as shown:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;GRANT PRIVILEGE ON database.table TO 'username'@'host';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lets grant our user access to the customers database.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;GRANT PRIVILEGE ON customers.* TO 'marrie'@'localhost';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It’s good practice to run the FLUSH PRIVILEGES command. This will free up any memory that the server cached as a result of the preceding CREATE USER and GRANT statements:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;FLUSH PRIVILEGES;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Working in our current database
&lt;/h2&gt;

&lt;p&gt;Now that we have our database assigned to a user, we can start issuing commands to create tables and insert our data in the tables. First we move to our database.&lt;br&gt;
Syntax:&lt;br&gt;
&lt;code&gt;USE database;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;code&gt;USE customers;&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a table
&lt;/h3&gt;

&lt;p&gt;Once inside our customers database , we can now be able to create various tables as discussed above.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
CREATE TABLE table_name (&lt;br&gt;
  column1 datatype constraint,&lt;br&gt;
  column2 datatype constraint,&lt;br&gt;
  column3 datatype constraint&lt;br&gt;
);&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lets create a table users as an example.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
CREATE TABLE users (&lt;br&gt;
  id INT PRIMARY KEY,&lt;br&gt;
  first_name VARCHAR(50) NOT NULL,&lt;br&gt;
  last_name VARCHAR(50) NOT NULL,&lt;br&gt;
  email VARCHAR(100) UNIQUE,&lt;br&gt;
  birth_day INT,&lt;br&gt;
  born_day DATE()&lt;br&gt;
);&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
You can also confirm if the table is created using the following command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SHOW TABELS;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note that the exact syntax and behavior of the SHOW TABLES statement may vary slightly depending on the specific SQL database management system being used.&lt;/p&gt;

&lt;h3&gt;
  
  
  Alter table
&lt;/h3&gt;

&lt;p&gt;We can also change the outlook of the table in case  we want to add, delete, or modify columns, constraints, indexes, and other attributes of a table.&lt;br&gt;
Syntax:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ALTER TABLE table_name action;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Where &lt;strong&gt;table_name&lt;/strong&gt; is the name of the table you want to modify, and &lt;strong&gt;action&lt;/strong&gt; is the specific alteration you want to make to the table.&lt;br&gt;
Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Add a new column to an existing table:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
ALTER TABLE users &lt;br&gt;
ADD COLUMN age INT;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Modify an existing column in a table:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
ALTER TABLE users&lt;br&gt;
MODIFY birth_day DATE();&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Delete a column from an existing table:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
ALTER TABLE user&lt;br&gt;
DROP COLUMN age;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a primary key to a table:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
ALTER TABLE user&lt;br&gt;
ADD CONSTRAINT PRIMARY KEY (id)&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note that the exact syntax and behavior of the ALTER TABLE statement may vary slightly depending on the specific SQL database management system being used. Also, it's important to be careful when using ALTER TABLE, as modifying the structure of a table can affect the data that is stored in the table.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inserting data in a table
&lt;/h3&gt;

&lt;p&gt;We have our table, we can now feed it with some data.&lt;br&gt;
Syntax:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;INSERT INTO table_name (column_name_1, column_name_2, column_name_3)VALUES (value_1, value_2, value_3);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note that the order of the values in the "VALUES" clause must match the order of the columns in the "INSERT INTO" statement.&lt;br&gt;
Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;INSERT INTO users (id, first_name, last_name, email, birth_day, born_day)&lt;br&gt;
VALUES (1, 'John', 'Doe', 'johndoe@example.com', '1980-01-01', '2000-01-01');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can also insert multiple rows at once by separating each set of values with a comma, like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
INSERT INTO users (id, first_name, last_name, email, birth_day, born_day)&lt;br&gt;
VALUES&lt;br&gt;
  (1, 'John', 'Doe', 'johndoe@example.com', '1980-01-01', '2000-01-01'),&lt;br&gt;
  (2, 'Jane', 'Kim', 'janekim@example.com', '1985-02-02', '2005-02-02'),&lt;br&gt;
  (3, 'Bob', 'Smith', 'bobsmith@example.com', '1990-03-03', '2010-03-03');&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
This would insert three new rows into the "users" table, one for each set of values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Viewing the data in the table.
&lt;/h3&gt;

&lt;p&gt;Now lets verify that we added something to our table.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT * FROM users;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You will notice that the above displays all the data in the users table.&lt;br&gt;
We can also select a specific column from the table.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT first_name, last_name FROM users;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can also place a condition within  which the data we want can be collected.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT first_name, last_name FROM users WHERE birth_day='1980-01-01';&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating the data in the database
&lt;/h2&gt;

&lt;p&gt;It is also possible to modify the data we inserted in the table in case you need to change the data already existing in the database table.&lt;br&gt;
Syntax:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
UPDATE table_name&lt;br&gt;
SET column1 = value1, column2 = value2, ...&lt;br&gt;
WHERE condition;&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
Lets change the  name of John Doe to Joe Doo&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
UPDATE users&lt;br&gt;
SET first_name = 'Joe', last_name = 'Doo'&lt;br&gt;
WHERE email = 'johndoe@example.com';&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Deleting a record from the table
&lt;/h2&gt;

&lt;p&gt;We can also remove records from a table.&lt;br&gt;
Syntax:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
DELETE FROM table_name&lt;br&gt;
WHERE condition;&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
Example&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
DELETE FROM users &lt;br&gt;
WHERE born_day &amp;lt; 2010-03-03;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  SQL Constraints
&lt;/h2&gt;

&lt;p&gt;Constraints ensure that the data in a table is consistent and accurate by limiting the type of data that can be inserted into a table, or by defining relationships between tables. Here are some examples of constraints in SQL:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NOT NULL
This constraint ensures that a column cannot have a NULL value.
Example:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;ALTER TABLE users MODIFY last_name VARCHAR(60) NOT NULL&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PRIMARY KEY 
This constraint uniquely identifies each record in a table. A primary key column cannot have NULL values, and the values in the column must be unique. 
Example:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;ALTER TABLE users ADD PRIMARY KEY (id)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;FOREIGN KEY &lt;br&gt;
This constraint establishes a relationship between two tables by defining a column in one table as a foreign key that refers to the primary key of another table.&lt;br&gt;
&lt;code&gt;`&lt;br&gt;
ALTER TABLE users &lt;br&gt;
ADD CONSTRAINT fk_customer FOREIGN KEY(age) REFERENCES customers(id);&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;UNIQUE&lt;br&gt;
This constraint ensures that the values in a column are unique.&lt;br&gt;
&lt;code&gt;ALTER TABLE users MODIFY email VARCHAR(100) UNIQUE&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CHECK&lt;br&gt;
This constraint allows you to specify a condition that must be true for a record to be inserted or updated.&lt;br&gt;
&lt;code&gt;ALTER TABLE customers ADD CONSTRAINT ck_age CHECK (age &amp;gt;= 18);&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ENUM&lt;br&gt;
This constraint contains a string object with a value chosen from a list of permitted values.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
-- Creating the table&lt;br&gt;
CREATE TABLE shops (&lt;br&gt;
id INTEGER PRIMARY KEY,&lt;br&gt;
name VARCHAR (55),&lt;br&gt;
quality ENUM ('High','Average','Low')&lt;br&gt;
);&lt;br&gt;
-- Insert into the table&lt;br&gt;
INSERT INTO shops VALUES(1 'Johny' 'Low');&lt;br&gt;
INSERT INTO shops VALUES(1 'Doe' 'Average');&lt;br&gt;
INSERT INTO shops VALUES(1 'Marrie' 'High');&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SET
A set can have zero or more values chosen from a list of permitted values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
CREATE TABLE students (&lt;br&gt;
id INTEGER PRIMARY KEY,&lt;br&gt;
name VARCHAR(60),&lt;br&gt;
cert SET('A1','A2','B1','B2')&lt;br&gt;
);&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
Each student can have O,1 or more of these certificates. In ENUM however, you can have only one distinct value.&lt;br&gt;
&lt;code&gt;INSERT INTO students VALUES(1, 'Paul', A1,B1,B2);&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Aggregate Functions in SQL
&lt;/h2&gt;

&lt;p&gt;They are used to perform various operations on the data stored , hence enabling us to compute data not found in the database.&lt;br&gt;
Some of the most commonly used functions include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AVG():It returns the average value of a numeric column.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT AVG(column_name) FROM table_name;&lt;br&gt;
Example: SELECT AVG(age) FROM users;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;COUNT():It returns the number of rows in a table or the number of non-null values in a column.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT COUNT(column_name) FROM table_name;&lt;br&gt;
Example: SELECT COUNT(*) FROM users;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MAX():It returns the maximum value in a column.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT MAX(column_name) FROM table_name;&lt;br&gt;
Example: SELECT MAX(age) FROM users;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MIN():It returns the minimum value in a column.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT MIN(column_name) FROM table_name;&lt;br&gt;
Example: SELECT MIN(age) FROM users;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SUM():It returns the sum of values in a numeric column.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT SUM(column_name) FROM table_name;&lt;br&gt;
Example: SELECT SUM(age) FROM users;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UPPER(): It converts a string to uppercase.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT UPPER(column_name) FROM table_name;&lt;br&gt;
Example: SELECT UPPER(first_name) FROM user;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LOWER(): It converts a string to lowercase.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT LOWER(column_name) FROM table_name;&lt;br&gt;
Example: SELECT LOWER(last_name) FROM employees;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SUBSTRING():It extracts a sub string from a string.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT SUBSTRING(column_name, start_position, length) FROM table_name;&lt;br&gt;
Example: SELECT SUBSTRING(first_name, 1, 3) FROM user;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CONCAT():It concatenates two or more strings.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT CONCAT(string1, string2, ...) FROM table_name;&lt;br&gt;
Example: SELECT CONCAT(first_name, ' ', last_name) FROM users;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DATE():It returns the current date.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT DATE();&lt;br&gt;
Example: SELECT DATE();&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MONTH():It returns the month of a date.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT MONTH(date_column) FROM table_name;&lt;br&gt;
Example: SELECT MONTH(birth_day) FROM users;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;YEAR():It returns the year of a date.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT YEAR(date_column) FROM table_name;&lt;br&gt;
Example: SELECT YEAR(birth_day) FROM users;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DATEDIFF():It calculates the difference between two dates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT DATEDIFF(date1, date2) FROM table_name;&lt;br&gt;
Example: SELECT DATEDIFF(born_date, birth_day) FROM employees;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NOW():It returns the current date and time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
Syntax: SELECT NOW();&lt;br&gt;
Example: SELECT NOW();&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The where clause
&lt;/h3&gt;

&lt;p&gt;This clause is used to filter sql statements base on a certain condition or set of conditions.It is used in conjunction with the SELECT, UPDATE, DELETE statements to filter data from tables. Forexample:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT * FROM people WHERE age &amp;gt;= 18;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above example will select all the data for people above 18 years from the database.&lt;/p&gt;

&lt;h3&gt;
  
  
  The AND clause
&lt;/h3&gt;

&lt;p&gt;We can also combine our where clause with an AND clause in  order to select data based on multiple conditions. Lets view data about all developers above 18 years.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT * &lt;br&gt;
FROM people &lt;br&gt;
WHERE age &amp;gt;= 18&lt;br&gt;
AND job = 'dev';&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
We can also improve our comparison and add the &lt;strong&gt;OR&lt;/strong&gt; clause as shown.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT * &lt;br&gt;
FROM people &lt;br&gt;
WHERE (age &amp;gt;= 18 OR age &amp;lt; 40)&lt;br&gt;
AND (job = 'dev' OR job = 'IT');&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The BETWEEN clause
&lt;/h3&gt;

&lt;p&gt;Sometimes it might be necessary to return values within a specified range, inclusive of the start and end values.&lt;br&gt;
We can modify the example above as shown.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT * &lt;br&gt;
FROM people &lt;br&gt;
WHERE age&lt;br&gt;
BETWEEN 18 AND 40;&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
We can also combine the BETWEEN clause with AND / OR. Now lets pick data for all products with prices between 10 and 20 0r 30 and 40.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT *&lt;br&gt;
FROM products&lt;br&gt;
WHERE (price BETWEEN 10 AND 20) &lt;br&gt;
OR (price BETWEEN 30 AND 40);&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The IN and NOT IN clauses
&lt;/h3&gt;

&lt;p&gt;The IN clause is used to specify multiple values in a WHERE clause.Lets select name and age of people with 18,19 and 20 years of age.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT id, name, age&lt;br&gt;
FROM people&lt;br&gt;
WHERE age IN (18, 19, 20);&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
The NOT IN clause fulfills  the opposite of an IN  clause. Lets return data of people and their ages and name except those who are 18, 19 and 20.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT id, name, age&lt;br&gt;
FROM people&lt;br&gt;
WHERE age NOT IN (18, 19, 20);&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The LIKE clause
&lt;/h3&gt;

&lt;p&gt;It can be used in the WHERE clause to search for a pattern in a column.&lt;br&gt;
The '%' pattern matches 0, 1 or many characters in a text.&lt;br&gt;
The '_' pattern matches a single character.&lt;br&gt;
We can have a table called employee with the data shown bellow&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;department&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;John Smith&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Jane Doe&lt;/td&gt;
&lt;td&gt;dev&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Mike Brown&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Sarah Lee&lt;/td&gt;
&lt;td&gt;dev&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Bill Wong&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;We can use the '%' pattern to select employees whose name contain 'i' character.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT * &lt;br&gt;
FROM employees&lt;br&gt;
WHERE name LIKE '%i%';&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
This prints&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Mike Brown&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bill Wong&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;We can also select employees whose name begins with 'J' and contain 'ohn'.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT * &lt;br&gt;
FROM employees&lt;br&gt;
WHERE name LIKE '_ohn';&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
This prints&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;John Smith&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;NOT LIKE&lt;/strong&gt; - is used to find patterns that do not match the records specified.&lt;/p&gt;

&lt;h3&gt;
  
  
  The GROUPBY clause
&lt;/h3&gt;

&lt;p&gt;Used to put together rows with the same value in one column or set of columns and then perform aggregate functions (such as SUM, COUNT, AVG, MIN, MAX) on the grouped data.&lt;br&gt;
Now lets group the people column according to their ages&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT age, COUNT(*) as total_age&lt;br&gt;
FROM people&lt;br&gt;
GROUP BY age;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;age&lt;/th&gt;
&lt;th&gt;total_age&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;26&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;All the aggregate functions discussed above can be used with the GROUPBY clause. The column to be grouped by MUST be selected as well otherwise you will get an error.&lt;/p&gt;

&lt;h3&gt;
  
  
  ORDER BY clause
&lt;/h3&gt;

&lt;p&gt;It sorts the results of a query in ascending or descending order based on the selected columns.&lt;br&gt;
We can select the name and age columns from people and order them by their age in descending order.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT name, age&lt;br&gt;
FROM people&lt;br&gt;
ORDER BY age DESC;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note that by default the columns are ordered in ascending order. So you can remove the 'DESC' to maintain the same order.&lt;/p&gt;

&lt;p&gt;The GROUP BY and ORDER BY even the WHERE clause can be used in one SELECT query. When this happens, make sure the ORDER BY clause comes after the GROUP BY. For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT name, age, count(age) as total_age&lt;br&gt;
FROM people&lt;br&gt;
WHERE (age = 18 OR age =19)&lt;br&gt;
GROUP BY age, name&lt;br&gt;
ORDER BY age, name DESC;&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
In the query above we have grouped the people's data by age and name and arranged them in descending order.&lt;/p&gt;

&lt;h3&gt;
  
  
  The HAVING clause
&lt;/h3&gt;

&lt;p&gt;It is used to specify the conditions that must be met with the group in order to be included in the query result. It also helps us to filter based on the result of an aggregate functions which cannot be used in a WHERE clause.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT age, SUM(*)&lt;br&gt;
FROM people&lt;br&gt;
GROUP BY age&lt;br&gt;
HAVING SUM(*) &amp;gt; 100;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The query above prints the sum of each age category in the database with a sum greater than 100.&lt;/p&gt;

&lt;h2&gt;
  
  
  SQL JOINS
&lt;/h2&gt;

&lt;p&gt;Joins are used to combine two or more tables in order to easily see data from the two tables and their relationship.&lt;br&gt;
The join is specified in the &lt;strong&gt;FROM&lt;/strong&gt; clause of a &lt;strong&gt;SELECT&lt;/strong&gt; query.&lt;br&gt;
&lt;code&gt;SELECT * FROM users NATURAL JOIN orders&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above query creates a joined table with matched values on both tables. This establishes a one to many relationship between the user and the customers table. Meaning one user can be a customer of various products that the company offers. Note that the users who are not customers are not going to be selected.&lt;/p&gt;

&lt;h3&gt;
  
  
  Join Types
&lt;/h3&gt;

&lt;p&gt;A natural join however, does not take into consideration the primary key and the foreign key which are very vital in a table relationship. It only considers common columns in both tables.If there are any common columns that are not primary key and foreign key, they will also be matched and only one copy of the output produced.&lt;br&gt;
Lets look at some useful join types that can be used in place of a natural join.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;INNER JOIN
It returns common rows in both table, just like a natural join taking into consideration both the Primary Key and the Foreign Key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu0ce2r3favm7n5gnbl4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu0ce2r3favm7n5gnbl4e.png" alt=" " width="273" height="184"&gt;&lt;/a&gt;&lt;br&gt;
&lt;code&gt;`&lt;br&gt;
SELECT *&lt;br&gt;
FROM users&lt;br&gt;
INNER JOIN customers&lt;br&gt;
ON user.id = customers.user_id;&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
You can also specify an alias for the tables for easy identification as shown.&lt;br&gt;
&lt;code&gt;`&lt;br&gt;
SELECT *&lt;br&gt;
FROM users AS u&lt;br&gt;
INNER JOIN customers AS c&lt;br&gt;
ON u.id = c.user_id;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OUTER JOIN&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One effect of INNER JOIN and NATURAL JOIN is that unmatched primary key is dropped ie a user who is not a customer of the company is simply dropped.&lt;br&gt;
We can use an OUTER JOIN to include such customers.&lt;br&gt;
A &lt;strong&gt;FULL OUTER JOIN&lt;/strong&gt; returns all the rows from both tables along with NULL values where there is no match in either table. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6yjmj30hufqyu1inugn1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6yjmj30hufqyu1inugn1.png" alt=" " width="220" height="149"&gt;&lt;/a&gt;&lt;br&gt;
&lt;code&gt;`&lt;br&gt;
SELECT *&lt;br&gt;
FROM users&lt;br&gt;
FULL OUTER JOIN customers&lt;br&gt;
ON users.id = customers.user_id;&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
We can also have a FULL OUTER JOIN without intersection. Meaning we shall return all the records matching the ON clause, excluding rows found in both tables.&lt;br&gt;
&lt;code&gt;`&lt;br&gt;
SELECT *&lt;br&gt;
FROM users&lt;br&gt;
FULL OUTER JOIN customers&lt;br&gt;
ON users.id = customers.user_id;&lt;br&gt;
WHERE users.id IS NULL&lt;br&gt;
OR customers.user_id IS NULL&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To give users a higher priority we use &lt;strong&gt;LEFT OUTER JOIN&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fej04xe2hx13dn31zssn6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fej04xe2hx13dn31zssn6.png" alt=" " width="220" height="149"&gt;&lt;/a&gt;&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
SELECT *&lt;br&gt;
FROM users&lt;br&gt;
LEFT OUTER JOIN customers&lt;br&gt;
ON users.id = customers.user_id;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
To give customers a higher priority we use a &lt;strong&gt;RIGHT OUTER JOIN&lt;/strong&gt; ie all values in the right and matching values in the left and NULL where a value in the left is missing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4g9ycgklmj9hcoi39q6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4g9ycgklmj9hcoi39q6.png" alt=" " width="220" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Self Joins
This is a selfish type of join where a table is joined to itself as if it were two different tables with different aliases for each instances of the table.
Consider the student table below:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;student_id&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Jane&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Tom&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Sarah&lt;/td&gt;
&lt;td&gt;null&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Michael&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Jessica&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;We can do a self join of the table above as shown below:&lt;br&gt;
&lt;code&gt;`&lt;br&gt;
SELECT s.name AS student, l.name as leader&lt;br&gt;
FROM student AS s&lt;br&gt;
LEFT JOIN student AS l&lt;br&gt;
ON s.student_id = l.id;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The output of the above code will be:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;student&lt;/th&gt;
&lt;th&gt;leader&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;Tom&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Jane&lt;/td&gt;
&lt;td&gt;Tom&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tom&lt;/td&gt;
&lt;td&gt;Sarah&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sarah&lt;/td&gt;
&lt;td&gt;null&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Michael&lt;/td&gt;
&lt;td&gt;Sarah&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Jessica&lt;/td&gt;
&lt;td&gt;Jane&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Cross Joins
They are used to create all the possible combinations of two tables.It is also known as a Cartesian product, since it produces a result set that contains all possible combinations of rows between the two tables. It is similar to the Inner Join, where the join condition is not available with this clause.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs2leexpmi799vgqjsbjj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs2leexpmi799vgqjsbjj.png" alt=" " width="350" height="210"&gt;&lt;/a&gt;&lt;br&gt;
The syntax is as follows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT *&lt;br&gt;
FROM table1&lt;br&gt;
CROSS JOIN table2;&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
Consider the following department and students table:&lt;br&gt;
departments&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Medicine&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;students&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;department&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;Medicine&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The query will be as shown:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT *&lt;br&gt;
FROM students&lt;br&gt;
CROSS JOIN departments;&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
The resultant table will be as shown:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;department&lt;/th&gt;
&lt;th&gt;id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Medicine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Medicine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;Medicine&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;Medicine&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;Medicine&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Medicine&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Note that in the result set, there are 9 rows, which is the product of the number of rows in students (3) and the number of rows in departments (3).&lt;/p&gt;

&lt;h2&gt;
  
  
  Set Operations
&lt;/h2&gt;

&lt;p&gt;They are used to manipulate and combine data from multiple tables and queries.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UNION&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The UNION operator combines the result sets of two or more SELECT statements into a single result set. The columns in the SELECT statements must have the same data type.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT first_name, last_name&lt;br&gt;
FROM users&lt;br&gt;
UNION&lt;br&gt;
SELECT prod_1, prod_2&lt;br&gt;
FROM customer;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;INTERSECT&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The INTERSECT operator returns only the rows that appear in both result sets of two or more SELECT statements.&lt;br&gt;
&lt;code&gt;`&lt;br&gt;
SELECT first_name, last_name&lt;br&gt;
FROM users&lt;br&gt;
INTERSECT&lt;br&gt;
SELECT prod_1, prod_2&lt;br&gt;
FROM customer;&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
-EXCEPT&lt;/p&gt;

&lt;p&gt;The EXCEPT operator returns only the distinct rows that appear in the first result set of two SELECT statements but not in the second result set.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
SELECT first_name, last_name&lt;br&gt;
FROM users&lt;br&gt;
EXCEPT&lt;br&gt;
SELECT prod_1, prod_2&lt;br&gt;
FROM customer;&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;SQL is  a very powerful query language , with very many advantages when it comes to data analysis and manipulation. There is a lot that is not covered in this article, I just picked a few things I found worth recalling. For more information, visit the &lt;a href="https://www.w3schools.com/sql/sql_quickref.asp" rel="noopener noreferrer"&gt;W3school SQL documentation&lt;/a&gt; and &lt;a href="https://docs.snowflake.com/en/sql-reference-commands" rel="noopener noreferrer"&gt;snowflake&lt;/a&gt; as well. Once you understand the SQL syntax you can referrer to this &lt;a href="https://learnsql.com/blog/sql-basics-cheat-sheet/" rel="noopener noreferrer"&gt;cheat-sheet&lt;/a&gt; for a quick reminder of the queries. Happy coding and as always stay golden dev!!&lt;/p&gt;

</description>
      <category>career</category>
      <category>management</category>
      <category>productivity</category>
      <category>mentorship</category>
    </item>
    <item>
      <title>Consuming Restful Apis in reactjs</title>
      <dc:creator>Marriane Akeyo</dc:creator>
      <pubDate>Tue, 10 May 2022 21:10:40 +0000</pubDate>
      <link>https://dev.to/marrie/consuming-restful-apis-in-reactjs-360p</link>
      <guid>https://dev.to/marrie/consuming-restful-apis-in-reactjs-360p</guid>
      <description>&lt;p&gt;If you are a react developer and you would like to learn more about consuming restapis ,or you are having issues with consuming rest apis then this article is for you.&lt;br&gt;
&lt;strong&gt;Rest apis&lt;/strong&gt;can be defined as endpoints which we use to fetch data to and from users in our react application.They are very usefull in creating a connection between our client and our backend.So when we talk about &lt;strong&gt;consuming&lt;/strong&gt; restapis, we try to figure out how to call our endpoints in our frontend application so that when a user of aour application wants to make requests to our database he can do so easiy.&lt;/p&gt;

&lt;p&gt;Consuming REST apis in react can be done in various ways .However, in this article we are going to discuss two most porpular ways known as &lt;strong&gt;Axios&lt;/strong&gt; (a promise-based HTTP client) and &lt;strong&gt;Fetch API&lt;/strong&gt; (a browser in-built web API).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;:_ A good knowledge of ReactJS, React Hooks, JavaScript and CSS will come in handy as you work your way throughout this post's content._&lt;/p&gt;
&lt;h2&gt;
  
  
  Using the Fetch Api
&lt;/h2&gt;

&lt;p&gt;The fetch() method is an in-built Javascript method for fetching resources from the server or an API endpoint.This method always takes in a compulsory URL argument containing the path to the resource you want to fetch and returns a promise that points to the request's response weather it was successful or not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parameters we can use for the Fetch API&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Resource:This is the url with the path to the resource to       be fetched.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;headers&lt;br&gt;
This is for specifying any headers you would like to add to your request, usually contained in an object or an object literal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;body&lt;br&gt;
This is for specifying a body that you want to add to your request: this can be a Blob, BufferSource, FormData, URLSearchParams, USVString, or ReadableStream object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;mode&lt;br&gt;
This is for specifying the mode you want to use for the request, e.g., cors, no-cors, or same-origin.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;credentials&lt;br&gt;
This for specifying the request credentials you want to use for the request, this option must be provided if you consider sending cookies automatically for the current domain.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fetch request syntax&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('https://github.com/Marriane791?tab=repositories')
  .then(response =&amp;gt; response.json())
  .then(data =&amp;gt; console.log(data));

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

&lt;/div&gt;



&lt;p&gt;In the code above , I used my personal github repository as an example url.The response is just a regular HTTP response and not the actual JSON. In order to get the JSON body content from the response, we’d have to change the response to actual JSON using the json() method on the response. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using axios&lt;/strong&gt;&lt;br&gt;
Axios() is a promised based method which is commonly preffered when fetching api endpoints in javscript.Since it is a promised based method , we take advantage of the async await just as we shall see below to make our requests.Axios also contains an in-built feature that protects the client aganist cross-site fogery.It also enables the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Streamlined error handling.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Request and response interception.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Upload progress.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Response timeout .&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cancellation of requests&lt;br&gt;
Just to mention a few.The syntax includes:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;axios.post(url,{data}).then(response =&amp;gt; {
console.log(response.data)
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now freely view your application in the browser,right click then inspect your code to view the actual data being fetched from the api.&lt;br&gt;
Example:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepei6djrc64r2g46x9ft.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepei6djrc64r2g46x9ft.png" alt="axios example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is an example of a code snippet from an application  I created called diagnostic disease prediction which can be found &lt;a href="https://github.com/Marriane791/communicable-disease-diagnostic-system" rel="noopener noreferrer"&gt;here&lt;/a&gt; which enables doctors to chat with each other.&lt;br&gt;
The method getChats, contains all the logic to  fetch our data from an endpoint.The method is then called in the useEffect method and the implementation continues.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>womenintech</category>
    </item>
    <item>
      <title>The Ultimate Guide to Getting Started in Data Science</title>
      <dc:creator>Marriane Akeyo</dc:creator>
      <pubDate>Sat, 02 Apr 2022 19:16:27 +0000</pubDate>
      <link>https://dev.to/marrie/the-ultimate-guide-to-the-ultimate-guide-to-getting-started-in-data-science-cad</link>
      <guid>https://dev.to/marrie/the-ultimate-guide-to-the-ultimate-guide-to-getting-started-in-data-science-cad</guid>
      <description>&lt;p&gt;We can define data science as an inter disciplinary field that uses scientific methods to prepare data for analysis, including cleansing, aggregating, and manipulating the data to perform advanced data analysis.In this article I am going to provide a guide on the various topics you can cover ,in order to get a head start at this awesome field.&lt;/p&gt;

&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;1.Introduction to Python, Mastering Python basics.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation.Due to its simple structure and the qualifications above it is highly preferred for data science and data analytics.You can visit my posts &lt;a href="https://dev.to/marrie/introduction-to-modern-python-48hh"&gt;on introduction to python&lt;/a&gt; and &lt;a href="https://dev.to/marrie/data-structures-and-algorithims-with-python-3k1d"&gt;python data structures&lt;/a&gt; to get a quick introduction to this wonderfull language.&lt;br&gt;
A quick and easy way to run your python codes would be using Jupyter notebook on anaconda.Jupyter notebook is a text editor which comes in very handy when advancing from writing simple python codes to using python libraries to perform data science tasks.&lt;br&gt;
Anaconda on the other hand, is an open distribution of the R and python programming languages that simplifies package management and deployment.You are first required to install anaconda then install jupyter notebook present in anaconda.&lt;br&gt;
&lt;a href="https://medium.com/codingthesmartway-com-blog/getting-started-with-jupyter-notebook-for-python-4e7082bd5d46"&gt;Here&lt;/a&gt; is a blog post that sheds more light on jupyter notebook for python and anaconda.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;:Once you have anaconda installed and you are familiar with vs code , you can install jupyter notebook as an extension and create a new file using the (.ipynb) extension to use jupyter notebook  in vs code.&lt;br&gt;
Once you run a file,you will be prompted to select the environment you want to use to run your notebook.Ensure you select the &lt;em&gt;&lt;strong&gt;base&lt;/strong&gt;&lt;/em&gt; environment to smoothen your process.&lt;/p&gt;

&lt;p&gt;You can also use the &lt;strong&gt;google colab&lt;/strong&gt; which is much easier since all the packages are pre installed ,all you need to do is import and use them,so long as you have a stable network connection.Please opt for this option after you get the basics right so as to ease your coding process&lt;/p&gt;

&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;2.Introduction to Databases&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once you have tried out some python projects and feel comfortable with moving forward , then learn about databases.&lt;br&gt;
A database simply consists of a collection of data which are stored and accessed electronically.Data is fetched from the database with the help of queries which ask the database to perform various functions.There exists two types of databases , &lt;em&gt;relational&lt;/em&gt; and &lt;em&gt;non-relational&lt;/em&gt; databases.&lt;br&gt;
A &lt;strong&gt;relational&lt;/strong&gt; database usually has tables and data is stored in the tables in form of rows and columns.An example is the &lt;a href="https://www.sqltutorial.org/"&gt;SQL database&lt;/a&gt;.&lt;br&gt;
A &lt;strong&gt;non-relational&lt;/strong&gt; database is also refferred to as NoSQL database.It stores data in a format known as &lt;em&gt;JSON&lt;/em&gt; which uses human readable formart to store and retrieve data. Data is stored as  a collection of objects containing key value pairs which are separated from each other with the help of a semi-colon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user
 {
'id':'qwe245ert',
'name':'John',
'occupation':'Doctor'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above , we have a user object with keys id,name and occupation each containing its own value.Some non-relational databases include &lt;a href="https://www.mongodb.com/docs/manual/tutorial/getting-started/"&gt;Mongodb&lt;/a&gt; and &lt;a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html"&gt;Amazon DynamoDB&lt;/a&gt;.&lt;br&gt;
For further insight on the differences of relational database and non-relational database checkout &lt;a href="https://youtu.be/qkod5bY10lU"&gt;this&lt;/a&gt; video.&lt;/p&gt;

&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;3.Understanding Python libraries used in Datascience&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is always the exciting part for me and I do hope it excites you too.Python contains very useful libraries that are used to perform various data science tasks .Thus understanding these packages is essential to your progress in this field.To use these libraries we must install them using &lt;code&gt;pip3 install library_name&lt;/code&gt; and then import it into our file.&lt;/p&gt;

&lt;p&gt;The libraries include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Pandas&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Numpy&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Matplotlib&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Seaborn&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pyforest&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;a)Pandas&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the python package or library that is used in data analysis.It provides a dataframe that allows you to play around with your data and structure it however you want.Pandas is mostly prefered for its &lt;strong&gt;&lt;em&gt;flexibility&lt;/em&gt;&lt;/strong&gt; and ability to work with &lt;strong&gt;&lt;em&gt;big data&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
Most people prefer to refer to a dataframe as (df) when analysing their data.Some functionalities that come with pandas include :&lt;br&gt;
Reading the dataset in any format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;df.read_csv('url_of_the_excel_sheet')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Viewing part of the dataset&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;df.head()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Locate anything:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;df.loc['column_name']

#locating using an integer
df.iloc[column(s)]

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

&lt;/div&gt;



&lt;p&gt;Sorting&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;df.sort_values('column_to_sort_by' , ascending=False)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just to mention a few .Check out &lt;a href="https://medium.com/bhavaniravi/python-pandas-tutorial-92018da85a33"&gt;this &lt;/a&gt; blog for more clarifications&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b)Numpy&lt;/strong&gt;&lt;br&gt;
Numpy is the same a lists in python.It is used to store multidimentional arrays eg 1D,2D or 3D arrays.Numpy is ussually preferred over lists since it uses fixed values wherease values in list vary.Numpy less storage space compared to list due to the fact that all the blocks storing data are next to each other (contiguous blocks) whereas  in list the blocks are far apart and pointers are used to store data.Numpy enables us to do all the mathematics we need .&lt;br&gt;
Using this library, we can perform more that we could not with lists eg&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a=[1,2,3,4]
b=[4,5,6,7]
a*b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code produces and error.However, in numpy we accomplish this using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a=np.array([1,2,3,4])
b=np.array([4,5,6,7])
a*b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we get [4,10,12,28]&lt;br&gt;
Now click &lt;a href="https://medium.com/nerd-for-tech/a-complete-guide-on-numpy-for-data-science-c54f47dfef8d"&gt;here&lt;/a&gt; and get more information on how to get started with numpy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;c.)Matplotlib&lt;/strong&gt;&lt;br&gt;
This is an open source drawing library that converts our  dataset into drawings that can be used to understand and further explain our dataset.You can generate various charts eg plots,histograms,bar charts,pie charts and scatter plots.If you are curious about what I am talking about , let &lt;a href="https://www.simplilearn.com/tutorials/python-tutorial/matplotlib"&gt;this&lt;/a&gt; tutorial feed your interests.&lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; this library feeds on data that is as a result of the two libraries above it .In short, analyse your data by performing mathematical operations on it then plot a suitable chart of it.&lt;br&gt;
&lt;strong&gt;d.)Seaborn&lt;/strong&gt;&lt;br&gt;
This is where we visualise information from matrices and dataframes  in order to make attractive statistical plots.This is just a compliment of matplotlib and  not a substitute.In short it adds more flavor to our charts and makes them look more appealing.In order to use seaborn, you need to understand how it approaches data visualizations.&lt;br&gt;
&lt;a href="https://python.plainenglish.io/seaborn-a-step-by-step-guide-to-catch-your-audience-part-1-42d9e6e30bea"&gt;More info.&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now ,after understanding the functionalities of the above libraries, we can use one awesome library called &lt;strong&gt;pyforest&lt;/strong&gt; to import all our required libraries with ease.How cool is that guys.....&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#First install the library in your terminal 
pip3 install pyforest 

#Now write this 
from pyforest import *

#You can veiw all imports by typing
lazy_imports()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;4. Connecting to a database of your choice&lt;/strong&gt;&lt;br&gt;
We now know the python syntax ,libraries and databases present.How about we combine the knowledge and come up with something cool....&lt;br&gt;
Yes I am talking about connecting our python files to the database.This is very important since we have seen we will work with a collection of data that we will regularly store and retrieve.Check this post about connecting a &lt;a href="https://medium.com/analytics-vidhya/how-to-setup-a-python-application-with-a-postgres-database-f965e7c1581e"&gt;postgres database to python&lt;/a&gt;,&lt;a href="https://medium.com/swlh/a-basic-introduction-to-boto3-a66df5548475"&gt;Python and Dynamodb data&lt;/a&gt;and &lt;a href="https://medium.com/codex/connect-to-an-mysql-database-via-python-9c88ceac999a"&gt;connecting to MYSQL&lt;/a&gt;.When working with databases ,we will also get to understand some usefull techniques that data must undergo like &lt;a href="https://towardsdatascience.com/flattening-json-objects-in-python-f5343c794b10"&gt;data flattening and how to flatten data into a table&lt;br&gt;
&lt;/a&gt; and some common errors found in data like missing values ,bad values , duplicates and methods of avoiding such errors .&lt;/p&gt;

&lt;p&gt;I hope this article helps you to get started with data science.Do not let the topics overwhelm you.Try to learn on each day and for sure you will be at a better place within a month or even weeks..... &lt;br&gt;
&lt;a href="https://github.com/Marriane791/Day3and4_DSC"&gt;Here&lt;/a&gt; are some code  examples of the topics covered aboveincase you need more examples.&lt;br&gt;
Happy coding!!!&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>datasciencemarathon</category>
      <category>python</category>
      <category>datanalysis</category>
    </item>
    <item>
      <title>Denormaliztion ,its pros and cons</title>
      <dc:creator>Marriane Akeyo</dc:creator>
      <pubDate>Thu, 17 Mar 2022 09:29:37 +0000</pubDate>
      <link>https://dev.to/marrie/denormaliztion-its-pros-and-cons-4h6d</link>
      <guid>https://dev.to/marrie/denormaliztion-its-pros-and-cons-4h6d</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Denormalization can be defined as an &lt;strong&gt;optimization technique&lt;/strong&gt; used to make queries more efficient by &lt;strong&gt;reducing the number of costly joins necessary to retrieve data&lt;/strong&gt;.In short we decide to be okey with redundancy unlike in normalization,where no data redundancy is allowed.Instead each table contains its own data and only joins are used to link on data with the other.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Pros of denormalization
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Queries are executed much more faster.&lt;br&gt;
This is because we fetch data from one table instead of joining numerous tables to find the data we need.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Writing queries becomes much more faster.&lt;br&gt;
Since there is no need to search for joining keys to fetch our data.However , one should always remember to update the tables accordingly once each update is made.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Cons of denormalization
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Perfoming updates and insert queries becomes more expensive and even harder to write.&lt;br&gt;
This is due to the increase in data processing as a result of data redundancy and possible data duplication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Increase in table size &lt;br&gt;
This demands an large storage space for the table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data may be inconsistent&lt;br&gt;
A failure to update the required tables might lead to inconsistent data which might be expensive to debug.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks for reading this short post ....i hope this gives you some insight.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>python</category>
      <category>womenwhocode</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Mastering python the right way</title>
      <dc:creator>Marriane Akeyo</dc:creator>
      <pubDate>Fri, 04 Mar 2022 22:47:51 +0000</pubDate>
      <link>https://dev.to/marrie/mastering-python-the-right-way-l59</link>
      <guid>https://dev.to/marrie/mastering-python-the-right-way-l59</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Python is a high level programming language  with alot of benefits  to beginners ,intermidiate and also experts in programming.It is also one of the functional programming languages .For more information about it's syntax and getting started with python , check out my article &lt;a href="https://dev.to/marrie/introduction-to-modern-python-48hh"&gt;here&lt;/a&gt;.&lt;br&gt;
Today however, we are going to focus on the tricks in understanding and mastering this language the right way.&lt;br&gt;
We can further break down our mastery into the following:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Basic syntax&lt;/strong&gt;&lt;br&gt;
The basic syntax of any language never change.It is therefore important start by understanding the foundations that make up the language.They include the simple operations,datatypes ,comparisons,functions and so on as described &lt;a href="https://dev.to/marrie/introduction-to-modern-python-48hh"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Control Structures&lt;/strong&gt;&lt;br&gt;
This describes the order in which we excecute our code.This is ussually made possible using loops and function calls.They are classified into three main group&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sequencial-which is the default mode.As the name suggests,excecution happens in a sequence such that the later line of code depends on the former one.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a=4
b=5
c=5%4
print(c)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Selection best for decision making eg if else and nested if statements.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;day = rainy
if day == rainy:
   print("Carry an umbrella")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Repetition used to excecute code more than once.For example while loops and  for loops
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers=[1,2,3,4,5]
for i in numbers:
  print(numbers[i])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Data Structures&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;List&lt;/em&gt; for example,store multiple items in a single variable.&lt;br&gt;
&lt;em&gt;Tuples&lt;/em&gt; are simillar to lists, except that they cannot be changed and use parantesis ().&lt;br&gt;
&lt;em&gt;Dictionaries&lt;/em&gt;,store data in form of key,value pairs which map abitrary keys to their values.A &lt;em&gt;set&lt;/em&gt; on the other hand is an unordered collection of items and every set element stored os unique and must be immutable.&lt;br&gt;
You can visit this &lt;a href="https://dev.to/marrie/introduction-to-modern-python-48hh"&gt;link&lt;/a&gt; for perfect code examples on the same.&lt;/p&gt;



&lt;p&gt;Mastering when to use what data structure is also very important aspect.&lt;/p&gt;

&lt;p&gt;It is therefore important to know that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Dictionaries are used when you need a logical association between a key and value pair,when you need a fast look up for your data based on a custom key or when your data is being constantly modified since they are mutable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lists when dealing with data that does not deal with random access.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sets when the elements are unique in nature.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tuples when your data cannot change.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Indentation and whitespaces&lt;/strong&gt;&lt;br&gt;
Python uses a maximum of 4 whitespaces for indentation and a minimum of one, however ,the number of spaces must be uniform in a block of code.The first line of code cannot be indented.Indentation begins from the second line if need be.This reduces the lines of code and also increases readability.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Decorators&lt;/strong&gt;&lt;br&gt;
They aid in modifying functions or extend their functionality using other functions.We use the @ symbol followed by the decorator_name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def meal_decorator(function):
       def cook():
          return "preparing something nice"
   @meal_decorator
   def eat():
      return "Lets dive in"
   print (eat())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Python Classes&lt;/strong&gt;&lt;br&gt;
A class is a blueprint of an object and in turn we can classify an object as an instance of a class.&lt;br&gt;
It contains attributes and methods as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person:
     name = "John" #attribute

     def skate(self,wheels):
        return f'{name}, skates using {wheels}' 

     #constructor
     def __init__(self,age):
       self.age = age
#instantiate
Student = Person(25)

print(Student.skate())

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

&lt;/div&gt;



&lt;p&gt;It is important to master that a class can also inherit from another class without modifying it.The class thus becomes a child class of the parent class and has all the characteristics of the parent class.&lt;br&gt;
In this case we shall just add the parent name to the new class as follows&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Encapsulation&lt;/strong&gt;&lt;br&gt;
This involves hiding the complexity of the code from the user .&lt;br&gt;
&lt;strong&gt;Polymorphisim&lt;/strong&gt;&lt;br&gt;
Involves using a common interface for many forms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Frameworks&lt;/strong&gt;&lt;br&gt;
Lastly we shall look at frameworks .Python frameworks help hide the complexity of the code into something simple that can be understood and used by many people.Some common python frameworks include &lt;a href="https://flask.palletsprojects.com/en/2.0.x/"&gt;FlaskAPI&lt;/a&gt;,&lt;a href="https://fastapi.tiangolo.com/"&gt;FastApi&lt;/a&gt; and &lt;a href="https://docs.djangoproject.com/en/4.0/"&gt;Django&lt;/a&gt;, just to mention a few. Each framework has its own strength and weaknesses, depending on the task at hand.Having a better understanding of this also enables one to master python the right way.&lt;/p&gt;

&lt;p&gt;I hope this gives you an incite  of the modern python and ts mastery.Thanks for reading.&lt;/p&gt;

</description>
      <category>wecoded</category>
      <category>python</category>
      <category>theycoded</category>
    </item>
    <item>
      <title>Data Structures and Algorithims with python.</title>
      <dc:creator>Marriane Akeyo</dc:creator>
      <pubDate>Mon, 21 Feb 2022 18:25:31 +0000</pubDate>
      <link>https://dev.to/marrie/data-structures-and-algorithims-with-python-3k1d</link>
      <guid>https://dev.to/marrie/data-structures-and-algorithims-with-python-3k1d</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Data Structures&lt;/strong&gt; basically describes how we can ogarnise  and stored in memory when a program processes it.For example, we can store a list of items having the same data-type using the array data structure.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h4HWXL3m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8adxoqosnaminmrk646x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h4HWXL3m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8adxoqosnaminmrk646x.png" alt="Image description" width="429" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;An Algorithm&lt;/strong&gt; on the other hand, is step by step set of instruction to process the data for a specific purpose. So, an algorithm uterlises various data structures in a logical way to solve a specific computing problem.This is a very important concept in computer science .It gives us more insight on the data we are working with.It enables data scientists to make better machine learning predictions.This is however a challenging topic to most people in the tech industry , according to most people.We are going to look at various python data structures in python and their code examples.  &lt;/p&gt;
&lt;h4&gt;
  
  
  Lists
&lt;/h4&gt;

&lt;p&gt;The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets.Lists are mutable and ordered. It can contain a mix of different data types.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list1 = ['chicken', 'pizza', 2022, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;We can access values in a list using their index.&lt;br&gt;
&lt;strong&gt;NOTE&lt;/strong&gt;: we start counting from 0&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print (list1[0]) #prints the element in the 0 index
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;We also use the .append() method to add new items into the list eg&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list2.append(6) #add 6 to the existing list2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Incase you want to add to a specific place in the list ,we do it as follows&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list3[2] = "e" # returns ["a", "b", "e", "d"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Some basic list operations include:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;len([1, 2, 3]) resulting to 3 which tests the Length&lt;/li&gt;
&lt;li&gt;[1, 2, 3] + [4, 5, 6]resulting to[1, 2, 3, 4, 5, 6]|Concatenation&lt;/li&gt;
&lt;li&gt;['Hi!'] * 3 resulting to ['Hi!', 'Hi!', 'Hi!'] which tests Repetition&lt;/li&gt;
&lt;li&gt;3 in [1, 2, 3] resulting to True which tests Membership&lt;/li&gt;
&lt;li&gt;for x in [1, 2, 3]:print x resulting to 1 2 3 which tests Iteration&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Dictionaries
&lt;/h4&gt;

&lt;p&gt;Dictionary is a mutable and unordered data structure. It permits storing a pair of items (i.e. keys and values).Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this − {}.&lt;br&gt;
Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.&lt;br&gt;
&lt;em&gt;&lt;u&gt;Accessing Values in Dictionary&lt;u&gt;&lt;/u&gt;&lt;/u&gt;&lt;/em&gt;&lt;br&gt;
To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict = {'Name': 'Marrie', 'Age': 27, 'Language': 'Python'}
print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following result :&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict['Name']:  Marrie
dict['Age']:  27
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If we attempt to access a data item with a key, which is not part of the dictionary, we get an error as follows:&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Using the data above.
print "dict['Alice']: ", dict['Alice']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following result :&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict['Alice']:
Traceback (most recent call last):
   File "test.py", line 4, in &amp;lt;module&amp;gt;
      print "dict['Alice']: ", dict['Alice'];
KeyError: 'Alice'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Updating Dictionary&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
You can update a dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an existing entry as shown below in the simple example:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict = {'Name': 'Marrie', 'Age': 27, 'Language': 'Python'}
dict['Age'] = 28; # update existing entry
dict['School'] = "LuxAcademy"; # Add new entry

print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following result :&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict['Age']:  28
dict['School']:LuxTech
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Delete Dictionary Elements&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
You can either remove individual dictionary elements or clear the entire contents of a dictionary. You can also delete entire dictionary in a single operation.&lt;br&gt;
To explicitly remove an entire dictionary, just use the del statement.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict = {'Name': 'Marrie', 'Age': 27, 'Language': 'Python'}
del dict['Name']; # remove entry with key 'Name'
dict.clear();     # remove all entries in dict
del dict ;        # delete entire dictionary

print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; −that an exception is raised because after del dict dictionary does not exist any more:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict['Age']:
Traceback (most recent call last):
   File "test.py", line 8, in &amp;lt;module&amp;gt;
      print "dict['Age']: ", dict['Age'];
TypeError: 'type' object is unsubscriptable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Properties of Dictionary Keys&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Dictionary values have no restrictions. They can be any arbitrary Python object, either standard objects or user-defined objects. However, same is not true for the keys.&lt;br&gt;
There are two important points to remember about dictionary keys:&lt;br&gt;
*More than one entry per key not allowed. Which means no duplicate key is allowed. When duplicate keys encountered during assignment, the last assignment wins.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict = {'Name': 'Marrie', 'Age': 27, 'Name': 'Python'}
print "dict['Name']: ", dict['Name']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following result:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict['Name']:  Python
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*Keys must be immutable. Which means you can use strings, numbers or tuples as dictionary keys but something like ['key'] is not allowed.&lt;br&gt;
An example is as follows :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict = {['Name']: 'Marrie', 'Age': 27}
print "dict['Name']: ", dict['Name']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following result :&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Traceback (most recent call last):
   File "test.py", line 3, in &amp;lt;module&amp;gt;
      dict = {['Name']: 'Marrie', 'Age': 27};
TypeError: list objects are unhashable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Tuples
&lt;/h4&gt;

&lt;p&gt;A tuple is another container. It is a data type for immutable ordered sequences of elements. Immutable because you can’t add and remove elements from tuples, or sort them in place.&lt;br&gt;
Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also.&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tuple_one = ('python', 'javascript', 'c++', 2000);
tuple_two = (1, 2, 3, 4, 5 );
tuple_3 = "a", "b", "c", "d";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The empty tuple is written as two parentheses containing nothing −&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;languages = ();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;To write a tuple containing a single value you have to include a comma, even though there is only one value −&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tup1 = (50,);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.&lt;br&gt;
&lt;u&gt;&lt;em&gt;Accessing Values in Tuples&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
To access values in tuple, use the square brackets for slicing along with the index or indices to obtain value available at that index.&lt;br&gt;
For example&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tuple_one = ('python', 'javascript', 'c++', 2000);
tuple_two = (1, 2, 3, 4, 5 );
print "tuple_one[0]: ", tuple_two[0];
print "tuple_two[1:5]: ",tuple_two[1:5];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following result :&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tuple_one[0]:  python
tuple_two[1:5]:  [2, 3, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Updating Tuples&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Tuples are immutable which means you cannot update or change the values of tuple elements. You are able to take portions of existing tuples to create new tuples as the following example demonstrates −&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');

# Following action is not valid for tuples
# tup1[0] = 100;

# So let's create a new tuple as follows
tup3 = tup1 + tup2;
print tup3;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following result:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(12, 34.56, 'abc', 'xyz')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Delete Tuple Elements&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded.&lt;br&gt;
To explicitly remove an entire tuple, just use the del statement.&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tuple_one = ('python', 'javascript', 'c++', 2000);
print tuple_one;
del tuple_one;
print "After deleting tup : ";
print tuple_one;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note − an exception raised, this is because after del tup tuple does not exist anymore.&lt;br&gt;
This produces the following result:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;('python', 'javascript', 'c++', 2000)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;After deleting tup :&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Traceback (most recent call last):
   File "test.py", line 9, in &amp;lt;module&amp;gt;
      print tuple_one;
NameError: name 'tuple_one' is not defined.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Assuming our file name is "test.py"&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Basic Tuples Operations&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new tuple, not a string.&lt;br&gt;
In fact, tuples respond to all of the general sequence operations we used on strings in the prior chapter.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;len((1, 2, 3)) which results to 3 used to test Length&lt;/li&gt;
&lt;li&gt;(1, 2, 3) + (4, 5, 6)which results to(1, 2, 3, 4, 5,6) and is used to test Concatenation&lt;/li&gt;
&lt;li&gt;('Hi!',) * 4 which results to('Hi!', 'Hi!', 'Hi!', 'Hi!') and is used to test Repetition&lt;/li&gt;
&lt;li&gt; 3 in (1, 2, 3) which results to True and is used to test Membership&lt;/li&gt;
&lt;li&gt;for x in (1, 2, 3): print x which results to 1 2 3 and is used to test Iteration&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Sets
&lt;/h4&gt;

&lt;p&gt;Set is a mutable and unordered collection of unique elements. It can permit us to remove duplicate quickly from a list.The sets in python are typically used for mathematical operations like union, intersection, difference and complement etc. &lt;br&gt;
A Python set is similar to this mathematical definition with below additional conditions:&lt;br&gt;
*The elements in the set cannot be duplicates.&lt;br&gt;
*The elements in the set are immutable(cannot be modified) but the set as a whole is mutable.&lt;br&gt;
*There is no index attached to any element in a python set. So they do not support any indexing or slicing operation.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;em&gt;Creating a set&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
A set is created by using the set() function or placing all the elements within a pair of curly braces.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
Months={"Jan","Feb","Mar"}
Dates={21,22,17}
print(Days)
print(Months)
print(Dates)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note how the order of the elements has changed in the result.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set(['Wed', 'Sun', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat'])
set(['Jan', 'Mar', 'Feb'])
set([17, 21, 22])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Accessing Values in a Set&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
We cannot access individual values in a set. We can only access all the elements together as shown above. But we can also get a list of individual elements by looping through the set.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Considering the data above.
Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])

for d in Days:
   print(d)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following :&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Wed
Sun
Fri
Tue
Mon
Thu
Sat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Adding Items to a Set&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
We can add elements to a set by using add() method. Remember,there is no specific index attached to the newly added element.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Adding to the data above. 
Days.add("Sun")
print(Days)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;results&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set(['Wed', 'Sun', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Removing Item from a Set&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
We can remove elements from a set by using discard() method. &lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Using the data above.
Days.discard("Sun")
print(Days)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set(['Wed', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Union of Sets&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
The union operation on two sets produces a new set containing all the distinct elements from both the sets. In the below example the element “Wed” is present in both the sets.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA|DaysB
print(AllDays)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Output will be as shown,note the result has only one “wed”.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set(['Wed', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Intersection of Sets&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
The intersection operation on two sets produces a new set containing only the common elements from both the sets. In the below example the element “Wed” is present in both the sets.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA &amp;amp; DaysB
print(AllDays)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set(['Wed'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Difference of Sets&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
The difference operation on two sets produces a new set containing only the elements from the first set and none from the second set. In the below example the element “Wed” is present in both the sets so it will not be found in the result set.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA - DaysB
print(AllDays)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Output&lt;br&gt;
When the above code is executed, it produces the following result. Please note the result has only one “wed”.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set(['Mon', 'Tue'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Compare Sets&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
We can check if a given set is a subset or superset of another set. The result is True or False depending on the elements present in the sets.&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
SubsetRes = DaysA &amp;lt;= DaysB
SupersetRes = DaysB &amp;gt;= DaysA
print(SubsetRes)
print(SupersetRes)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;True
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Queue
&lt;/h4&gt;

&lt;p&gt;The queue is a linear data structure where elements are in a sequential manner. It follows the F.I.F.O mechanism that means first in first out. &lt;br&gt;
Below the aspects that characterize a queue.&lt;br&gt;
Two ends:&lt;br&gt;
    *front → points to starting element&lt;br&gt;
    *rear → points to the last element&lt;br&gt;
There are two operations:&lt;br&gt;
*enqueue → inserting an element into the queue. It will be       done at the rear.&lt;br&gt;
*dequeue → deleting an element from the queue. It will be done at the front.&lt;br&gt;
There are two conditions:&lt;br&gt;
*overflow → insertion into a queue that is full&lt;br&gt;
*underflow → deletion from the empty queue&lt;br&gt;
Lets see a code example of this:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class myqueue:

    def __init__(self):
        self.data = []

    def length(self):
        return len(self.data)

    def enque(self, element): # put the element in the queue
        if len(self.data) &amp;lt; 5:
            return self.data.append(element)
        else:
            return "overflow"

    def deque(self): # remove the first element that we have put in queue
        if len(self.data) == 0:
             return "underflow"
        else:
            self.data.pop(0)

b = myqueue()
b.enque(2) # put the element into the queue
b.enque(3)
b.enque(4)
b.enque(5)
print(b.data)
b.deque()# # remove the first element that we have put in the queue
print(b.data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will procuce the  following results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[2, 3, 4, 5]
[3, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Stack
&lt;/h4&gt;

&lt;p&gt;In the english dictionary the word stack means arranging objects on over another. Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).&lt;br&gt;
In the following program we implement it as add and and remove functions. We declare an empty list and use the append() and pop() methods to add and remove the data elements.&lt;br&gt;
&lt;em&gt;Pushing into a Stack&lt;/em&gt;&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Stack:
   def __init__(self):
      self.stack = []

   def add(self, dataval):
# Use list append method to add element
      if dataval not in self.stack:
         self.stack.append(dataval)
         return True
      else:
         return False
# Use peek to look at the top of the stack
   def peek(self):     
       return self.stack[-1]

AStack = Stack()
AStack.add("Mon")
AStack.add("Tue")
AStack.peek()
print(AStack.peek())
AStack.add("Wed")
AStack.add("Thu")
print(AStack.peek())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Tue
Thu
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;POP from a Stack&lt;/em&gt;&lt;br&gt;
As we know we can remove only the top most data element from the stack, we implement a python program which does that. The remove function in the following program returns the top most element. We check the top element by calculating the size of the stack first and then use the in-built pop() method to find out the top most element.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Stack:
   def __init__(self):
      self.stack = []

   def add(self, dataval):
# Use list append method to add element
      if dataval not in self.stack:
         self.stack.append(dataval)
         return True
      else:
         return False

# Use list pop method to remove element
   def remove(self):
      if len(self.stack) &amp;lt;= 0:
         return ("No element in the Stack")
      else:
         return self.stack.pop()

AStack = Stack()
AStack.add("Mon")
AStack.add("Tue")
AStack.add("Wed")
AStack.add("Thu")
print(AStack.remove())
print(AStack.remove())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Thu
Wed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;h3&gt;
  
  
  Linked list
&lt;/h3&gt;

&lt;p&gt;A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers as shown in the below image:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hcyXEIoY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ajjq426s60mxxk5qtvn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hcyXEIoY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ajjq426s60mxxk5qtvn.png" alt="Image description" width="759" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Now lets create a linked list&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node:
   def __init__(self, dataval=None):
      self.dataval = dataval
      self.nextval = None

class SLinkedList:
   def __init__(self):
      self.headval = None

list1 = SLinkedList()
list1.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")
# Link first Node to second node
list1.headval.nextval = e2

# Link second Node to third node
e2.nextval = e3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Traversing a Linked List&lt;/em&gt;&lt;br&gt;
Singly linked lists can be traversed in only forward direction starting form the first data element. We simply print the value of the next data element by assigning the pointer of the next node to the current data element.&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node:
   def __init__(self, dataval=None):
      self.dataval = dataval
      self.nextval = None

class SLinkedList:
   def __init__(self):
      self.headval = None

   def listprint(self):
      printval = self.headval
      while printval is not None:
         print (printval.dataval)
         printval = printval.nextval

list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

# Link first Node to second node
list.headval.nextval = e2

# Link second Node to third node
e2.nextval = e3

list.listprint()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mon
Tue
Wed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Insertion in a Linked List&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Inserting element in the linked list involves reassigning the pointers from the existing nodes to the newly inserted node. Depending on whether the new data element is getting inserted at the beginning or at the middle or at the end of the linked list, we have the below scenarios.&lt;br&gt;
&lt;u&gt;&lt;em&gt;Inserting at the Beginning&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
This involves pointing the next pointer of the new data node to the current head of the linked list. So the current head of the linked list becomes the second data element and the new node becomes the head of the linked list.&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node:
   def __init__(self, dataval=None):
      self.dataval = dataval
      self.nextval = None

class SLinkedList:
   def __init__(self):
      self.headval = None
# Print the linked list
   def listprint(self):
      printval = self.headval
      while printval is not None:
         print (printval.dataval)
         printval = printval.nextval
   def AtBegining(self,newdata):
      NewNode = Node(newdata)

# Update the new nodes next val to existing node
   NewNode.nextval = self.headval
   self.headval = NewNode

list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

list.headval.nextval = e2
e2.nextval = e3

list.AtBegining("Sun")
list.listprint()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sun
Mon
Tue
Wed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;Inserting at the End&lt;/u&gt;&lt;br&gt;
This involves pointing the next pointer of the the current last node of the linked list to the new data node. So the current last node of the linked list becomes the second last data node and the new node becomes the last node of the linked list.&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node:
   def __init__(self, dataval=None):
      self.dataval = dataval
      self.nextval = None
class SLinkedList:
   def __init__(self):
      self.headval = None
# Function to add newnode
   def AtEnd(self, newdata):
      NewNode = Node(newdata)
      if self.headval is None:
         self.headval = NewNode
         return
      laste = self.headval
      while(laste.nextval):
         laste = laste.nextval
      laste.nextval=NewNode
# Print the linked list
   def listprint(self):
      printval = self.headval
      while printval is not None:
         print (printval.dataval)
         printval = printval.nextval

list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

list.headval.nextval = e2
e2.nextval = e3

list.AtEnd("Thu")

list.listprint()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mon
Tue
Wed
Thu
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Inserting in between two Data Nodes&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
This involves changing the pointer of a specific node to point to the new node. That is possible by passing in both the new node and the existing node after which the new node will be inserted. So we define an additional class which will change the next pointer of the new node to the next pointer of middle node. Then assign the new node to next pointer of the middle node.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node:
   def __init__(self, dataval=None):
      self.dataval = dataval
      self.nextval = None
class SLinkedList:
   def __init__(self):
      self.headval = None

# Function to add node
   def Inbetween(self,middle_node,newdata):
      if middle_node is None:
         print("The mentioned node is absent")
         return

      NewNode = Node(newdata)
      NewNode.nextval = middle_node.nextval
      middle_node.nextval = NewNode

# Print the linked list
   def listprint(self):
      printval = self.headval
      while printval is not None:
         print (printval.dataval)
         printval = printval.nextval

list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Thu")

list.headval.nextval = e2
e2.nextval = e3

list.Inbetween(list.headval.nextval,"Fri")

list.listprint()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mon
Tue
Fri
Thu
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Removing an Item&lt;/em&gt;&lt;u&gt;&lt;br&gt;
We can remove an existing node using the key for that node. In the below program we locate the previous node of the node which is to be deleted.Then, point the next pointer of this node to the next node of the node to be deleted.&lt;br&gt;
Example&lt;br&gt;
&lt;/u&gt;&lt;/u&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node:
   def __init__(self, data=None):
      self.data = data
      self.next = None
class SLinkedList:
   def __init__(self):
      self.head = None

   def Atbegining(self, data_in):
      NewNode = Node(data_in)
      NewNode.next = self.head
      self.head = NewNode

# Function to remove node
   def RemoveNode(self, Removekey):
      HeadVal = self.head

      if (HeadVal is not None):
         if (HeadVal.data == Removekey):
            self.head = HeadVal.next
            HeadVal = None
            return
      while (HeadVal is not None):
         if HeadVal.data == Removekey:
            break
         prev = HeadVal
         HeadVal = HeadVal.next

      if (HeadVal == None):
         return

      prev.next = HeadVal.next
         HeadVal = None

   def LListprint(self):
      printval = self.head
      while (printval):
         print(printval.data),
         printval = printval.next

llist = SLinkedList()
llist.Atbegining("Mon")
llist.Atbegining("Tue")
llist.Atbegining("Wed")
llist.Atbegining("Thu")
llist.RemoveNode("Tue")
llist.LListprint()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Thu
Wed
Mon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Algorithims
&lt;/h4&gt;

&lt;p&gt;Algorithms are instructions that are formulated in a finite and sequential order to solve problems.&lt;br&gt;
The word algorithm derives itself from the 9th-century Persian mathematician Muḥammad ibn Mūsā al-Khwārizmī, whose name was Latinized as Algorithmi. Al-Khwārizmī was also an astronomer, geographer, and a scholar in the House of Wisdom in Baghdad.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As you already know algorithms are instructions that are formulated in a finite and sequential order to solve problems.&lt;/p&gt;

&lt;p&gt;When we write an algorithm, we have to know what is the exact problem, determine where we need to start and stop and formulate the intermediate steps.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are three main approaches to solve algorithms:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;*Divide et Impera (also known as divide and conquer) → it divides the problem into sub-parts and solves each one separately&lt;br&gt;
  *Dynamic programming → it divides the problem into sub-parts remembers the results of the sub-parts and applies it to similar ones&lt;br&gt;
   *Greedy algorithms → involve taking the easiest step while solving a problem without worrying about the complexity of the future steps&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Tree Traversal Algorithm&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Trees in python are non-linear data structures. They are characterized by roots and nodes. I take the class I constructed before for the binary tree.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Tree Traversal refers to visiting each node present in the tree exactly once, in order to update or check them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k1DK4eyP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8qq4zja9nqqy134i27o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k1DK4eyP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8qq4zja9nqqy134i27o.png" alt="Image description" width="648" height="446"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# create the class Node and the attrbutes 
class Node:
    def __init__(self, letter):
        self.childleft = None
        self.childright = None
        self.nodedata = letter

# create the nodes for the tree
root = Node('A')
root.childleft = Node('B')
root.childright = Node('C')
root.childleft.childleft = Node('D')
root.childleft.childright = Node('E')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;There are three types of tree traversals:&lt;br&gt;
*In-order traversal → refers to visiting the left node, followed by the root and then the right nodes.&lt;br&gt;
Here D is the leftmost node where the nearest root is B. The right of root B is E. Now the left sub-tree is completed, so I move towards the root node A and then to node C.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def InOrd(root):
    if root:
        InOrd(root.childleft)
        print(root.nodedata)
        InOrd(root.childright)
InOrd(root)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Out:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D
B
E
A
C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;*Pre-order traversal → refers to visiting the root node followed by the left nodes and then the right nodes.&lt;br&gt;
In this case, I move to the root node A and then to the left child node B and to the sub child node D. After that I can go to the nodes E and then C.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def PreOrd(root):
    if root:
        print(root.nodedata)
        PreOrd(root.childleft)
        PreOrd(root.childright)
PreOrd(root)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;out:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A
B
D
E
C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;*Post-order traversal → refers to visiting the left nodes followed by the right nodes and then the root node.&lt;br&gt;
I go to the most left node which is D and then to the right node E. Then, I can go from the left node B to the right node C. Finally, I move towards the root node A.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def PostOrd(root):
    if root:
        PostOrd(root.childleft)
        PostOrd(root.childright)
        print(root.nodedata)
PostOrd(root)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;out:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D
E
B
C
A
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Sorting Algorithm&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
The sorting algorithm is used to sort data in some given order. It can be classified in Merge Sort and Bubble Sort.&lt;br&gt;
*Merge Sort → it follows the divide et Impera rule. The given list is first divided into smaller lists and compares adjacent lists and then, reorders them in the desired sequence. So, in summary from unordered elements as input, we need to have ordered elements as output. Below, the code with each step described.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def merge_sort(ourlist, left, right): #left and right corresponds to starting and ending element of ourlist
    if right -left &amp;gt; 1: # check if the length of ourlist is greater than 1
        middle = (left + right) // 2 # we divide the length in two parts
        merge_sort(ourlist, left, middle) # recursevely I call the merge_sort function from left to middle
        merge_sort(ourlist, middle, right) # then from middle to right
        merge_list(ourlist, left, middle, right) # finally I create ourlist in complete form(left, middle and right) 

def merge_list(ourlist, left, middle, right):# I create the function merged_list
    leftlist = ourlist[left:middle] # I define the leftlist
    rightlist = ourlist[middle:right] # I define the right list
    k = left # it is the the temporary variable
    i = 0 # this variable that corespond to the index of the first group help me to iterate from left to right
    j = 0 # this variable that corespond to the index of the second group help me to iterate from left to right
    while (left + i &amp;lt; middle and middle+ j &amp;lt; right): # the condition that I want to achive before to stop my iteration
        if (leftlist[i] &amp;lt;= rightlist[j]): #if the element in the leftlist is less or equal to the element in the rightlist
            ourlist[k] = leftlist[i] # In this case I fill the value of the leftlist in ourlist with index k
            i = i + 1 #now I have to increment the value by 1
        else: # if the above codition is not match
            ourlist[k] = rightlist[j] # I fill the rightlist element in ourlist with index k
            j = j + 1 # I increment index j by 1
        k = k+1 # now I increment the value of k by 1
    if left + i &amp;lt; middle: # check if left + i is less than middle
        ourlist[k] = leftlist[i] # I place all elements of my leftlist in ourlist
        i = i + 1
        k = k + 1
    else: # otherwise if my leftlist is empty
        while k &amp;lt; right: # untill k is less then right
            ourlist[k] = rightlist[j] # I place all elements of rightlist in ourlist 
            j = j + 1
            k = k + 1

ourlist = input('input - unordered elements: ').split() # insert the input and split
ourlist = [int(x) for x in ourlist]
merge_sort(ourlist, 0, len(ourlist))
print('output - ordered elements: ')
print(ourlist)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;input - unordered elements: 15 1 19 93
output - ordered elements: 
[1, 15, 19, 93]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*Bubble Sort → it first compares and then sorts adjacent elements if they are not in the specified order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def bubble_sort(ourlist): # I create my function bubble_sort with the argument called ourlist
    b=len(ourlist)-1 # for every list, I will have a minus 1 iteration
    for x in range(b): # for each element in the range of b, I check if they are ordered or not
        for y in range(b-x): 
            if ourlist[y]&amp;gt;ourlist[y+1]: # if one element is greater than the nearest elemnt in the list
                ourlist[y],ourlist[y+1]=ourlist[y+1],ourlist[y] # I have to swap the elemnts, in other words
                                                          # I exchange the position of the two elements
        return ourlist

ourlist=[15,1,9,3]
bubble_sort(ourlist)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 3, 9, 15]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*Insertion Sort → it picks one item of a given list at the time and places it at the exact spot where it is to be placed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def ins_sort(ourlist):
    for x in range(1, len(ourlist)): # loop for each element starting from 1 to the length of our list
        k = ourlist[x] # element with the index x
        j = x-1 # j is the index previus the index x
        while j &amp;gt;=0 and k &amp;lt; ourlist[j]: # untill each elermnt of the list are less than their previous element my loop don't stop
                ourlist[j+1] = ourlist[j] # the elemnt indexed before the element considered is set to the next one index
                j -= 1 # I decrement index j by 1
        ourlist[j+1] = k # now k is the element in the index j+1
    return ourlist

ourlist = [15, 1, 9, 3]
ins_sort(ourlist)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 3, 9, 15]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;There are other Sorting Algorithms like Selection Sort and Shell Sort.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Searching Algorithms&lt;/p&gt;

&lt;p&gt;*Searching algorithms are used to seek for some elements present in a given dataset. There are many types of search algorithms such as Linear Search, Binary Search, Exponential Search, Interpolation Search, and so on. In this section, we will see the Linear Search and Binary Search.&lt;/p&gt;

&lt;p&gt;*Linear Search → in a single-dimensional array we have to search a particular key element. The input is the group of elements and the key element that we want to find. So, we have to compare the key element with each element of the group. In the following code, Lets try to seek element 27 in our list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def lin_search(ourlist, key):

    for index in range(0, len(ourlist)):
        if (ourlist[index] == key):
            return  index
    else:
        return "not fund"

ourlist = [15, 1, 9, 3]

lin_search(ourlist, 27)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'not fund'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*Binary Search → in this algorithm, we assume that the list is in ascending order. So, if the value of the search key is less than the element in the middle of the list, we narrow the interval to the lower half. Otherwise, we narrow to the upper half. We continue our check until the value is found or the list is empty.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def bin_search(ourlist, key):
    left = 0 # I assign left position to zero
    right = len(ourlist)-1 # I assign right position by defining the length of ourlist minus one 
    matched = False
    while(left&amp;lt;=right and not matched): # the loop will continue untill the left element is less or equal to the right element and the matched is True
        mid = (left+right)//2 # I find the position of the middle element
        if ourlist[mid] == key: # if the middle element correponds to the key element
             matched = True
        else: #otherwise 
            if key &amp;lt; ourlist[mid]: # if key element is less than the middle element
                right = mid - 1 #I assign the position of the right element as mid - 1
            else: #otherwise
                left = mid + 1 #left position will become the middle position plus 1
    return matched

print(bin_search([1, 3, 9, 15], 17))
print(bin_search([1, 3, 9, 15], 3))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;False
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>datascience</category>
      <category>algorithms</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Data Structures and algorithims in Javascript</title>
      <dc:creator>Marriane Akeyo</dc:creator>
      <pubDate>Mon, 21 Feb 2022 18:16:05 +0000</pubDate>
      <link>https://dev.to/marrie/data-structures-and-algorithim-in-javascript-4p7n</link>
      <guid>https://dev.to/marrie/data-structures-and-algorithim-in-javascript-4p7n</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Data Structures&lt;/strong&gt; basically describes how we can ogarnise  and stored in memory when a program processes it.For example, we can store a list of items having the same data-type using the array data structure.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h4HWXL3m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8adxoqosnaminmrk646x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h4HWXL3m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8adxoqosnaminmrk646x.png" alt="Image description" width="429" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;An Algorithm&lt;/strong&gt; on the other hand, is step by step set of instruction to process the data for a specific purpose. So, an algorithm uterlises various data structures in a logical way to solve a specific computing problem.This is a very important concept in computer science .It gives us more insight on the data we are working with.It enables data scientists to make better machine learning predictions.This is however a challenging topic to most people in the tech industry , according to most people.We are going to look at various python data structures in python and their code examples.  &lt;/p&gt;
&lt;h4&gt;
  
  
  Lists
&lt;/h4&gt;

&lt;p&gt;The list is a most versatile datatype available in Javascript which can be written as a list of comma-separated values (items) between square brackets.Lists are mutable and ordered. It can contain a mix of different data types.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list1 = ['chicken', 'pizza', 2022, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;We can access values in a list using their index.&lt;br&gt;
&lt;strong&gt;NOTE&lt;/strong&gt;: we start counting from 0&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log (list1[0]) //prints the element in the 0 index
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;We also use the .push() method to add new items into the list eg&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list2.push(6) //add 6 to the existing list2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Incase you want to add to a specific place in the list ,we do it as follows&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list3[2] = "e" // returns ["a", "b", "e", "d"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Dictionaries
&lt;/h4&gt;

&lt;p&gt;Dictionary is a mutable and unordered data structure. It permits storing a pair of items (i.e. keys and values).Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this − {}.&lt;br&gt;
Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;&lt;u&gt;Accessing Values in Dictionary&lt;u&gt;&lt;/u&gt;&lt;/u&gt;&lt;/em&gt;&lt;br&gt;
To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict = {'Name': 'Marrie', 'Age': 27, 'Language': 'Javascript'}
console.log( "dict['Name']: ", dict['Name'])
console.log( "dict['Age']: ", dict['Age'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following result :&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict['Name']:  Marrie
dict['Age']:  27
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Updating Dictionary&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
You can update a dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an existing entry as shown below in the simple example:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict = {'Name': 'Marrie', 'Age': 27, 'Language': 'Python'}
dict['Age'] = 28; // update existing entry
dict['School'] = "LuxAcademy"; # Add new entry

console.log ("dict['Age']: ", dict['Age'])
console.log ("dict['School']: ", dict['School'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following result :&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict['Age']:  28
dict['School']:LuxAcademy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Delete Dictionary Elements&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
You can either remove individual dictionary elements or clear the entire contents of a dictionary. You can also delete entire dictionary in a single operation.&lt;br&gt;
To explicitly remove an entire dictionary, just use the del statement.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict = {'Name': 'Marrie', 'Age': 27, 'Language': 'Python'}
del dict['Name']; // remove entry with key 'Name'
dict.clear();     // remove all entries in dict
del dict ;        // delete entire dictionary

console.log( "dict['Age']: ", dict['Age'])
console.log ("dict['School']: ", dict['School'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; −that an exception is raised because after del dict dictionary does not exist any more.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;em&gt;Properties of Dictionary Keys&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Dictionary values have no restrictions. They can be any arbitrary javavscript object, either standard objects or user-defined objects. However, same is not true for the keys.&lt;br&gt;
There are two important points to remember about dictionary keys:&lt;br&gt;
*More than one entry per key not allowed. Which means no duplicate key is allowed. When duplicate keys encountered during assignment, the last assignment wins.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict = {'Name': 'Marrie', 'Age': 27, 'Name': 'Javascript'}
console.log( "dict['Name']: ", dict['Name'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following result:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dict['Name']:  Javascript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*Keys must be immutable. Which means you can use strings, numbers or tuples as dictionary keys but something like ['key'] is not allowed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Tuples
&lt;/h4&gt;

&lt;p&gt;A tuple is another container. It is a data type for immutable ordered sequences of elements. Immutable because you can’t add and remove elements from tuples, or sort them in place.&lt;br&gt;
Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also.&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tuple_one = ('javascript', 'java', 'c++', 2000);
tuple_two = (1, 2, 3, 4, 5 );
tuple_3 = "a", "b", "c", "d";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The empty tuple is written as two parentheses containing nothing −&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;languages = ();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;To write a tuple containing a single value you have to include a comma, even though there is only one value −&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tup1 = (50,);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.&lt;br&gt;
&lt;u&gt;&lt;em&gt;Accessing Values in Tuples&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
To access values in tuple, use the square brackets for slicing along with the index or indices to obtain value available at that index.&lt;br&gt;
For example&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tuple_one = ('python', 'javascript', 'c++', 2000);
tuple_two = (1, 2, 3, 4, 5 );
console.log ("tuple_one[0]: ", tuple_two[0]);
console.log ("tuple_two[1:5]: ",tuple_two[1:5]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following result :&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tuple_one[0]:  python
tuple_two[1:5]:  [2, 3, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Updating Tuples&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Tuples are immutable which means you cannot update or change the values of tuple elements. You are able to take portions of existing tuples to create new tuples as the following example demonstrates −&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');

// Following action is not valid for tuples
// tup1[0] = 100;

// So let's create a new tuple as follows
tup3 = tup1 + tup2;
console.log(tup3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following result:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(12, 34.56, 'abc', 'xyz')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Delete Tuple Elements&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded.&lt;br&gt;
To explicitly remove an entire tuple, just use the del statement.&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tuple_one = ('python', 'javascript', 'c++', 2000);
console.log( tuple_one);
del tuple_one;
print "After deleting tup : ";
print tuple_one;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note − an exception raised, this is because after del tup tuple does not exist anymore.&lt;br&gt;
This produces the following result:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;('python', 'javascript', 'c++', 2000)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Sets
&lt;/h4&gt;

&lt;p&gt;Set is a mutable and unordered collection of unique elements. It can permit us to remove duplicate quickly from a list.The sets in javacript are typically used for mathematical operations like union, intersection, difference and complement etc. &lt;br&gt;
A javascript set is similar to this mathematical definition with below additional conditions:&lt;br&gt;
*The elements in the set cannot be duplicates.&lt;br&gt;
*The elements in the set are immutable(cannot be modified) but the set as a whole is mutable.&lt;br&gt;
*There is no index attached to any element in a python set. So they do not support any indexing or slicing operation.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;em&gt;Creating a set&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
A set is created by using the set() function or placing all the elements within a pair of curly braces.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
Months={"Jan","Feb","Mar"}
Dates={21,22,17}
console.log(Days)
console.log(Months)
console.log(Dates)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note how the order of the elements has changed in the result.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set(['Wed', 'Sun', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat'])
set(['Jan', 'Mar', 'Feb'])
set([17, 21, 22])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Accessing Values in a Set&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
We cannot access individual values in a set. We can only access all the elements together as shown above. But we can also get a list of individual elements by looping through the set.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Considering the data above.
Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])

for d in Days:
   console.log(d)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the above code is executed, it produces the following :&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Wed
Sun
Fri
Tue
Mon
Thu
Sat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Adding Items to a Set&lt;/em&gt;&lt;/u&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can add elements to a set by using add() method. Remember,there is no specific index attached to the newly added element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Adding to the data above. 
Days.add("Sun")
 console.log(Days)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;results&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set(['Wed', 'Sun', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Removing Item from a Set&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
We can remove elements from a set by using discard() method. &lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Using the data above.
Days.discard("Sun")
 console.log(Days)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set(['Wed', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Union of Sets&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
The union operation on two sets produces a new set containing all the distinct elements from both the sets. In the below example the element “Wed” is present in both the sets.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA|DaysB
 console.log(AllDays)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Output will be as shown,note the result has only one “wed”.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set(['Wed', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Intersection of Sets&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
The intersection operation on two sets produces a new set containing only the common elements from both the sets. In the below example the element “Wed” is present in both the sets.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA &amp;amp; DaysB
console.log(AllDays)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set(['Wed'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Difference of Sets&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
The difference operation on two sets produces a new set containing only the elements from the first set and none from the second set. In the below example the element “Wed” is present in both the sets so it will not be found in the result set.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA - DaysB
 console.log(AllDays)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Output&lt;br&gt;
When the above code is executed, it produces the following result. Please note the result has only one “wed”.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set(['Mon', 'Tue'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Compare Sets&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
We can check if a given set is a subset or superset of another set. The result is True or False depending on the elements present in the sets.&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
SubsetRes = DaysA &amp;lt;= DaysB
SupersetRes = DaysB &amp;gt;= DaysA
 console.log(SubsetRes)
 console.log(SupersetRes)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;True
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Queue
&lt;/h4&gt;

&lt;p&gt;The queue is a linear data structure where elements are in a sequential manner. It follows the F.I.F.O mechanism that means first in first out. &lt;br&gt;
Below the aspects that characterize a queue.&lt;br&gt;
Two ends:&lt;br&gt;
    *front → points to starting element&lt;br&gt;
    *rear → points to the last element&lt;br&gt;
There are two operations:&lt;br&gt;
*enqueue → inserting an element into the queue. It will be       done at the rear.&lt;br&gt;
*dequeue → deleting an element from the queue. It will be done at the front.&lt;br&gt;
There are two conditions:&lt;br&gt;
*overflow → insertion into a queue that is full&lt;br&gt;
*underflow → deletion from the empty queue&lt;br&gt;
Lets see a code example of this:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// program to implement queue data structure

class Queue {
    constructor() {
        this.items = [];
    }

    // add element to the queue
    enqueue(element) {
        return this.items.push(element);
    }

    // remove element from the queue
    dequeue() {
        if(this.items.length &amp;gt; 0) {
            return this.items.shift();
        }
    }

    // view the last element
    peek() {
        return this.items[this.items.length - 1];
    }

    // check if the queue is empty
    isEmpty(){
       return this.items.length == 0;
    }

    // the size of the queue
    size(){
        return this.items.length;
    }

    // empty the queue
    clear(){
        this.items = [];
    }
}

let queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(4);
queue.enqueue(8);
console.log(queue.items);

queue.dequeue();
console.log(queue.items);

console.log(queue.peek());

console.log(queue.isEmpty());

console.log(queue.size());

queue.clear();
console.log(queue.items);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will procuce the  following results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 2, 4, 8]
[2, 4, 8]
8
false
3
[]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Stack
&lt;/h4&gt;

&lt;p&gt;In the english dictionary the word stack means arranging objects on over another. Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).&lt;br&gt;
In the following program we implement it as add and and remove functions. We declare an empty list and use the append() and pop() methods to add and remove the data elements.&lt;br&gt;
&lt;em&gt;Pushing into a Stack&lt;/em&gt;&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let city = ["New York", "Madrid", "Kathmandu"];

// add "London" to the array
city.push("London");


console.log(city);

// Output: [ 'New York', 'Madrid', 'Kathmandu', 'London' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;POP from a Stack&lt;/em&gt;&lt;br&gt;
As we know we can remove only the top most data element from the stack, we implement a python program which does that. The remove function in the following program returns the top most element. We check the top element by calculating the size of the stack first and then use the in-built pop() method to find out the top most element.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let cities = ["Madrid", "New York", "Kathmandu", "Paris"];

// remove the last element
let removedCity = cities.pop();

console.log(cities)         // ["Madrid", "New York", "Kathmandu"]
console.log(removedCity);   // Paris
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;h3&gt;
  
  
  Linked list
&lt;/h3&gt;

&lt;p&gt;A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers as shown in the below image:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hcyXEIoY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ajjq426s60mxxk5qtvn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hcyXEIoY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ajjq426s60mxxk5qtvn.png" alt="Image description" width="759" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Traversing a Linked List&lt;/em&gt;&lt;br&gt;
Singly linked lists can be traversed in only forward direction starting form the first data element. We simply print the value of the next data element by assigning the pointer of the next node to the current data element.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct node *temp = head;
printf("\n\nList elements are - \n");
while(temp != NULL) {
  printf("%d ---&amp;gt;",temp-&amp;gt;data);
  temp = temp-&amp;gt;next;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;List elements are - 
1 ---&amp;gt;2 ---&amp;gt;3 ---&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Insertion in a Linked List&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Inserting element in the linked list involves reassigning the pointers from the existing nodes to the newly inserted node. Depending on whether the new data element is getting inserted at the beginning or at the middle or at the end of the linked list, we have the below scenarios.&lt;br&gt;
&lt;u&gt;&lt;em&gt;Inserting at the Beginning&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
This involves pointing the next pointer of the new data node to the current head of the linked list. So the current head of the linked list becomes the second data element and the new node becomes the head of the linked list.&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode-&amp;gt;data = 4;
newNode-&amp;gt;next = head;
head = newNode;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;Inserting at the End&lt;/u&gt;&lt;br&gt;
This involves pointing the next pointer of the the current last node of the linked list to the new data node. So the current last node of the linked list becomes the second last data node and the new node becomes the last node of the linked list.&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode-&amp;gt;data = 4;
newNode-&amp;gt;next = NULL;

struct node *temp = head;
while(temp-&amp;gt;next != NULL){
  temp = temp-&amp;gt;next;
}

temp-&amp;gt;next = newNode;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Inserting in between two Data Nodes&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
This involves changing the pointer of a specific node to point to the new node. That is possible by passing in both the new node and the existing node after which the new node will be inserted. So we define an additional class which will change the next pointer of the new node to the next pointer of middle node. Then assign the new node to next pointer of the middle node.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode-&amp;gt;data = 4;

struct node *temp = head;

for(int i=2; i &amp;lt; position; i++) {
  if(temp-&amp;gt;next != NULL) {
    temp = temp-&amp;gt;next;
  }
}
newNode-&amp;gt;next = temp-&amp;gt;next;
temp-&amp;gt;next = newNode;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Removing an Item&lt;/em&gt;&lt;u&gt;&lt;/u&gt;&lt;/u&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can remove an existing node using the key for that node. In the below program we locate the previous node of the node which is to be deleted.Then, point the next pointer of this node to the next node of the node to be deleted.&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct node* temp = head;
while(temp-&amp;gt;next-&amp;gt;next!=NULL){
  temp = temp-&amp;gt;next;
}
temp-&amp;gt;next = NULL;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Algorithims
&lt;/h4&gt;

&lt;p&gt;Algorithms are instructions that are formulated in a finite and sequential order to solve problems.&lt;br&gt;
The word algorithm derives itself from the 9th-century Persian mathematician Muḥammad ibn Mūsā al-Khwārizmī, whose name was Latinized as Algorithmi. Al-Khwārizmī was also an astronomer, geographer, and a scholar in the House of Wisdom in Baghdad.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As you already know algorithms are instructions that are formulated in a finite and sequential order to solve problems.&lt;br&gt;
When we write an algorithm, we have to know what is the exact problem, determine where we need to start and stop and formulate the intermediate steps.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are three main approaches to solve algorithms:&lt;br&gt;
 *Divide et Impera (also known as divide and conquer) → it divides the problem into sub-parts and solves each one separately&lt;br&gt;
  *Dynamic programming → it divides the problem into sub-parts remembers the results of the sub-parts and applies it to similar ones&lt;br&gt;
   *Greedy algorithms → involve taking the easiest step while solving a problem without worrying about the complexity of the future steps&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;em&gt;Tree Traversal Algorithm&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
Trees in python are non-linear data structures. They are characterized by roots and nodes. I take the class I constructed before for the binary tree.&lt;br&gt;
Tree Traversal refers to visiting each node present in the tree exactly once, in order to update or check them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k1DK4eyP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8qq4zja9nqqy134i27o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k1DK4eyP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8qq4zja9nqqy134i27o.png" alt="Image description" width="648" height="446"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct node {
    int data;
    struct node* left;
    struct node* right;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;There are three types of tree traversals:&lt;br&gt;
    *In-order traversal → refers to visiting the left node, followed by the root and then the right nodes.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;inorder(root-&amp;gt;left)
display(root-&amp;gt;data)
inorder(root-&amp;gt;right)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;*Pre-order traversal → refers to visiting the root node followed by the left nodes and then the right nodes.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;display(root-&amp;gt;data)
preorder(root-&amp;gt;left)
preorder(root-&amp;gt;right)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;*Post-order traversal → refers to visiting the left nodes followed by the right nodes and then the root node.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postorder(root-&amp;gt;left)
postorder(root-&amp;gt;right)
display(root-&amp;gt;data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;u&gt;&lt;em&gt;Sorting Algorithm&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
The sorting algorithm is used to sort data in some given order. It can be classified in Merge Sort and Bubble Sort.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;*Merge Sort → it follows the divide et Impera rule. The given list is first divided into smaller lists and compares adjacent lists and then, reorders them in the desired sequence. So, in summary from unordered elements as input, we need to have ordered elements as output. &lt;br&gt;
*Bubble Sort → it first compares and then sorts adjacent elements if they are not in the specified order.&lt;/p&gt;

&lt;p&gt;*Insertion Sort → it picks one item of a given list at the time and places it at the exact spot where it is to be placed.&lt;br&gt;
There are other Sorting Algorithms like Selection Sort and Shell Sort.&lt;/p&gt;

&lt;h4&gt;
  
  
  Searching Algorithms
&lt;/h4&gt;

&lt;p&gt;*Searching algorithms are used to seek for some elements present in a given dataset. There are many types of search algorithms such as Linear Search, Binary Search, Exponential Search, Interpolation Search, and so on. In this section, we will see the Linear Search and Binary Search.&lt;/p&gt;

&lt;p&gt;*Linear Search → in a single-dimensional array we have to search a particular key element. The input is the group of elements and the key element that we want to find. So, we have to compare the key element with each element of the group. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>datastructures</category>
      <category>algorithms</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Introduction to Modern Javascript 101</title>
      <dc:creator>Marriane Akeyo</dc:creator>
      <pubDate>Mon, 14 Feb 2022 20:54:34 +0000</pubDate>
      <link>https://dev.to/marrie/introduction-to-modern-javascript-101-41ha</link>
      <guid>https://dev.to/marrie/introduction-to-modern-javascript-101-41ha</guid>
      <description>&lt;p&gt;Javascript is a &lt;strong&gt;lightweight programming language&lt;/strong&gt; which is &lt;strong&gt;dynamically&lt;/strong&gt; typed.Some of the key benefits of learning javascript include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;JavaScript usage has now extended to mobile app development, desktop app development, and game development. This opens many opportunities for you as Javascript Programmer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Javascript is everywhere, it comes installed on every modern web browser and so to learn Javascript you really do not need any special environment setup. For example Chrome, Mozilla Firefox , Safari and every browser you know as of today, supports Javascript.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Great thing about Javascript is that you will find tons of frameworks and Libraries already developed which can be used directly in your software development to reduce your time to market.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Javascript is the most popular programming language in the world and that makes it a programmer’s great choice. Once you learnt Javascript, it helps you developing great front-end as well as back-end softwares using different Javascript based frameworks like jQuery, Node.JS etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Javascript is everywhere, it comes installed on every modern web browser and so to learn Javascript you really do not need any special environment setup. For example Chrome, Mozilla Firefox , Safari and every browser you know as of today, supports Javascript.&lt;br&gt;
Let us now look at a simple hello world code&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Hello world")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;How simple is that right...&lt;br&gt;
Javascript mostly uses the console as noticed from the code above.We are now going to look at the syntax that will enable us write usefull and appealing codes in javascript.&lt;br&gt;
So in javascript one can declare a &lt;em&gt;const&lt;/em&gt; which means it is a constant value and thus cannot be changed ,a &lt;em&gt;var&lt;/em&gt; which means the value declared is a variable and can be changed or a &lt;em&gt;let&lt;/em&gt; which means reassigning of the value is possible.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;data types&lt;/strong&gt;.&lt;br&gt;
In simple terms they explain or demonstrate the type of data being stored.Javascript supports the following datatypes:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;NULL&lt;/em&gt; which includes an object without any value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;undefined&lt;/em&gt; which includes a variable or constant with no value.eg:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var m;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so when we say :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;typeof(m);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then, we shall get undefined. Since javascript is a dynamically typed language you do not need to define other datatypes like strings and numbers ,once you write them , the compiler will automatically understand.&lt;br&gt;
However you can convert  from one datatype to another, eg: &lt;strong&gt;string()&lt;/strong&gt; to convert to string  and &lt;strong&gt;parseInt()&lt;/strong&gt; to convert to int.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Boolean&lt;/strong&gt;&lt;br&gt;
Unlike in other languages like python, the boolean values are declared using small letters in javascript.ie true or false.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conditional Statements&lt;/strong&gt;&lt;br&gt;
Javascript supports conditional statements like if else eg&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (x&amp;lt;3)
{
console.log (true)
}else{
console.log(false)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt; &lt;br&gt;
Double equals (==) in javascript are use to show equality of values.(===) are used to check the type of values wherease (=) is used during assignment of values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Logical Operators&lt;/strong&gt;&lt;br&gt;
Javascript makes use of the &amp;amp;&amp;amp;(and ) and ||(or) operators as better illustrated bellow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (x&amp;lt;3 &amp;amp;&amp;amp; y&amp;lt;10)
{
console.log (true)
}
elseif(z==8 || w&amp;lt;10)
{
console.log(true)
}
else{
console.log(false)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Functions&lt;/strong&gt;&lt;br&gt;
We can declare functions in javascript using two diffrent ways:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function (name)
{
console.log(`Hello ${name}`)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt; the above function accepts parameters.&lt;br&gt;
The other way is using the arrow key function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;username = () =&amp;gt;
{
console.log(`Hello ${name}`}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Operators&lt;/strong&gt; &lt;br&gt;
Javascript supports all the  common operators ie +,-,*,/,increment(++) and decrement(--).eg&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num = 0
while (num &amp;lt; 10)
{
console.log(num);
num++;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Extra&lt;/strong&gt;&lt;br&gt;
You might be wondering ...when do I use var,let or even const when writing your javascript code. I would advise you use const when you don't want your values to be changed. Use var when working with the global scope and finally, use let when you are referring only to the block scope .&lt;/p&gt;

&lt;p&gt;I hope you got some insight on the modern javascript.&lt;br&gt;
Thank you and happy coding!!! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>node</category>
    </item>
    <item>
      <title>Introduction to modern python</title>
      <dc:creator>Marriane Akeyo</dc:creator>
      <pubDate>Fri, 11 Feb 2022 21:34:30 +0000</pubDate>
      <link>https://dev.to/marrie/introduction-to-modern-python-48hh</link>
      <guid>https://dev.to/marrie/introduction-to-modern-python-48hh</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Python is an interpreted, high-level language created by Guido van Rossum and released in 1991. It is dynamically typed and garbage collected.Python programs have the extension.py and can be run from the command line by typing python file_name.py.The common features provided by python include:&lt;br&gt;
-&lt;strong&gt;Simplicity&lt;/strong&gt;: This includes less of the syntax of the language and more of the code.&lt;br&gt;
-&lt;strong&gt;Open Source&lt;/strong&gt;: A powerful language and it is free for everyone to use and alter as needed.&lt;br&gt;
-&lt;strong&gt;Portability&lt;/strong&gt;: Python code can be shared and it would work the same way it was intended to.&lt;br&gt;
-&lt;strong&gt;Embeddable &amp;amp; Extensible&lt;/strong&gt;: Python can have snippets of other languages inside it to perform certain functions.&lt;br&gt;
-&lt;strong&gt;Being Interpreted&lt;/strong&gt;: The worries of large memory tasks and other heavy CPU tasks are taken care of by Python itself leaving you to worry only about coding.&lt;br&gt;
-&lt;strong&gt;Huge amount of libraries&lt;/strong&gt;:from &lt;em&gt;data science&lt;/em&gt; all the way to &lt;em&gt;web development&lt;/em&gt;.&lt;br&gt;
-&lt;strong&gt;Object Orientation&lt;/strong&gt;: Objects help breaking-down complex real-life problems into such that they can be coded and solved to obtain solutions.&lt;br&gt;
A simple hello world in python can be written as:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("Hello world")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;What therefore do we mean when we say modern python?How is it helpful in a programmers life and why should we prefer it to the old python language?&lt;br&gt;
&lt;strong&gt;Modern python&lt;/strong&gt; simply means agreed modern best practices that are used when coding in python in order to give you better results .They include:&lt;/p&gt;
&lt;h3&gt;
  
  
  Tooling
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1.Use of python3&lt;/strong&gt; &lt;br&gt;
Python3 is the latest existing version of python that is being supported by all the major libraries and frameworks.Hence advisable for use.&lt;br&gt;
&lt;strong&gt;2.Use of virtual environment&lt;/strong&gt;&lt;br&gt;
It enables you to define separate sets of packages for each Python project, so that they do not conflict with each other.&lt;br&gt;
There are several tools that help you to manage your Python projects, and use virtual environments. The most popular are pipenv and poetry. Poetry is considered to be better designed, but pipenv is more widely supported. If you prefer, you can also manually set up and manage virtual environments.&lt;br&gt;
&lt;strong&gt;3.Formatting your code&lt;/strong&gt;&lt;br&gt;
Use a formatting tool with a plugin to your editor, so that your code is automatically formatted to a consistent style.&lt;br&gt;
Black is being adopted by the Python Software Foundation and other projects. It formats Python code to a style that follows the PEP 8 standard, but allows longer line lengths. Use Black for new projects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Language Syntax
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1.Format strings with f string&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The new f-string syntax is both more readable and has better performance than older methods.A good example of the fstring includes:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name = "nduta"
print(f'Hi, my name is {name}')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can use either the double or single quotes.&lt;br&gt;
&lt;strong&gt;2.Use enum or Named Tuples for Immutable Sets of Key-Value Pairs.&lt;/strong&gt;&lt;br&gt;
The properties of an enumeration and named tuples are useful for defining an immutable, related set of constant values that may or may not have a semantic meaning.&lt;br&gt;
&lt;strong&gt;3.Create Data Classes for Custom Data Objects&lt;/strong&gt;&lt;br&gt;
The data classes feature enables you to reduce the amount of code that you need to define classes for objects that exist to store values.&lt;br&gt;
&lt;strong&gt;4 Use collections.abc for Custom Collection Types&lt;/strong&gt;&lt;br&gt;
They provide the components for building your own custom collection types.These classes are well dvocated for because they are fast and well-tested.&lt;br&gt;
&lt;strong&gt;5.Use breakpoint() for Debugging&lt;/strong&gt;&lt;br&gt;
This function drops you into the debugger,both the inbuilt and external debuggers can use these breakpoints.An example would be:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo()
breakpoint()
bar()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The above code simply states that python will enter into the debugger after executing foo() and before executing bar().&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Application design
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1.Use Logging for Diagnostic Messages, Rather Than print()&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The built-in print() statement is convenient for adding debugging information, but you should include logging in your scripts and applications. Use the logging module in the standard library, or a third-party logging module.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;2.Only Use asyncio Where It Makes Sense&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The asynchronous features of Python enable a single process to avoid blocking on I/O operations. You can achieve concurrency by running multiple Python processes, with or without asynchronous I/O.&lt;/p&gt;

&lt;p&gt;To run multiple Web application processes, use Gunicorn or another WSGI server. Use the multiprocessing package in the Python standard library to build custom applications that run as multiple processes.&lt;/p&gt;

&lt;p&gt;Code that needs asynchronous I/O must not call any function in the standard library that synchronous I/O, such as open(), or the logging module.&lt;/p&gt;

&lt;p&gt;If you would like to work with asyncio, always use the most recent version of Python. Each new version of Python has improved the performance and features of async.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Libraries
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1.Handle Command-line Input with argparse&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The argparse module is now the recommended way to process command-line input. Use argparse, rather than the older optparse and getopt.&lt;br&gt;
The optparse module is officially deprecated, so update code that uses optparse to use argparse instead.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;2.Use pathlib for File and Directory Paths&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use pathlib objects instead of strings whenever you need to work with file and directory pathnames.&lt;/p&gt;

&lt;p&gt;Consider using the the pathlib equivalents for os functions.&lt;/p&gt;

&lt;p&gt;The existing methods in the standard library have been updated to support Path objects.&lt;/p&gt;

&lt;p&gt;To list all of the the files in a directory, use either the .iterdir() function of a Path object, or the os.scandir() function.&lt;br&gt;
&lt;strong&gt;3.Use os.scandir() Instead of os.listdir()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The os.scandir() function is significantly faster and more efficient than os.listdir(). Use os.scandir() wherever you previously used the os.listdir() function.&lt;/p&gt;

&lt;p&gt;This function provides an iterator, and works with a context manager:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

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

with os.scandir('some_directory/') as entries:
    for entry in entries:
        print(entry.name)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The context manager frees resources as soon as the function completes. Use this option if you are concerned about performance or concurrency.&lt;/p&gt;

&lt;p&gt;The os.walk() function now calls os.scandir(), so it automatically has the same improved performance as this function.&lt;br&gt;
&lt;strong&gt;4.Run External Commands with subprocess&lt;/strong&gt;&lt;br&gt;
The subprocess module provides a safe way to run external commands. Use subprocess rather than shell backquoting or the functions in os, such as spawn, popen2 and popen3. The subprocess.run() function in current versions of Python is sufficient for most cases.&lt;br&gt;
&lt;strong&gt;5.Use Requests for HTTP Clients&lt;/strong&gt;&lt;br&gt;
Use the requests package for HTTP, rather than the urllib.request in the standard library.&lt;br&gt;
&lt;strong&gt;6.Test with pytest&lt;/strong&gt;&lt;br&gt;
The pytest package has super-ceded nose as the most popular testing system for Python hence most advisable .You can use unittest for situations where you cannot add the pytest library to your project.&lt;br&gt;
This is just a sample of the modern improvements done in python . As they always say , practice makes perfect.So working with the language and its packages will give you a clear picture of the dos and don't s.If you haven't started using the library ,my advise would be  to give it a shot and see how far it goes.The language also has a wide community which is willing to help once you get stuck.&lt;br&gt;
Those who are advanced in the same my take would be to be on the look out since change is inevitable especially in the tech industry.&lt;br&gt;
I hope this gives you some incite.&lt;/p&gt;
&lt;/blockquote&gt;

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