Python Programs





Program-1: Example of Printing the values in different ways with use of variables.

#This is the comment line in the Python.
#Program to explain basic printing and using of variables.
#Type the following code (as it is) in the new Windows opened by IDE.
#Different ways to print the statements and use of variables.

print("Hello Friends","Welcome to Python")

language='Python is a High Level Programming Language'
author="Guido Van Rossum in 1990s developed Python Programming Language."
name="Python was named after a British TV Show namely 'Monty Python's Flying Circus"
save='Save Python file with .py extension'

print(language)
print(author)
print(name)
print(save)
#print("These Values are not printed")

Output
>>> 
Hello Friends Welcome to Python
Python is a High Level Programming Language
Guido Van Rossum in 1990s developed Python Programming Language.
Python was named after a British TV Show namely 'Monty Python's Flying Circus
Save Python file with .py extension
>>> 

========================================================================

Program-2: Example of Various String Literals.
#Example of String Literals
#Python has various Literals, which are shown in this program

print("Literals are oftenly known as CONSTATNT values, which have fixed values.")

String1="String Literals are written in double quotes."
String2='String Literals are written in double quotes.'
String3="Amy's brother is an 'Engineer'.
#Here multi line literal for string is used.
String4="Hello\
World"     
String5="'A\
B\
C\'"

print(String1);
print(String2)
print(String3);
print(String4)
print(String5)

Output
>>>
Literals are oftenly knwon as CONSTATNT values, which have fixed values.
String Literals are written in double quotes.
String Literals are written in double quotes.
Amy's brother is an 'Engineer'.
HelloWorld
'ABC'
>>> 

========================================================================

Program-3: Example of Various Numeric Literals.
#Numerical Literals

print("Some Numerical Literals")
print("Decimal Integer Literals:")
print(-50)
print(758)
print(+3654)

print("Octal Integer Literals:")
print(0o57)
print(0O777)
print(0o234)

print("HexaDecimal Integer Literals:")
print(0x34);
print(0X4A);
print(0xC01);

print("Floating Point Literals:")
print("Fractional Form")
print(27.5);
print(.95);
print(18.);
print(-29.0);
print(0.15);

print("Exponent Form")
print(0.75E03)
print(-2.25E-3)
print(7.5E04)
print(75.e2)

print("Boolean Literals")
print(True)
print(False)

print("Special Literal")
print(None)

Output
>>>
Some Numerical Literals
Decimal Integer Literals:
-50
758
3654
Octal Integer Literals:
47
511
156
HexaDecimal Integer Literals:
52
74
3073
Floating Point Literals:
Fractional Form
27.5
0.95
18.0
-29.0
0.15
Exponent Form
750.0
-0.00225
75000.0
7500.0
Boolean Literals
True
False
Special Literal
None
>>>

========================================================================

Program-4: Example of Arithmetic Operators in Python.
#Arithmetic Operators with variables

a = 10
b = 4

