<?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: Hambolu Akintomiwa</title>
    <description>The latest articles on DEV Community by Hambolu Akintomiwa (@hambolu4u).</description>
    <link>https://dev.to/hambolu4u</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%2F1404945%2F7630f4f2-5238-4013-bf3b-4ac38bcc1fe8.png</url>
      <title>DEV Community: Hambolu Akintomiwa</title>
      <link>https://dev.to/hambolu4u</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hambolu4u"/>
    <language>en</language>
    <item>
      <title>Python Data Types and Variables</title>
      <dc:creator>Hambolu Akintomiwa</dc:creator>
      <pubDate>Tue, 23 Apr 2024 10:51:20 +0000</pubDate>
      <link>https://dev.to/hambolu4u/python-data-types-and-variables-1257</link>
      <guid>https://dev.to/hambolu4u/python-data-types-and-variables-1257</guid>
      <description>&lt;p&gt;Python has several built-in data types such as integers, floats, strings, and booleans. Variables in Python are used to store these data types and manipulate them in our programs.&lt;/p&gt;

&lt;p&gt;Understanding these concepts is fundamental to programming in Python. In this guide, we'll explore different Python data types and variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Data Types in Python
&lt;/h2&gt;

&lt;p&gt;Basic data types in Python include Numeric Data Types, Text Data Type, and Boolean. &lt;/p&gt;

&lt;h3&gt;
  
  
  Numeric Data Types
&lt;/h3&gt;

&lt;p&gt;There are two main numeric data types, including &lt;code&gt;float&lt;/code&gt;, integer (&lt;code&gt;int&lt;/code&gt;) in Python. They are used to represent numerical values and perform mathematical operations. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Integers (int)
&lt;/h3&gt;

&lt;p&gt;Integers are data types used to represent both negative and positive whole numbers (without decimals) in Python and other programming languages.&lt;/p&gt;

&lt;p&gt;You can use them for different mathematical operations, including multiplication, division, subtraction , and addition. The following code is an example of two variables(&lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;)  containing integers:&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;50&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="nf"&gt;print&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="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&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="n"&gt;y&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 code above, we have multiplied as well as added the two variables using the &lt;code&gt;print&lt;/code&gt; function. So, if you run the code on your IDE or terminal, you’ll get the following 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;100&lt;/span&gt;
&lt;span class="mi"&gt;52&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Floats
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Floats&lt;/code&gt; are used to represent both positive and negative numbers with decimals or fractions in Python and other programming languages.&lt;/p&gt;

&lt;p&gt;Just like &lt;code&gt;integers&lt;/code&gt;, you can also perform numeric operations like addition, multiplication, division, and subtraction with floating numbers. The following code multiplies and add two floating numbers.&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="mf"&gt;20.5&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;2.2&lt;/span&gt;

&lt;span class="nf"&gt;print&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="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&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="n"&gt;y&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="mf"&gt;45.1&lt;/span&gt;
&lt;span class="mf"&gt;22.7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, you can also use &lt;code&gt;integers&lt;/code&gt; and &lt;code&gt;floats&lt;/code&gt; together or interchangeably if it's suitable for your Python program. Let’s take another example where you can perform numerical operations on &lt;code&gt;floats&lt;/code&gt; and &lt;code&gt;int&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;20.5&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="nf"&gt;print&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="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&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="n"&gt;y&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="mf"&gt;22.5&lt;/span&gt;
&lt;span class="mf"&gt;41.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Text Data Type
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Str&lt;/code&gt;(string) is the primary text data type in Python. Strings are used to represent textual data and can contain any sequence of characters, including letters, numbers, symbols, and whitespace. The following is an example of a variable containing strings.&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;myString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Booleans
&lt;/h3&gt;

&lt;p&gt;Booleans are data types in Python and other programming languages used to represent truth values. They are represented by keywords such as &lt;code&gt;True&lt;/code&gt; and &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Booleans&lt;/code&gt; are fundamental in decision making and flow control within programs, as they allow for conditional statements and logical operations.&lt;/p&gt;

