How to start with Python - The Basics for Absolute Beginners
Tags: python, beginners, programming, tutorial
Published: 2023-02-20 14:04:59

<!-- wp:paragraph -->
<p>Python is a popular programming language known for its simplicity and ease of use. One of the fundamental concepts in programming is the use of variables. In this tutorial, we will discuss the basic aspects of python language, starting with variables and data types.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>What are variables?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A variable is a named storage location in a computer's memory that is used to store data. The data stored in a variable can be changed or manipulated during the execution of a program. Variables are given a name and a data type, which defines the type of data that can be stored in the variable.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Why are variables important?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Variables are important because they allow us to store and manipulate data in a program. Without variables, we would have to write the same data over and over again in a program, which would be both time-consuming and error-prone. By using variables, we can store data once and then refer to it multiple times throughout the program.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>What are data types in Python</strong>?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In Python, there are several data types that can be used to store data in variables. Some of the most commonly used data types include:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Integer (int):</strong> An integer is a whole number (positive, negative, or zero) that can be stored in a variable. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">age = 25</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p><strong>Float (float):</strong> A float is a decimal number that can be stored in a variable. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">height = 1.75</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p><strong>String (str):</strong> A string is a sequence of characters that can be stored in a variable. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">name = "John Doe"</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p><strong>Boolean (bool):</strong> A boolean is a data type that can only store two values: <code>True</code> or <code>False</code>. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">is_student = True</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p><strong>List (list)</strong>: represents a collection of values, such as [1, 2, 3], and can be stored in a variable. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">numbers = [1, 2, 3, 4]</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p><strong>Tuple (tuple)</strong>: similar to lists, but the values cannot be changed after the tuple is created and can be stored in a variable. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">person = ('John', 25, 68.5)</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p><strong>Dictionary (dict):</strong> represents a collection of key-value pairs and can be stored in a variable. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">person = {'name': 'John', 'age': 25, 'weight': 68.5}</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p><strong>Set (set):</strong> represents a collection of unique values and can be stored in a variable. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">unique_numbers = {1, 2, 3, 4}</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p><strong>Declaring Variables in Python</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To declare a variable in Python, you simply need to assign a value to a variable name. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">name = "Jane Doe"</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this example, the variable <code>name</code> is assigned the string value of <code>"Jane Doe"</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Using Variables in Python</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once you have declared a variable in Python, you can use the variable in various ways in your program. For example, you can use the value stored in a variable to perform calculations or to print the value to the screen.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For example, to print the value stored in the <code>name</code> variable to the screen, you can use the following code:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">print(name)

