- Python Basics
- Python Home
- Python History
- Python Applications
- Python Features
- 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 & 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 & Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes & Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class Vs Static Method
- Python @property Decorator
- Python Regular Expressions
- Python CGI Programming
- Python Network Programming
- Python Send E-mail
- Python Multi-threading
- Python XML Processing
- Python MySQL Database
- Python GUI Programming
- Python Event Handling
- Python Keywords
- Python All 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 Test
- Python Online Test
- Give Online Test
- All Test List
Python Basic Syntax
Basic syntax of a Python program means rules to define the correct structure of statements and expressions used in that program. Therefore, under 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 little simple syntax to work on. That is, to print Hello World in Python, we need to write only this single statement:
print("Hello World")
Note - Even semicolon does not require to put at last of the statement. This is the very simple structure of Python.
Since I've written above program in PyCharm IDE. Therefore after executing the above program, here is the output produced:
Whatever the IDE you use, the output will be Hello World.
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 program. So let's talk about it.
Indentation is Most Important
Now the question is, what is indentation ?
The answer is, indentation refers to the spaces available/given at beginning of a
statement in 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 program produces same output, that is:
Note - The number of space(s) to give before the statement, is up to you/programmer, but the statement needs to be in its same block of code.
The program given below produces error:
val = 10 if val>0: print("Positive") elif val<0: print("Negative")
The error produces by above Python program is only due to its indentation as shown in the snapshot given below:
The following program also produces 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 above program is:
Note - Whatever the space you provide, the space must be same for the same block of code.
You can refer to get user input from user in Python to get every required things on receiving any user inputs at run-time of the program with step by step description.
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 »