&lt;p&gt;Let’s see how you can use &lt;code&gt;booleans&lt;/code&gt; in a Python program with the following 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;raining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;sunny&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;raining&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Don&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;t forget to take your umbrella along&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;sunny&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;It&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s sunny outside&lt;/span&gt;&lt;span class="sh"&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 code above, we assigned the two &lt;code&gt;boolean&lt;/code&gt; values &lt;code&gt;True&lt;/code&gt; and &lt;code&gt;False&lt;/code&gt; to two different variables (&lt;code&gt;raining&lt;/code&gt; and &lt;code&gt;sunny&lt;/code&gt;). The &lt;code&gt;if&lt;/code&gt; block will print any of the conditions that is &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, in this code, the &lt;code&gt;raining&lt;/code&gt; variable contains the boolean value, &lt;code&gt;True&lt;/code&gt; and will print the conditions attached. If you print the code in your terminal or IDE, you’ll get the following 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;Don&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;t forget your umbrella
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Variables in Python
&lt;/h2&gt;

&lt;p&gt;Variables are considered containers where you can save different kinds of data in a Python program. Unlike Javascript, you don’t need to write any command before declaring a variable.&lt;/p&gt;

&lt;p&gt;It's declared the moment you assign a value to it. Now, let’s declare two variables and assign values to them:&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="mi"&gt;47&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="nf"&gt;print&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="nf"&gt;print&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s very crucial to note that variables in Python are case sensitive. The fact that the both variables are of the same letter doesn’t mean they are the same. they are considered two different variables.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Python for Loop - A Beginner’s Guide</title>
      <dc:creator>Hambolu Akintomiwa</dc:creator>
      <pubDate>Wed, 17 Apr 2024 09:02:23 +0000</pubDate>
      <link>https://dev.to/hambolu4u/python-for-loop-a-beginners-guide-2b85</link>
      <guid>https://dev.to/hambolu4u/python-for-loop-a-beginners-guide-2b85</guid>
      <description>&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop in Python is used to iterate over a sequence, including list, tuple, string, or other iterable objects. Yet, its functionality differs from loops in languages like C++. It works similarly to the iterator methods in other object-oriented languages.&lt;/p&gt;

&lt;p&gt;The syntax of the Python for loop is written as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for variable in iterable:
     #statement
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, the for keyword indicates the start of the loop and instructs Python to perform the action in the statement block of code on each item of the sequence (or iterable).&lt;/p&gt;

&lt;p&gt;Also, item serves as a placeholder in the code and can be replaced with any name or word. Examples of different ways you can use the for loop are contained in the sections below.&lt;/p&gt;

&lt;h3&gt;
  
  
  Looping Through a List
&lt;/h3&gt;

&lt;p&gt;A list in Python can contain objects such as Boolean, strings, integers, and floats and you can as well loop through all of them using the &lt;code&gt;for&lt;/code&gt; loop. &lt;/p&gt;

&lt;p&gt;For example, the code below prints out all the items contained in the list on a new line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myList = [23, "life", True , 24.0, "learn"]
for i in myList:
    print(i)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;i&lt;/code&gt; in the code above is a variable representing all the items in the list. Also, the line for i in myList indicates that we want to access all the items contained in the variable myList.&lt;/p&gt;

&lt;p&gt;So when you run the code above in your editor or IDE, as shown below, each of the items contained in the list will be printed on a new line.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Let’s take another example to get a better understanding of looping through a list with a little twist. This time, we want the output to printout horizontally. We can do this by adding the &lt;code&gt;end&lt;/code&gt; parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;newList = [30, 40, 70, "learn", 90]
for items in newList:
    print(items, end= " ")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code creates a list called &lt;code&gt;newList&lt;/code&gt; containing integers (30, 40, 70, 90) and a string ("learn"). Then, it iterates through each item in the list using a &lt;code&gt;for&lt;/code&gt; loop. Within the loop, it prints each item followed by a space.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;end=" "&lt;/code&gt; parameter in the print function specifies that after printing each item, a space should be used as the separator instead of the default newline character. So, when you run this code, it will print each item in the list separated by a space, 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;30 40 70 learn 90 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Looping Through a Tuple
&lt;/h3&gt;

