Python Syntax
The syntax of the any programming language is the set of rules that defines how a program will be written and interpreted (by both the runtime system and by human readers).
Python syntax can be executed either using Command Line Or by creating a python file on the server, using the .py file extension and execute it in the Command Line.
Writing directly in the Command Line:
>>> print("Hello, Students!") Hello, Students!
By creating a python file and running it in the Command Line:
C:\Users\Your Name>python file1.py
-
Python Indentation
Python Multiline Statements
In Python a new line means a new statement. But sometimes, you may want to split a statement in two or more lines.Python Variables
Data type is not necessary to declare a variable in Python Programming language.Python variables are created by assigning values of desired type.Python Comments
Comments are used for code documentation.
Indentation in Python helps to create block of code.Without proper indentation python code will not get compiled and displaying IndentationError.Where in other programming languages such as C, java use braces({}) to define a block of code,Python uses indentation.
Indentation refers to the spaces at the beginning of a code line.
Example:
if 7 > 2: print("seven is greater than two!")
The same number of spaces must be used in the same block of code,Otherwise Python will show error .
if 9>5: print("9 is greter than 5") print("5 is less than 9")The above code is correct.
if 9>5: print("9 is greter than 5") print("5 is less than 9")The above code is incorrect.Error will be displayed.
Example of Python Syntax
Python is a user friendly language because of its intelligent syntax structure.Let’s do a simple Python program and you will get an idea of how programming in Python looks like.
#Simple Python Program to see whether a user is Minor or Adult. # getting user’s name print("Enter your name:") name = input() # getting user’s age print("Enter your age:") age = int(input()) # condition to check whether user is Minor or Adult if( age >= 18 ): print( name, ' is Adult.') else: print( name, ' is Minor.')
Output Enter your name: Rishi Enter your age: 19 Rishi is Adult.
Carefully, look at all print() statements given above.Could you notice that first two print()statements are enclosed in double quotes and the next two print() statements are enclosed in single quotes.
print("Enter your name:")
print("Enter your age:")
statements enclosed in double quotes
print( name, ' is Adult.')
print( name, ' is Minor.')
statements enclosed in single quotes
It may be to aid readability. You can do so in the following ways.
i. Use a backward slash
>>> print("Hello students\ how are you?")
output Hello students how are you?You can also use it to distribute a statement.
>>>x\ =\ 5 >>> print(x)
Output: 5ii. Put the String in Triple Quotes
>>> print("""Hello students how are you?""")
Output: Hello students how are you?
>>> ns='''he\ ll\ o''' >>> print(ns)
helloiii. Multiple Statements in One Line
>>>x=5;print(x);
output: 5
Example
Variables in Python:
>>>x = 5 >>>print(x) 5 >>>x="Hello, World!" >>>print(x) Hello, World
Python Comments start with a #
Example
Comments in Python:
#This is a comment. print("Hello, students!!")