<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Michael Abraham Wekesa</title>
    <description>The latest articles on Forem by Michael Abraham Wekesa (@wekesa360).</description>
    <link>https://forem.com/wekesa360</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%2F848135%2F854ed9ed-9561-4c68-a057-db8bc337fff0.png</url>
      <title>Forem: Michael Abraham Wekesa</title>
      <link>https://forem.com/wekesa360</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/wekesa360"/>
    <language>en</language>
    <item>
      <title>Python Classes and Objects - Get to know how classes and objects work!</title>
      <dc:creator>Michael Abraham Wekesa</dc:creator>
      <pubDate>Sat, 24 Dec 2022 19:30:48 +0000</pubDate>
      <link>https://forem.com/wekesa360/classes-and-objects-in-python-1mll</link>
      <guid>https://forem.com/wekesa360/classes-and-objects-in-python-1mll</guid>
      <description>&lt;p&gt;Python is a high level  and an object oriented programming language.&lt;br&gt;
In python classes are used to construct objects, thus a class is a code template for creating objects.&lt;/p&gt;

&lt;p&gt;A class being a blueprint for an object creation, it is a form of data structure that is user-defined, binding an objects data members and its methods in a single unit.&lt;/p&gt;

&lt;p&gt;An object on the other hand is an instance of a class. Objects have states and behaviors. The state of an object represents its attributes and the behavior its methods.&lt;br&gt;
An object must also have an identity in order of it to be identified uniquely.&lt;br&gt;
Hence an object must have identity, state and behavior.&lt;/p&gt;

&lt;p&gt;A real world example of a class and objects:&lt;/p&gt;

&lt;p&gt;A car can be a class, as it is a template of a vehicle of any model, brand, different car engine size etc.&lt;br&gt;
So lets define a car in a class template;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;class&lt;/strong&gt;: Car&lt;br&gt;
&lt;strong&gt;State&lt;/strong&gt;: model, color, brand&lt;br&gt;
&lt;strong&gt;Behavior&lt;/strong&gt;: fuel consumption&lt;/p&gt;

&lt;p&gt;Using the class above, we can now create car objects:&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;First Object&lt;/strong&gt;&lt;/u&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;brand&lt;/strong&gt;: Toyota&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;model&lt;/strong&gt;: Corolla&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;color&lt;/strong&gt;: Blue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Behavior&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fuel consumption&lt;/strong&gt;: For every 35 kilometers it consumes 1 gallon of fuel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Second Object&lt;/strong&gt; &lt;/u&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;brand&lt;/strong&gt;: Honda&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;model&lt;/strong&gt;: Civic&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;color&lt;/strong&gt;: Green&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Behavior&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fuel consumption&lt;/strong&gt;: For every 40 kilometers it consumes 1 gallon of fuel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From the first and second objects, they are both created from the same class but have different states and behaviors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a class in python&lt;/strong&gt;&lt;br&gt;
The keyword &lt;code&gt;class&lt;/code&gt; is used in python to create a class, followed by the class name. It is best practice to have a class name with the &lt;strong&gt;UpperCaseCamelCase&lt;/strong&gt; naming convention.&lt;/p&gt;

&lt;p&gt;After defining the class name, the block of statements that follow for the specific class define its attributes and methods.&lt;br&gt;
With python classes, there is &lt;em&gt;Docstrings&lt;/em&gt; which is the first string in a class that briefly describes the class. It is not necessary but it is best practice in python classes, methods or functions to have docstrings. &lt;/p&gt;

&lt;p&gt;We will use the example of the car class in the code examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Defining a class in Python&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&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;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
       &lt;span class="c1"&gt;# instance variables(state)
&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;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;color&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;brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;brand&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;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;

    &lt;span class="c1"&gt;# behavior (instance method)
&lt;/span&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;car_details&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;'Car brand: &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;brand&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, model: &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;model&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, color: &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;color&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="c1"&gt;# behavior (instance method)
&lt;/span&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fuel_consumption&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;distance&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="n"&gt;fuel_amount&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="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;'For every &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;distance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; kilometers it consumes &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;fuel_amount&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; gallon of fuel'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;self&lt;/code&gt; keyword represents the instance of the class within itself. Meaning the class attributes and methods can be accessed within itself using the "self" keyword.&lt;/p&gt;

