Simple Programs in Python
The following Python section contains a wide collection of basic Python programming examples.
including conditional statements,loop.
- Write a Program to display the message "Good Morning!!"
- Assign two numbers in two variables n1 and n2.The task is to write a Python program to find the addition of these two numbers.
# Python program to add two numbers n1 = 10 n2 = 20 # Adding two nos sum = n1 + n2 # printing values print("Sum of {0} and {1} is {2}" .format(n1, n2, sum))
Output: Sum of 10 and 20 is 30
In that case the value of n1 will be shown in {0}, the value of n2 will be shown in {1} and the value of sum will be shown in {2}
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
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.
print("Good Morning!!")
output: Good Morning!!
print() function is used to display string which is within quotation.
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!!")
ADVERTISEMENT