<?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: Saravanan</title>
    <description>The latest articles on DEV Community by Saravanan (@__saravanan__).</description>
    <link>https://dev.to/__saravanan__</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%2F1019284%2Fcd2d6623-1dea-42f2-a5a8-e8312fd117de.png</url>
      <title>DEV Community: Saravanan</title>
      <link>https://dev.to/__saravanan__</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/__saravanan__"/>
    <language>en</language>
    <item>
      <title>Django</title>
      <dc:creator>Saravanan</dc:creator>
      <pubDate>Tue, 04 Apr 2023 10:34:03 +0000</pubDate>
      <link>https://dev.to/__saravanan__/django-2l5f</link>
      <guid>https://dev.to/__saravanan__/django-2l5f</guid>
      <description>&lt;h2&gt;
  
  
  Secret key
&lt;/h2&gt;

&lt;p&gt;In Django, the secret key is a string of random characters used for cryptographic signing and protection against attacks such as session hijacking, cross-site request forgery, and other malicious activities.&lt;/p&gt;

&lt;p&gt;In a new Django project, a secret key is automatically generated and stored in the settings.py file. The key is secret and it should not be shared with others, as anyone with access to it could potentially impersonate site users or modify data on the site.&lt;/p&gt;

&lt;p&gt;A new secret key can be generated using django-secret-key or any other application.&lt;/p&gt;

&lt;p&gt;It can be saved as an environment variable to prevent it from getting uploaded from the settings file through VCS. Cloud-specific tools can also be used to save it. Using separate settings files for development and production can also be done.&lt;/p&gt;

&lt;h2&gt;
  
  
  Default Django apps
&lt;/h2&gt;

&lt;p&gt;There are more apps than the default Django apps given in settings. They can be found in &lt;a href="https://github.com/django/django/tree/main/django/contrib"&gt;src&lt;/a&gt;. They are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.admin&lt;/code&gt;: Provides a web administrative interface for managing the Django project's data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.auth&lt;/code&gt;: Provides user authentication and authorization mechanisms.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.contenttypes&lt;/code&gt;: Provides a framework for associating metadata with models.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.sessions&lt;/code&gt;: Provides session management functionality.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.messages&lt;/code&gt;: Provides a way to display one-time messages to users.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.staticfiles&lt;/code&gt;: Provides a framework for managing static files.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.humanize&lt;/code&gt;: Provides a set of template filters for humanizing data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.redirects&lt;/code&gt;: Provides a way to redirect URLs.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.sitemaps&lt;/code&gt;: Provides a framework for generating sitemaps.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.sites&lt;/code&gt;: Provides a way to manage multiple sites using a single Django installation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.admindocs&lt;/code&gt;: Provides a way to automatically generate documentation for the project's models.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.postgres&lt;/code&gt;: Provides support for PostgreSQL-specific functionality.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.gis&lt;/code&gt;: Provides support for geographic data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.syndication&lt;/code&gt;: Provides a framework for generating RSS and Atom feeds.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django.contrib.webdesign&lt;/code&gt;: Provides a set of template tags for generating dummy data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Middleware and different kinds of middleware
&lt;/h2&gt;

&lt;p&gt;Middleware in Django is a component that sits between the web server and the view and provides a way to process requests and responses in a modular way. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Process Request Middleware: This middleware is executed at the beginning of the request cycle and can be used to perform tasks such as authentication, setting up the request, or modifying the request object.&lt;/li&gt;
&lt;li&gt;View Middleware: This middleware is executed just before the view function is called and can be used to modify the view's context or perform additional processing on the request.&lt;/li&gt;
&lt;li&gt;Template Middleware: This middleware is executed during the rendering of the template and can be used to add additional variables or processing to the template context.&lt;/li&gt;
&lt;li&gt;Process Response Middleware: This middleware is executed at the end of the request cycle and can be used to modify the response object or perform any final processing before the response is sent back to the client.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  CSRF
&lt;/h2&gt;

&lt;p&gt;CSRF attacks let anyone use another person's website account without their permission. Django can stop this attack with its built-in protection. Django checks for a secret code in each form submission, so anyone needs to know the secret code to trick the website. This secret code is user-specific and stored in cookies. When using HTTPS, Django checks that the form is coming from the same place as the website. Using HTTPS helps make things more secure. The csrf_exempt decorator must be used only when it is necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  XSS
&lt;/h2&gt;

&lt;p&gt;XSS attacks are when someone injects harmful scripts into a website that can affect other people's browsers. Django templates can help stop these attacks. Django templates can protect against certain dangerous characters, but not all.&lt;/p&gt;

&lt;h2&gt;
  
  
  ClickJacking
&lt;/h2&gt;

&lt;p&gt;Clickjacking is when a bad website puts another website inside a frame, tricking people into doing things they didn't mean to do. Django has a way to protect against this called X-Frame-Options middleware, which can stop a website from being shown inside a frame in some browsers.&lt;/p&gt;

&lt;h2&gt;
  
  
  WSGI
&lt;/h2&gt;

&lt;p&gt;WSGI stands for Web Server Gateway Interface. It is a specification that defines how a web server communicates with a Python web application.&lt;/p&gt;

&lt;p&gt;In Django, WSGI is used to allow a web server to interact with a Django application. It acts as a bridge between the two, allowing the webserver to send requests to the Django application and receive responses. The WSGI specification provides a standard interface for web servers and Python web applications to communicate with each other.&lt;/p&gt;

&lt;h2&gt;
  
  
  Models
&lt;/h2&gt;

&lt;h2&gt;
  
  
  ondelete
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;on_delete&lt;/code&gt; is a parameter that can be used when defining a foreign key relationship in Django models. It specifies what should happen when the referenced object is deleted.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;on_delete=CASCADE&lt;/code&gt; is one of the options available for the on_delete parameter. It specifies that when the referenced object is deleted, all objects that have a foreign key relationship to it should also be deleted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fields and Validators
&lt;/h2&gt;

&lt;p&gt;A model field represents a database column and defines the type of data that can be stored in that column. Validators are functions that validate the data entered into a field according to some predefined rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  Module and Class
&lt;/h2&gt;

&lt;p&gt;A module is a file containing Python code that can be imported and used in other Python files or modules. A module can contain functions, variables, classes, and other objects. A class is a blueprint for creating objects that define a set of properties and methods that the objects will have.&lt;/p&gt;

&lt;h2&gt;
  
  
  Django ORM in shell
&lt;/h2&gt;

&lt;p&gt;Django's Object-Relational Mapping (ORM) provides functionality to interact with a database using Python code instead of SQL queries. To use it in the shell import the model in the shell and use ORM functions on it.&lt;/p&gt;

&lt;h2&gt;
  
  
  ORM to SQL in Django shell
&lt;/h2&gt;

&lt;p&gt;The ORM can be converted into SQL using &lt;code&gt;.query&lt;/code&gt; from the queryset.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;queryset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random_name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;random_val&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;queryset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Aggregation and Annotation
&lt;/h2&gt;

&lt;p&gt;Aggregate calculates values for the entire queryset. Annotate calculates summary values for each item in the queryset. Aggregate are functions such as &lt;code&gt;Sum()&lt;/code&gt;, &lt;code&gt;Avg()&lt;/code&gt; etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Migration file
&lt;/h2&gt;

&lt;p&gt;A migration file is a script of instructions to modify the database schema. It is changed when a model is changed. It is needed to maintain schema in alignment with the models. &lt;code&gt;makemigrations&lt;/code&gt; is used to generate the migration file and &lt;code&gt;migrate&lt;/code&gt; is used to apply the changes to the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  SQL Transactions
&lt;/h2&gt;

&lt;p&gt;SQL transactions are a way of grouping together a set of database operations so that they can be executed as a single atomic unit. A transaction allows the performing of multiple database operations as a single, consistent unit, either all succeeding or no change. It prevents incomplete execution of queries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Atomic transaction
&lt;/h2&gt;

