0% found this document useful (0 votes)
33 views

Python .g3g3

The document contains Python programs written by Gayathri MJ to demonstrate various programming concepts. These include programs to add two numbers, convert temperatures between Celsius and Fahrenheit, calculate the square root of a number, find the area of a triangle, solve quadratic equations, plot sine, ideal gas, van der Waals and projectile motion equations. Each program contains the code, inputs/outputs and a brief description of what the program is demonstrating.

Uploaded by

myran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Python .g3g3

The document contains Python programs written by Gayathri MJ to demonstrate various programming concepts. These include programs to add two numbers, convert temperatures between Celsius and Fahrenheit, calculate the square root of a number, find the area of a triangle, solve quadratic equations, plot sine, ideal gas, van der Waals and projectile motion equations. Each program contains the code, inputs/outputs and a brief description of what the program is demonstrating.

Uploaded by

myran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

PYTHON PROGRAMMES

By:- Gayathri MJ
Program that adds two numbers

# -*- coding: utf-8 -*-

"""

@author: Gayathri MJ

"""

# This Program Adds two numbers

num1 = float(input("Enter number one: "))

num2 = float(input("Enter number two: "))

sum = num1 + num2

print("Sum of {} and {} is {}".format(num1,num2,sum))

Output
Enter number one: 2

Enter number two: 3

Sum of 2 and 3 is 5.0


Program that convert Temperature
# -*- coding: utf-8 -*-

"""

@author: Gayathri MJ

"""

# This program converts temrature to celcius or fahrenheit

userInput = int(input("Do you want to convert \n (1)Celcius to farhenheit \n (2) Farhenheit to Celcius
\n Your Choice: "))

if userInput == 1:

celcius = float(input("Enter celcius temprature: "))

fahrenheit = (celcius*1.8) + 32

print("{} degree celcius is equal to {} degree farhenheit".format(celcius,fahrenheit))

elif userInput == 2:

fahrenheit = float(input("Enter fahrenheit temprature: "))

celcius = (fahrenheit -32)/1.8

print("{} degree farhenheit is equal to {} degree celcius".format(fahrenheit,celcius))

else:

print("Invalid Option")

Output
Do you want to convert

(1)Celcius to farhenheit

(2) Farhenheit to Celcius

Your Choice: 1

Enter celcius temprature: 36

36.0 degree celcius is equal to 96.8 degree farhenheit


Program that finds square root of a number
# -*- coding: utf-8 -*-

"""

@author: Gayathri MJ

"""

# Square root

import math

number = float(input("Enter the number : "))

print("Square root of " + str(number) + " is equal to " + str(math.sqrt(number)))

Output
Enter the number: 16

Square root of 16.0 = 4.0


Program that Finds area of a triangle
# -*- coding: utf-8 -*-

"""

@author: Gayathri MJ

"""

# Python program to find the area of a triangle

import math

a = float(input("Input a : "))

b = float(input("Input b : "))

c = float(input("Input c : "))

# calculate the semi perimeter

s = (a+b+c)/2

area = math.sqrt((s*(s-a)*(s-b)*(s-c)))

print("Area of the triange is equal to {}".format(area))

Output
Input a : 4

Input b : 5

Input c : 6

Area of the triangle is equal to 9.921567416492215


Program that solves quadratic equation
# -*- coding: utf-8 -*-

"""

@author: Gayathri MJ

"""

#Progarm to solve quadratic equation

import cmath #for complex numbers

a = float(input("Enter coefficient of x squared (a): "))

b = float(input("Enter coefficient of x (b): "))

c = float(input("Enter the constant (c): "))

discriminant = (b**2)-(4*a*c)

sol1 = (-b-cmath.sqrt(discriminant)/(2*a))

sol2 = (-b+cmath.sqrt(discriminant)/(2*a))

print("The solutions are {} and {}".format(sol1,sol2))

Output
Enter coefficient of x squared (a): 1

Enter coefficient of x (b): 5

Enter the constant (c): 6

The solutions are (-5.5+0j) and (-4.5+0j)


Program that plots sine function
# -*- coding: utf-8 -*-

"""

@author: Gayathri MJ

"""

#Plotting sine function

import numpy

import matplotlib.pyplot as plt

t = numpy.linspace(0, 10, 1000)

y = numpy.sin(t)

plt.plot(t,y)

plt.xlabel('time')

plt.ylabel('Amplitude')

plt.legend('Sint')

plt.title("Plot of sine function")

plt.show()

Output
Program that plots ideal gas equation

# -*- coding: utf-8 -*-

"""

@author: Gayathri MJ

"""

# Plotting Ideal gas Equation

import numpy

import matplotlib.pyplot as plt

T = 2.10505

R = 8.314

v = numpy.linspace(.1,1,1000)

p = R*T/v

plt.plot(v,p)

plt.xlabel("Volume")

plt.ylabel("Pressure")

plt.legend("P = RT/V")

plt.title("Plot of Ideal gas Equation")

plt.show()
Output
Program that plots Wander vaals equation
# -*- coding: utf-8 -*-

"""

@author: Gayathri MJ

"""

#Wander vaals equation

import numpy

import matplotlib.pyplot as plt

a = 5.489

b = 0.063

T = 3.10505

R = 8.314

v = numpy.linspace(.1,1,1000)

p = R*T/(v-b)-a/v**2

plt.plot(v,p)

plt.xlabel("Volume")

plt.ylabel("Pressure")

plt.legend("P = RT/(v-b) - a/v**2")

plt.title("Plot of wander vals Equation")

plt.show()
Output
Program that plots projectile motion
# -*- coding: utf-8 -*-

"""

@author: Gayathri MJ

"""

#Projectile motion

import numpy

import matplotlib.pyplot as plt

v0 = 5

g = 9.81

t = numpy.linspace(0,1,1000)

y = v0*t - 0.5*g*t**2

largest_height = y[0]

for i in range (1,1000):

if y[i] > largest_height:

largest_height = y[i]

print("The maximum height achieved was {} m".format(largest_height))

plt.plot(t,y)

plt.xlabel('Time (s)')

plt.ylabel('Height (m)')

plt.show()
Output

You might also like