# output
# Jane Doe</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:image {"align":"center","id":1304,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://nerdleveltech.com/wp-content/uploads/road-g6157b33f2_640.jpg" alt="Start text on the road" class="wp-image-1304"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Your First Python Program with Python</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let's write another simple Python program that calculates the area of a circle. This program will take the radius of the circle as input and calculate its area.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here is the code:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">radius = float(input("Enter the radius of the circle: "))
area = 3.14 * radius * radius
print("The area of the circle with radius", radius, "is", area)</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>When you run the program, it will ask you to enter the radius of the circle. Enter a value and press Enter. You should see the output of the program, which is the area of the circle with the given radius.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Working of the Program</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this program, we first take the radius of the circle as input from the user using the <code>input</code> function. The <code>input</code> function returns the input as a string, so we need to convert it to a float using the <code>float</code> function.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Next, we calculate the area of the circle using the formula <code>3.14 * radius * radius</code>. Finally, we use the <code>print</code> function to display the result.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This program demonstrates how you can use variables and the <code>input</code> function to take input from the user, perform calculations, and display the result.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Before We Begin and dig deeper into coding</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before we dive into the coding part, let's go over a few important points.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li><strong>Overcoming the fear of coding:</strong> Many people feel intimidated by coding and think it is difficult to learn. However, just like any other skill, coding can be learned with practice and dedication. Don't be afraid to make mistakes, as they are an important part of the learning process.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:list {"ordered":true,"start":2} -->
<ol start="2"><!-- wp:list-item -->
<li><strong>Coding is a form of communication: </strong>Just like spoken languages, coding is a way to communicate with computers. With programming, you can give <a href="https://nerdleveltech.com/how-to-solve-an-algorithm-problem-with-examples/" target="_blank" rel="noreferrer noopener">instructions to a computer</a> to perform specific tasks.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:list {"ordered":true,"start":3} -->
<ol start="3"><!-- wp:list-item -->
<li><strong>Basic math skills are all you need:</strong> Although coding may seem complex, it only requires basic arithmetic skills. Most of the time, coding involves logical thinking and problem-solving skills, rather than complex mathematical calculations.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>With these points in mind, let's get started with learning the fundamentals of Python.</p>
<!-- /wp:paragraph -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading -->
<h2>Python Fundamental Concepts</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before we start writing programs, there are a few basic concepts in Python that we need to understand. Let's go over these concepts now:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li><strong>Numbers and Texts:</strong> In Python, there are two main data types that we use to store values - numbers and texts. Numbers can be integers or floating-point numbers, while texts are called strings. For example:</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">age = 25
weight = 72.5
name = "John Doe"</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this example, <code>age</code> is an integer, <code>weight</code> is a floating-point number, and <code>name</code> is a string.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true,"start":2} -->
<ol start="2"><!-- wp:list-item -->
<li><strong>Print Output:</strong> The <code>print</code> function is used to display the output of a program. You can print numbers, strings, or any other data type. For example:</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">print(25)
print(72.5)
print("John Doe")</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>This will output the following:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">25
72.5
John Doe</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>These two concepts are the building blocks for <a href="https://www.dataquest.io/blog/learn-python-the-right-way/" target="_blank" rel="noreferrer noopener">writing programs in Python</a>. In the next lesson, we will <a href="https://www.freecodecamp.org/news/the-best-way-to-learn-python-python-programming-tutorial-for-beginners/" target="_blank" rel="noreferrer noopener">learn</a> how to use these concepts to write simple programs.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":1303,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://nerdleveltech.com/wp-content/uploads/activity-g563e63b75_640.jpg" alt="foot print with rocks on the beach" class="wp-image-1303"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Using numbers, texts, comments, and print in program</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In programming, we can use fixed values directly in our programs, known as literals. Here are the most commonly used literals in Python:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Integers (int):</strong> Integers are whole numbers without any decimal parts. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">age = 25
temperature = -10</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this example, <code>age</code> is an integer with the value of 25, and <code>temperature</code> is an integer with the value of -10.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Floating-point Numbers (float): </strong>Floating-point numbers are numbers with decimal parts. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">height = 1.75
weight = 72.5</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this example, <code>height</code> is a floating-point number with the value of 1.75, and <code>weight</code> is a floating-point number with the value of 72.5.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Strings (str):</strong> Strings are sequences of characters wrapped in quotation marks. In Python, you can use either single quotes or double quotes to create strings. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">name = "Jane Doe"
address = '123 Main Street'</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this example, <code>name</code> is a string with the value of "Jane Doe", and <code>address</code> is a string with the value of "123 Main Street".</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>These are the most commonly used literals in Python. By understanding these data types, you can store different types of values in your programs and use them to perform various operations.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Printing Numbers and Strings in Python</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In Python, we use the <code>print</code> function to display output on the screen. In this section, we will learn how to print numbers and strings.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's start with printing numbers:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">print(25)
print(-12)
print(3.14)
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>This will output the following:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">25
-12
3.14
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>As you can see, we can simply pass a number as an argument to the <code>print</code> function, and it will print the number on the screen.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Next, let's look at how to print strings:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">print("Hello, World!")
print("My name is John.")
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>This will output the following:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">Hello, World!
My name is John.
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>o print a string, you need to wrap the text in quotes. You can use either single quotes or double quotes.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>the <code>print</code> function in more detail and also look at common mistakes that you might face while using <code>print</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-"># print an integer
print(37)    # Output: 37
 
# print a string
print('Superheroes')    # Output: Superheroes
 