&lt;p&gt;Atomic transactions are used in Django to ensure all is completed or no change. It is the same as an SQL transaction. It is then done using SQL transactions depending upon the database used. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Revision 15-03-2023</title>
      <dc:creator>Saravanan</dc:creator>
      <pubDate>Wed, 15 Mar 2023 07:48:31 +0000</pubDate>
      <link>https://dev.to/__saravanan__/revision-15-03-2023-26f3</link>
      <guid>https://dev.to/__saravanan__/revision-15-03-2023-26f3</guid>
      <description>&lt;h2&gt;
  
  
  1. How can you copy objects in JS?
&lt;/h2&gt;

&lt;p&gt;There are three ways to copy an object in JS. They are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;using spread syntax.&lt;/li&gt;
&lt;li&gt;using &lt;code&gt;Object.asssign()&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;using &lt;code&gt;JSON.stringify()&lt;/code&gt; and &lt;code&gt;JSON.parse()&lt;/code&gt; methods.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// using spread ...&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// using  Object.assign() method&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// using JSON&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;p3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. What is the difference between a named function and an anonymous function in JS?
&lt;/h2&gt;

&lt;p&gt;A named function is a function declaration and an anonymous function is a function expression. The named function is hoisted but, anonymous functions are not. Anonymous functions are mostly used as a callback functions. Named function uses function keyword and anonymous function uses arrow syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;my_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;named function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;my_function&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs "named function"&lt;/span&gt;

&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;anonymous function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt; &lt;span class="c1"&gt;// logs "anonymous function"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. How to check if a string contains a substring in JS?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;String.prototype.includes&lt;/code&gt; function returns a boolean value true if the substring is present.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;String.prototype.indexOf&lt;/code&gt; function returns &lt;code&gt;-1&lt;/code&gt; if the substring is not present and a positive index value if present.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcdefgh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// logs true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcdefgh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;zz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// logs -1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. What is the difference between indexOf() and lastIndex() methods in JS?
&lt;/h2&gt;

&lt;p&gt;The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Is JS static or dynamically typed language?
&lt;/h2&gt;

&lt;p&gt;JS is a dynamically typed language.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. How can you iterate over elements of an array without using for and while loop in JS?
&lt;/h2&gt;

&lt;p&gt;Using &lt;code&gt;Array.prototype.forEach&lt;/code&gt; function helps to iterate over an array without using the traditional for and while loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// logs 1 2 3 4 5 in a newline.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. What is a Cartesian product in SQL?
&lt;/h2&gt;

&lt;p&gt;In SQL cartesian product is cross-join which gives the cross product of two tables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;COLOR&lt;/span&gt;
&lt;span class="k"&gt;CROSS&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="k"&gt;SIZE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;COLOR&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;RED&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BLUE&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;SIZE&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;LARGE&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MEDIUM&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SMALL&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;COLOR&lt;/th&gt;
&lt;th&gt;SIZE&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;RED&lt;/td&gt;
&lt;td&gt;SMALL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RED&lt;/td&gt;
&lt;td&gt;MEDIUM&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RED&lt;/td&gt;
&lt;td&gt;LARGE&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BLUE&lt;/td&gt;
&lt;td&gt;SMALL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BLUE&lt;/td&gt;
&lt;td&gt;MEDIUM&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BLUE&lt;/td&gt;
&lt;td&gt;LARGE&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  8. In which array methods can we use negative indexes and how can we use them?
&lt;/h2&gt;

&lt;p&gt;JS does not support a negative index to access the item directly. The way to access the item using a negative index is &lt;code&gt;Array.prototype.at&lt;/code&gt; method. All the array function which has an index as a parameter also accepts a negative index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// logs undefined&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// logs 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  9. What is destructuring in JS?
&lt;/h2&gt;

&lt;p&gt;Destructuring is a feature introduced in ECMAScript 6 (ES6) that allows you to extract values from arrays or properties from objects and bind them to variables in a concise and readable way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// a is 1 and b is 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  10. What is the expected output when giving 'None' as a parameter while using the filter function on a list in Python?
&lt;/h2&gt;

&lt;p&gt;When &lt;code&gt;None&lt;/code&gt; is passed as a parameter to the &lt;code&gt;filter()&lt;/code&gt; function it returns an iterator that filters out 'falsy' values such as empty list, string numerical zero, etc, or having the length as 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;my_vals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"word"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;my_vals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_vals&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# my_vals is [1, "word"] 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  11. What is the actual use case of JSON.stringify() and JSON.parse()?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It helps to store objects in &lt;code&gt;localStorage&lt;/code&gt; and &lt;code&gt;sessionStorage&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Used while making API requests.&lt;/li&gt;
&lt;li&gt;Store objects in a flat file.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>HTML/CSS concepts</title>
      <dc:creator>Saravanan</dc:creator>
      <pubDate>Thu, 02 Mar 2023 06:51:03 +0000</pubDate>
      <link>https://dev.to/__saravanan__/htmlcss-concepts-2dha</link>
      <guid>https://dev.to/__saravanan__/htmlcss-concepts-2dha</guid>
      <description>&lt;h2&gt;
  
  
  Box model
&lt;/h2&gt;

&lt;p&gt;In HTML and CSS, the box model specifies how elements are presented on a webpage. It consists of four components: content, padding, border, and margin. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Content: refers to the text, images, or other media contained within the element.&lt;/li&gt;
&lt;li&gt;Padding: the space between the content and the border.&lt;/li&gt;
&lt;li&gt;Border: the visual boundary for the element.&lt;/li&gt;
&lt;li&gt;Margin: the area outside the border, which separates the element from other neighboring elements on the page.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"padding: 10px; border: 2px solid black; margin: 20px;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Text content.
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the div element has 10 pixels of padding, a 2-pixel black solid border, and 20 pixels of margin. The text "Text content." is the actual content of the element.&lt;/p&gt;

&lt;p&gt;The default box model is content-box. In this model, the size of the element is determined by its content, and any padding, border, or margin is added to the outside of the element. The border-box is a model where the size of the element includes the padding and border, but not the margin. It means that the content area will be smaller than in the content-box model, but the overall size of the element will be the same.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"box-sizing: border-box; width: 100px; padding: 10px; border: 1px solid black; margin: 10px;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Text content.
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, for the div element border-box mode is specified using the box-sizing property with a width of 100 pixels. The content area will be 78 pixels wide because 10 pixels of padding and 1 pixel of the border are added to each side. However, the overall width of the element will be 120 pixels, because the margin is added to the outside of the element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inline vs Block elements
&lt;/h2&gt;

&lt;p&gt;In HTML, elements can be classified as either inline or block-level. Block-level elements are those that take up the full width of their container and are separated from other elements by a line break. Inline elements are those that are part of a line of text and are surrounded by other inline elements or text.&lt;/p&gt;

&lt;p&gt;Examples of block-level elements include div, p, and h1 to h6, while examples of inline elements include span, a, and img.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"display: inline-block; border: 1px solid black;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    block-level element inside an inline element.
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we have a div element with an inline-block display style. This means that the element will be displayed inline, but will also behave like a block-level element in terms of width and height.&lt;/p&gt;

&lt;h2&gt;
  
  
  Relative vs Absolute positioning
&lt;/h2&gt;

&lt;p&gt;In CSS, positioning helps to control the layout of elements on a webpage. Two commonly used types of positioning are relative and absolute positioning. It can be set by giving the property &lt;code&gt;position&lt;/code&gt; values &lt;code&gt;relative&lt;/code&gt; and &lt;code&gt;absolute&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Relative positioning allows an element to be moved relative to its normal position in the document flow. When an element is relatively positioned, it can be moved using the &lt;code&gt;top&lt;/code&gt;, &lt;code&gt;right&lt;/code&gt;, &lt;code&gt;bottom&lt;/code&gt;, and &lt;code&gt;left&lt;/code&gt; properties. These properties define the distance by how much the element is moved from its original position in the corresponding direction. For example, setting &lt;code&gt;top: 10px&lt;/code&gt; will move the element 10 pixels down from its original position.&lt;/p&gt;

&lt;p&gt;Absolute positioning allows an element to be placed in a specific location on the webpage, regardless of its position in the document flow. When an element is absolutely positioned, it is positioned relative to its closest-positioned ancestor. If no ancestor is positioned, the element is positioned relative to the initial containing block, which is the body element. An absolutely positioned element can be moved using the &lt;code&gt;top&lt;/code&gt;, &lt;code&gt;right&lt;/code&gt;, &lt;code&gt;bottom&lt;/code&gt;, and &lt;code&gt;left&lt;/code&gt; properties.&lt;/p&gt;

