Python - Basic Syntax

The Syntax of Python language has many similarities to Perl, C, and Java. However, there are some definite differences between the languages.

First: Python Program in Interactive Mode

Example 1: Type the following text at the Python prompt and press the Enter

print ("Hello, Python");

Second: Python Program in Script Mode

Example 2: Let us write a simple Python program in a script. Python files have extension .py. Type the following source code in a test.py file

print ("Hello, Python");

print("Hello","Python")
print("Hello"+"Python") #concatenate
a=10
print("A = " , a)

a="Program"
print(a*3) #print program 3 times

The print() function inserts a new line at the end, by default. In Python 3, end=" " appends space instead of newline.

print(x, end=" ")  # Appends a space instead of a newline in Python 3

Reading Input from User

The input() function is use to input data from user. The data entered by the user is treated as string. If you want to do some arthmetic functions you must convert it into its appropriate data type.

name = input("Enter your name:")
age = input("Enter your age:")
print('Name = '+name)
print('Age = ',age)
Enter your name:Ankit WebLogic
Enter your age:20
Name = Ankit WebLogic
Age = 20