# create a variable
capital = 'Paris'
 
# print the capital variable
print(capital)     # Output: Paris
 
# print multiple items at once
print(19, 'Spiderman')    # Output: 19 Spiderman
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this example, we have used the <code>print</code> function to print integers, strings, and variables. We have also shown how to print multiple items at once.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Note that when we print strings, the outer quotation marks are not displayed in the output.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Common Mistakes with <code>print</code></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Forgetting to put quotes around strings</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>When printing strings, make sure to wrap the text in either single quotes or double quotes, or else you will get an error.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true,"start":2} -->
<ol start="2"><!-- wp:list-item -->
<li>Using commas instead of the <code>+</code> operator to concatenate strings</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>To concatenate two strings, you need to use the <code>+</code> operator, not a comma. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">print("Hello" + "World")    # Output: Hello World
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>The <code>print</code> function is a useful tool for displaying output in Python. By understanding the <code>print</code> function and avoiding common mistakes, you can make sure that your programs produce the output you expect.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>That's it! With these examples, you now know how to print numbers and strings using the <code>print</code> function in Python.</p>
<!-- /wp:paragraph -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":4} -->
<h4>Python Comments</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In programming, we can make computers ignore certain parts of the program by using comments. In Python, a comment starts with the symbol <code>#</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-"># This is a comment
print(25)
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this example, the line <code># This is a comment</code> is a comment, and it is ignored by the computer. The <code>print</code> function is executed and the output will be:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">25
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p><strong>Why Use Comments in Python?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Even though comments are ignored by the computer, they play an important role in helping other people who read the code understand it. For example, if you are working on a project with a team of developers, comments can help them understand the logic and thought process behind the code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In addition, comments can also be used to temporarily disable a portion of the code or to explain the working of a particular section of the code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-"># Calculate the area of a circle
radius = float(input("Enter the radius of the circle: "))
area = 3.14 * radius * radius
print("The area of the circle with radius", radius, "is", area)
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this example, the comment <code># Calculate the area of a circle</code> explains what the code below it does.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In conclusion, comments are a useful tool in programming, and it is a good practice to add comments to your code to help others understand it.</p>
<!-- /wp:paragraph -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:image {"align":"center","id":1302,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://nerdleveltech.com/wp-content/uploads/slip-up-g0919d3c78_640.jpg" alt="Slip-on banana's mistake" class="wp-image-1302"/></figure>
<!-- /wp:image -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3>Common mistakes with creating variables and the use of print function</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>As a beginner in Python, it is common to make certain mistakes. In this section, we will cover two common mistakes that you might make.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li><strong>Spelling Errors:</strong> Spelling errors are a common cause of errors in programming. For example, if you use the wrong variable name or misspell a word, you will get an error. For example:</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">salary = 95000
# Error because of a spelling mistake
print(salary_icome)
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>This will result in an error:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">File "&lt;string>", line 2, in &lt;module>
NameError: name 'salary_icome' is not defined
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this case, the error message clearly states that <code>salary_icome</code> is not defined.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It is important to note that Python is case-sensitive. This means that <code>salary</code> and <code>Salary</code> are considered to be two different variables.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true,"start":2} -->
<ol start="2"><!-- wp:list-item -->
<li><strong>Adding Spaces at the Beginning:</strong> In Python, indentation has a special meaning. So, if you add unnecessary spaces at the beginning of a line, you will get an error. For example:</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-"> print('Hello')  # Error
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>If you run this code, you will get an error because there is a space at the beginning of the line.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In conclusion, being aware of these common mistakes can help you avoid them and write error-free code in Python.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true,"start":3} -->
<ol start="3"><!-- wp:list-item -->
<li><strong>Error Due to Quotation Marks:</strong> A common mistake made by beginners is forgetting to add the ending quotation marks in a string. For example:</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">print('Hello)   # Error
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>n this case, Python doesn't know what <code>'Hello</code> is. It is not a string because it doesn't have the ending quotation mark, and it is not a variable because you cannot use quotation marks in variable names.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true,"start":4} -->
<ol start="4"><!-- wp:list-item -->
<li><strong>Forgetting Commas in the <strong>Print</strong> Function:</strong> Another common mistake is forgetting to use commas in the <code>print</code> function. For example:</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">age = 20
print('Age:' age)   # Error
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this case, Python interprets <code>'Age:' age</code> as a single item. Since it is neither a string nor a variable, you will get an error.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In conclusion, being aware of these common mistakes can help you avoid them and write error-free code in Python.</p>
<!-- /wp:paragraph -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:image {"align":"center","id":1301,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://nerdleveltech.com/wp-content/uploads/to-learn-g5a3f85f83_640.jpg" alt="a child learning arithmetic" class="wp-image-1301"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Introduction to Operators in Python</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Up until now, we have learned about values, variables, and how to print them. However, in real-world projects, we need to perform operations on these values and variables, not just print them.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this lesson, we will learn about operators, which are special symbols that allow us to perform operations on values and variables.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">number = 5
total = number + 10
print(total)
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this program, <code>+</code> is an operator that is used to perform addition.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here is a list of operators we will learn in this lesson:</p>
<!-- /wp:paragraph -->

<!-- wp:table -->
<figure class="wp-block-table"><table><thead><tr><th>Operator</th><th>Syntax</th><th>Meaning</th></tr></thead><tbody><tr><td><code>+</code></td><td><code>x + y</code></td><td>Addition</td></tr><tr><td><code>-</code></td><td><code>x - y</code></td><td>Subtraction</td></tr><tr><td><code>*</code></td><td><code>x * y</code></td><td>Multiplication</td></tr><tr><td><code>/</code></td><td><code>x / y</code></td><td>Division</td></tr><tr><td><code>%</code></td><td><code>x % y</code></td><td>Remainder</td></tr><tr><td><code>**</code></td><td><code>x ** y</code></td><td>Exponentiation</td></tr></tbody></table></figure>
<!-- /wp:table -->

<!-- wp:paragraph -->
<p>Let's start with the <code>+</code> operator.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>The <code>+</code> Operator</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The <code>+</code> operator is used to add two operands (values or variables). For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">x = 10
y = 20

# using + to perform addition
total = x + y + 30

print(total)    # Output: 60
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>n this program, <code>total = x + y + 30</code> first computes the sum of <code>x</code>, <code>y</code>, and <code>30</code>. Then, the result is assigned to the <code>total</code> variable.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We can also perform addition directly without using an extra variable. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">x = 12.5
y = 10

print(x + y)     # Output: 22.5
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this example, the line <code>print(x + y)</code> first computes the sum of <code>x</code> and <code>y</code>. Then, the result is directly printed on the screen.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>The <code>-</code> Operator</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The <code>-</code> operator subtracts the value on the right from the value on the left. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">x = 10
y = 6

result = x - y
print(result)    # Output: 4
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this program, <code>result = x - y</code> first subtracts <code>y</code> from <code>x</code>. Then, the result is assigned to the <code>result</code> variable.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here is another example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">x = 10
y = 6

print('Result:', x - y)
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>This statement prints the string 'Result:' and then prints the difference of <code>x</code> and <code>y</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The <code>-</code> operator works by subtracting the value on the right from the value on the left.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>The <code>*</code> Operator</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The <code>*</code> operator multiplies two values or variables. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">number1 = 5
number2 = 10

# computing the product of number1 and number2
product = number1 * number2
print(product)    # Output: 50
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this program, we first define two variables, <code>number1</code> and <code>number2</code>, and store values in them. Then, we calculate the product of <code>number1</code> and <code>number2</code> and store it in the <code>product</code> variable. Finally, we print the product using the <code>print</code> function.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here is another example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">number = 5.5

# computing the product of number and 10
product = number * 10
print(product)    # Output: 55.0
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this program, we define a variable <code>number</code> and store a value in it. Then, we calculate the product of <code>number</code> and <code>10</code> and store it in the <code>product</code> variable. Finally, we print the product using the <code>print</code> function.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>The <code>/</code> Operator</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The <code>/</code> operator is used for division. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">number = 25

result = number / 4
print(result)    # Output: 6.25
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this program, we define a variable <code>number</code> and store a value in it. Then, we divide <code>number</code> by 4 and store the result in the <code>result</code> variable. Finally, we print the result using the <code>print</code> function.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here is another example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">number = 25

result = number / 5
print(result)    # Output: 5.0
</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>In this program, we divide <code>number</code> by 5 and store the result in the <code>result</code> variable. Finally, we print the result using the <code>print</code> function.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Note that the <code>/</code> operator always returns a float value. In this case, 25 divided by 5 results in 5.0 and not 5.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>The % Operator</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The % operator computes remainder when one number is divided by another.</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">divisor = 5
dividend = 25

# compute remainder
remainder = dividend % divisor

print(remainder) # Output: 0
# In the above program, 25 divided by 5 is 5 with 0 as the remainder.</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:heading {"level":3} -->
<h3>The ** Operator</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The ** operator computes the exponential (power) of a number. Let's take an example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">base = 2.5
power = 3
 
result = base ** power
print(result)

# Output

# 15.625</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:image {"align":"center","id":1300,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://nerdleveltech.com/wp-content/uploads/change-gc54c8914b_640.jpg" alt="Time for conversion" class="wp-image-1300"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Conversions</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We can convert data of one type to another using type conversion <a href="https://nerdleveltech.com/javascript-functions-why-theyre-essential-to-understand-easy-guide-part2/" target="_blank" rel="noreferrer noopener">functions</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In Python, there are types of conversion functions:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>int(x) - Converts x to an integer. float(x) - Converts x to a float. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-"># convert string to integer
string = "25"
number = int(string)
print(number) # Output: 25

# convert integer to float
integer = 25
float_number = float(integer)
print(float_number) # Output: 25.0

# convert string to float
string = "25.5"
float_number = float(string)
print(float_number) # Output: 25.5</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:image {"align":"center","id":1299,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://nerdleveltech.com/wp-content/uploads/keyboard-gec8eb30ff_640.jpg" alt="keyboard for user input" class="wp-image-1299"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>User Inputs</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In Python, we can also ask the user for input and assign it to a variable. This can be done using the input() function. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">name = input('Enter your name: ')
print('Your name is', name)</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:heading {"level":3} -->
<h3>User Input Without Prompt</h3>
<!-- /wp:heading -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-">full_name = input()
print('Full name:', full_name)</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:paragraph -->
<p>The full_name = input() statement doesn't print anything but makes the program wait until the user gives input.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When we don't provide a prompt message in the input function, the user might not know what to enter. But when we provide a prompt message, it gives clear instructions to the user about what kind of input is expected. In the above example, when the user runs the program, it waits for the input without displaying any prompt message. Hence, it's always a good practice to provide a clear prompt message to the user.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's put conversion with user inputs together in the next example:</p>
<!-- /wp:paragraph -->

<!-- wp:prismatic/blocks -->
<pre class="wp-block-prismatic-blocks"><code class="language-"># Take inputs from the user
number1 = input("Enter a number: ")
number2 = input("Enter another number: ")

# converting the strings to integers
number1 = int(number1)
number2 = int(number2)

product = number1 * number2
print('Product:', product)

# Sample Output
# -------------
# Enter a number: 5
# Enter another number: 10
# Product: 50</code></pre>
<!-- /wp:prismatic/blocks -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:image {"align":"center","id":1298,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://nerdleveltech.com/wp-content/uploads/meet-g494a15973_640.jpg" alt="Conclusion" class="wp-image-1298"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The article covers the basic concepts of Python programming. It discusses numbers, including integers and floating-point numbers, and strings, which are sequences of characters wrapped in quotation marks. Variables are introduced as a way to store data for later use in the program, and the print() function is explained as a way to display output. The article also covers the use of comments in a program, which starts with a # symbol, and various arithmetic operators. Data conversion is discussed, including the use of the int() and float() functions to convert data types. The article concludes with a discussion of the input() function, which is used to get input from the user and always returns input as a string.</p>
<!-- /wp:paragraph -->