{"id":73,"date":"2020-10-03T01:11:12","date_gmt":"2020-10-03T01:11:12","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=73"},"modified":"2025-03-26T06:23:45","modified_gmt":"2025-03-26T06:23:45","slug":"python-syntax","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-syntax\/","title":{"rendered":"Python Syntax"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: In this tutorial, you&#8217;ll learn about the basic Python syntax so that you can quickly get started with the Python language.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='whitespace-and-indentation'>Whitespace and indentation <a href=\"#whitespace-and-indentation\" class=\"anchor\" id=\"whitespace-and-indentation\" title=\"Anchor for Whitespace and indentation\">#<\/a><\/h2>\n\n\n\n<p>If you&#8217;ve been working in other programming languages such as Java, C#, or C\/C++, you know that these languages use semicolons (<code>;<\/code>) to separate the statements.<\/p>\n\n\n\n<p>However, Python uses&nbsp;whitespace&nbsp;and indentation to construct the code structure.<\/p>\n\n\n\n<p>The following shows a snippet of Python code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># define main function to print out something<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    i = <span class=\"hljs-number\">1<\/span>\n    max = <span class=\"hljs-number\">10<\/span>\n    <span class=\"hljs-keyword\">while<\/span> (i &lt; max):\n        print(i)\n        i = i + <span class=\"hljs-number\">1<\/span>\n\n<span class=\"hljs-comment\"># call function main <\/span>\nmain()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The meaning of the code isn&#8217;t important to you now. Please pay attention to the code structure instead.<\/p>\n\n\n\n<p>You don&#8217;t see any semicolon at the end of each line to terminate the statement. And the code uses indentation to format the code.<\/p>\n\n\n\n<p>By using indentation and&nbsp;whitespace to organize the code, Python code gains the following advantages:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, you&#8217;ll never miss a block&#8217;s beginning or ending code, unlike in other programming languages, such as Java or <a href=\"https:\/\/www.csharptutorial.net\/\" target=\"_blank\" rel=\"noreferrer noopener\">C#<\/a>.<\/li>\n\n\n\n<li>Second, the coding style is essentially uniform. If you have to maintain another developer&#8217;s code, that code looks the same as yours.<\/li>\n\n\n\n<li>Third, the code is more readable and clearer than other programming languages.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='comments'>Comments <a href=\"#comments\" class=\"anchor\" id=\"comments\" title=\"Anchor for Comments\">#<\/a><\/h2>\n\n\n\n<p>The comments are as important as the code because they describe why a piece of code was written.<\/p>\n\n\n\n<p>When the Python interpreter executes the code, it ignores the comments.<\/p>\n\n\n\n<p>In Python, a single-line comment begins with a hash (#) symbol followed by the comment. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># This is a single line comment in Python<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And Python also supports other kinds of <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-comments\/\">comments<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='continuation-of-statements'>Continuation of statements <a href=\"#continuation-of-statements\" class=\"anchor\" id=\"continuation-of-statements\" title=\"Anchor for Continuation of statements\">#<\/a><\/h2>\n\n\n\n<p>Python uses a newline character to separate statements. It places each statement on one line.<\/p>\n\n\n\n<p>However, a long statement can span multiple lines by using the backslash (<code>\\<\/code>) character.<\/p>\n\n\n\n<p>The following example illustrates how to use the backslash (<code>\\<\/code>) character to continue a statement in the second line:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">if<\/span> (a == <span class=\"hljs-literal\">True<\/span>) <span class=\"hljs-keyword\">and<\/span> (b == <span class=\"hljs-literal\">False<\/span>) <span class=\"hljs-keyword\">and<\/span> \\\n   (c == <span class=\"hljs-literal\">True<\/span>):\n    print(<span class=\"hljs-string\">\"Continuation of statements\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='identifiers'>Identifiers <a href=\"#identifiers\" class=\"anchor\" id=\"identifiers\" title=\"Anchor for Identifiers\">#<\/a><\/h2>\n\n\n\n<p>Identifiers are names that identify <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-variables\/\">variables<\/a>, <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">functions<\/a>, <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-module\/\">modules<\/a>, <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">classes<\/a>, and other objects in Python.<\/p>\n\n\n\n<p>The name of an identifier needs to begin with a letter or underscore (<code>_<\/code>). The following characters can be alphanumeric or underscore.<\/p>\n\n\n\n<p>Python identifiers are case-sensitive. For example, the <code>counter<\/code> and <code>Counter<\/code> are different identifiers.<\/p>\n\n\n\n<p>In addition, you cannot use Python keywords for naming identifiers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='keywords'>Keywords <a href=\"#keywords\" class=\"anchor\" id=\"keywords\" title=\"Anchor for Keywords\">#<\/a><\/h2>\n\n\n\n<p>Some words have special meanings in Python. They are called keywords.<\/p>\n\n\n\n<p>The following shows the list of keywords in Python:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-literal\">False<\/span>      <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>      <span class=\"hljs-title\">finally<\/span>    <span class=\"hljs-title\">is<\/span>         <span class=\"hljs-title\">return<\/span>\n<span class=\"hljs-title\">None<\/span>       <span class=\"hljs-title\">continue<\/span>   <span class=\"hljs-title\">for<\/span>        <span class=\"hljs-title\">lambda<\/span>     <span class=\"hljs-title\">try<\/span>\n<span class=\"hljs-title\">True<\/span>       <span class=\"hljs-title\">def<\/span>        <span class=\"hljs-title\">from<\/span>       <span class=\"hljs-title\">nonlocal<\/span>   <span class=\"hljs-title\">while<\/span>\n<span class=\"hljs-title\">and<\/span>        <span class=\"hljs-title\">del<\/span>        <span class=\"hljs-title\">global<\/span>     <span class=\"hljs-title\">not<\/span>        <span class=\"hljs-title\">with<\/span>\n<span class=\"hljs-title\">as<\/span>         <span class=\"hljs-title\">elif<\/span>       <span class=\"hljs-title\">if<\/span>         <span class=\"hljs-title\">or<\/span>         <span class=\"hljs-title\">yield<\/span>\n<span class=\"hljs-title\">assert<\/span>     <span class=\"hljs-title\">else<\/span>       <span class=\"hljs-title\">import<\/span>     <span class=\"hljs-title\">pass<\/span>\n<span class=\"hljs-title\">break<\/span>      <span class=\"hljs-title\">except<\/span>     <span class=\"hljs-title\">in<\/span>         <span class=\"hljs-title\">raise<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Python is a growing and evolving language. So its keywords will keep increasing and changing.<\/p>\n\n\n\n<p>Python provides a special <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-module\/\">module<\/a> for listing its keywords called <code>keyword<\/code><em>.&nbsp;<\/em><\/p>\n\n\n\n<p>To find the current keyword list, you use the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> keyword\n\nprint(keyword.kwlist) <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='string-literals'>String literals <a href=\"#string-literals\" class=\"anchor\" id=\"string-literals\" title=\"Anchor for String literals\">#<\/a><\/h2>\n\n\n\n<p>Python uses single quotes (<code>'<\/code>), double quotes (<code>\"<\/code>), triple single quotes (<code>'''<\/code>) and triple-double quotes (<code>\"\"\"<\/code>) to denote a string literal.<\/p>\n\n\n\n<p>The string literal needs to be surrounded with the same type of quotes. For example, if you use a single quote to start a string literal, you need to use the same single quote to end it.<\/p>\n\n\n\n<p>The following shows some examples of string literals:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">s = <span class=\"hljs-string\">'This is a string'<\/span>\nprint(s)\ns = <span class=\"hljs-string\">\"Another string using double quotes\"<\/span>\nprint(s)\ns = <span class=\"hljs-string\">''' string can span\n        multiple line '''<\/span>\nprint(s)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A Python statement ends with a newline character.<\/li>\n\n\n\n<li>Python uses spaces and indentation to organize its code structure.<\/li>\n\n\n\n<li>Identifiers are names that identify variables, functions, modules, classes, etc..<\/li>\n\n\n\n<li>Comments describe why the code works. They are ignored by the Python interpreter.<\/li>\n\n\n\n<li>Use the single quote, double-quotes, triple-quotes, or triple double-quotes to denote a string literal.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<p><\/p>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=syntax\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"73\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-syntax\/\"\n\t\t\t\tdata-post-title=\"Python Syntax\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"73\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-syntax\/\"\n\t\t\t\tdata-post-title=\"Python Syntax\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the basic Python syntax so that you can get started with the Python language quickly.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-73","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/73\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}