0% found this document useful (0 votes)
24 views4 pages

'Enter First Side: ' 'Enter Second Side: ' 'Enter Third Side: '

The document contains code snippets for several Python programs: 1) A program to calculate the area of a triangle given its three sides. 2) A program to swap the values of two variables. 3) A program to generate a random number between 0 and 9. 4) A program to print the Fibonacci sequence up to a number of terms input by the user. 5) A program to sort and print the words in a string alphabetically. 6) A program to convert a temperature from Celsius to Fahrenheit.

Uploaded by

nikhil khanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

'Enter First Side: ' 'Enter Second Side: ' 'Enter Third Side: '

The document contains code snippets for several Python programs: 1) A program to calculate the area of a triangle given its three sides. 2) A program to swap the values of two variables. 3) A program to generate a random number between 0 and 9. 4) A program to print the Fibonacci sequence up to a number of terms input by the user. 5) A program to sort and print the words in a string alphabetically. 6) A program to convert a temperature from Celsius to Fahrenheit.

Uploaded by

nikhil khanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

# Python Program to fi nd the area of triangle

# Three sides of the triangle a, b and c are provided by the user


a = fl oat(input('Enter fi rst side: '))
b = fl oat(input('Enter second side: '))
c = fl oat(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)

===========================================
=================
# Python program to swap two variables provided by the user
x = input('Enter value of x: ')
y = input('Enter value of y: ')
# create a temporary variable and swap the values
temp = x
x = y
y = temp
print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))

Program to generate a random number between 0 and 9


# import the random module
import random
print(random.randint(0,9))

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

# Program to display the Fibonacci sequence up to n-th term where n is


provided by the user

# take input from the user


nterms = int(input("How many terms? "))
# fi rst two terms
n1 = 0
n2 = 1
count = 2
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence:")
print(n1)
else:
print("Fibonacci sequence:")
print(n1,",",n2,end=', ')
while count < nterms:

nth = n1 + n2
print(nth,end=' , ')
# update values
n1 = n2
n2 = nth
count += 1

Program to sort alphabetically the words form a string provided by the


user
# take input from the user
my_str = input("Enter a string: ")
# breakdown the string into a list of words
words = my_str.split()
# sort the list
words.sort()
# display the sorted words
for word in words:
print(word)

Python Program to convert temperature in celsius to fahrenheit


# Input is provided by the user in degree celsius
# take input from the user

celsius = fl oat(input('Enter degree Celsius: '))


# calculate fahrenheit
fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %
(celsius,fahrenheit))

You might also like