&lt;p&gt;The difference between relative and absolute positioning is that absolutely positioned elements are taken out of the normal document flow, while relatively positioned elements remain in the flow. This means that absolutely positioned elements can overlap other elements, while relatively positioned elements cannot. The position of an absolutely positioned element does not affect the position of other elements on the page, while the position of a relatively positioned element can affect the position of other elements.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Structural Classes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:first-child&lt;/code&gt; Selects the first child element from the parent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:last-child&lt;/code&gt; Selects the last child element from the parent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:nth-child(n)&lt;/code&gt; Selects the nth child element from the parent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:nth-last-child(n)&lt;/code&gt; Selects the nth child element from the end of the parent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:first-of-type&lt;/code&gt; Selects the first element of its type in the parent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:last-of-type&lt;/code&gt; Selects the last element of its type in the parent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:nth-of-type(n)&lt;/code&gt; Selects the nth element of its type in the parent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:nth-last-of-type(n)&lt;/code&gt; Selects the nth element of its type from the end of the parent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:only-child&lt;/code&gt; Selects an element that is the only child of the parent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:only-of-type&lt;/code&gt; Selects an element that is the only element of its type within the parent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:empty&lt;/code&gt; Selects an element that has no child elements or text content.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:root&lt;/code&gt; Selects the root element which is normally HTML.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common CSS Styling Classes
&lt;/h2&gt;

&lt;p&gt;In CSS, a styling class is a way to apply a specific set of styles to one or more elements on a webpage.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.container&lt;/code&gt; This class is used to create a container element that holds the content of a webpage. The container can be styled with a fixed or fluid width and can be centered on the page using margin or padding depending upon the content.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.button&lt;/code&gt; This class is used to create buttons on a webpage. Buttons can be styled with background colors, borders, and hover effects to make them stand out and provide visual feedback.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.navbar&lt;/code&gt; This class is used to create a navigation bar at the top of a webpage. The navbar can be styled with background colors, borders, and dropdown menus to provide easy access to different sections of the site.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.card&lt;/code&gt; This class is used to create a container element that displays information in a card-like format. Cards can be styled with borders, background colors, and shadows to create a visually appealing layout.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.form&lt;/code&gt; This class is used to create input forms on a webpage. Forms can be styled with input fields, labels, buttons, and validation messages.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.header&lt;/code&gt; This class is used to style the header section of a webpage. Headers can be styled with background images, logos, and navigation links.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.footer&lt;/code&gt; This class is used to style the footer section of a webpage. Footers can be styled with copyright information, contact links, and social media icons.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Styling classes help to maintain the same look throughout the website and easier to manage changes to the style of the website.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Specificity
&lt;/h2&gt;

&lt;p&gt;In CSS, specificity is a way of determining which styles should be applied to an element when there are conflicting styles. Each CSS selector has a specificity value, which is based on the number of ID, class, and element selectors used in the selector.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ID selectors have the highest specificity value and are calculated as 1-0-0. For example, &lt;code&gt;#my-id&lt;/code&gt; has a specificity value of 1-0-0.&lt;/li&gt;
&lt;li&gt;Class and attribute selectors have the next highest specificity value, and are calculated as 0-1-0. For example, &lt;code&gt;.my-class&lt;/code&gt; or &lt;code&gt;[type="text"]&lt;/code&gt; has a specificity value of 0-1-0.&lt;/li&gt;
&lt;li&gt;Element selectors have the lowest specificity value and are calculated as 0-0-1. For example, &lt;code&gt;p&lt;/code&gt; or &lt;code&gt;div&lt;/code&gt; has a specificity value of 0-0-1.&lt;/li&gt;
&lt;li&gt;When multiple selectors are used in a single selector, the specificity value is calculated by adding up the values for each selector. For example, the selector &lt;code&gt;#my-id .my-class&lt;/code&gt; would have a specificity value of 1-1-0, because it contains one ID selector and one class selector.&lt;/li&gt;
&lt;li&gt;If two selectors have the same specificity value, the one that appears later in the CSS code will be applied.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  CSS responsive queries
&lt;/h2&gt;

&lt;p&gt;CSS responsive queries, also known as media queries, are used to adjust the presentation of a webpage based on the characteristics of the device that it is being viewed on. This allows webpages that are optimized for different screen sizes, orientations, and device types.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;only&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;600px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;14px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, a media query is used to adjust the font size of the body element on devices with a screen width of 600 pixels or less. When the device's screen width is greater than 600 pixels, the default font size will be used. Media queries are used to create webpages that are flexible and adaptable to different screen sizes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Flexbox and Grid
&lt;/h2&gt;

&lt;p&gt;Flexbox and Grid are layout tools in CSS that allows the creation of flexible and responsive layouts for web pages. Both of them can be used depending on the situation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flexbox
&lt;/h3&gt;

&lt;p&gt;Flexbox is a layout module in CSS that helps to arrange elements within a container. To use Flexbox, we first define a container element with the &lt;code&gt;display: flex&lt;/code&gt; property. This creates a flex container, and all child elements of the container become flex items. Some of the key properties of Flexbox are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;flex-direction&lt;/code&gt;: Determines the direction of the main axis of the Flexbox. It can be set to &lt;code&gt;row&lt;/code&gt;, &lt;code&gt;row-reverse&lt;/code&gt;, &lt;code&gt;column&lt;/code&gt;, or &lt;code&gt;column-reverse&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;justify-content&lt;/code&gt;: Determines how flex items are distributed along the main axis of the Flexbox. It can be set to &lt;code&gt;flex-start&lt;/code&gt;, &lt;code&gt;flex-end&lt;/code&gt;, &lt;code&gt;center&lt;/code&gt;, &lt;code&gt;space-between&lt;/code&gt;, &lt;code&gt;space-around&lt;/code&gt;, or &lt;code&gt;space-evenly&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;align-items&lt;/code&gt;: Determines how flex items are aligned along the cross-axis of the Flexbox. It can be set to &lt;code&gt;flex-start&lt;/code&gt;, &lt;code&gt;flex-end&lt;/code&gt;, &lt;code&gt;center&lt;/code&gt;, &lt;code&gt;baseline&lt;/code&gt;, or &lt;code&gt;stretch&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;flex-wrap&lt;/code&gt;: Determines whether flex items should wrap onto multiple lines if they cannot fit within the Flexbox. It can be set to &lt;code&gt;nowrap&lt;/code&gt;, &lt;code&gt;wrap&lt;/code&gt;, or &lt;code&gt;wrap-reverse&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Grid
&lt;/h3&gt;

&lt;p&gt;CSS Grid is a layout system that provides a two-dimensional grid-based layout for the placement and sizing of elements. To use Grid, we first define a container element with the &lt;code&gt;display: grid&lt;/code&gt; property. This creates a grid container, and child elements of the container become grid items. Some of the key properties of Grid are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;grid-template-columns&lt;/code&gt; and &lt;code&gt;grid-template-rows&lt;/code&gt;: To define the columns and rows of the grid respectively. We can specify the size and position of each column and row using % fr px etc, as well as keywords like auto and minmax.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;grid-template-areas&lt;/code&gt;: It is to define named areas within the grid, which we can be assigned to specific grid items using the &lt;code&gt;grid-area&lt;/code&gt; property.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;grid-gap&lt;/code&gt;: It is to add spacing between grid items horizontally and vertically.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;grid-auto-columns&lt;/code&gt; and &lt;code&gt;grid-auto-rows&lt;/code&gt;: It helps determine the size of grid tracks that are not explicitly defined by the grid-template-columns or grid-template-rows properties.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common meta tags in the header
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;meta charset="utf-8"&amp;gt;&lt;/code&gt;: Specifies the character encoding of the document. "utf-8" is the common character encoding.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;&lt;/code&gt;: Sets the viewport width to the device width and specifies the initial zoom level when the page is first loaded.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;meta name="description" content="description of the page"&amp;gt;&lt;/code&gt;: It is to mention a brief description of the page content. It is used by search engines to display site descriptions in search results.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;meta name="keywords" content="comma-separated list of keywords"&amp;gt;&lt;/code&gt;: It specifies a list of keywords related to the page content. It is used in SEO and to help index the page.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;meta name="author" content="name of the author"&amp;gt;&lt;/code&gt;: It specifies the author of the page.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;meta name="robots" content="index"&amp;gt;&lt;/code&gt;: It specifies whether search engines should index the links on the page or not.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;meta name="theme-color" content="black"&amp;gt;&lt;/code&gt;: It specifies the theme color of the page. It is used to customize the browser UI.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Passing by Assignment, References, Shallow and Deep Copy - Python</title>
      <dc:creator>Saravanan</dc:creator>
      <pubDate>Mon, 27 Feb 2023 12:39:12 +0000</pubDate>
      <link>https://dev.to/__saravanan__/passing-by-assignment-references-shallow-and-deep-copy-python-3jjj</link>
      <guid>https://dev.to/__saravanan__/passing-by-assignment-references-shallow-and-deep-copy-python-3jjj</guid>
      <description>&lt;h2&gt;
  
  
  Passing by Assignment
