{"id":20779,"date":"2022-12-09T06:14:00","date_gmt":"2022-12-09T05:14:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=20779"},"modified":"2023-01-05T16:19:25","modified_gmt":"2023-01-05T15:19:25","slug":"basic-python-syntax","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/","title":{"rendered":"Basic Python syntax with code examples"},"content":{"rendered":"\n<h4 class=\"has-text-align-center wp-block-heading\"><strong><em>Tutorial about the basic Python syntax with indentation, variables, loops and control structures code examples.<\/em><\/strong><\/h4>\n\n\n\n<p>What are the basic Python syntax and how to use them in practical examples? Python is a popular programming language known for its simplicity, readability, and versatility. In this blog post, we will cover the basic syntax of Python, including indentation, variables, loops, and control structures.<\/p>\n\n\n\n<p>To start this tutorial about basic programming syntaxes in Python, indentation is the first thing to talk about.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#1-python-indentation-syntax\" >1. Python indentation syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#2-python-basic-syntax-for-variables\" >2. Python basic syntax for variables<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#21-python-variables-assignement\" >2.1 Python variables assignement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#22-python-variables-are-untyped\" >2.2 Python variables are untyped<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#23-more-complex-examples-with-python-variables\" >2.3 More complex examples with Python variables<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#3-python-loops\" >3. Python loops<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#31-for-loop-in-python\" >3.1 For loop in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#32-while-loop-in-python\" >3.2 While loop in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#33-the-break-and-continue-statements\" >3.3 The break and continue statements<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#4-syntax-of-basic-control-structures\" >4. Syntax of basic control structures<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#41-if-statement-in-python\" >4.1 If statement in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#42-if-else-statement\" >4.2 If else statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#43-if-elif-else-statement\" >4.3 If elif else statement<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#5-conclusion-on-python-syntax\" >5. Conclusion on Python syntax<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-python-indentation-syntax\"><span class=\"ez-toc-section\" id=\"1-python-indentation-syntax\"><\/span>1. Python indentation syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Indeed, in <a href=\"https:\/\/expert-only.com\/en\/python\/introduction-to-python-programming\/\">Python programming<\/a>, indentation is used to indicate which lines of code belong to the same block. This is different from many other programming languages, which use curly braces to enclose blocks of code. For example, check the following code example, it does not use parenthesis to mark the end of if section, but the indentation.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if x > 0:\n    print('x is positive')\n    x += 1\n    print('x has been incremented')\nelse:\n    print('x is not positive')\n<\/pre>\n\n\n\n<p>In this code, the lines <code>print('x is positive')<\/code> and <code>x += 1<\/code> are both indented, indicating that they belong to the same block of code that is executed if <code>x<\/code> is positive. The line <code>print('x has been incremented')<\/code> is also indented, indicating that it belongs to the same block of code as the first two lines.<\/p>\n\n\n\n<p>It is important to maintain consistent indentation throughout the code, as incorrect indentation can lead to syntax errors. Here are more examples to illustrate how indentation is used in Python code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Example 1\nx = 8 \nif x > 0:\n    print('x is positive')\n    x += 1\nelse:\n    print('x is not positive')\n\n# Example 2\nfor i in range(5):\n    print(i)\n    if i % 2 == 0:\n        print('i is even')\n\n# Example 3\ndef welcome(name):\n    print('Hello, welcome ' + name + '!')\n\nname ='expert'\nprint( welcome(name) )\n<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"780\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-basic-syntax-variables-and-function.jpg\" alt=\"Basic Python syntax with code examples to set and print variables\" class=\"wp-image-20842\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-basic-syntax-variables-and-function.jpg 640w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-basic-syntax-variables-and-function-246x300.jpg 246w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Basic Python syntax with code examples to set and print variables<\/figcaption><\/figure><\/div>\n\n\n<p><\/p>\n\n\n\n<p>Another basic but fundamental element of the coding learning curve is the <a href=\"https:\/\/expert-only.com\/en\/python\/python-data-types\/\">built-in data types available in the Python language<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"9KtUeyNQND\"><a href=\"https:\/\/expert-only.com\/en\/python\/python-data-types\/\">Python data types with code examples<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Python data types with code examples&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/python-data-types\/embed\/#?secret=mJNx4jYsgf#?secret=9KtUeyNQND\" data-secret=\"9KtUeyNQND\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2-python-basic-syntax-for-variables\"><\/span>2. Python basic syntax for variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To store data in Python, you can use variables. A variable is a named location in memory where you can store a value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"21-python-variables-assignement\"><\/span>2.1 Python variables assignement<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>To create a variable, you just need to give it a name and assign it a value. For example, this is a simple code example to create and print 3 variables with 3 different <a href=\"https:\/\/expert-only.com\/en\/python\/python-data-types\/\">Python data types<\/a>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Integer<\/li>\n\n\n\n<li>String<\/li>\n\n\n\n<li>Boolean<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Simple assignment of a single value to a variable\nx = 5\nprint(x)   # Output: 5\n\n# Assignment of a string value to a variable\nmessage = 'Hello, world!'\nprint(message)   # Output: 'Hello, world!'\n\n# Assignment of a Boolean value to a variable\nflag = True\nprint(flag)   # Output: True\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"22-python-variables-are-untyped\"><\/span>2.2 Python variables are untyped<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In Python, variables are untyped, meaning that you can assign a value of any data type to a variable without explicitly declaring its type. So it is possible to assign a value of a data type to a variable of a different data type. This is in contrast to languages like C or Java, where you must specify the type of a variable when you declare it.<\/p>\n\n\n\n<p>For example, assign a varchar to an integer variable, and then a Boolean value.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Assignment of an integer value to a variable\nx = 5\nprint(x)   # Output: 5\n\n# Reassignment of a string value to the same variable\nx = 'Hello, world!'\nprint(x)   # Output: 'Hello, world!'\n\n# Reassignment of a Boolean value to the same variable\nx = True\n<\/pre>\n\n\n\n<p>The advantage of untyped variables is that they can be more flexible and easier to use. However, it is important to be aware of the data type of a variable when you are working with it, as certain operations may only be valid for certain data types. For example, you cannot concatenate an integer and a string in Python, as the <code>+<\/code> operator is used for both addition and concatenation.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x = 5\ny = 'hello'\n# This will raise a TypeError\nz = x + y   \n<\/pre>\n\n\n\n<p>To avoid this type of error, you can use type coercion to explicitly convert a value to a different data type.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x = 5\ny = 'hello'\n# This will concatenate the string representation of x and y\nz = str(x) + y   \n# the output will be : 5hello<\/pre>\n\n\n\n<p>You can use variables to perform operations and store the result. For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Addition of two variables and assign the result to a new variable : z\nx = 5\ny = 3\nz = x + y\nprint(z)\n# Output: 8\n\n# Concatenation of two string variables and assign the result to c\na = 'Hello, '\nb = 'world!'\nc = a + b\nprint(c)\n# The output is : 'Hello, world!'\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"23-more-complex-examples-with-python-variables\"><\/span>2.3 More complex examples with Python variables<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can also use variables to store the result of a function call. Like in the following code example.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Call to the len() function to get the length of a string\n# and assign the result to a variable\nword = 'hello'\nlength = len(word)\nprint(length)\n# Output: 5\n\n# Call to the sum() function to add the elements of a list\n# and assign the result to a variable\nnumbers = [1, 2, 3, 4, 5]\nsum = sum(numbers)\nprint(sum)\n# Output: 15\n<\/pre>\n\n\n\n<p>Finally, you can use variables to store the result of a more complex expression. In the second code exemple, the variable is used to store the result of a function that checks if the string is a <a href=\"https:\/\/www.masterclass.com\/articles\/palindromes-explained\" target=\"_blank\" rel=\"noreferrer noopener\">palindrome<\/a>. I.e., words or phrases that are spelled the same way forwards and backwards, like : <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>level<\/li>\n\n\n\n<li>madam<\/li>\n\n\n\n<li>rotator<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Calculation of the average of a list of numbers\n# then assign the result to the average variable\nnumbers = [1, 2, 3, 4, 5]\naverage = sum(numbers) \/ len(numbers)\nprint(average)\n# Output: 3.0\n\n# Check if a string is a palindrome (i.e., reads the same backward as forward)\n# then assign the result to the variable called is_palindrome\nword = 'racecar'\nis_palindrome = word == word[::-1]\nprint(is_palindrome)\n# Output: True\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3-python-loops\"><\/span>3. Python loops<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To continue and discover more on the Basic Python syntax, there are two types of loops in Python, the for loops and the while loops. As you can see in the code below, loops are a powerful tool in Python that can be used to perform repetitive tasks and process data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"31-for-loop-in-python\"><\/span>3.1 For loop in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A <code>for<\/code> loop is used to iterate over a sequence of elements, such as a list or a string. For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Simple for loop example\nfor i in range(5):\n    print(i)\n\n# Output: 0 1 2 3 4\n\n# For loop with a list\ncolors = ['red', 'green', 'blue']\nfor color in colors:\n    print(color)\n\n# Output: red green blue\n\n# For loop with a string\nword = 'hello'\nfor letter in word:\n    print(letter)\n\n# Output: h e l l o\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"32-while-loop-in-python\"><\/span>3.2 While loop in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A <code>while<\/code> loop is used to repeat a block of code as long as a certain condition is true.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Simple while loop example\ni = 0\nwhile i &lt; 5:\n    print(i)\n    i += 1\n\n# Output: 0 1 2 3 4\n\n# While loop with a counter\ncounter = 0\nwhile True:\n    print('Hello, world!')\n    counter += 1\n    if counter == 5:\n        break\n\n# Output: Hello, world! Hello, world! Hello, world! Hello, world! Hello, world!\n\n# While loop with user input\nwhile True:\n    input_text = input('Enter a number: ')\n    if input_text.isdigit():\n        input_number = int(input_text)\n        print('You entered the number', input_number)\n        break\n    else:\n        print('Invalid input, please try again.')<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"33-the-break-and-continue-statements\"><\/span>3.3 The break and continue statements<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can also use the break and continue statements within a loop to control its execution. The break statement will exit the loop prematurely, while the continue statement will skip the rest of the current iteration and move on to the next one.<\/p>\n\n\n\n<p><strong>First examples using break continue in a for loop<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># For loop with a break statement\nfor i in range(10):\n    if i == 5:\n        break\n    print(i)\n\n# Output: 0 1 2 3 4\n\n# For loop with a continue statement\nfor i in range(10):\n    if i % 2 == 0:\n        continue\n    print(i)\n\n# Output: 1 3 5 7 9<\/pre>\n\n\n\n<p><strong>Examples using break continue in a while loop<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># While loop with a break statement\ni = 0\nwhile True:\n    print(i)\n    i += 1\n    if i == 5:\n        break\n\n# Output: 0 1 2 3 4\n\n# While loop with a continue statement\ni = 0\nwhile True:\n    i += 1\n    if i % 2 == 0:\n        continue\n    print(i)\n    if i == 10:\n        break\n\n# Output: 1 3 5 7 9<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4-syntax-of-basic-control-structures\"><\/span>4. Syntax of basic control structures<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Control structures are used to control the flow of execution of a program. In Python, there are three control structures: <code>if<\/code>, <code>elif<\/code>, and <code>else<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"41-if-statement-in-python\"><\/span>4.1 If statement in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>An <code>if<\/code> statement is used to execute a block of code if a certain condition is true.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x = 5\nif x > 0:\n    print('x is positive')\n\n# Output: x is positive\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"42-if-else-statement\"><\/span>4.2 If else statement<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can also use an <code>if<\/code> statement in conjunction with an <code>else<\/code> clause to execute a different block of code if the condition is false.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x = 5\nif x > 0:\n    print('x is positive')\nelse:\n    print('x is not positive')\n\n# Output: x is positive\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"43-if-elif-else-statement\"><\/span>4.3 If elif else statement<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you have multiple conditions that you want to check, you can use an <code>elif<\/code> clause to specify additional conditions.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x = 5\nif x > 0:\n    print('x is positive')\nelif x &lt; 0:\n    print('x is negative')\nelse:\n    print('x is zero')\n\n# Output: x is positive\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5-conclusion-on-python-syntax\"><\/span>5. Conclusion on Python syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this programming tutorial, we covered the basic syntax of Python, including variables, loops, and control structures.<\/p>\n\n\n\n<p>We also learned how to use variables to store and operate on data, and how to use loops and control structures to perform tasks and make decisions in our code. And about some of the unique features of Python, such as its use of indentation to indicate blocks of code and its support for untyped variables.<\/p>\n\n\n\n<p>With these fundamentals, you can now start writing your own Python programs. Whether you want to build a simple script to automate a task, or a complex application to solve a real-world business problem, Python is a powerful and flexible language that can help you achieve your goals.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Popular IT tutorials that might interest you<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/expert-only.com\/en\/power-bi\/last-year-value-dax-power-bi\/\">Calculate the last year value in Power BI with DAX<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/t-sql\/calculate-difference-between-two-dates\/\">Calculate the difference between two dates with SQL Server<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/t-sql\/insert-or-remove-sql-server-line-breaks\/\">Insert or remove SQL Server line breaks from text<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/ms-dos\/rename-multiple-windows-files\/\">Rename multiple Windows files at the same time<\/a><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"ecFeTVePBx\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/manage-sql-server-user-defined-functions\/\">Manage SQL Server user defined functions<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Manage SQL Server user defined functions&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/manage-sql-server-user-defined-functions\/embed\/#?secret=dvjq4JE2n1#?secret=ecFeTVePBx\" data-secret=\"ecFeTVePBx\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Tutorial about the basic Python syntax with indentation, variables, loops and control structures code examples. What are the basic Python syntax and how to use them in practical examples? Python is a popular programming language known for its simplicity, <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/\" title=\"Basic Python syntax with code examples\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":6400,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[657],"tags":[],"class_list":{"0":"post-20779","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Basic Python syntax - Programming - Python tutorials<\/title>\n<meta name=\"description\" content=\"Tutorial about the basic Python syntax with code examples to illustrate indentation, variables, loops and control structures.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Basic Python syntax with code examples\" \/>\n<meta property=\"og:description\" content=\"Tutorial about the basic Python syntax with code examples to illustrate indentation, variables, loops and control structures.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-09T05:14:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-05T15:19:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"Basic Python syntax with code examples\",\"datePublished\":\"2022-12-09T05:14:00+00:00\",\"dateModified\":\"2023-01-05T15:19:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/\"},\"wordCount\":1029,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg\",\"articleSection\":[\"Python tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/\",\"url\":\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/\",\"name\":\"Basic Python syntax - Programming - Python tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg\",\"datePublished\":\"2022-12-09T05:14:00+00:00\",\"dateModified\":\"2023-01-05T15:19:25+00:00\",\"description\":\"Tutorial about the basic Python syntax with code examples to illustrate indentation, variables, loops and control structures.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Basic Python syntax with code examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Basic Python syntax - Programming - Python tutorials","description":"Tutorial about the basic Python syntax with code examples to illustrate indentation, variables, loops and control structures.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/","og_locale":"en_US","og_type":"article","og_title":"Basic Python syntax with code examples","og_description":"Tutorial about the basic Python syntax with code examples to illustrate indentation, variables, loops and control structures.","og_url":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2022-12-09T05:14:00+00:00","article_modified_time":"2023-01-05T15:19:25+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"Basic Python syntax with code examples","datePublished":"2022-12-09T05:14:00+00:00","dateModified":"2023-01-05T15:19:25+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/"},"wordCount":1029,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg","articleSection":["Python tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/","url":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/","name":"Basic Python syntax - Programming - Python tutorials","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg","datePublished":"2022-12-09T05:14:00+00:00","dateModified":"2023-01-05T15:19:25+00:00","description":"Tutorial about the basic Python syntax with code examples to illustrate indentation, variables, loops and control structures.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2020\/07\/fresh-glass-coffee-cup-2D256C64A99_1920x1080.jpeg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/python\/basic-python-syntax\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"Basic Python syntax with code examples"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/20779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=20779"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/20779\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/6400"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=20779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=20779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=20779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}