&lt;p&gt;You can also loop through other sequences including Tuples, using the Python &lt;code&gt;for&lt;/code&gt; Loop. The code below is used to print out each of the objects in the Tuple on a new line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myTuple = ("life", 24, 75, False, 43.0, "why")
for i in myTuple:
   print(i)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is not compulsory you use the &lt;code&gt;i&lt;/code&gt; in the statement - you can use any letter or word in there. So, if you run the code, the following output will be shown:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;life
24
75
False
43.0
why
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us loop through a tuple with another example, without printing the output on a new line for each items (with the &lt;code&gt;end&lt;/code&gt; parameter).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;newTuple = (32, 56, "tuple", 40.2)
for items in newTuple:
    print(items, end= " ")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&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;32 56 tuple 40.2 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Looping Through a String
&lt;/h3&gt;

&lt;p&gt;You can also loop through a string the same way we looped through a list and tuple above using the same syntax. The following code loops through all the characters of the  string and prints each of them on a new line:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;If you run the code above, the following output will be executed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;P
y
t
h
o
n
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s loop through another string with another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;newString = "I am learning Python"
for items in newString:
     print(items, end= "")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&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;I am learning Python
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Nested for Loop
&lt;/h3&gt;

&lt;p&gt;Basically, a loop within a loop is termed a &lt;code&gt;nested for Loop&lt;/code&gt; and is supported by most programming languages including Python. It’s usually indented under the main loop. The main loop is called the outer loop, while the nested loop is called the inner loop. The basic syntax for the nested &lt;code&gt;for&lt;/code&gt; loop is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for outer_variable in outer_sequence:
  #outer loop statement

    for inner_variable in inner_sequence:
       #inner loop statement
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nested &lt;code&gt;for&lt;/code&gt; loops in Python allows iteration over multiple sequences within each other. The outer loop iterates through one sequence, while the inner loop iterates through another sequence for each iteration of the outer loop.&lt;/p&gt;

&lt;p&gt;As per the syntax above, the code iterates through &lt;code&gt;outer_sequence&lt;/code&gt;, then iterates through &lt;code&gt;inner_sequence&lt;/code&gt; for each outer value, executing statements for the two loops.&lt;/p&gt;

&lt;p&gt;Example:  In the following code, we will use the Python nested &lt;code&gt;for&lt;/code&gt; loop to multiply each content of the second variable &lt;code&gt;y&lt;/code&gt; by every items in the &lt;code&gt;x&lt;/code&gt; variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = [4, 5]
y = [2, 3]
for i in x:
    for j in y:
        print(i*j)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We used the variable &lt;code&gt;i&lt;/code&gt; to access all the items contained in &lt;code&gt;x&lt;/code&gt; as well as &lt;code&gt;j&lt;/code&gt; to access all the content of &lt;code&gt;y&lt;/code&gt; and we multiplied them using the &lt;code&gt;*&lt;/code&gt; operator. &lt;/p&gt;

&lt;p&gt;Also, notice that the inner &lt;code&gt;for&lt;/code&gt; loop is indented below the outer loop. If it’s not properly indented, the code will give an indentation error when you print. If you run the code, the output will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8
12
10
15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, you can prevent the code from printing each outcome on different lines. You can do this by using the &lt;code&gt;end&lt;/code&gt; parameter within the &lt;code&gt;print()&lt;/code&gt; function. &lt;/p&gt;

&lt;p&gt;By default, the &lt;code&gt;print()&lt;/code&gt; function automatically adds a new line &lt;code&gt;(\n)&lt;/code&gt; to the end of any output it prints. The &lt;code&gt;end&lt;/code&gt; parameter allows you to stop that default behaviour. &lt;/p&gt;

&lt;p&gt;So, if we use the &lt;code&gt;end&lt;/code&gt; parameter on the initial nested &lt;code&gt;for&lt;/code&gt; loop code, the code will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = [4, 5]
y = [2, 3]
for i in x:
    for j in y:
        print(i*j, end= " ") 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&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;8 12 10 15 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the output is printed out in form of a row instead of the initial column. You can also replace the spacing between the output to a comma (,) by writing it like &lt;code&gt;print(i*j, end= ",")&lt;/code&gt;. Running the code in this form will add commas between all the output.&lt;/p&gt;