&lt;/h2&gt;

&lt;p&gt;In Python, the concept of pass-by-value and pass-by-reference in functions does not exist. Instead, Python implements pass by assignment. This means that neither the values nor the names associated with them are transferred. Rather, every value is associated with the parameter through an assignment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_lowest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# prints [3, 7, 2, 4, 0, 9]
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;find_lowest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# prints 0
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# prints [0, 2, 3, 4, 7, 9]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;In lists and collections, each item is a reference. Similar to how a name is associated with a value, items within collections are associated with values through references. This association is known as reference binding.&lt;/p&gt;

&lt;p&gt;Let us look at the references with an example and how it works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# tic-tac-toe board matrix
&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="s"&gt;"-"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# prints 3 X 3 matrix with hyphen.
&lt;/span&gt;
&lt;span class="c1"&gt;# make an X mark in the center 
&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"X"&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;#Above print prints the below
# - X -
# - X -
# - X -
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code the default expectation is to change only the index [1][1] to "X" but, all the rows with index 1 become "X". This is because all three lists are aliases for a single list. To get the normally expected tic-tac-toe board where every list is not aliased below list comprehension can be used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="s"&gt;"-"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the value of any index is changed in the board, it does not affect other values as in the previous example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shallow copy and Deep copy
&lt;/h2&gt;

&lt;p&gt;A shallow copy creates a new object and copies the references to the original object's elements. It means that if the original object contains mutable objects, both the original and the copied objects will refer to the same mutable objects in memory.&lt;/p&gt;

&lt;p&gt;A deep copy creates a new object and recursively copies the contents of the original object and all of its nested objects. In other words, a deep copy creates a new copy of the objects within the original object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# shallow copy
&lt;/span&gt;
&lt;span class="n"&gt;original_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="n"&gt;new_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;new_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# prints [1, 2, [3, 4, 5]]
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# prints [1, 2, [3, 4, 5]]
&lt;/span&gt;
&lt;span class="c1"&gt;# deep copy
&lt;/span&gt;
&lt;span class="n"&gt;original_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="n"&gt;new_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deepcopy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;new_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# prints [1, 2, [3, 4]]
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# prints [1, 2, [3, 4, 5]]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copying is the most common way to solve the problem of passing around mutable objects in python in functions. Using id to check if the values have been modified does not work in python as shown below example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# prints id of tuple c
&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# prints ([1, 2, 3, 0], [4, 5, 6])
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="c1"&gt;# prints the same id as before for c as c has only reference of a and b as its value.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>SQL Concepts</title>
      <dc:creator>Saravanan</dc:creator>
      <pubDate>Wed, 22 Feb 2023 06:41:57 +0000</pubDate>
      <link>https://dev.to/__saravanan__/sql-4i1o</link>
      <guid>https://dev.to/__saravanan__/sql-4i1o</guid>
      <description>&lt;p&gt;SQL(Structured Query Language) is a domain-specific language that is used for managing data in a relational database. We will look at some of the concepts of SQL in detail below.&lt;/p&gt;

&lt;h2&gt;
  
  
  ACID
&lt;/h2&gt;

&lt;p&gt;ACID&lt;sup id="fnref1"&gt;1&lt;/sup&gt; - Atomicity, Consistency, Isolation, and Durability. &lt;/p&gt;

&lt;h3&gt;
  
  
  Atomicity
&lt;/h3&gt;

&lt;p&gt;Atomicity guarantees that a transaction, which consists of one or multiple SQL statements, is either entirely executed or not executed at all. A transaction can be committed, making all its changes permanent, or rolled back, undoing all of its changes. As a result, the database has only two states - one before the transaction is executed and one after it is completed - with no intermediate states.&lt;/p&gt;

&lt;h3&gt;
  
  
  Consistency
&lt;/h3&gt;

&lt;p&gt;Consistency ensures that the database is in a uniform state both before and after the execution of a transaction. This encompasses all rules, constraints, and triggers, and any deviations from this state of uniformity will result in the transaction being aborted.&lt;/p&gt;

&lt;h3&gt;
  
  
  Isolation
&lt;/h3&gt;

&lt;p&gt;In situations where multiple transactions are executed simultaneously in parallel, the final state of the database must be identical to the state that would result from sequentially executing those transactions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Durability
&lt;/h3&gt;

&lt;p&gt;Durability ensures that the modifications made to the database persist once a transaction is committed. Such changes should only result from SQL transactions, and external factors should not impact them.&lt;/p&gt;

&lt;h2&gt;
  
  
  CAP Theorem
&lt;/h2&gt;

&lt;p&gt;Eric Brewer proposed the CAP theorem&lt;sup id="fnref2"&gt;2&lt;/sup&gt;, which states that any distributed data store can provide only two out of the three following properties: consistency, availability, and partition tolerance.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consistency - At any given moment, all users view identical data, and any alterations made are instantaneously reflected everywhere.&lt;/li&gt;
&lt;li&gt;Availability - The database responds to every request, regardless of whether or not the response is consistent.&lt;/li&gt;
&lt;li&gt;Partition tolerance - A partition occurs when one or more nodes of a database become unavailable. Partition tolerance refers to the system's ability to continue functioning despite such partitions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When a node fails, the system must choose whether to prioritize consistency or availability. If consistency is prioritized, transactions are rolled back to synchronize the non-failed nodes with the failed ones. On the other hand, if availability is prioritized, transactions proceed, but the nodes may become inconsistent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Joins
&lt;/h2&gt;

&lt;p&gt;A join is a keyword in SQL that joins two or more tables to form a single table based upon a condition on matching fields. Five common types of joins are used.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JOIN/INNER JOIN - returns rows from both tables where matching values are found in the joined columns of both tables.&lt;/li&gt;
&lt;li&gt;LEFT JOIN -  returns every row from the left table. When a row is found with a matching value in the right table, values from that row are included in the results.&lt;/li&gt;
&lt;li&gt;RIGHT JOIN -  returns every row from the right table. When a row is found with a matching value in the left table, values from that row are included in the results.&lt;/li&gt;
&lt;li&gt;FULL OUTER JOIN - returns every row from both tables and joins the rows where values in the joined columns match. If there's no match for a value in either the left or right table, the query result contains no values for that table.&lt;/li&gt;
&lt;li&gt;CROSS JOIN - return every possible combination of rows from both tables.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Aggregations
&lt;/h2&gt;

&lt;p&gt;Aggregate functions combine values from multiple rows, perform an operation on those values, and return a single result. Some of the commonly used aggregate functions are given below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;count - returns a count of the column with values that are not null. To count all rows with null values use &lt;code&gt;*&lt;/code&gt; instead of a column name.&lt;/li&gt;
&lt;li&gt;max - returns the maximum value of a given column.&lt;/li&gt;
&lt;li&gt;min - returns the minimum value of a given column.&lt;/li&gt;
&lt;li&gt;sum - returns the sum value of a given column.&lt;/li&gt;
&lt;li&gt;avg - returns the average of the given numeric column.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Aggregate functions are often used with &lt;code&gt;GROUP BY&lt;/code&gt; to group the result by one or more columns. The aggregate function works on the set of values based on group by and not by the whole column.&lt;/p&gt;

