- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
Python Basic Syntax
The basic syntax of a Python program means there are rules to define the correct structure of statements and expressions used in that program. Therefore, in this section, we look up the basic structure of a Python program, or how a Python program can be written to produce effective output.
Unlike most other programming languages like C, C++, Java, etc., Python provides a simple syntax to work on. That is, to print "Hello World" in Python, we need to write only this single statement:
print("Hello World")
It should be noted that a semicolon is not required at the end of the statement. This is the very simple structure of Python.
Since I've written the above program in the PyCharm IDE. Therefore, after executing the above program, here is the output produced:
The output will be "Hello World" regardless of which IDE you use.
While learning about the basic syntax of Python or the basic structure of a Python program, the main thing to look up is the indentation of the program. So let's talk about it.
Indentation is the most important
Now the question is, what is indentation?
The answer is that "indentation" refers to the spaces available or given at the beginning of a statement in a Python program. Before going deep into it, let's
take a look at the program given below:
val = 10 if val>0: print("Positive") elif val<0: print("Negative")
The above program can also be written as:
val = 10 if val>0: print("Positive") elif val<0: print("Negative")
Both the programs produce the same output, which is:
Note: The number of spaces to give before the statement is up to you, the programmer, but the statement needs to be in the same block of code.
The program given below produces the error:
val = 10 if val>0: print("Positive") elif val<0: print("Negative")
The error produced by the above Python program is only due to its indentation, as shown in the snapshot given below:
The following program also produces an indentation error:
val = 10 if val>10: print("Positive") print("The value is", val)
The above program should be structured in this way:
val = 10 if val>0: print("Positive") print("The value is", val)
The output produced by the above program is:
Note: Whatever the space you provide, the space must be the same for the same block of code.
You can refer to "get input from the user in Python" to understand the step-by-step process of how different types of inputs can be processed in the Python programming language.
More Examples
- Get Input from User
- Add Two Numbers
- Check Even or Odd
- Check Prime Number or Not
- Check Vowel or Not
- Check Leap Year or Not
- Make Simple Calculator
« Previous Tutorial Next Tutorial »