&lt;h3&gt;
  
  
  Looping Through a Range
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop in Python can also be used to loop through a range of sequences with the range function  (&lt;code&gt;range()&lt;/code&gt;). It is used to generate a sequence of numbers and can take a maximum of 3 arguments (start, stop, and step size).&lt;/p&gt;

&lt;p&gt;The default value of the first argument in the range function is (0). Meaning if not set to any number, the range will start printing from zero (0). Also, the second argument is the number where the sequence stops (usually n - 1).&lt;/p&gt;

&lt;p&gt;Finally, the third optional argument (step size) is by default set to 1 and can be changed to any size, depending on what you want to achieve with the code. The following is the basic syntax of looping through a range of sequence:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for var_name in range(start, stop, stepsize):
        #statement
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the help of the syntax above, let’s run a simple code that will loop through the sequence of a range below:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;The starting point of the sequence in this code is set at &lt;code&gt;0&lt;/code&gt; and will iterate up to and not including &lt;code&gt;16&lt;/code&gt;, with a step size of &lt;code&gt;2&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;However, it is not compulsory to set a step size or a starting point in your code. The default step and starting point are set at 1 and 0 respectively. So, If you run the code, the following output will be printed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
2
4
6
8
10
12
14

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Looping Through a Range with Else and If Statements
&lt;/h3&gt;

&lt;p&gt;You can also make conditional statements in your Python codes while looping through a range with the &lt;code&gt;else&lt;/code&gt; and &lt;code&gt;if&lt;/code&gt; statements. &lt;/p&gt;

&lt;p&gt;As you may have known, both statements works together with some conditions. The &lt;code&gt;else&lt;/code&gt; statement will execute based on the outcome of the &lt;code&gt;if&lt;/code&gt; statements (true or false). &lt;/p&gt;

&lt;p&gt;Now, let’s loop through a range with these statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range (2,16,2):
    if i == 8:
        pass
    print(i)
else:
    print("end of the loop")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code is looping through a range with a starting number of &lt;code&gt;2&lt;/code&gt;, stopping number of &lt;code&gt;16&lt;/code&gt;, and &lt;code&gt;2&lt;/code&gt; as the step size. If the code is without an &lt;code&gt;if&lt;/code&gt; statement or condition, it will will run till the end down to the &lt;code&gt;else&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;For a code with an &lt;code&gt;if&lt;/code&gt; statement like the one above, the &lt;code&gt;else&lt;/code&gt; block will on only execute based on the conditions that comes with the &lt;code&gt;if&lt;/code&gt; block. For this code, the &lt;code&gt;pass&lt;/code&gt; statement is a null operation; nothing happens if it is executed. It is usually used as a placeholder. Meaning, the code will run to the &lt;code&gt;else&lt;/code&gt; block successfully. &lt;/p&gt;

&lt;p&gt;So, if you run the code above, the following output will be executed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2
4
6
8
10
12
14
end of the loop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, if the &lt;code&gt;if&lt;/code&gt; block in the code has other conditions such as &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; instead of the &lt;code&gt;pass&lt;/code&gt; statements, the outcome of the code will change. Let’s include the &lt;code&gt;break&lt;/code&gt; statement into the code above and see the outcome:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range (2,16,2):
    if i == 8:
        break
    print(i)
else:
    print("end of the loop")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&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;2
4
6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the code did not run down to the &lt;code&gt;else&lt;/code&gt; block. This is because there is a &lt;code&gt;break&lt;/code&gt; statement in the code. The &lt;code&gt;break&lt;/code&gt; statement executed when &lt;code&gt;i == 8&lt;/code&gt;. So the code exited the loop prematurely and didn't reach the &lt;code&gt;else&lt;/code&gt; block. If the &lt;code&gt;break&lt;/code&gt; statement had not been encountered, the loop would have continued iterating through all the numbers in the specified range, and then the &lt;code&gt;else&lt;/code&gt; block would have executed, printing "end of the loop".&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