&lt;h2&gt;
  
  
  Filter in queries
&lt;/h2&gt;

&lt;p&gt;Filtering the result based on any condition is done using the WHERE clause in SQL. The filtering is done by comparison and matching of values from selected data.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;= equal to(case sensitive)&lt;/li&gt;
&lt;li&gt;!= not equal to(case sensitive)&lt;/li&gt;
&lt;li&gt;/&amp;gt; greater than&lt;/li&gt;
&lt;li&gt;&amp;lt; lesser than&lt;/li&gt;
&lt;li&gt;/&amp;gt;= greater than or equal to&lt;/li&gt;
&lt;li&gt;&amp;lt;= lesser than or equal to&lt;/li&gt;
&lt;li&gt;~ matches with a regular expression&lt;/li&gt;
&lt;li&gt;BETWEEN - passes value only within the range&lt;/li&gt;
&lt;li&gt;IN - if the column value in the given set&lt;/li&gt;
&lt;li&gt;LIKE - match the column value with the pattern&lt;/li&gt;
&lt;li&gt;ILIKE - same as LIKE with case insensitive match&lt;/li&gt;
&lt;li&gt;NOT - prefix to a match to negate it&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Data normalization&lt;sup id="fnref3"&gt;3&lt;/sup&gt;
&lt;/h2&gt;

&lt;p&gt;Normalization involves organizing data in a database to increase its flexibility by removing redundancy and inconsistent dependency. There are several forms of normalization, but databases are typically designed up to the third normal form, despite the existence of higher levels of normalization.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First normal form

&lt;ul&gt;
&lt;li&gt;Eliminate repeating groups in individual tables.&lt;/li&gt;
&lt;li&gt;Create a separate table for each set of related data.&lt;/li&gt;
&lt;li&gt;Identify each set of related data with a primary key.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Second normal form

&lt;ul&gt;
&lt;li&gt;Create separate tables for sets of values that apply to multiple records.&lt;/li&gt;
&lt;li&gt;Relate these tables with a foreign key.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Third normal form

&lt;ul&gt;
&lt;li&gt;Eliminate fields that do not depend on the key.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h2&gt;
  
  
  Indexes&lt;sup id="fnref4"&gt;4&lt;/sup&gt;
&lt;/h2&gt;

&lt;p&gt;An index is a data structure that provides search results fast for a particular column in a database. Generating an index on a table's field produces another data structure that holds the field's value and a pointer to its corresponding record. This index structure is then sorted, enabling Binary Searches to be conducted on it.&lt;/p&gt;

&lt;p&gt;The data of the database are stored as data blocks. These data blocks are accessed entirely, making them the atomic disk access operation. Sorting these data blocks helps the database to access the data using binary search, which has a significant performance improvement over searching for the data in unsorted and randomly places data blocks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Transactions
&lt;/h2&gt;

&lt;p&gt;A transaction in a database consists of a sequence of data manipulation statements that must execute either completely or not at all, resulting in the database being in a consistent state. The main purpose of a transaction is to group several steps into a single, all-or-nothing operation. The intermediate states of a transaction are not observable by other concurrent transactions, and if an error prevents the transaction from completing, none of its steps will affect the database. Transactions are designed to support the ACID principles and provide concurrent access to multiple users and systems that are resistant to failures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Locking mechanism&lt;sup id="fnref5"&gt;5&lt;/sup&gt;
&lt;/h2&gt;

&lt;p&gt;Locking enables concurrent access to a database by multiple users. To access a data item for reading or writing, a lock is placed on it. A user requests a lock on the part of the data they need to access, and no conflicting lock is granted on the same data until the current lock is released. Two types of locks are shared and exclusive.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;shared lock&lt;/strong&gt; allows reading from the locked row or table, hence also called a read lock. Multiple transactions can hold a shared lock on the same resource and read from it. However, no transaction is permitted to update the resource while a shared lock is held on it.&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;exclusive lock&lt;/strong&gt; completely locks the row or table and enables the transaction to update the row in isolation. Only one transaction can obtain an exclusive lock on a particular resource at a time. Other processes that wish to acquire the lock on the same resource must wait until the lock is released. Once released, the remaining processes can acquire the lock and make modifications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Database isolation&lt;sup id="fnref6"&gt;6&lt;/sup&gt;
&lt;/h2&gt;

&lt;p&gt;The level of isolation in a database determines how much a transaction should be separated from the data changes made by other transactions. It is to avoid the retrieval or storage of temporary, aborted, or inconsistent data written by simultaneous transactions. There are four various levels of database isolation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read Uncommitted -  no transaction can update a database row if another transaction has already updated it and not committed but, allows dirty reads.&lt;/li&gt;
&lt;li&gt;Read Committed - does not allow any other transaction to write or read a row to which another transaction has written but not yet committed.&lt;/li&gt;
&lt;li&gt;Repeatable Read - a transaction that reads data from a row blocks any other writing transactions from accessing the same row.&lt;/li&gt;
&lt;li&gt;Serializable - locks the whole table to prevent any other transactions from inserting or reading data from it.&lt;/li&gt;
&lt;li&gt;Snapshot - transaction for modifying data works on a copy of committed data with a lock. Parallel read transactions are given the snapshot of the committed data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Triggers
&lt;/h2&gt;

&lt;p&gt;A trigger is a special type of stored procedure that automatically runs when an event occurs in the database such as INSERT, UPDATE, DELETE, and TRUNCATE.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TRIGGER&lt;/span&gt; &lt;span class="n"&gt;my_trigger&lt;/span&gt;
&lt;span class="k"&gt;BEFORE&lt;/span&gt; &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;my_table&lt;/span&gt;
&lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;EACH&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;span class="k"&gt;EXECUTE&lt;/span&gt; &lt;span class="k"&gt;FUNCTION&lt;/span&gt; &lt;span class="n"&gt;my_function&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above example is to create a trigger in postgres named my_trigger which gets executes my_function for every insert in the my_table.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="https://www.ibm.com/docs/en/cics-ts/5.4?topic=processing-acid-properties-transactions"&gt;https://www.ibm.com/docs/en/cics-ts/5.4?topic=processing-acid-properties-transactions&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;a href="https://www.ibm.com/topics/cap-theorem"&gt;https://www.ibm.com/topics/cap-theorem&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/office/troubleshoot/access/database-normalization-description"&gt;https://learn.microsoft.com/en-us/office/troubleshoot/access/database-normalization-description&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/1108/how-does-database-indexing-work"&gt;https://stackoverflow.com/questions/1108/how-does-database-indexing-work&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;&lt;a href="https://medium.com/inspiredbrilliance/what-are-database-locks-1aff9117c290"&gt;https://medium.com/inspiredbrilliance/what-are-database-locks-1aff9117c290&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;&lt;a href="https://medium.com/nerd-for-tech/understanding-database-isolation-levels-c4ebcd55c6b9"&gt;https://medium.com/nerd-for-tech/understanding-database-isolation-levels-c4ebcd55c6b9&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>SOLID concepts</title>
      <dc:creator>Saravanan</dc:creator>
      <pubDate>Mon, 20 Feb 2023 07:12:32 +0000</pubDate>
      <link>https://dev.to/__saravanan__/solid-concepts-24hf</link>
      <guid>https://dev.to/__saravanan__/solid-concepts-24hf</guid>
      <description>&lt;p&gt;The SOLID principles&lt;sup id="fnref1"&gt;1&lt;/sup&gt; are principles that help in designing the structure of a program that uses object-oriented programming. SOLID principles are a subset of principles of Object-oriented design by Robert C. Martin&lt;sup id="fnref2"&gt;2&lt;/sup&gt;. SOLID is an acronym for the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;S - Single Responsibility Principle&lt;/li&gt;
&lt;li&gt;O - Open-Closed Principle&lt;/li&gt;
&lt;li&gt;L - Liskov Substitution Principle&lt;/li&gt;
&lt;li&gt;I - Interface Segregation Principle&lt;/li&gt;
&lt;li&gt;D - Dependency Inversion Principle&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why SOLID and other software design principles?
&lt;/h2&gt;