&lt;p&gt;From the above class, with the &lt;code&gt;fuel_consumption&lt;/code&gt; method, there are two parameters, this is to ensure that each object has its unique rate of fuel consumption based on a specific distance covered by the car object.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;__init__&lt;/code&gt; is a type of function known as a &lt;strong&gt;constructor&lt;/strong&gt; which in python is a special method used to create and initialize an object of a class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating an object of a class&lt;/strong&gt;&lt;br&gt;
Here is how we get to create an object of an already defined class. We will use the &lt;code&gt;Car&lt;/code&gt; class that we defined above;&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;toyota_corolla&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Corolla'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Toyota'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Blue'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;toyota_corolla&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;car_details&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

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

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Car brand: Toyota, model: Corolla, color: Blue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the &lt;code&gt;fuel_consumption&lt;/code&gt; method, we have to pass in the &lt;code&gt;distance&lt;/code&gt; and &lt;code&gt;fuel_amount&lt;/code&gt; arguments.&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;toyota_corolla&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Corolla'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Toyota'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Blue'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;toyota_corolla&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fuel_consumption&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;distance&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fuel_amount&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;For every 35 kilometers it consumes 1 gallon of fuel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When defining the &lt;code&gt;fuel_consumption&lt;/code&gt; method there is a default value for both &lt;code&gt;fuel_amount&lt;/code&gt; and &lt;code&gt;distance&lt;/code&gt; parameters, in case there are no arguments passed in, then the default values will be used instead. The default values are &lt;code&gt;distance=10&lt;/code&gt; and &lt;code&gt;fuel_amount=1&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;toyota_corolla&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Corolla'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Toyota'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Blue'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;toyota_corolla&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fuel_consumption&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

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

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;For every 10 kilometers it consumes 1 gallon of fuel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Class Attributes&lt;/strong&gt;&lt;br&gt;
When designing a class, there are two types of variables, the instance variables and the class variables.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instance variables&lt;/em&gt; - These are attributes attached to an instance class. They are variables defined in the constructor, in the &lt;code&gt;__init__&lt;/code&gt; class method.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Class variables&lt;/em&gt; - Are attributes or variables declared in the class but outside any instance method including the &lt;code&gt;__init__&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;When accessing a class attribute can be accessed by using a dot on an instance followed by the attribute name; &lt;code&gt;instance_name.attribute_name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is in the same way that an attribute can be modified, example(using &lt;code&gt;the Car&lt;/code&gt; class):&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;toyota_corolla&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Corolla'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Toyota'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Blue'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;toyota_corolla&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'Yellow'&lt;/span&gt;
&lt;span class="n"&gt;toyota_corolla&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;car_details&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

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

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Car brand: Toyota, model: Corolla, color: Yellow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the above example, the Toyota Corolla's color is changed from Blue to Yellow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Class Methods&lt;/strong&gt;&lt;br&gt;
With Object Oriented Programming a class can have 3 types of methods within the class:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Static Method&lt;/em&gt;: It is a general-purpose utility method that finishes just one task. We don't use instance or class variables within this static method because it doesn't have access to the class attributes.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Class Method&lt;/em&gt;: Used to access or modify the class's state. When implementing a method, if we only use class variables, we should declare that method as a class method.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instance Method&lt;/em&gt;: Used to access or alter the status of the object. Methods are referred to as instance methods if they contain instance variables. &lt;/p&gt;

&lt;p&gt;A class method is linked to the class itself, not to any class objects.&lt;br&gt;
Only class variables are accessible to it. &lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>python</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Python Functions – How to write, call and the best ways to write a function</title>
      <dc:creator>Michael Abraham Wekesa</dc:creator>
      <pubDate>Sat, 24 Dec 2022 16:51:55 +0000</pubDate>
      <link>https://forem.com/wekesa360/functions-in-python-34a</link>
      <guid>https://forem.com/wekesa360/functions-in-python-34a</guid>
      <description>&lt;p&gt;Functions are a set of statements bundled together to perform a specific task. A function can accept arguments that are manipulated to cause about a specific output or effect as intended. &lt;br&gt;