print("The Initialized value of A is     = ", a);
print("The Initialized value of B is     = ", b);
print()
print("Addition Operator        : A + B  = ", a+b)
print("Subtraction Operator     : A - B  = ", a-b)
print("Multiplication Operator  : A * B  = ", a*b)
print("Division Operator        : A / B  = ", a/b)
print("Floor Division Operator  : A // B = ", a//b)
print("Modulus Operator         : A % B  = ", a%b)
print("Exponent Operator        : A ** B = ", a**b)

Output
>>>
The Initialized value of A is     =  10
The Initialized value of B is     =  4

Addition Operator        : A + B  =  14
Subtraction Operator     : A - B  =  6
Multiplication Operator  : A * B  =  40
Division Operator        : A / B  =  2.5
Floor Division Operator  : A // B =  2
Modulus Operator         : A % B  =  2
Exponent Operator        : A ** B =  10000
>>>

========================================================================

Program-5: Example of Bitwise Operators in Python.
#Bitwise Operators with variables

a = 7
b = 4
print("The Initialized value of A is     = ", a);
print("The Initialized value of B is     = ", b);
print()
print("Bitwise AND Operator     : A & B  = ", a&b)
print("Bitwise XOR Operator     : A ^ B  = ", a^b)
print("Bitwise OR  Operator     : A | B  = ", a|b)

print()
a = 14
b = 6
print("The Initialized value of A is     = ", a);
print("The Initialized value of B is     = ", b);
print()
print("Bitwise AND Operator     : A & B  = ", a&b)
print("Bitwise XOR Operator     : A ^ B  = ", a^b)
print("Bitwise OR  Operator     : A | B  = ", a|b)

Output
>>>
The Initialized value of A is     =  7
The Initialized value of B is     =  4

Bitwise AND Operator     : A & B  =  4
Bitwise XOR Operator     : A ^ B  =  3
Bitwise OR  Operator     : A | B  =  7

The Initialized value of A is     =  14
The Initialized value of B is     =  6

Bitwise AND Operator     : A & B  =  6
Bitwise XOR Operator     : A ^ B  =  8
Bitwise OR  Operator     : A | B  =  14
>>> 

========================================================================

Program-6: Example of Multi Line Comments and Various ways of Assignments
'''
Example of Multi Line Comments. In Python using three times single quotes will create the Multi Line comment. It is used for detailed additional information related tot he program. It helps to clarify the important things '''
#Multiple Ways for Assignment
# 1. Assigning Same value to multiple variables.
x = y = z = 500
print("X = ",x,": Y = ",y,": Z = ",z)

# 2. Assigning Multiple Values in Multiple Variables.
x,y,z = 100, 200, 300
print("X = ",x,": Y = ",y,": Z = ",z)

#3. Swapping of values of variables
print("Swapping of values of variables")
x, y = y, x
print("X = ",x,": Y = ",y)

Program-7: Example of Variable Declarations in Python.
# Variable Definition
num  = 150              # An integer assignment
mtrs = 15000.0          # A floating point
name = "Wilson Alfred"  # A string
print(num)
print(mtrs)
print(name)
print()
                        #Multiple Assignment
n, r, s = 10, 25.5, "Hello Python"
print(n)               # An integer number
print(r)               # A floating number
print(s)               # A String

Program-8: Example of Arithmetic Operators in Python.
#Arithmetic Operators with variables
a = 10
b = 4
print("The Initialized value of A is     = ", a);
print("The Initialized value of B is     = ", b);
print()
print("Addition Operator        : A + B  = ", a+b)
print("Subtraction Operator     : A - B  = ", a-b)
print("Multiplication Operator  : A * B  = ", a*b)
print("Division Operator        : A / B  = ", a/b)
print("Floor Division Operator  : A // B = ", a//b)
print("Modulus Operator         : A % B  = ", a%b)
print("Exponent Operator        : A ** B = ", a**b)

'''Example of Taking Inputs and
Printing the same as Outputs using
variables with proper presentation'''

name=input("What is your name ?")
age=input("What is your age ?")
school=input("In which school you study?")


print("Welcome      : ",name)
print("Your age is  : ",age)
print("You study in : ",school," school")

print("The Data type of name variable is : ",type(name))
print("The Data type of age variable is : ",type(age))
print("The Data type of school variable is : ",type(school))

Program-9: Example of variables with data types
'''Example of Taking Inputs and
Printing the same as Outputs using
variables with proper presentation'''
name=str(input("What is your name ?"))
age=int(input("What is your age ?"))
school=str(input("In which school you study?"))
fees=float(input("How much fees you pay?"))

print("Welcome      :",name)
print("Your age is  :",age)
print("You study in :",school," school")
print("Your Fees is :",fees)

print("The Data type of name variable is  :",type(name))
print("The Data type of age variable is   :",type(age))
print("The Data type of school variable is:",type(school))
print("The Data type of fees variable is  :",type(fees))

Program-10: Example of end character with variables and printing
name="Jonathan"
age=18
print("Hello",name)
print("Your age is",age,"Years")
print("Hello",name,end='@')
print("Your age is",age,"Years")

Program-11: Example of Seperator (sep) in Python
print("My","Name","is","Narendra")
print("My","Name","is","Narendra",sep="...")
print("My","Name","is","Narendra",sep="!!!")
sep='###'       #Not a way to use seperator
print("My","Name","is","Narendra")
Program-12: Input three numbers and print their sum
a=int(input("Enter 1st Number : "))
b=int(input("Enter 1st Number : "))
c=int(input("Enter 1st Number : "))
total=a+b+c;
print("The sum of",a,",",b,", and ",c,"is",total)



Program-13: Get Length & Breadth of Rectangle to print its Area

length=float(input("Enter Length:"))
breadth=float(input("Enter Breadth:"))
area=length*breadth
print("The Area of Rectangle with",length,"Length
and",breadth,"Breadth is",area)

Program-14: Program to calculate the BMI (Body Mass Index) of a person
            # BMI = kg/m2

weight=(float(input("Enter weight in kg :"))
height=(float(input("Enter height in mtr:"))
bmi=weight/(height*height)
print("BMI is : ",bmi)




2 comments:

  1. I started sharing programs on Python.
    Definitely you will get many programs of python as well.
    Keep Reading.
    God bless you.

    ReplyDelete

Post your comments

Powered by Blogger.