&lt;p&gt;When software reaches its first release it is clean and elegant. The issue arises when there is any change in requirements and that change causes the software to not be clean and elegant which then results in redesign after a few releases down the road. Robert C. Martin calls this software rot and there are 4 symptoms of rotting. They are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rigidity - The software becomes too hard to implement changes. Each change causes cascading changes.&lt;/li&gt;
&lt;li&gt;Fragility - When a part of the software is changed, another part breaks which has no relation.&lt;/li&gt;
&lt;li&gt;Immobility - When it's too hard to reuse part of software leading to rewriting it again.&lt;/li&gt;
&lt;li&gt;Viscosity(design) - When multiple ways are present to implement change, the design preserving change being the hard one to implement.&lt;/li&gt;
&lt;li&gt;Viscosity(environment) - The implementation of change is slow and inefficient.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;SOLID principles are some of the principles that will help design better software from start.&lt;/p&gt;

&lt;h2&gt;
  
  
  Single Responsibility Principle&lt;sup id="fnref3"&gt;3&lt;/sup&gt;
&lt;/h2&gt;

&lt;p&gt;The Single Responsibility Principle states that each software module should have one and only one reason to change. The 'only one reason to change' means that whenever there is a change request from a person/department/team on software when done should result in a new version of the software with a change in only one particular part of the software. No change request from another person/department/team should result in changes in the same part of the software.&lt;/p&gt;

&lt;p&gt;It essentially means that software should be separated into different classes or modules depending upon from which stakeholder the request arises, therefore meaning that the class or module has a single responsibility.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;calculate_pay&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;report_hours&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this example, consider an organization with CFO, CTO, and COO at the top. Here the calculate_pay method is allocated to CFO, the report_hours method is allocated to the COO, save method which saves all data would be allocated to CTO inside the Employee class. Whenever there is any change by any C-level person, it would not affect other parts of the employee which are allocated to other C-level persons.&lt;/p&gt;

&lt;h2&gt;
  
  
  Open-Closed Principle
&lt;/h2&gt;

&lt;p&gt;The Open-Closed Principle states that a module should be open for extension but closed for modification. It means that when we want to change the modules, we should be able to change them without changing the code of the modules. This is achieved through the use of abstraction and polymorphism. &lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Modem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;abstractmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AnalogModem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Modem&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Dialing &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; using an analog modem."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DigitalModem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Modem&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Dialing &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; using a digital modem."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;logOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;modem&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Modem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;modem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;analog_modem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;AnalogModem&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;digital_modem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DigitalModem&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;logOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;analog_modem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"12345"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"pass"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;logOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;digital_modem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"67890"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"pass"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here in the above example, the logOn function is open for modification by implementing analog and digital modems but, is closed from the logOn function. When a new type of modem is created in the future there is no need to modify logOn code but the logOn can be changed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Liskov Substitution Principle
&lt;/h2&gt;

&lt;p&gt;The Liskov Substitution Principle states that subclasses should be suitable for their base classes. When using inheritance the derived class must be able to substitute the base class in usage. If there is any significant change between the base class and the derived class then there shouldn't be inheritance.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_sound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_sound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bark"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_sound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"meow"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_animal_sound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;make_sound&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Usage:
&lt;/span&gt;&lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;make_animal_sound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# bark
&lt;/span&gt;&lt;span class="n"&gt;make_animal_sound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# meow
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we can pass both cat and dog instances with the Animal base class as an argument to the function and it still works as expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interface Segregation Principle
&lt;/h2&gt;

&lt;p&gt;The Interface Segregation Principle states that many client interfaces are better than one general-purpose interface. It means that if a class has several clients, rather than loading the class with all the methods that the clients need, having specific interfaces for each client and multiplying inherit them into class. This will help the client to inherit a class where they don't need to know the parts of the interface that they won't use.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Printer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;abstractmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Fax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;abstractmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;abstractmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MultiFunctionDevice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Printer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Fax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Scanner&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Printer"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Fax"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Scanner"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimplePrinter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Printer&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"modern printer"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the interfaces Printer, Fax, and Scanner are implemented in all or separately instead of one big fat PrintFaxScan class even though they are closely related in the real world. The person implementing only one interface does not know the others. &lt;/p&gt;

&lt;h2&gt;
  
  
  Dependency Inversion Principle
&lt;/h2&gt;

&lt;p&gt;The Interface Segregation Principle states "Depend upon Abstractions. Do not depend upon concretions.". When a class depends upon other classes, it can result in repeated code. Every dependency in design should target an interface or abstract class. No dependency should target the concrete class. This is to ensure that a change in one part of the code won't result in a break in another of the code.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Driver&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;drive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vehicle&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;vehicle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;drive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Vehicle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;drive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Vehicle&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;drive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Driving &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Boat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Vehicle&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;drive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Sailing &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, if the Driver is created by extending Car and Boat, it'll result in repeated code and will require cascading changes to implement change to Driver. Having the Driver target the class Vehicle will help it to automatically use the Car and Boat class.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/SOLID"&gt;https://en.wikipedia.org/wiki/SOLID&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;a href="http://staff.cs.utu.fi/%7Ejounsmed/doos_06/material/DesignPrinciplesAndPatterns.pdf"&gt;http://staff.cs.utu.fi/~jounsmed/doos_06/material/DesignPrinciplesAndPatterns.pdf&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;&lt;a href="https://blog.cleancoder.com/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html"&gt;https://blog.cleancoder.com/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>solid</category>
    </item>
    <item>
      <title>Objects and Object-Oriented Programming - Python</title>
      <dc:creator>Saravanan</dc:creator>
      <pubDate>Thu, 16 Feb 2023 06:35:05 +0000</pubDate>
      <link>https://dev.to/__saravanan__/objects-and-object-oriented-programming-python-2b9n</link>
      <guid>https://dev.to/__saravanan__/objects-and-object-oriented-programming-python-2b9n</guid>
      <description>&lt;p&gt;An object is an instance of a class in python. A class is like a blueprint for the instances. Objects have member variables and member functions which are known as attributes and methods in python. The main aim of OOP is the encapsulation of data and functions that work on those data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ClassNameCamelCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AnotherClass&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# class def
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are two main relationships in OOP, they are composition and inheritance. Composition is where an object contains another object and inheritance is where one class builds on top of another class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Constructors, Initializer, and Finalizer
&lt;/h2&gt;

&lt;p&gt;A class has a built-in initializer &lt;code&gt;__init__()&lt;/code&gt; method to define the initial values of the instance attributes and it should not have a return in it.&lt;/p&gt;

&lt;p&gt;The constructor in python can be defined by using &lt;code&gt;__new__()&lt;/code&gt;. The constructor allocates memory for objects and is the only function called before creating the instance. For the constructor, the class is passed as an argument instead of the self like other class functions. In most cases, constructors are not defined.&lt;/p&gt;

&lt;p&gt;The finalizer &lt;code&gt;__del__()&lt;/code&gt; is called when the instance is at the end of the scope and its lifetime is complete. The garbage collector is run when the finalizer is run. Post-processing on instances depends upon the python implementation and instance attributes may not be available inside the finalizer at all times.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attributes - Python
&lt;/h2&gt;

&lt;p&gt;There are no private or public labels in python to control the access of the instance attributes. A normal attribute has no preceding underscores and it is meant to be accessed using the instance name and dot operator.&lt;/p&gt;

&lt;p&gt;The attributes that are not meant to be accessed by the public are preceded with an underscore like &lt;code&gt;_random_name&lt;/code&gt;. Even though they can be accessed by using the dot operator, it is there to say that they should not be accessed.&lt;/p&gt;

&lt;p&gt;Using name mangling for an attribute or member function can prevent it from being used by a dot operator. It can still be accessed using a class name surrounded by an underscore like &lt;code&gt;obj_instance._ClassName___mangledname&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Class attributes are attributes that are available to all instances. It exists for the class and not for every instance. It is usually defined before all member functions inside the class definition.&lt;/p&gt;

&lt;h2&gt;
  
  
  Methods - Python
&lt;/h2&gt;