Functions are often used in order to organize code, including getting rid of repetitive code, thus helps greatly in achieving the &lt;strong&gt;DRY&lt;/strong&gt; principle in software engineering. DRY stands for 'Don't Repeat Yourself'.&lt;/p&gt;

&lt;p&gt;Functions can be seen as executable code blocks and can be called once or more.&lt;/p&gt;

&lt;p&gt;In Python we define functions using the &lt;code&gt;def&lt;/code&gt; keyword followed by the function's name and finally the parenthesis. Parameters to the function are defined within the parenthesis. &lt;/p&gt;

&lt;p&gt;When calling a function that accepts some arguments, we pass them in the parenthesis.&lt;/p&gt;

&lt;p&gt;An example of a function:&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;print_hello_world&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;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When calling the function:&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;print_hello_world&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Hello&lt;/span&gt; &lt;span class="n"&gt;World&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Functions can have parameters, parameters are used as placeholders or variables for inputs that will be passed as inputs during the calling of a function. Arguments on the other hand are values that are passed in a function as inputs to the function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functions with parameters&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Declaring parameters when defining a function:&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;sum_two_numbers&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="nb"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When calling the function we pass in the arguments, which in this case are &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;. For a = 3 and b = 4;&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;sum_two_numbers&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="mi"&gt;7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you take a look at the &lt;code&gt;sum_two_numbers&lt;/code&gt; function, there is the keyword &lt;code&gt;return&lt;/code&gt;. The &lt;code&gt;return&lt;/code&gt; keyword is used to output the results back to the caller. It is also used as the exit point of a function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lambda functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lambda functions are small and anonymous(unlike the normal functions, they're defined without a name) functions, they take any number of arguments and return a result, however the can only have one expression.&lt;/p&gt;

&lt;p&gt;A simple example of a lambda function that squares a value&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="c1"&gt;# print a the square of x
&lt;/span&gt;&lt;span class="n"&gt;square_fun&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&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;x&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;square_fun&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="mi"&gt;16&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Docstrings in functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A docstring is a string literal that is used in a function, class or when defining a class method. They give a brief description about the class or method, or a function in this case. It is not necessary to have a docstring but it is recommended.&lt;/p&gt;

&lt;p&gt;The docstring of a function can be accessed using the &lt;code&gt;__doc__&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;Example of a docstring in functions:&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;mul_number&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="s"&gt;""" multiply  to values, a and b"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Getting the docstring of a function:&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;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mul_number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__doc__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;multiply  to values, a and b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is important to note that with functions or blocks of code in python, indentation is very crucial, it is easy to break code or have semantic errors, with unintended indentation.&lt;/p&gt;

&lt;p&gt;This is a simple tutorial to give a brief introduction to functions from which you can lay a basis of understanding functions in python.&lt;/p&gt;

&lt;p&gt;There is more to functions, being an explorer is one way to find out!&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What are loops and conditional statements in Python? Understand them better!</title>
      <dc:creator>Michael Abraham Wekesa</dc:creator>
      <pubDate>Tue, 10 May 2022 18:44:31 +0000</pubDate>
      <link>https://forem.com/wekesa360/python-102-the-way-to-go-5flb</link>
      <guid>https://forem.com/wekesa360/python-102-the-way-to-go-5flb</guid>
      <description>&lt;p&gt;With the &lt;a href="https://dev.to/wekesa360/python-101-ultimate-python-guide-3ebg"&gt;basics of python&lt;/a&gt;, you can now speak the language. in order to get to know and dive in further in to programming and putting some serious logic in code, there are a few things to learn and understand. Let's do it the python way!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Conditional statements&lt;/li&gt;
&lt;li&gt;Loops&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Conditional statements
&lt;/h4&gt;

&lt;p&gt;Conditional statements are commands used in handling decisions, in which a decision is made if and only if the pre-stated conditions are either true or false.&lt;br&gt;
For example the traffic lights, you only start driving if and only if the lights turn from red to green else you wait.&lt;/p&gt;

&lt;p&gt;Before diving deep into conditional statements there are some logical conditions to consider, of which play a crucial part in making conditions with conditional statements, in python these basic logical conditions from math are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Less than, e.g a &amp;lt; b&lt;/li&gt;
&lt;li&gt;Equals, e.g a == b&lt;/li&gt;
&lt;li&gt;Greater than, e.g a &amp;gt; b&lt;/li&gt;
&lt;li&gt;Less than or equal to, e.g a &amp;lt;= b&lt;/li&gt;
&lt;li&gt;Greater than or equal to, e.g a&amp;gt;= b&lt;/li&gt;
&lt;li&gt;Not Equals, e.g a != b&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These logical conditions are used with &lt;strong&gt;if statements&lt;/strong&gt; and &lt;strong&gt;loops&lt;/strong&gt; in manipulating the control flow in programming.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. If statement&lt;/strong&gt;&lt;br&gt;
A program will only continue to be executed if only the test expression evaluates to true and if the test expression is false, the statements after the condition is not executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if 2 &amp;gt; 1:
   print("Greater!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Nested if statements&lt;/strong&gt;&lt;br&gt;
One condition is executed and if true then the next condition is executed and so on till the last if condition is met. These if statements are declared inside other if statements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n = 10
if n &amp;lt; 20:
   n = 20
   print(" Greater than 10")
   if n &amp;lt; 30:
      n = 30
      print(" Greater than 20")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Greater than 10!
Greater than 20!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Elif statements&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;elif&lt;/em&gt; is a keyword that enables a program to go through another if condition if the previous one is false e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = 43
b = 57
if a == 57:
   print("a is equal to 57!")
elif b == 57:
   print("b is equal to 57!")
else:
   print("Sorry, neither a nor b is equal to 57!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;b is equal to 57!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. If not Statements&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;not&lt;/em&gt; is a keyword that returns True if the statement is not true. returns true if  a value is false;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num_list = [2,4,6,8,9]
num = 5
if num not in num_list:
   print("num not in num_list")
else:
   print("Found it in num_list!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num not in num_list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Python loops
&lt;/h4&gt;

&lt;p&gt;A loop is basically a set of instructions or statements of a given program that is repeated a finite number of times until a certain condition is met.&lt;br&gt;
There are two types of loops in python:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For loops&lt;/li&gt;
&lt;li&gt;while loops&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;1. for loops&lt;/strong&gt;&lt;br&gt;
A for loop is used for iterating over a sequence of statements multiple times, e.g a list, dictionary or a tuple.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;instruments = ["Guitar", "Piano", "Flute", "Drums"]
for instrument in instruments:
   print(instrument)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Guitar
Piano
Flute
Drums
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. while loops&lt;/strong&gt;&lt;br&gt;
While loops repeat a statement or a block of statements until a given condition is false. It checks the condition before executing the loop body, if the condition is true, it will go on until it is false, then exit the loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i = 1
while i != 5:
  print(i)
  i += 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
3
4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Break statement&lt;/strong&gt;&lt;br&gt;
A break statement is used in stopping a loop even if the condition still passes as true.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i = 1
while i != 8:
  print(i)
  if i == 6:
    break
  i += 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
3
4
5
6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Continue statement&lt;/strong&gt;&lt;br&gt;
The &lt;em&gt;continue&lt;/em&gt; keyword can stop the current iteration and continue with the next iteration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i = 0
while i != 8:
  i += 1
  if i == 6:
    continue
  print(i)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
3
4
5
7
8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the knowledge of loops and conditional statements. There's more to do with python. As we progress we unleash its potential.&lt;br&gt;
Here is a simple calculator in which we get to implement the conditional statements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simple calculator&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("The simple calculator")
print("===========")
num1 = input("Enter the first value: ")
num2 = input("Enter the second value: ")
print("===========")
print("\n1.Addition\t2.Subtraction\n3.Multiplication\t4.Division")
choice = input("Pick a choice (1,2,3 or 4): ")
num1 = int(num1)
num2 = int(num2)

if choice == "1":
    print( num1 + num2)
elif choice == "2":
    print(num1 - num2)
elif choice == "3":
    print(num1 * num2)
elif choice == "4":
    print(num1 / num2)
else:
    print("invalid option")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
With that, adios! And happy coding. Hope you found this informative.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Welcome to Python 101! — Lets get started with the basics</title>
      <dc:creator>Michael Abraham Wekesa</dc:creator>
      <pubDate>Sat, 16 Apr 2022 07:59:13 +0000</pubDate>
      <link>https://forem.com/wekesa360/python-101-ultimate-python-guide-3ebg</link>
      <guid>https://forem.com/wekesa360/python-101-ultimate-python-guide-3ebg</guid>
      <description>&lt;p&gt;Python is a high-level programming language, in layman's language, it is closer to human understanding(English like) than it is to machines. Python is an interpreted language and a general purpose programming language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A brief history of python&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python  is programming language that came into life in the 1980s and was developed by Guido van Rossum at Centrum Wiskunde &amp;amp; Informatica (CWI) in the Netherlands, with the ideology of it being a successor to the ABC programming language.&lt;/p&gt;

&lt;p&gt;Since then python has evolved through several versions to the latest version python 3.10 (as of the time of writing this article) with a first release date of 4 October, 2021&lt;/p&gt;

&lt;p&gt;Python 2.0, released 16 October 2000, with new features including a cycle-detecting garbage collector for memory management and support for Unicode.&lt;/p&gt;

&lt;p&gt;Python 3.0, released on 3 December 2008.With this version there was massive revision that it was not backward compatible with previous python versions.&lt;/p&gt;

&lt;p&gt;Before we get our hands dirty, let's set some rules of engagement, shall we!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax in Python&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python has a very simple and user friendly syntax, that you'll find simple and easy to grasp.&lt;br&gt;
Some include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Indentation in python is crucial, it can break your code!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python is case sensitive, State and state are different&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All variables should start with a lowercase letter&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;classes begin with capital letters&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The standard is to actually use English names in programming&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Identifiers (names you define for variables, types, functions, and labels) should not be keywords in python and start with a symbol or number.&lt;/p&gt;

&lt;p&gt;For more on style and best practices in python check out on &lt;a href="https://www.python.org/dev/peps/pep-0008/"&gt;PEP-8&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installing python&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/co-learning-lounge/how-to-download-install-python-on-windows-2021-44a707994013"&gt;On windows machine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://opensource.com/article/20/4/install-python-linux"&gt;On linux machine&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-local-programming-environment-on-macos"&gt;On MacOS&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Python Basics&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Base types&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;int (integer)
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;value = 76
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;float
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;value = 56.21
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;bool (boolean)
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;is_blue = True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;str (string)
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;message = "Hello world!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Backslash is used for escaping characters.&lt;br&gt;
e.g&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;\' - Single quote&lt;/li&gt;
&lt;li&gt;\b - backspace&lt;/li&gt;
&lt;li&gt;\n - new line&lt;/li&gt;
&lt;li&gt;\r - carriage return&lt;/li&gt;
&lt;li&gt; \t - tab
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;message = " \t Sammy\'s cat ran away\nand into the bush it went.\n"
# print - an inbuilt function to print an output
print(message)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# output
#     Sammy's cat ran away
# and into the bush it went.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Multiline strings&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   message = """ Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
   sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
   Ut enim ad minim veniam, 
   quis nostrud exercitation ullamco laboris nisi ut 
   aliquip ex ea commodo consequat."""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sequence types&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;list&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Lists are modifiable and can hold values of different types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list_country = ["Russia",  "South Africa", "Japan", "Peru"]
random_list  = ["x", 3.14, 4, False, "No way"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;tuple&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Tuples cannot be modified once created. They are immutable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;coordinates = (33.56, 4.56, 21.89)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;range&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Is a function returning a sequence of numbers, starts from 0 and increments by 1 by default.&lt;br&gt;
Format: range(start, stop, step)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for x in range(0,10,2):
    print(x)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Mapping types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dictionary&lt;/p&gt;

&lt;p&gt;Is a collection of data that is stored in key-value pair, key is unique&lt;br&gt;
e.g "Laptop" is the key and "Mac book" the value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;backpack = {"Laptop":"Mac book", "Headphones": "Sony",  "Hard drive": "SSD"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Set types&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;set&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A set is an unordered collection that is mutable, iterable and has unique elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fruits = {"apple", "mango", "orange"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.frozen-set&lt;/p&gt;

&lt;p&gt;Freezes the elements, making them unchangeable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fruits = ({"apple, "mango", "orange"})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Conversions
&lt;/h4&gt;

&lt;p&gt;Change a value of one data type to another.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;""" 1.str to int"""
int("10") #output: 10

""" 2. float to int"""
int(10.34) #output: 10

""" 3. any data type to str"""
str(10.10) #output: "10.1"
str(3==3) #output:  "True"

""" 4. character to ASCII code """
ord('*') #output: 42

""" 5. string to list"""
message = "Hello world!"
message_list = list(message)
# print- print output
print(message_list)
# output
# ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']

""" 6. dictionary to list """
day = {"day":14, "month": "Feb", "year": 2022}
# create a list with list compression
generated_list = [(x, y) for x, y  in day.items()]
# print - print output
print(generated_list)
# output
# [('day', 14), ('month', 'Feb'), ('year', 2022)]

""" 7. list to string """
word_list = ["The", "new", "version", "of", "slack", "is", "amazing"]
word_str = " ".join(word_list)
print(word_str)
# output
# The new version of slack is amazing

""" 8. list to set """
number_list = [34, 45, 67 ,87]
number_set = set(number_list)
print(number_set)
# output
# {34, 67, 45, 87}

""" 9. list to dictionary """
day_list = [('day', 14), ('month', 'Feb'), ('year', 2022)]
day_dict = dict(day_list)
print(day_dict)
# output
# {"day":14, "month": "Feb", "year": 2022}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Working with Strings
&lt;/h4&gt;

&lt;p&gt;There are several ways in which strings can be manipulated.&lt;br&gt;
&lt;/p&gt;

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

# output
# Hello

print("Hello", "world", "!")

# output
# Hello world !

print("Hello", "world", sep="-")

# output
# Hello-world

print("Hello") # for every print, output is set to a new line
print("world")

# output
# Hello
# World

print("Hello", "world", sep="-", end="!") 

# output
# Hello-world!

print("Hello", end=" ") # without new line
print("World")

# output
# Hello world
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  String Concatenation
&lt;/h4&gt;

&lt;p&gt;String concatenation is the operation of joining character strings end-to-end.(wikipedia)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;first_name = "John"
middle_name = "Magarita"
last_name = "Omondi"

print(first_name + " " + middle_name + " " + last_name)

# output
# John Magarita Omondi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  String formatting
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;first_name = "John"
middle_name = "Magarita"
last_name = "Omondi"
salary = 50000
print("{0} {1} earns a salary of ${2}".format(first_name, middle_name, salary))

# output
# John Magarita earns a salary of $50000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With knowing the basics of python, you can write simple command line programs&lt;br&gt;
for example:&lt;/p&gt;

&lt;h4&gt;
  
  
  Simple mad-libs generator program
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;""" A simple word game that takes user input based on the word type and 
inserting in to a paragraph """

noun1 = input("Enter a noun(Plural): ")
place = input("Enter a place: ")
noun2 = input("Enter a noun: ")
noun3 = input("Enter a noun(Plural): ")
adjective1 = input("Enter an Adjective: ")
verb1 = input("Enter a verb: ")
verb2 = input("Enter a verb: ")
number = input("Enter a number: ")
adjective2 = input("Enter an Adjective: ")
body_part = input("Enter a body part: ")
verb3 = input("Enter a verb: ")

mad_lib = """Two {0}, both alike in dignity,
In fair {1} where we lay our scene,
From ancient {2} break to new mutiny,
Where civil blood makes civil hands unclean.
From forth the fatal loins of these two foes
A pair of star-cross`d {3} take their life;
Whole misadventured piteous overthrows
Do with their {4} bury their parents` strife.
The fearful passage of their {5} love,
And the continuance of their parents` rage,
Which, but their children`s end, nought could {6},
Is now the {7} hours` traffic of our stage;
The which if you with {8} {9} attend,
What here shall {10}, our toil shall strive to mend. """.format(noun1, place, noun2, noun3, adjective1, verb1, verb2,number, adjective2,body_part, verb3)

print("\n====Mad Lib (Romeo and Juliet: Prologue Ad-lib) ====\n")
print(mad_lib)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python is an amazing multi-purpose programming language that can be used in almost all fields in technology, from AI to scripting and data science. The best part is, it is fun to learn and user-friendly. &lt;br&gt;
Happy coding!&lt;/p&gt;

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