Python Basic Syntax
In this lesson, you will learn about the Python basic syntax, python identifiers, variables, lines and indentation.
As we know, python is an interpreted high-level programming language, you can directly enter a code in the command line and execute it or you can create a separate file and run it in the command line.
Running a code directly on the command line is known as an interactive mode.
Whereas, executing a script file is called Script mode.
Let’s see how to execute a code in script mode.
- First, create a new file by pressing Ctrl+N
- Enter your code in the file.
- For instance, you can add the following code to your file.
print(“Hello World! Welcome to Developer Publish Academy”)
- Save the file as first_program.py
- Now, click on the run module option from the Run menu.
- You can see the contents of the file getting executed in the shell.
Keyword and Identifiers
Keywords are reserved words that cannot be used as a variable or a value in any sort of program/script.
In python, there are a total of 31 reserved or keywords that cannot be used as a variable or value.
To find out the list of keywords, run the following code in the python shell.
import keyword Keyword.kwlist
[gamipress_button type=”submit” value=”RUN CODE SNIPPET” onclick=”window.open(‘https://coderseditor.com/?id=1163′,’_blank’)”]
Generally, an identifier is a variable, a function, a class, a module. Whenever you provide a name to a certain object in python it is termed as an identifier.
Rules for a valid identifier
It should start with a letter(A-Z, a-z both apply).
It can also start with an underscore(_) followed by alphanumeric characters.
Types of Identifiers in Python
- Other Objects
How to add comments in python?
Comments are non-executable sentences in python. It is used to provide a detailed meaning of your code. You can include a comment into your code to make it more understandable to yourself/other programmers.
To write a comment in python:
- You can start with the hash character, #, This method is only applicable till the end of the physical line.
- Incase you need to add a multiline comment, then you can use Triple Quotation (‘’’)
Print(“Developer Publish”) #this is a simple code for printing Developer Publish
Python Lines and Indentation
Unlike C, python does not have braces to determine the block of code. So to tackle this, python follows a strict indentation policy.
Indentation is the white space before certain lines of code in python. The number of spaces may vary depending on the line of code.
The ‘for’ statement will have one space before it and the ‘if’ statement will have two spaces.
Indentation increases the readability of the code which helps python to execute or run the code properly. Python throws an indentation error if the indentation provided in the code is incorrect
Follow these simple rules to avoid Indentation errors:
- If you are using a statement involving indentation, use the ‘:’ to start a block of code and then press Enter.
- Every line of a code in the same block should use the same indentation, either space or a tab.
- Generally, the python interpreter uses four spaces as indentation to make the code more readable.
- Never use both space and tab in the same block.
- A block of code can also have an inner block of codes with next level indentation.