&lt;p&gt;Instance methods are methods that can access the instance attributes and class attributes. These are normal functions that work on instance attributes.&lt;/p&gt;

&lt;p&gt;Class methods are methods that only work on class attributes and are declared as such using the &lt;code&gt;@classmethod&lt;/code&gt; decorator on top of the function. This can be called from both the class and instance.&lt;/p&gt;

&lt;p&gt;Static methods are methods that can't access both instance and class attributes. A static method is there to add any function or business logic such as an algorithm that is being used by an instance of a class.&lt;/p&gt;

&lt;p&gt;Property methods are methods to provide neat getters and setters in Python. A property method for the attribute members can be defined by using the below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nb"&gt;property&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fget&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fdel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;fget argument is for the function that returns the attribute value, fset is for the function assigning the value, fdel is for the function that runs when an instance is deleted, and the doc is for docstring. Property methods can also be set using decorators as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;random_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;property&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;random_name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getter&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;randon_fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;random_name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setter&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;randon_fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;random_name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deleter&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;randon_fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Special methods or dunder(double underscore) methods add special functionality such as &lt;code&gt;__str__()&lt;/code&gt; is used when the object is converted to a string. There are many different dunder methods in python each having its use case.&lt;/p&gt;

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

&lt;p&gt;Inheritance is a concept that allows the creation of a class that shares a common code with another class. In an inherited class some of the attributes and methods are overridden and additional attributes and methods are added.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;InheritedClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ParentClass&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;#class definition
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A class can also inherit from multiple class but, resolution for functions must be made whenever there is conflicting functions or attributes are there in parent classes. By default, python uses C3 Method Resolution Order to resolve attribute and method lookup conflicts when a class has multiple inheritance or multilevel inheritance. If a class has a local attribute or method it comes first. In other cases, the order is followed as per the inheritance graph.&lt;/p&gt;

&lt;p&gt;Mixin is a class that is inherited by different unrelated classes that requires common functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Abstract class and Virtual subclasses
&lt;/h2&gt;

&lt;p&gt;An abstract class is a class that only has methods without any definition. Any class that inherits an abstract class needs to implement all the methods in the abstract base class. It helps to provide a structure for all inheriting classes to conform to a structure.&lt;/p&gt;

&lt;p&gt;Virtual subclasses are classes that implement the interface defined by the abstract base class without explicitly inheriting it. It is mainly used to decouple the interface from the class hierarchy and make it easier to extend. To register a class as a virtual subclass of an abstract class register method is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;RandAbstractClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;VirtualSubClass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>oops</category>
      <category>python</category>
      <category>class</category>
    </item>
    <item>
      <title>String Methods</title>
      <dc:creator>Saravanan</dc:creator>
      <pubDate>Thu, 16 Feb 2023 04:27:34 +0000</pubDate>
      <link>https://dev.to/__saravanan__/string-methods-69d</link>
      <guid>https://dev.to/__saravanan__/string-methods-69d</guid>
      <description>&lt;p&gt;There are three different types of strings in python. They are &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;string literals&lt;/li&gt;
&lt;li&gt;raw strings&lt;/li&gt;
&lt;li&gt;formatted strings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;String literals&lt;/strong&gt; - String literals can be defined using single quotes &lt;code&gt;'&lt;/code&gt;, double quotes &lt;code&gt;"&lt;/code&gt; and triple quotes &lt;code&gt;"""&lt;/code&gt;. The escape characters are mentioned inside using a backslash &lt;code&gt;\&lt;/code&gt; for example a new line would be &lt;code&gt;\n&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Raw strings&lt;/strong&gt; - In raw string a backslash is treated as a literal character. A string is treated as a raw string of its prefixed with an &lt;code&gt;r&lt;/code&gt; in front. Raw strings are used for regular expressions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Formatted strings&lt;/strong&gt; - Also called f-strings allows us to insert values of variables into a string. The value of the variable or expression inside curly braces will replace the curly brace section inside the f-string. Placing the semicolon &lt;code&gt;:&lt;/code&gt; after the variable inside the curly brace will help change the format specification.&lt;/p&gt;

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

&lt;p&gt;String concatenation can be done by both the &lt;code&gt;+&lt;/code&gt; operator and the join function and f-string. In terms of performance, the f-string is the fastest one, followed by the join and plus operators the last.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common String methods
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;capitalize&lt;/strong&gt;() - capitalizes the first character of a string and all others in lowercase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;first Second&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;capitalize&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# returns "First second"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;center&lt;/strong&gt;(width[, fillchar]) - returns a string in the center within a specified width.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;word&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;center&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# returns "---word---"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;count&lt;/strong&gt;(sub[, start[, end]]) - returns the number of occurrences of a substring within a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;l&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# returns 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;find&lt;/strong&gt;(sub[, start[, end]]) - returns the lowest index of the substring within the string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello world&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;o&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# returns 4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;format&lt;/strong&gt;(*args, **kwargs) - performs string formatting operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi {}, It&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s {} now.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# returns "Hi name, It's 2 now."
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;format_map&lt;/strong&gt;(mapping) - formats a string using a dictionary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi {name}, It&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s {time} now.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format_map&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Batman&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;time&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="c1"&gt;# returns "Hi Batman, It's 12 now."
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;index&lt;/strong&gt;(sub[, start[, end]]) - returns the lowest index of the substring within the string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;o&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# returns 4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;isalnum&lt;/strong&gt;() - returns True if all characters in the string are alphanumeric.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;a1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isalnum&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# returns True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;isalpha&lt;/strong&gt;() - returns True if all characters in the string are alphabetic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isalpha&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# returns True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;isdecimal&lt;/strong&gt;() - returns True if all characters in the string are decimal. Decimal characters are those that can be used to form numbers in base 10.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;123&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isdecimal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# returns True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;isdigit&lt;/strong&gt;() - returns True if all characters in the string are digits. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;¹²³&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isdigit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# returns True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;isnumeric&lt;/strong&gt;() - returns True if all characters in the string are numeric. Numeric characters include digit characters, and all characters that have the Unicode numeric value property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ⅠⅩⅤⅬⅭⅮⅯ&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isnumeric&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# returns True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;islower&lt;/strong&gt;() - returns True if all characters in the string are lowercase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;islower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# returns True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;isupper&lt;/strong&gt;() - returns True if all characters in the string are uppercase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HELLO&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isupper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# returns True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;join&lt;/strong&gt;(iterable) - returns a string concatenated with the elements of an iterable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;World&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;# returns "Hello World"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;lower&lt;/strong&gt;() - converts the string to lowercase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# returns "hello"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;replace&lt;/strong&gt;(old, new[, count]) - returns a string with all occurrences of the old string replaced with the new string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi First&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;First&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;New&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# returns "Hello New"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;split&lt;/strong&gt;([sep[, maxsplit]]) - returns a list of the words in the string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello World&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# returns ["Hello", "World"]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;strip&lt;/strong&gt;() - returns string with leading and trailing whitespace removed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello  &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# returns "hello"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;upper&lt;/strong&gt;() - converts the string to uppercase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# returns "HELLO"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>opensource</category>
      <category>contributorswanted</category>
      <category>community</category>
    </item>
    <item>
      <title>Array Methods - Python</title>
      <dc:creator>Saravanan</dc:creator>
      <pubDate>Thu, 16 Feb 2023 04:21:34 +0000</pubDate>
      <link>https://dev.to/__saravanan__/array-methods-python-2i65</link>
      <guid>https://dev.to/__saravanan__/array-methods-python-2i65</guid>
      <description>&lt;p&gt;Python doesn't have a built-in array data type. The &lt;code&gt;array&lt;/code&gt; module is used to work with the array. An array can be created 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;import array

arr = array.array("i", [1, 2, 3, 4, 5])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above line creates an array of elements 1 to 5 of type signed integer. Here the &lt;code&gt;array()&lt;/code&gt; function is used to create an array. The first &lt;code&gt;"i"&lt;/code&gt; is to mention that the type code of the array is a signed integer. &lt;/p&gt;

