<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python</link><description /><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python</link><pubDate>Thu, 04 May 2023 17:05:17 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>pchan</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Current Revision posted to Documents by pchan on 5/4/2023 5:05:17 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series - Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&amp;nbsp;&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.on()&lt;/span&gt; &lt;br /&gt;&lt;span class="xs-ml4"&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.off()&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;time.sleep(0.1)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again. The &lt;em&gt;while True:&lt;/em&gt; line tells the loop to repeat indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input parameters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=e14c-referral-cmty-handler-essentials-programming1-doc&amp;CMP=e14c-essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: coding, python, Interpreter, interpreted language, programming, raspberry pi, oop, programming language, object oriented, function, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/21</link><pubDate>Thu, 04 May 2023 12:12:25 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>pchan</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 21 posted to Documents by pchan on 5/4/2023 12:12:25 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series - Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&amp;nbsp;&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.on()&lt;/span&gt; &lt;br /&gt;&lt;span class="xs-ml4"&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.off()&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;time.sleep(0.1)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again. The &lt;em&gt;while True:&lt;/em&gt; line tells the loop to repeat indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input parameters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=e14c-essentials-programming1-doc&amp;CMP=e14c-essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: coding, python, Interpreter, interpreted language, programming, raspberry pi, oop, programming language, object oriented, function, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/20</link><pubDate>Fri, 17 Mar 2023 21:14:13 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>pchan</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 20 posted to Documents by pchan on 3/17/2023 9:14:13 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series - Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&amp;nbsp;&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.on()&lt;/span&gt; &lt;br /&gt;&lt;span class="xs-ml4"&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.off()&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;time.sleep(0.1)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again. The &lt;em&gt;while True:&lt;/em&gt; line tells the loop to repeat indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input parameters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: coding, python, Interpreter, interpreted language, programming, raspberry pi, oop, programming language, object oriented, function, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/19</link><pubDate>Wed, 15 Mar 2023 17:24:52 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>srutledge</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 19 posted to Documents by srutledge on 3/15/2023 5:24:52 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&amp;nbsp;&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.on()&lt;/span&gt; &lt;br /&gt;&lt;span class="xs-ml4"&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.off()&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;time.sleep(0.1)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again. The &lt;em&gt;while True:&lt;/em&gt; line tells the loop to repeat indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input parameters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: coding, python, Interpreter, interpreted language, programming, raspberry pi, oop, programming language, object oriented, function, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/18</link><pubDate>Wed, 15 Mar 2023 15:25:58 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>dychen</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 18 posted to Documents by dychen on 3/15/2023 3:25:58 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.on()&lt;/span&gt; &lt;br /&gt;&lt;span class="xs-ml4"&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.off()&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;time.sleep(0.1)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again. The &lt;em&gt;while True:&lt;/em&gt; line tells the loop to repeat indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input parameters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: coding, python, Interpreter, interpreted language, programming, raspberry pi, oop, programming language, object oriented, function, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/17</link><pubDate>Wed, 15 Mar 2023 15:16:41 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>dychen</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 17 posted to Documents by dychen on 3/15/2023 3:16:41 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.on()&lt;/span&gt; &lt;br /&gt;&lt;span class="xs-ml4"&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.off()&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;time.sleep(0.1)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again. The &lt;em&gt;while True:&lt;/em&gt; line tells the loop to repeat indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/16</link><pubDate>Wed, 15 Mar 2023 15:12:45 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>dychen</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 16 posted to Documents by dychen on 3/15/2023 3:12:45 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.on()&lt;/span&gt; &lt;br /&gt;&lt;span class="xs-ml4"&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;fan_pin.off()&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;time.sleep(0.1)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again. The &lt;em&gt;while True:&lt;/em&gt; line tells the loop to repeat indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/15</link><pubDate>Wed, 15 Mar 2023 15:11:34 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>dychen</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 15 posted to Documents by dychen on 3/15/2023 3:11:34 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml8"&gt;fan_pin.on()&lt;/span&gt; &lt;br /&gt;&lt;span class="xs-ml4"&gt;else:&lt;br /&gt; &lt;span class="xs-ml8"&gt;fan_pin.off()&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;time.sleep(0.1)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again. The &lt;em&gt;while True:&lt;/em&gt; line tells the loop to repeat indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/14</link><pubDate>Wed, 15 Mar 2023 15:10:33 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>dychen</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 14 posted to Documents by dychen on 3/15/2023 3:10:33 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.on()&lt;/span&gt; &lt;br /&gt;&lt;span class="xs-ml4"&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.off()&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;time.sleep(0.1)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again. The &lt;em&gt;while True:&lt;/em&gt; line tells the loop to repeat indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/13</link><pubDate>Wed, 15 Mar 2023 15:03:41 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>dychen</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 13 posted to Documents by dychen on 3/15/2023 3:03:41 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.on()&lt;/span&gt; &lt;br /&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.off()&lt;/span&gt;&lt;br /&gt; time.sleep(0.1)&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again. The &lt;em&gt;while True:&lt;/em&gt; line tells the loop to repeat indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/12</link><pubDate>Wed, 15 Mar 2023 14:45:17 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>dychen</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 12 posted to Documents by dychen on 3/15/2023 2:45:17 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.on()&lt;/span&gt; &lt;br /&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.off()&lt;/span&gt;&lt;br /&gt; time.sleep(0.1)&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again, and the loop repeats indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/11</link><pubDate>Wed, 15 Mar 2023 14:43:17 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>dychen</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 11 posted to Documents by dychen on 3/15/2023 2:43:17 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.on()&lt;/span&gt; &lt;br /&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.off()&lt;/span&gt;&lt;br /&gt; time.sleep(0.1)&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again, and the loop repeats indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/10</link><pubDate>Wed, 15 Mar 2023 14:30:31 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>dychen</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 10 posted to Documents by dychen on 3/15/2023 2:30:31 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&amp;nbsp;&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;Reusability: Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.on()&lt;/span&gt; &lt;br /&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.off()&lt;/span&gt;&lt;br /&gt; time.sleep(0.1)&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again, and the loop repeats indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/9</link><pubDate>Tue, 14 Mar 2023 21:27:02 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>pchan</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 9 posted to Documents by pchan on 3/14/2023 9:27:02 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;Reusability: Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.on()&lt;/span&gt; &lt;br /&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.off()&lt;/span&gt;&lt;br /&gt; time.sleep(0.1)&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again, and the loop repeats indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;/div&gt;
&lt;p&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/p&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/8</link><pubDate>Tue, 14 Mar 2023 21:19:40 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>pchan</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 8 posted to Documents by pchan on 3/14/2023 9:19:40 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;Reusability: Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.on()&lt;/span&gt; &lt;br /&gt;else:&lt;br /&gt; &lt;span class="xs-ml5"&gt;fan_pin.off()&lt;/span&gt;&lt;br /&gt; time.sleep(0.1)&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again, and the loop repeats indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/7</link><pubDate>Tue, 14 Mar 2023 21:18:03 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>pchan</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 7 posted to Documents by pchan on 3/14/2023 9:18:03 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;Reusability: Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml4"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml4"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml4"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml4"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml4"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml4"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml4"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml8"&gt;fan_pin.on()&lt;/span&gt; &lt;br /&gt;else:&lt;br /&gt; &lt;span class="xs-ml8"&gt;fan_pin.off()&lt;/span&gt;&lt;br /&gt; time.sleep(0.1)&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again, and the loop repeats indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/6</link><pubDate>Tue, 14 Mar 2023 21:14:13 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>pchan</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 6 posted to Documents by pchan on 3/14/2023 9:14:13 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml2"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml2"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;Reusability: Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml2"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml2"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml2"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml2"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml2"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml2"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml2"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml2"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml4"&gt;fan_pin.on()&lt;/span&gt; &lt;br /&gt;else:&lt;br /&gt; &lt;span class="xs-ml4"&gt;fan_pin.off()&lt;/span&gt;&lt;br /&gt; time.sleep(0.1)&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again, and the loop repeats indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/5</link><pubDate>Tue, 14 Mar 2023 21:11:04 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>pchan</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 5 posted to Documents by pchan on 3/14/2023 9:11:04 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:
&lt;p class="xbold" style="margin-left:2em;"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:
&lt;p class="xbold" style="margin-left:2em;"&gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:
&lt;p class="xbold" style="margin-left:2em;"&gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; &lt;span class="xs-ml2"&gt;result = i + 10&lt;/span&gt;&lt;br /&gt;&lt;span class="xs-ml2"&gt;print(i)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(v)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;gt; 5:&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(i)&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;Reusability: Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;br /&gt; &lt;span class="xs-ml2"&gt;result = x + y&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;return result&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;br /&gt; &lt;span class="xs-ml2"&gt;i += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/span&gt;&lt;br /&gt; else:&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;br /&gt; &lt;span class="xs-ml2"&gt;sum += num&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;count += 1&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xbold xs-ml10"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span class="xbold xs-ml10"&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="text-green xs-ml10"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml10"&gt;contents = f.read()&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml2"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;&amp;quot;age&amp;quot;: 30,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt; &lt;span class="xs-ml2"&gt;data = json.load(f)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f&lt;br /&gt; &lt;span class="xs-ml2"&gt;json.dump(data, f)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;br /&gt; &lt;span class="xs-ml2"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;&amp;quot;age&amp;quot;: 31,&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/span&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;br /&gt; &lt;span class="xs-ml2"&gt;print(row)&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;br /&gt; &lt;span class="xs-ml2"&gt;temp = sensor.read()&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml2"&gt;if temp &amp;gt; 100:&lt;/span&gt;&lt;br /&gt; &lt;span class="xs-ml3"&gt;fan_pin.on()&lt;/span&gt; &lt;br /&gt;else:&lt;br /&gt; &lt;span class="xs-ml3"&gt;fan_pin.off()&lt;/span&gt;&lt;br /&gt; time.sleep(0.1)&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again, and the loop repeats indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/4</link><pubDate>Tue, 14 Mar 2023 20:27:14 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>pchan</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 4 posted to Documents by pchan on 3/14/2023 8:27:14 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:&lt;br /&gt;
&lt;p style="margin-left:2em;"&gt;class=&amp;quot;xbold&amp;quot; style=&amp;quot;font-size: 1.2em;&amp;quot;&amp;gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:&lt;br /&gt;
&lt;p style="margin-left:2em;"&gt;class=&amp;quot;xbold&amp;quot; style=&amp;quot;font-size: 1.2em;&amp;quot;&amp;gt;cd ~/Desktop&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:&lt;br /&gt;
&lt;p style="margin-left:2em;"&gt;class=&amp;quot;xbold&amp;quot; style=&amp;quot;font-size: 1.2em;&amp;quot;&amp;gt;python helloworld.py&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;/p&gt;
&lt;p class="xbold" style="margin-left:2em;"&gt;result = i + 10 &lt;br /&gt;print(i)&lt;/p&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;br /&gt; else:&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/p&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;print(v)&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;gt; 5:&lt;br /&gt; print(i)&lt;br /&gt; i += 1&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;Reusability: Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;result = x + y&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;return result&lt;/p&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;i += 1&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;br /&gt; else:&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/p&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;sum += num&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;count += 1&lt;/p&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;br /&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt;&lt;span class="text-green"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt;contents = f.read()&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;&amp;quot;age&amp;quot;: 30,&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;data = json.load(f)&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;json.dump(data, f)&lt;/p&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;&amp;quot;age&amp;quot;: 31,&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;print(row)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;temp = sensor.read()&lt;br /&gt; if temp &amp;gt; 100:&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;fan_pin.on()&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;&lt;br /&gt; else:&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;fan_pin.off()&lt;/p&gt;
&lt;p style="margin-left:2em;"&gt;time.sleep(0.1)&lt;/p&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again, and the loop repeats indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/3</link><pubDate>Tue, 14 Mar 2023 20:16:09 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>pchan</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 3 posted to Documents by pchan on 3/14/2023 8:16:09 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:&lt;br /&gt;
&lt;pre style="font-size:1.2em;"&gt; class=&amp;quot;xbold&amp;quot; style=&amp;quot;font-size: 1.2em;&amp;quot;&amp;gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:&lt;br /&gt;
&lt;pre style="font-size:1.2em;"&gt; class=&amp;quot;xbold&amp;quot; style=&amp;quot;font-size: 1.2em;&amp;quot;&amp;gt;cd ~/Desktop&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:&lt;br /&gt;
&lt;pre style="font-size:1.2em;"&gt; class=&amp;quot;xbold&amp;quot; style=&amp;quot;font-size: 1.2em;&amp;quot;&amp;gt;python helloworld.py&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt; class=&amp;quot;xbold&amp;quot;&amp;gt;result = i + 10 &lt;br /&gt;print(i)&lt;/pre&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/pre&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/pre&gt;
&lt;p class="xbold"&gt;&lt;br /&gt; else:&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/pre&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;print(v)&lt;/pre&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;gt; 5:&lt;br /&gt; print(i)&lt;br /&gt; i += 1&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;Reusability: Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;result = x + y&lt;/pre&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;return result&lt;/pre&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;i += 1&lt;/pre&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/pre&gt;
&lt;p class="xbold"&gt;&lt;br /&gt; else:&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/pre&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;sum += num&lt;/pre&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;count += 1&lt;/pre&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;br /&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt;&lt;span class="text-green"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt;contents = f.read()&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/pre&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;&amp;quot;age&amp;quot;: 30,&lt;/pre&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/pre&gt;
&lt;p class="xbold"&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;data = json.load(f)&lt;/pre&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;json.dump(data, f)&lt;/pre&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/pre&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;&amp;quot;age&amp;quot;: 31,&lt;/pre&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/pre&gt;
&lt;p class="xbold"&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;print(row)&lt;/pre&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;/p&gt;
&lt;pre style="font-size:1.2em;"&gt;temp = sensor.read()&lt;br /&gt; if temp &amp;gt; 100:&lt;/pre&gt;
&lt;pre style="font-size:1.2em;"&gt;fan_pin.on()&lt;/pre&gt;
&lt;pre style="font-size:1.2em;"&gt;&lt;br /&gt; else:&lt;/pre&gt;
&lt;pre style="font-size:1.2em;"&gt;fan_pin.off()&lt;/pre&gt;
&lt;pre style="font-size:1.2em;"&gt;time.sleep(0.1)&lt;/pre&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again, and the loop repeats indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item><item><title>An Introduction to Programming with Python</title><link>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python/revision/2</link><pubDate>Tue, 14 Mar 2023 20:08:39 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:22c19380-54f2-4f06-a73b-70e39e456956</guid><dc:creator>pchan</dc:creator><comments>https://community.element14.com/learn/learning-center/essentials/w/documents/28063/an-introduction-to-programming-with-python#comments</comments><description>Revision 2 posted to Documents by pchan on 3/14/2023 8:08:39 PM&lt;br /&gt;
&lt;h2 id="mcetoc_1grglvhvs0" class="essTitle xbold" title="Python Programming"&gt;&lt;a name="top"&gt;&lt;/a&gt;&lt;a href="/learn/learning-center/essentials" data-icid="essentials-programming1-doc"&gt;&lt;img style="padding-right:18px;vertical-align:middle;" alt="essentials logo" src="/e14/assets/legacy/2017/learningess.png" width="175px" /&gt;&lt;/a&gt; Programming Series &amp;ndash; Part 1 - Python&lt;/h2&gt;
&lt;p&gt;In the modern world, where almost every device has smart functionality, being a programmer is an important tool in an engineer&amp;rsquo;s skillset.&amp;nbsp;Python is a popular programming language for both beginning and advanced programmers due to its simplicity, readability, and versatility. In this module, we will explore the fundamental concepts of programming and introduce you to Python&amp;#39;s basic syntax and structures.&lt;br /&gt; &lt;strong&gt;&lt;a href="#test"&gt;Test Your Knowledge &lt;img loading="lazy" style="vertical-align:middle;" alt="test link" src="/e14/assets/legacy/gen/LinkArrow.gif" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5 class="essSponsor" title="element14"&gt;&lt;a name="object"&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;h2 id="mcetoc_1grglvhvs1" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;2. Objectives&lt;/h2&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p style="margin:0;padding:12px 0px 8px 35px;"&gt;&lt;em&gt;Upon completion of this learning module, you will be able to:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Explain basic programming logic&lt;/li&gt;
&lt;li&gt;Understand the concepts of object-oriented programming (OOP)&lt;/li&gt;
&lt;li&gt;Recognize Python syntax&lt;/li&gt;
&lt;li&gt;Create a basic Python program&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt2" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;3. Basic Concepts&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;What&amp;rsquo;s the history of Python?&lt;/p&gt;
&lt;p&gt;Python was created by Guido van Rossum in the late 1980s, while he was working at the National Research Institute for Mathematics and Computer Science in the Netherlands. The language was named after Monty Python, the British comedy group. Initially, Python was developed as a hobby, but gained popularity over time. Python was released to the public in 1991, quickly gaining a following because of its easy-to-learn syntax, rich library support, and community-driven development model.&lt;/p&gt;
&lt;p&gt;Today, Python is widely used around the world for a variety of applications. It is popular in data science, machine learning, and artificial intelligence, due to its extensive libraries for numerical and scientific computing. Python is also used in web development, game development, automation and various other applications. Many popular websites, including Instagram, Pinterest, and Dropbox are built using Python. Additionally, Python is a popular language for embedded systems, where it is used to program devices such as microcontrollers, single-board computers (SBCs), and other devices.&lt;/p&gt;
&lt;p class="xbold"&gt;What is Python exactly?&lt;/p&gt;
&lt;p&gt;Python is an interpreted programming language, meaning that the programs are executed directly from the source code, without the need for a compilation step. In other words, an interpreter reads and executes the code line-by-line at runtime, converting it to machine code as it goes. This is in contrast to a compiled language, such as C++, where the source code is translated to machine code by a compiler before execution.&lt;/p&gt;
&lt;p&gt;Python is also dynamically typed. A dynamically typed language is a programming language where the data type of a variable is determined at runtime, rather than at compile time. In a dynamically typed language, you don&amp;#39;t need to declare the data type of a variable before using it, because the type is inferred based on the value assigned to the variable. This simplifies programming and makes the code more concise, but can also make programs more difficult to debug.&lt;/p&gt;
&lt;p&gt;Python is an object-oriented programming language (OOP) and uses the concept of objects to represent and manipulate data. Objects have both properties (data) and methods (behavior), and the focus is on the interactions between objects rather than the procedures or functions of a program. We&amp;rsquo;ll dive into OOP in a future learning module.&lt;a name="sec4"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt3" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;4. Getting Started with Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;To get started with learning how to code in Python, you&amp;#39;ll need a few basic things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;A computer:&lt;/strong&gt; Python can run on a variety of operating systems, including Windows, macOS, and Linux. It can also run on single-board computers (SBCs), such as the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; The interpreter is the program that reads and executes the code. You can download the latest version of the Python interpreter from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text editor or Integrated Development Environment (IDE):&lt;/strong&gt; You&amp;#39;ll need a text editor or an IDE to write your Python code. There are many options available, including popular text editors like &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer" target="_blank"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://www.sublimetext.com/" rel="noopener noreferrer" target="_blank"&gt;Sublime Text&lt;/a&gt;, and &lt;a href="https://notepad-plus-plus.org/" rel="noopener noreferrer" target="_blank"&gt;Notepad++&lt;/a&gt;. IDEs like &lt;a href="https://www.jetbrains.com/pycharm/" rel="noopener noreferrer" target="_blank"&gt;PyCharm&lt;/a&gt; and &lt;a href="https://www.spyder-ide.org/" rel="noopener noreferrer" target="_blank"&gt;Spyder&lt;/a&gt; offer more advanced features and integrated debugging tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After finishing this tutorial, you&amp;rsquo;ll probably want to seek out further learning resources. There are many free online resources available, including the official Python documentation, YouTube tutorials, and interactive coding websites such as &lt;a href="https://www.codecademy.com/" rel="noopener noreferrer" target="_blank"&gt;Codecademy&lt;/a&gt; and &lt;a href="https://www.datacamp.com/" rel="noopener noreferrer" target="_blank"&gt;DataCamp&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hello World!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Creating a simple &amp;ldquo;Hello World!&amp;rdquo; application will introduce you to creating a Python program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download and install the latest version of Python from the &lt;a href="https://www.python.org/downloads" rel="noopener noreferrer" target="_blank"&gt;official Python website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open a simple text editor such as Notepad (on Windows), TextEdit (on macOS), or Gedit (on Linux) to create your Python program.&lt;/li&gt;
&lt;li&gt;Type the following code into your text editor:&lt;br /&gt;
&lt;pre class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Save the file with the name &amp;quot;helloworld.py&amp;quot;. The &amp;quot;.py&amp;quot; extension signifies that it is a Python file.&lt;/li&gt;
&lt;li&gt;Open a terminal or command prompt on your computer.&lt;/li&gt;
&lt;li&gt;Use the &amp;quot;cd&amp;quot; command to navigate to the folder where you saved the file. If you saved the file on your Desktop, you can navigate to it using the command:&lt;br /&gt;
&lt;pre class="xbold"&gt;cd ~/Desktop&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Type the following command to run the program:&lt;br /&gt;
&lt;pre class="xbold"&gt;python helloworld.py&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The program will output &amp;ldquo;Hello World!&amp;rdquo;&lt;/p&gt;
&lt;p class="xbold"&gt;Tabs or spaces&lt;/p&gt;
&lt;p&gt;In Python, tabs or spaces are used to mark blocks of code that are grouped together, such as the body of a loop or function. Python uses indentation as a way of grouping statements, rather than using characters such as curly brackets or parentheses.&lt;/p&gt;
&lt;p&gt;Proper indentation is essential in Python, as it affects how the code is interpreted and executed. If the indentation is incorrect, the code will result in a syntax error. Python code conventionally uses four spaces for indentation, but it is possible to use tabs, or a combination of tabs and spaces, as long as it is consistent throughout the program.&lt;/p&gt;
&lt;p&gt;For example, in the following code, the indented lines are grouped as part of the if statement:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;/p&gt;
&lt;blockquote class="xbold"&gt;result = i + 10 &lt;br /&gt;print(i)&lt;/blockquote&gt;
&lt;p&gt;The following code would result in a syntax error because the indentation is incorrect:&lt;/p&gt;
&lt;p class="xbold"&gt;if i == 0:&lt;br /&gt; result = i + 10&lt;br /&gt; print(i)&lt;/p&gt;
&lt;p&gt;Proper indentation also makes code more readable, an important quality when dealing with complex logic or nested structures.&lt;/p&gt;
&lt;p class="xbold"&gt;Commenting in Python&lt;/p&gt;
&lt;p&gt;A comment is a piece of text that is ignored by the interpreter and used to provide information or context about the code. Comments can help explain the purpose of the code, how it works, or provide other important details. To write a comment in Python, start it with the # symbol. Anything that follows the # symbol on a line is treated as a comment and is ignored by the interpreter. For example:&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# This is a comment in Python&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello, World!&amp;quot;)&lt;a name="sec5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt4" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;5. Learning Python Syntax&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p class="xbold"&gt;Variable types&lt;/p&gt;
&lt;p&gt;A variable is a named reference to a value stored in the computer&amp;#39;s memory. Variables can hold various types of data, including numbers, strings, lists, dictionaries, and more. To assign a value to a variable in Python, the assignment operator = is used. For example, the following code assigns the value 10 to a variable named x, which you can use later in the code:&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# This will print &amp;ldquo;10&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value of a variable can be reassigned to a new value.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 20&lt;br /&gt; print(x) &lt;span class="text-green"&gt;# Now this will print &amp;ldquo;20&amp;rdquo;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because Python is a dynamically typed language, the data types of variables do not have to be explicitly declared. The type of a variable is determined automatically based on the value assigned to it. Some of the built-in data types in Python include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Integer:&lt;/strong&gt;represents a whole number (e.g. 5, -3, 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Float:&lt;/strong&gt; represents a floating-point number (e.g. 3.14, -2.5, 0.0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String:&lt;/strong&gt; represents a sequence of characters (e.g. &amp;quot;hello&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;42&amp;quot;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; represents a logical value (True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;List:&lt;/strong&gt; represents a mutable sequence of elements (e.g. [1, 2, 3], ["car", "boat", "airplane"])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dictionary:&lt;/strong&gt; represents a collection of key-value pairs (e.g. {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;age&amp;quot;: 30})&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;if statements&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;if&lt;/em&gt; statement is a control structure that lets you execute code based on conditions. Certain code can be executed if a condition is True, with different code running if the condition is False.&lt;/p&gt;
&lt;p&gt;In Python, an &lt;em&gt;if&lt;/em&gt; statement is followed by a colon (:). The code to be executed if the condition is True is indented below the &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 10&lt;br /&gt; if x &amp;gt; 5:&lt;/p&gt;
&lt;blockquote&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/blockquote&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is True, because the value of &lt;em&gt;x&lt;/em&gt; is 10, which is greater than 5. The code inside the &lt;em&gt;if&lt;/em&gt; statement will be executed, outputting the text, &amp;quot;x is greater than 5&amp;quot;. If &lt;em&gt;x&lt;/em&gt; is set to 3, the &lt;em&gt;print&lt;/em&gt; statement would not be executed and the program would move on to the next line of code.&lt;/p&gt;
&lt;p&gt;An &lt;em&gt;else&lt;/em&gt; statement specifies the code to be executed if the condition is False.&lt;/p&gt;
&lt;p class="xbold"&gt;x = 3&lt;br /&gt; if x &amp;gt; 5:&lt;/p&gt;
&lt;blockquote&gt;print(&amp;quot;x is greater than 5&amp;quot;)&lt;/blockquote&gt;
&lt;p class="xbold"&gt;&lt;br /&gt; else:&lt;/p&gt;
&lt;blockquote&gt;print(&amp;quot;x is less than or equal to 5&amp;quot;)&lt;/blockquote&gt;
&lt;p&gt;In this example, the condition &lt;em&gt;x &amp;gt; 5&lt;/em&gt; is false, so the code inside the &lt;em&gt;if&lt;/em&gt; statement will not be executed; instead, the code inside the &lt;em&gt;else&lt;/em&gt; statement runs, outputting the text &amp;quot;x is less than or equal to 5&amp;quot;.&lt;/p&gt;
&lt;p&gt;Strings can also be compared with &lt;em&gt;if&lt;/em&gt; statements. Like numbers, checking if strings are equal use the == comparator, while checking if they are not equal uses the != comparator. Python&amp;#39;s built-in string methods can be used to compare strings in more complex ways. For example, the &lt;em&gt;startswith()&lt;/em&gt; method can be used to test if a string starts with a certain substring, while the &lt;em&gt;endswith()&lt;/em&gt; method can be used to test if a string ends with a certain substring. The &lt;em&gt;in&lt;/em&gt; operator can be used to test if a string contains a certain substring. For example, &amp;quot;hello&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to True, while &amp;quot;hi&amp;quot; in &amp;quot;hello world&amp;quot; would evaluate to False.&lt;/p&gt;
&lt;p&gt;Note that Python is case-sensitive. To ignore case sensitivity, strings can be converted to lowercase or uppercase using the &lt;em&gt;lower() or upper()&lt;/em&gt; functions before comparison.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Working with Lists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Data elements are often grouped together into a collection. In many languages, this is done using a datatype called an &lt;em&gt;array&lt;/em&gt;, which are collections of elements of a similar data type.&lt;/p&gt;
&lt;p&gt;Python does not have a built-in array, but instead has a built-in data type known as a &lt;em&gt;list&lt;/em&gt; that can be used in a similar fashion. Lists, however, can contain elements of differing data types. This example shows how to create a list in Python that includes numbers and a string.&lt;/p&gt;
&lt;p class="xbold"&gt;my_list = [1, 2, 3, "four", 5.0] &lt;span class="text-green"&gt;#A list uses square brackets []&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The individual elements in a list can be accessed by their &lt;em&gt;index&lt;/em&gt;. The index starts at 0. To access the first element of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_list[0]) &lt;span class="text-green"&gt;#This outputs a 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Elements in a list can be modified by assigning new values to their index. To change the second element of &lt;em&gt;my_list&lt;/em&gt; to 10:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list[1] = 10 &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New elements can be added to a list using the &lt;em&gt;append()&lt;/em&gt; method. To add the number 6 to the end of &lt;em&gt;my_list&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;my_list.append(6) &lt;span class="text-green"&gt;#my_list is now [1, 10, 3, &amp;ldquo;four&amp;rdquo;, 5.0, 6]&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;What is a Dictionary?&lt;/p&gt;
&lt;p&gt;Another way to group data is with a &lt;em&gt;dictionary&lt;/em&gt;. A dictionary is a collection of key-value pairs. This example shows how to create a dictionary in Python:&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict = {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 2, &amp;quot;plane&amp;quot;: 3} &lt;span class="text-green"&gt;#A dictionary uses curly brackets {}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example, &lt;em&gt;my_dict&lt;/em&gt; is a dictionary with three key-value pairs. The keys are &amp;quot;car&amp;quot;, &amp;quot;train&amp;quot;, and &amp;quot;plane&amp;quot;, and their respective values are 1, 2, and 3. The individual values in a dictionary can be accessed by their key.&lt;/p&gt;
&lt;p class="xbold"&gt;print(my_dict["car"]) &lt;span class="text-green"&gt;#This outputs 1&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The value associated with a key can be modified.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["train"] = 10 &lt;span class="text-green"&gt;#my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;New key-value pairs can be added to a dictionary by assigning a value to a new key.&lt;/p&gt;
&lt;p class="xbold"&gt;my_dict["boat"] = 4 &lt;span class="text-green"&gt;my_dict is now {&amp;quot;car&amp;quot;: 1, &amp;quot;train&amp;quot;: 10, &amp;quot;plane&amp;quot;: 3, &amp;ldquo;boat&amp;rdquo;: 4}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Looping through data: for and while loops&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;One of the most useful capabilities of programming is being able to quickly loop through large collections of data, repeating a set of instructions with each iteration. In Python, there are two types of loops, the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;while&lt;/em&gt; loop.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop is used when iterating over a sequence of elements.&lt;/p&gt;
&lt;p class="xbold"&gt;vehicles = ["car", "train", "plane"]&lt;br /&gt; for vehicle in vehicles:&lt;/p&gt;
&lt;blockquote&gt;print(v)&lt;/blockquote&gt;
&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop iterates over the &lt;em&gt;vehicles&lt;/em&gt; list, assigning each element to the variable &lt;em&gt;vehicle&lt;/em&gt;. The print statement then prints &lt;em&gt;vehicle&lt;/em&gt; to the console. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;car&lt;br /&gt; train&lt;br /&gt; plane&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop is used when you want to repeat a block of code as long as a certain condition is true.&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;gt; 5:&lt;br /&gt; print(i)&lt;br /&gt; i += 1&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;while&lt;/em&gt; loop repeats the code block as long as &lt;em&gt;i&lt;/em&gt; is less than 5. The code block first prints the value of &lt;em&gt;i&lt;/em&gt;, and then increments &lt;em&gt;i&lt;/em&gt; by 1. The output of this code would be:&lt;/p&gt;
&lt;p class="xbold"&gt;0&lt;br /&gt; 1&lt;br /&gt; 2&lt;br /&gt; 3&lt;br /&gt; 4&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a function and why should I use them?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A function is a block of code performing a specific task that can be called from other parts of a program. Functions are an essential part of writing clean, reusable, and modular code. The advantages of using functions include:&lt;/p&gt;
&lt;p&gt;Reusability: Functions can be used in different parts of a program without having to rewrite the same code over and over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modularity:&lt;/strong&gt; Functions allow you to break up a program into smaller, more manageable pieces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; Functions can be used to hide implementation details and provide a high-level interface for other parts of a program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt; Functions make code more organized, easier to read and understand, and easier to maintain over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Functions can be tested in isolation from the rest of a program, making it easier to find and fix bugs.&lt;/p&gt;
&lt;p&gt;In Python, functions are defined with the &lt;em&gt;def&lt;/em&gt; keyword, followed by the function name, and the parameters in parentheses. The following function takes in two numbers as arguments, adds them together, and returns the result:&lt;/p&gt;
&lt;p class="xbold"&gt;def add_numbers(x, y):&lt;/p&gt;
&lt;blockquote&gt;result = x + y&lt;/blockquote&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;blockquote&gt;return result&lt;/blockquote&gt;
&lt;p&gt;Once you have defined a function, you can call it from other parts of your program. This example calls our &lt;em&gt;add_numbers&lt;/em&gt; function, passing 3 and 5 as arguments.&lt;/p&gt;
&lt;p class="xbold"&gt;sum = add_numbers(3, 5)&lt;br /&gt; print(sum) &lt;span class="text-green"&gt;#This code outputs 8&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;Inputting data&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;input()&lt;/em&gt; function is a built-in function in Python that requests input from a user. When called, the program waits for the user to enter a value through the console.&lt;/p&gt;
&lt;p class="xbold"&gt;name = input(&amp;quot;What is your first name? &amp;quot;)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# format the name with the first letter capitalized and the rest in lowercase&lt;/span&gt;&lt;br /&gt; formatted_name = name.capitalize()&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# print the welcome message with the formatted name&lt;/span&gt;&lt;br /&gt; print(&amp;quot;Hello &amp;quot; + formatted_name + &amp;quot;! Welcome to the element14 Community!&amp;quot;)&lt;/p&gt;
&lt;p&gt;When you run the program, it will prompt the user to enter their first name:&lt;/p&gt;
&lt;p class="xbold"&gt;What is your first name?&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;Mary&amp;quot; and hits enter, the program will output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;If the user enters &amp;quot;mARY&amp;quot; instead, the program will still output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello Mary! Welcome to the element14 Community!&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;capitalize()&lt;/em&gt; function formats the name with the first letter capitalized and the rest in lowercase, regardless of how the user entered the name.&lt;a name="sec6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt5" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;6. Creating Hello World! On the Raspberry Pi&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Hardware and Software Requirements&lt;/em&gt;&lt;br /&gt; To follow this tutorial, you will need the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raspberry Pi (any model)&lt;/li&gt;
&lt;li&gt;MicroSD card with Raspbian installed&lt;/li&gt;
&lt;li&gt;Keyboard and mouse&lt;/li&gt;
&lt;li&gt;Monitor with an HDMI cable or a remote desktop app&lt;/li&gt;
&lt;li&gt;Internet connection (if you want to access your Pi remotely)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Step 1: Set up the Raspberry Pi&lt;/em&gt;&lt;br /&gt; Before we start coding, we need to set up our Raspberry Pi.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert the microSD card with Raspbian installed into the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Connect a keyboard, mouse, and monitor to the Raspberry Pi.&lt;/li&gt;
&lt;li&gt;Power on the Raspberry Pi and wait for Raspbian to boot up.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead of using a monitor, you also can connect to the Raspberry Pi remotely via SSH or VNC.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 2: Open a terminal&lt;/em&gt;&lt;br /&gt; We will be using the terminal to write our code. To open the terminal, click on the terminal icon on the desktop.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 3: Create a Python file&lt;/em&gt;&lt;br /&gt; In the terminal, type the following command:&lt;/p&gt;
&lt;p class="xbold"&gt;nano hello.py&lt;/p&gt;
&lt;p&gt;This creates a new file called hello.py and opens it in the Nano text editor.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 4: Write the code&lt;/em&gt;&lt;br /&gt; Type the following code in Nano:&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;Hello World!&amp;quot;)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 5: Save the file&lt;/em&gt;&lt;br /&gt; Press Ctrl+X (close and exit), then Y (Yes, to save), then Enter.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Step 6: Run the script&lt;/em&gt;&lt;br /&gt; To run the script, type the following command in the terminal:&lt;/p&gt;
&lt;p class="xbold"&gt;python3 hello.py&lt;/p&gt;
&lt;p&gt;This will execute the Python script and print &amp;quot;Hello World!&amp;quot; to the console.&lt;a name="sec7"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt6" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;7. Python Examples&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;&lt;em&gt;Find the first even number in a list&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;i = 0&lt;br /&gt; while i &amp;lt; len(numbers) and numbers[i] % 2 != 0:&lt;/p&gt;
&lt;blockquote&gt;i += 1&lt;/blockquote&gt;
&lt;p class="xbold"&gt;numbers = [3, 5, 7, 4, 9, 11, 8, 2]&lt;/p&gt;
&lt;p class="xbold"&gt;if i &amp;lt; len(numbers):&lt;/p&gt;
&lt;blockquote&gt;print(&amp;quot;The first even number is:&amp;quot;, numbers[i])&lt;/blockquote&gt;
&lt;p class="xbold"&gt;&lt;br /&gt; else:&lt;/p&gt;
&lt;blockquote&gt;print(&amp;quot;There are no even numbers in the array.&amp;quot;)&lt;/blockquote&gt;
&lt;p&gt;First, we initialize a list called &lt;em&gt;numbers&lt;/em&gt;. Then we create the variable &lt;em&gt;i&lt;/em&gt; and set it to 0. &lt;em&gt;i&lt;/em&gt; will store the index of the element containing the first even number. Our &lt;em&gt;while&lt;/em&gt; loop checks two conditions: that &lt;em&gt;i&lt;/em&gt; is less than the length of the numbers array (to avoid an index out of bounds error), and that the current number in the array at index &lt;em&gt;i&lt;/em&gt; is not even. If both conditions are true, we increment &lt;em&gt;i&lt;/em&gt; and continue the loop. Checking if the number is even is done with mod (%), which returns the remainder of a division operation. If the number divided by 2 gives a remainder of 0, that number is even and the loop stops.&lt;/p&gt;
&lt;p&gt;After exiting the &lt;em&gt;while&lt;/em&gt; loop, we use an &lt;em&gt;if&lt;/em&gt; statement to check whether we found an even number in the array. If &lt;em&gt;i&lt;/em&gt; is less than the length of the array, we print out the number at the index &lt;em&gt;i&lt;/em&gt; (the index where the loop was stopped). Otherwise, we print a message saying that that no even numbers are present in the array.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The first even number is: 4&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Find the average of a list of numbers&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;numbers = [10, 20, 30, 40, 50]&lt;br /&gt; sum = 0&lt;br /&gt; count = 0&lt;/p&gt;
&lt;p class="xbold"&gt;for num in numbers:&lt;/p&gt;
&lt;blockquote&gt;sum += num&lt;/blockquote&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;blockquote&gt;count += 1&lt;/blockquote&gt;
&lt;p class="xbold"&gt;average = sum / count&lt;/p&gt;
&lt;p class="xbold"&gt;print(&amp;quot;The average is &amp;quot;, average)&lt;/p&gt;
&lt;p&gt;The initial list of integers is called &lt;em&gt;numbers&lt;/em&gt;. We initialize two variables,&lt;em&gt; sum&lt;/em&gt; and &lt;em&gt;count&lt;/em&gt;, and set them to 0. We use a &lt;em&gt;for&lt;/em&gt; loop to iterate over each element in &lt;em&gt;numbers&lt;/em&gt;. During each iteration, we add the current element to &lt;em&gt;sum&lt;/em&gt;, and increment &lt;em&gt;count&lt;/em&gt; by 1. After the loop, the average can be calculated by dividing &lt;em&gt;sum&lt;/em&gt; by &lt;em&gt;count&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The output of the code should be:&lt;/p&gt;
&lt;p class="xbold"&gt;The average is 30.0&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/write to a text file&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# open a text file for writing. The &amp;lsquo;with&amp;rsquo; statement automatically closes the file after reading (or writing) is complete&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# write some text to the file&lt;/span&gt;&lt;br /&gt; f.write(&amp;#39;Hello, world!\n&amp;#39;)&lt;span class="text-green"&gt;#The &amp;lsquo;\n&amp;rsquo; character is used to create a new line in the file.&lt;/span&gt;&lt;br /&gt;f.write(&amp;#39;This is an example text file.\n&amp;#39;)&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# open the same text file for reading&lt;/span&gt;&lt;br /&gt; with open(&amp;#39;example.txt&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;br /&gt;&lt;span class="text-green"&gt;# read the contents of the file&lt;/span&gt;&lt;br /&gt;contents = f.read()&lt;/p&gt;
&lt;p class="xbold xs-ml10"&gt;&lt;span class="text-green"&gt;# print the contents of the file&lt;/span&gt;&lt;br /&gt; print(contents)&lt;/p&gt;
&lt;p&gt;Programs often need to read and write to text files. Examples include reading from a configuration or JSON file, or writing to a log file. In this example, we open a text file (&lt;em&gt;example.txt&lt;/em&gt;) that we want to write to, using the open function and the w mode (write). We then write some text to the file using the &lt;em&gt;write&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Next, we open the same text file using the open function and the &lt;em&gt;r&lt;/em&gt; mode (read). We then read the contents of the file into a variable called &lt;em&gt;contents&lt;/em&gt; using the &lt;em&gt;read&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Finally, we print the contents of the file to the console using the &lt;em&gt;print&lt;/em&gt; function. When you run this code, you should see the following output:&lt;/p&gt;
&lt;p class="xbold"&gt;Hello, world!&lt;br /&gt; This is an example text file.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Read/Update a JSON file&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, the built-in &lt;em&gt;json&lt;/em&gt; module allows us to work with JSON data. This example reads in a JSON file, updates one of its fields, and writes the modified data back to a new JSON file. First, let&amp;rsquo;s assume we have a JSON file named &lt;em&gt;data.json&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;/p&gt;
&lt;blockquote&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/blockquote&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;blockquote&gt;&amp;quot;age&amp;quot;: 30,&lt;/blockquote&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;blockquote&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/blockquote&gt;
&lt;p class="xbold"&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;The file can be read using the &lt;em&gt;json.load()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;import json&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;data.json&amp;#39;, &amp;#39;r&amp;#39;) as f:&lt;/p&gt;
&lt;blockquote&gt;data = json.load(f)&lt;/blockquote&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This outputs the contents of the JSON file:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 30, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;If we need to update this user&amp;rsquo;s age to 31, we simply assign a new value to the corresponding key in the data dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;data['age'] = 31&lt;/p&gt;
&lt;p class="xbold"&gt;print(data)&lt;/p&gt;
&lt;p&gt;This will output the updated dictionary:&lt;/p&gt;
&lt;p class="xbold"&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 31, &amp;#39;city&amp;#39;: &amp;#39;New York&amp;#39;}&lt;/p&gt;
&lt;p&gt;Now we write the modified data back to a new JSON file using the &lt;em&gt;json.dump()&lt;/em&gt; method:&lt;/p&gt;
&lt;p class="xbold"&gt;with open(&amp;#39;new_data.json&amp;#39;, &amp;#39;w&amp;#39;) as f:&lt;/p&gt;
&lt;blockquote&gt;json.dump(data, f)&lt;/blockquote&gt;
&lt;p&gt;This creates a new JSON file &lt;em&gt;(new_data.json)&lt;/em&gt; with the following contents:&lt;/p&gt;
&lt;p class="xbold"&gt;{&lt;/p&gt;
&lt;blockquote&gt;&amp;quot;name&amp;quot;: &amp;quot;Alice&amp;quot;,&lt;/blockquote&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;blockquote&gt;&amp;quot;age&amp;quot;: 31,&lt;/blockquote&gt;
&lt;p class="xbold"&gt;&lt;/p&gt;
&lt;blockquote&gt;&amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;&lt;/blockquote&gt;
&lt;p class="xbold"&gt;&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Connecting to a MySQL database with Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import mysql.connector&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Connect to the database&lt;/span&gt;&lt;br /&gt; conn = mysql.connector.connect(user=&amp;#39;user&amp;#39;, password=&amp;#39;password&amp;#39;, host=&amp;#39;host&amp;#39;,&lt;br /&gt; database=&amp;#39;database&amp;#39;) &lt;span class="text-green"&gt;#user, password, host, and database are the credentials of the MySQL database that is being connected to&lt;/span&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Execute a query&lt;/span&gt;&lt;br /&gt; cursor = conn.cursor() &lt;span class="text-green"&gt;#A cursor is a data access object that can iterate over or update the set of rows in a table&lt;/span&gt;&lt;br /&gt; query = &amp;quot;SELECT * FROM my_table&amp;quot; &lt;span class="text-green"&gt;#This SQL command retrieves all the rows from a table called my_table&lt;/span&gt;&lt;br /&gt; cursor.execute(query)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Fetch the results&lt;/span&gt;&lt;br /&gt; for row in cursor.fetchall():&lt;/p&gt;
&lt;blockquote&gt;print(row)&lt;/blockquote&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# Close the connection&lt;/span&gt;&lt;br /&gt; cursor.close()&lt;br /&gt; conn.close()&lt;/p&gt;
&lt;p&gt;This example connects to a MySQL database and executes a &lt;em&gt;SELECT&lt;/em&gt; query on a table called &lt;em&gt;my_table&lt;/em&gt;. The results are fetched and printed to the console.&lt;/p&gt;
&lt;p&gt;To connect to a MySQL database, the &lt;em&gt;mysql-connector-python&lt;/em&gt; library is required. The library can be installed using &lt;em&gt;pip&lt;/em&gt;:&lt;/p&gt;
&lt;p class="xbold"&gt;pip install mysql-connector-python&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Reading a temperature sensor with embedded Python&lt;/em&gt;&lt;/p&gt;
&lt;p class="xbold"&gt;import machine&lt;br /&gt; import time&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up temperature sensor&lt;/span&gt;&lt;br /&gt; sensor = machine.ADC(0)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# set up fan control&lt;/span&gt;&lt;br /&gt; fan_pin = machine.Pin(2, machine.Pin.OUT)&lt;/p&gt;
&lt;p class="xbold"&gt;&lt;span class="text-green"&gt;# read temperature and control fan&lt;/span&gt;&lt;br /&gt; while True:&lt;/p&gt;
&lt;blockquote&gt;temp = sensor.read()&lt;br /&gt; if temp &amp;gt; 100:&lt;br /&gt;
&lt;blockquote&gt;fan_pin.on()&lt;/blockquote&gt;
&lt;br /&gt; else:&lt;br /&gt;
&lt;blockquote&gt;fan_pin.off()&lt;/blockquote&gt;
time.sleep(0.1)&lt;/blockquote&gt;
&lt;p&gt;The code imports the machine module, which provides access to the hardware on the microcontroller. It sets up an analog-to-digital converter (ADC) to read from a temperature sensor connected to the microcontroller&amp;#39;s pin 0, and a digital output pin to control the fan connected to the microcontroller&amp;#39;s pin 2.&lt;/p&gt;
&lt;p&gt;The code then enters a &lt;em&gt;while&lt;/em&gt; loop where it reads the temperature sensor and turns the fan on or off based on the reading. If the temperature reading is above 100, it turns the fan on by setting the fan pin to ON. Otherwise, it turns the fan off by setting the fan pin to OFF. The code then waits for 0.1 seconds before reading the temperature sensor again, and the loop repeats indefinitely.&lt;a name="sec8"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="mcetoc_1grglvhvt7" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;8. Other forms of Python&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;p&gt;Several different types of Python have been created, each with its own strengths. While the syntax is similar, many of the implementations have been designed for use in specific use cases.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPython:&lt;/strong&gt; This is the default implementation of Python, written in C. CPython is the most widely used and is the reference implementation for Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jython:&lt;/strong&gt; Jython compiles Python code to Java bytecode, allowing Python code to run on the Java Virtual Machine (JVM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IronPython:&lt;/strong&gt; IronPython runs on the .NET framework. It is designed to integrate seamlessly with the .NET environment and allow Python and .NET code to interoperate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PyPy:&lt;/strong&gt; PyPy is a high-performance implementation of Python, with a just-in-time (JIT) compiler that can provide speed improvements over CPython in many cases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; MicroPython is a version of Python that is designed to run on microcontrollers and other embedded systems. It includes a reduced subset of the Python standard library, and is optimized for low memory and limited computing resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CircuitPython:&lt;/strong&gt; CircuitPython is a variant of MicroPython that is optimized for use with microcontrollers in circuitry and robotics. It provides a number of built-in libraries and tools, making it easier to integrate with electronics and sensors.&lt;a name="sec9"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="mcetoc_1grglvhvt8" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;9. Glossary&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;hr style="margin:0;" /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Abstraction:&lt;/strong&gt; a fundamental concept in programming that involves breaking down complex systems into simpler, more manageable parts. Abstraction allows programmers to create general, reusable solutions to common problems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Comment:&lt;/strong&gt; In terms of programming, a comment is a piece of text that is added to code for the purpose of providing explanations, notes, or reminders to the programmer or other readers of the code. Comments are ignored by the interpreter or compiler and do not affect the behavior of the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiled language:&lt;/strong&gt; a programming language where the source code must be first compiled into machine code that can be directly executed by a computer&amp;#39;s processor&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data type:&lt;/strong&gt; a classification of data based on the type of value it represents; examples include integers, floating-point numbers, and strings. Data types determine how the program will store, manipulate and interpret the data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at runtime, and do not have to be declared explicitly in the code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function:&lt;/strong&gt; a self-contained block of reusable code performing a specific task that can be called from other parts of a program. It can accept input paramaters and return output values, making it a powerful tool for the abstraction and organization of code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated Development Environment (IDE):&lt;/strong&gt; a software application that provides a comprehensive environment for developers to write, test and debug software. An IDE typically includes a code editor, compiler/interpreter, debugger, and other tools that assist with development, making it easier to write and maintain code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interpreted language:&lt;/strong&gt; a programming language where the source code is directly executed by an interpreter, without being compiled first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine code or bytecode:&lt;/strong&gt; a low-level programming language consisting of binary instructions that can be executed directly by a computer&amp;#39;s CPU&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object-oriented programming (OOP):&lt;/strong&gt; a programming paradigm based on the concept of &amp;quot;objects&amp;quot;, which can contain data, in the form of attributes, and code, in the form of methods. OOP focuses on creating reusable code by organizing data and behavior into classes and objects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python interpreter:&lt;/strong&gt; a program that reads and executes Python code. It translates Python code into machine code that can be understood and executed by the computer&amp;#39;s CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reusability:&lt;/strong&gt; the ability to reuse existing code to perform a specific task, rather than rewriting the same code again from scratch. This saves time and effort and makes the code more efficient and organized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statically typed language:&lt;/strong&gt; a programming language where the data type of a variable is determined at compile time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Syntax error:&lt;/strong&gt; a type of error that occurs when the code violates the grammar rules of the programming language. The code cannot be executed until the syntax error is corrected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; a named storage location in a computer&amp;#39;s memory that holds a value or data, which can be changed during program execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;*Trademark. &lt;strong&gt;element14 is a trademark of AVNET.&lt;/strong&gt; Other logos, product and/or company names may be trademarks of their respective owners.&lt;/h5&gt;
&lt;div style="border-bottom:1px solid #555555;padding-bottom:10px;"&gt;&lt;img loading="lazy" class="nolightbox essProfile" style="float:left;padding:0px 13px 13px 0px;vertical-align:top;" alt="Profile image" height="110" src="/e14/assets/main/2023/EssProgramming1_Profile.png" /&gt;
&lt;div style="font-size:1.2em;padding:12px 0px;"&gt;Python is a versatile and easy-to-learn programming language that can be used on many types of devices, including SBCs like the Raspberry Pi.&lt;/div&gt;
&lt;p&gt;&lt;span &gt;&lt;a class="e14-button e14-button--primary e14-button--large" href="https://referral.element14.com/OrderCodeView?url=%2Fraspberry%2520pi&amp;COM=essentials-programming1-doc&amp;CMP=essentials-programming1-doc" title="Shop Now" title="Shop Now" target="_blank"&gt;Shop Now&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;a name="test"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id="mcetoc_1gnl4mb880" class="xbold" style="color:#007fac;display:inline-block;vertical-align:middle;width:75%;"&gt;Test Your Knowledge&lt;/h2&gt;
&lt;div class="xbold" style="display:inline-block;font-size:11px;text-align:right;vertical-align:middle;width:24%;"&gt;&lt;a class="jive-link-anchor-small" href="#top"&gt;Back to Top&lt;/a&gt;&lt;/div&gt;
&lt;div style="float:left;padding-bottom:2px;padding-right:10px;width:150px;"&gt;            
&lt;style&gt;
   .e14-achievement-name{font-size:14px;font-weight:700;text-align:center;height:40px;margin:0;padding:5px}.e14-avatar{width:50% !important;margin:auto;border-radius:500px}.e14-avatar img{border-radius:543px}.e14-progress-bar{height:12px;border-radius:6px;background-clip:padding-box;background:#3575c2}.e14-achievement-thumb{height:200px}.e14-achievement-criteria{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria p{font-size:10px !important;overflow:hidden;margin-bottom:10px;line-height:1.5;height:60px}.e14-achievement-criteria.complete{height:38px}.e14-achievement-criteria.complete p{height:38px}.e14-greyout{-webkit-filter:grayscale(90%);-moz-filter:grayscale(90%);-o-filter:grayscale(90%);-ms-filter:grayscale(90%);filter:grayscale(90%)}.e14-complete{float:right;margin-top:-20px;padding-right:5px;margin-bottom:12px;font-weight:700;color:dimgray}.e14-achievement-status{margin:5px}.shimmer{position:relative;overflow:hidden;background:#f6f7f8;background:linear-gradient(-45deg,#eee 40%,#fafafa 50%,#eee 60%);background-size:300%;background-position-x:100%;animation:shimmer 8s infinite linear}@keyframes shimmer{0%{background-position:-1000px 0}
100%{background-position:1000px 0}}
@media only screen and (max-width:480px){.e14-achievement-criteria{display:none}.e14-complete{font-size:9px}.e14-user-achievement{width:100px !important;height:150px !important}.e14-achievement-status{margin-top:15px}.e14-achievement-thumb{height:130px}.e14-achievement-name{font-size:10px}} 
&lt;/style&gt;

    &lt;div id="embed-14ff07b8e0a24a6e9b631cb75a2eb1b4_thumbnails" data-columnclass="thumbnail-column"&gt;
        &lt;div class="content thumbnail e14-user-achievement" data-href="/achievements/6781d214-8985-4689-8d4a-04e24497afec" style="border: 3px solid #8d8d8d;cursor: pointer;"&gt;
        	       &lt;h2 class="name e14-achievement-name"&gt;
    		            Programming 1&lt;br&gt;
    		       &lt;/h2&gt;
    			&lt;div class="e14-avatar e14-greyout"&gt;
    				&lt;img loading="lazy" src="https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==" style="width: 140px;max-height:140px;overflow:hidden;background:url(&amp;#39;https://community-storage.element14.com/communityserver-components-imagefileviewer/achievements/6781d214898546898d4a04e24497afec/Programming1BDG.png-140x140x2.png?_=MEl7hR1BKaslmg9NVEv+2A==&amp;#39;) center center no-repeat;background-size:cover;aspect-ratio:140/140" alt="Programming 1" border="0" /&gt;
    		    &lt;/div&gt;
    		    &lt;div style="padding:5px;"&gt;
    		        &lt;div title="Score 100% on our&amp;nbsp;Programming I Quiz, then rate the document and leave your feedback as a comment to earn this badge." class="e14-achievement-criteria"&gt;Score 100% on our Programming I Quiz, then rate the document and leave your feedback as a comment...&lt;/div&gt;
    		        
    		    &lt;/div&gt;
    		&lt;/div&gt;
    &lt;/div&gt;   
&lt;/script&gt;&lt;/div&gt;
&lt;p&gt;Are you ready to demonstrate your Python essentials knowledge? &lt;strong&gt;Then take a quick 10-question multiple choice quiz to see how much you&amp;#39;ve learned from this module.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To earn the Essentials Programming 1 Badge&lt;/strong&gt;, read through the learning module, attain 100% in the quiz at the bottom, and leave us some feedback in the comments section below.&lt;/p&gt;
&lt;div class="e14-quiz-document-metadata e14-init-hidden" style="clear:both;"&gt;&lt;div class="voltquiz" data-formid="78e589b8-0205-407b-840e-705ddb99c71b" data-formtype="quiz" data-success-message="Q29uZ3JhdHVsYXRpb25zISBZb3UgZ290IGEgcGVyZmVjdCBzY29yZSE=" data-fail-message="U29ycnksIHlvdSBkaWRuJ3QgZ2V0IDEwMCUgb24geW91ciBzY29yZS4gUGxlYXNlIHRyeSBhZ2Fpbi4=" data-pass-mark="OTk=" &gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id="mcetoc_1grglvhvt9"&gt;&lt;em&gt;Bookmark this page for more programmming modules to come.&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;&lt;div class="e14-navigation-buttons"&gt;&lt;a href="/learn/learning-center/essentials?ICID=essentials-programming1-doc"&gt;
                    &lt;button class="e14-nav-button-prev"&gt;Previous&lt;/button&gt;
                &lt;/a&gt;&lt;/div&gt;&lt;script&gt;e14.func.queueScripts.add(function(){ e14.func.repositionNavButtons(); })&lt;/script&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: python, programming, programming language, ess_module&lt;/div&gt;
</description></item></channel></rss>