Wednesday, July 6, 2022

Python Assignment 1

  Python Assignment 1- Python Basics and IDE, Simple Python Programs

Lab Assignments

 SET A 


 1. Python Program to Calculate the Area of a Triangle

# Three sides of the triangle is a, b and c:  

a = float(input('Enter first side: '))  

b = float(input('Enter second side: '))  

c = float(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)   


2. Python Program to Swap Two Variables

# Python program to swap two variables

x = 5

y = 10

# To take inputs from 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))


3. Python Program to Generate a Random Number

# Program to generate a random number between 0 and 9

# importing the random module

import random

print(random.randint(0,9))


SET B

1. Write a Python Program to Check if a Number is Positive, Negative or Zero

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

if num > 0:

   print("It is positive number")

elif num == 0:

   print("It is Zero")

else:

   print("It is a negative number")


OR

n = float(input('Input a number: '))

print('Number is Positive.' if n > 0 else 'It is Zero!' if n == 0 else 'Number is Negative.')

   

2. Write a Python Program to Check if a Number is Odd or Even

num = int(input("Enter a number: "))

if (num % 2) == 0:

   print("{0} is Even".format(num))

else:

   print("{0} is Odd".format(num))


3. Write a Python Program to Check Prime Number

# Program to check if a number is prime or not

num = 29

# To take input from the user

#num = int(input("Enter a number: "))

# define a flag variable

flag = False

# prime numbers are greater than 1

if num > 1:

    # check for factors

    for i in range(2, num):

        if (num % i) == 0:

            # if factor is found, set flag to True

            flag = True

            # break out of loop

            break

# check if flag is True

if flag:

    print(num, "is not a prime number")

else:

    print(num, "is a prime number")


4. Write a Python Program to Check Armstrong Number

# Python program to check if the number is an Armstrong number or not

# take input from the user

num = int(input("Enter a number: "))

# initialize sum

sum = 0

# find the sum of the cube of each digit

temp = num

while temp > 0:

   digit = temp % 10

   sum += digit ** 3

   temp //= 10

# display the result

if num == sum:

   print(num,"is an Armstrong number")

else:

   print(num,"is not an Armstrong number")


5. Write a Python Program to Find the Factorial of a Number

num = int(input("Enter a number: "))    

factorial = 1    

if num < 0:    

   print(" Factorial does not exist for negative numbers")    

elif num == 0:    

   print("The factorial of 0 is 1")    

else:    

   for i in range(1,num + 1):    

       factorial = factorial*i    

   print("The factorial of",num,"is",factorial)    


PROGRAMS FOR PRACTICE:

1. Python Program to Convert Kilometers to Miles

kilometers = float(input("Enter value in kilometers: "))

conv_fac = 0.621371

miles = kilometers * conv_fac

print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))


2. Python Program to Convert Celsius To Fahrenheit

celsius_1 = float(input("Temperature value in degree Celsius: " ))  

Fahrenheit_1 = (celsius_1 * 1.8) + 32  

print('The %.2f degree Celsius is equal to: %.2f Fahrenheit'  

      %(celsius_1, Fahrenheit_1))  

  print("----OR----")  

celsius_2 = float (input("Temperature value in degree Celsius: " ))  

Fahrenheit_2 = (celsius_2 * 9/5) + 32  

print ('The %.2f degree Celsius is equal to: %.2f Fahrenheit'  

      %(celsius_2, Fahrenheit_2))  


3. Write a Python Program to Check Leap Year

year = 2000

if (year % 400 == 0) and (year % 100 == 0):

    print("{0} is a leap year".format(year))

elif (year % 4 ==0) and (year % 100 != 0):

    print("{0} is a leap year".format(year))

else:

    print("{0} is not a leap year".format(year))


4. Write a Python Program to Print all Prime Numbers in an Interval

lower = 900

upper = 1000

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):

   # all prime numbers are greater than 1

   if num > 1:

       for i in range(2, num):

           if (num % i) == 0:

               break

       else:

           print(num)

5. Write a Python Program to Print the Fibonacci sequence

n_terms = int(input ("How many terms the user wants to print? "))  

n_1 = 0  

n_2 = 1  

count = 0  

if n_terms <= 0:  

    print ("Please enter a positive integer, the given number is not valid")  

elif n_terms == 1:  

    print ("The Fibonacci sequence of the numbers up to", n_terms, ": ")  

    print(n_1)  

else:  

    print ("The fibonacci sequence of the numbers is:")  

    while count < n_terms:  

        print(n_1)  

        nth = n_1 + n_2  

        n_1 = n_2  

        n_2 = nth  

        count += 1  


6. Write a Python Program to Find Armstrong Number in an Interval

lower = int(input("Enter lower range: "))  

upper = int(input("Enter upper range: "))  

  for num in range(lower,upper + 1):  

   sum = 0  

   temp = num  

   while temp > 0:  

       digit = temp % 10  

       sum += digit ** 3  

       temp //= 10  

       if num == sum:  

            print(num)  

7. Write a Python Program to Find the Sum of Natural Numbers

num = int(input("Enter a number: "))  

  if num < 0:  

   print("Enter a positive number")  

else:  

   sum = 0  

   # use while loop to iterate un till zero  

   while(num > 0):  

       sum += num  

       num -= 1  

   print("The sum is",sum)  

No comments:

Post a Comment