&lt;p&gt;The list of type codes can be obtained by running below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array.typecodes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which returns &lt;code&gt;bBuhHiIlLqQfd&lt;/code&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;b - signed char&lt;/li&gt;
&lt;li&gt;B - unsigned char&lt;/li&gt;
&lt;li&gt;u - unicode char&lt;/li&gt;
&lt;li&gt;h - signed short&lt;/li&gt;
&lt;li&gt;H - unsigned short&lt;/li&gt;
&lt;li&gt;i - signed int&lt;/li&gt;
&lt;li&gt;I - unsigned int&lt;/li&gt;
&lt;li&gt;l - signed long&lt;/li&gt;
&lt;li&gt;L - unsigned long&lt;/li&gt;
&lt;li&gt;q - signed long long&lt;/li&gt;
&lt;li&gt;Q - unsigned long long&lt;/li&gt;
&lt;li&gt;f - float&lt;/li&gt;
&lt;li&gt;d - double&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the type code has a corresponding C Type. The byte size of the type depends upon the machine running the program. The byte size of arr can be checked below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.itemsize
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The array can be created from the list and converted to the list as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.fromlist([1, 2, 3, 4, 5])
arr.tolist() # returns python list [1, 2, 3, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;New items can be added to the array by appending or inserting a value at a position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.append(6)
arr.insert(2, 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;New items can be appended from strings or files also.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.append("word")
arr.append(file_source, 10) # reads 10 lines from file and appends it
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Elements can be removed by using the index or value of the element. The pop is to remove from an element by passing the index and the remove is by passing a value of the element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.pop(1)
arr.remove(4)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The array can also be reversed by calling the reverse function. This does not reverse the array in memory but, changes the access order.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Process management in GNU/Linux</title>
      <dc:creator>Saravanan</dc:creator>
      <pubDate>Thu, 02 Feb 2023 06:45:41 +0000</pubDate>
      <link>https://dev.to/__saravanan__/process-management-in-gnulinux-509l</link>
      <guid>https://dev.to/__saravanan__/process-management-in-gnulinux-509l</guid>
      <description>&lt;h2&gt;
  
  
  Kill
&lt;/h2&gt;

&lt;p&gt;Kill is a program that is used to send signals to a process or process groups. By default &lt;strong&gt;TERM&lt;/strong&gt; is sent to the process if no signal is specified. We can send any signal to the process as below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$kill -SIGNAL PID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The signal can be a name or number. the list of signals with number and name is listed with the below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$kill -l
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, kill will send &lt;strong&gt;SIGTERM&lt;/strong&gt; or &lt;strong&gt;15&lt;/strong&gt; to the process. This will send the signal to the process and it is handled as per the process. It can be blocked, handled, or ignored by the process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$kill -9 PID
$kill -KILL PID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above sends &lt;strong&gt;SIGKILL&lt;/strong&gt; causes the process and its child processes to terminate immediately. The process can't block or ignore this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$kill -HUP PID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above signal also terminates the program but, the difference is it is sent by the system when a terminal is closed and all the processes started from the terminal will receive &lt;strong&gt;SIGHUP&lt;/strong&gt; which means a hang-up signal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$kill -STOP PID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command sends &lt;strong&gt;SIGSTOP&lt;/strong&gt; to the process which suspends the process immediately. It can then be resumed by sending the &lt;strong&gt;SIGCONT&lt;/strong&gt; signal.&lt;/p&gt;

&lt;p&gt;For the most common use, &lt;strong&gt;killall&lt;/strong&gt; is used. It kills a process by its name. For example, to kill chrome below command can be used. The &lt;strong&gt;-w&lt;/strong&gt; flag is to wait till all signaled processes die.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$killall -w chromium-browser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  PS - process status
&lt;/h2&gt;

&lt;p&gt;The ps shows which processes are running currently running. There are 3 styles to the arguments for ps. GNU with long names and double dash, BSD usually without any dash, and UNIX with dashes. The below commands are essentially the same&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ps -ef
$ps aux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All default columns are self-explanatory except the &lt;strong&gt;STAT&lt;/strong&gt; which shows the status of the process. The meaning of each letter for this column is given below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;R: running&lt;/li&gt;
&lt;li&gt;Z: zombie&lt;/li&gt;
&lt;li&gt;D/S: sleep&lt;/li&gt;
&lt;li&gt;T: stopped&lt;/li&gt;
&lt;li&gt;N: low priority&lt;/li&gt;
&lt;li&gt;l: multithreading&lt;/li&gt;
&lt;li&gt;s: session leader&lt;/li&gt;
&lt;li&gt;+: foreground process group&lt;/li&gt;
&lt;li&gt;&amp;lt;: high priority
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ps auxf
$ps auxwww
$ps auxe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;f&lt;/strong&gt; is forest, which is used to show process trees in ASCII art. &lt;strong&gt;www&lt;/strong&gt; is to expand the width and shows processes with all of its arguments. &lt;strong&gt;e&lt;/strong&gt; will display with environment variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  TOP
&lt;/h2&gt;

&lt;p&gt;TOP is a cli task manager program. It gives a live update of top users of the system's resources.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;The load average with 3 numbers gives average CPU use in the last 1, 5, and 15 minutes. Each column is described below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;%CPU: shows percent use of single-core, for the 12-core processor it can be up to 1200%&lt;/li&gt;
&lt;li&gt;%MEM: use of disk space&lt;/li&gt;
&lt;li&gt;COMMAND: command line used to start the process&lt;/li&gt;
&lt;li&gt;RES: the amount of RAM used&lt;/li&gt;
&lt;li&gt;USER: owner of the process&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cryptocurrency</category>
      <category>crypto</category>
      <category>web3</category>
      <category>bitcoin</category>
    </item>
    <item>
      <title>GNU/Linux utilities for system info</title>
      <dc:creator>Saravanan</dc:creator>
      <pubDate>Thu, 02 Feb 2023 06:43:47 +0000</pubDate>
      <link>https://dev.to/__saravanan__/gnulinux-utilities-for-system-info-5gd8</link>
      <guid>https://dev.to/__saravanan__/gnulinux-utilities-for-system-info-5gd8</guid>
      <description>&lt;h2&gt;
  
  
  df
&lt;/h2&gt;

&lt;p&gt;Disk free or df is used to find the free space under each partition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$df -h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above command displays the free space in a human-readable format. We can also check if we can create new files with the below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$df -ih
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It displays inodes in each partition. If there is none then, new files cannot be created.&lt;/p&gt;

&lt;h2&gt;
  
  
  free
&lt;/h2&gt;

&lt;p&gt;Free is similar to disk free. It is used the check the free and used amount of RAM/memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$free -h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command displays the usage of memory in a human-readable format.&lt;/p&gt;

&lt;h2&gt;
  
  
  lspci
&lt;/h2&gt;

&lt;p&gt;lspci lists all PCI devices in the system.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Above command lists all PCI devices. The first column has data in the format of &lt;strong&gt;domain : bus : slot . func&lt;/strong&gt; followed by the device description.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$lspci -t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above command displays in ASCII tree format to show how devices are connected to buses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#lspci -vvvs $ID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To display complete information about the device &lt;strong&gt;-vvv&lt;/strong&gt; is used. A decrease in character v count reduces the detail of information. &lt;strong&gt;-s&lt;/strong&gt; is used to specify a device by its id without which it displays all device information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$lspci -k
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get the kernel module for each device using the &lt;strong&gt;-k&lt;/strong&gt; flag.&lt;/p&gt;

&lt;h2&gt;
  
  
  uname
&lt;/h2&gt;

&lt;p&gt;uname is used to print the system information. The &lt;strong&gt;-a&lt;/strong&gt; flag is to display all the information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$uname -a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Particular information can be printed with the flags. They are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;o: operating system name, not the distro name&lt;/li&gt;
&lt;li&gt;i: the hardware platform&lt;/li&gt;
&lt;li&gt;p: processor architecture&lt;/li&gt;
&lt;li&gt;v: kernel version&lt;/li&gt;
&lt;li&gt;s: kernel name&lt;/li&gt;
&lt;li&gt;m: machine hardware name&lt;/li&gt;
&lt;li&gt;n: system's name/node network hostname&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>crypto</category>
      <category>blockchain</category>
      <category>web3</category>
      <category>offers</category>
    </item>
  </channel>
</rss>
