Article 1: Basics Of Python
In this article, we are going to see some basic python programming examples which you can see as below:
Problem Statement 1: Write a Python program to swap two numbers
# Lab Assignment 1.1
# Write a Python program to swap two numbers.
#Taken input from User
a=int(input("Enter the first number:"))
b=int(input("Enter the second number:"))
print("Before Swapping:")
print(a)
print(b)
#swapping Operation
a,b=b,a
print("After Swapping")
print(a)
print(b)
Problem Statement 2: Write a Python program to find the greatest of 3 numbers.
Program:
#Lab
Assignment 1.2
# Write a
Python program to find the greatest of 3 numbers.
#taking
input from users
x=int(input("Enter
the first number:"))
y=int(input("Enter
the second number:"))
z=int(input("Enter
the third number:"))
#using if
and elif for finding largest number among entered number
if (x>y)
and (x>z):
print("The greatest number: "+x)
elif
(y>x) and (y>z):
print(" The greatest number: "+y)
else:
print(" The greatest number: "+z)
Problem Statement 3: Write
a Python program to convert days entered into months and days
Input: 1256
Program:
#Lab
Assignment 1.3
#Write a
Python program to convert days entered into months and days.
#taking
total number of days as input from user
days_enter=int(input("Enter
number of days you want to convert into month: "))
#for
example: days_enter=45
month=int(days_enter/30)
#Month is
equal 45/30 which is 1
day=int(days_enter
% 30)
#day is
equal to 45%30 which is 15
print("Number
of days entered by user: ",days_enter)
print("----------------------------------------")
print("Number
of Month: ",month)
print("Number of day: ",day)
Problem Statement 4: Write a Python program to find the roots of a Quadratic equation
Program:
#Lab
Assignment 1.4
#Write a
Python program to find the roots of a Quadratic equation.
#
importing math library from installed libraries
import math
#taking
value of a,b,c in quadratic equation
a=int(input("Enter
the value for A:"))
b=int(input("Enter
the value for B:"))
c=int(input("Enter
the value for C:"))
# using
formula of d
d=((b*b)-(4*a*c))
#using
if-elif for checking of roots from value of d
if (d>0):
print("First Root:",(((-b)
+math.sqrt(d))/(2*a)))
print("Second Root:",((-b
-math.sqrt(d))/(2*a)))
elif
(d<0):
print("Real Root does not exist")
else:
print("Root :",((-b)/(2*a)))
Problem Statement 5: Write a Python program to evaluate
the power function using while loop.
Program:
#Lab
Assignment 1.5
#Write a
Python program to evaluate the power function using while loop.
# taking
value of base and exponent from user
base =
int(input(" Please Enter any Positive Integer : "))
exponent =
int(input(" Please Enter Exponent Value : "))
#checking
if the value of exponent is not negative
if
(exponent>0):
answer=base
elif
(exponent==0):
answer=1
#calculating the answer
while(exponent
> 1) :
answer=answer * base
exponent=exponent-1
print("Answer is:",answer)
Problem Statement 6:
Write a Python program to evaluate the factorial function using while loop.
Program:
#Lab Assignment 1.6
#Write a Python program to evaluate the factorial function using
while loop.
#Taking the number for which user want to find factorial
number=int(input("Enter the value for which you want to get
factorial value:"))
#Determining the value of fact and running while loop for
calculation
fact=1
i=1
while (i<=number):
fact=fact*i
i=i+1
# Example: number is 3. fact is 1. the while loop will run from,
i=1 to i=3 in which fact will be multiplying with i and i will be incrementing
1
print("factorial of ",number,"is",fact)
Problem Statement 7: Write a Python program to evaluate the sum of integers between a and b entered by the user that is divisible by 3.
Program:
#Lab Assignment
1.7
#Write a Python
program to evaluate the sum of integers between a and b entered by the user
that is divisible by 3.
#taking the lower
bounds and upper bounds for given operation.
number1=int(input("Enter the
first number for range: "))
number2=int(input("Enter the
second number for range: "))
sum=0
# running for
loop from upper to lower bound
for i in range(number1,number2):
if((i%3)==0):
sum=sum+i
# example:
# num1 is 5 and
num2 is 10.
# in this range
only 6 and 9 are integers which are divisible by 3
# so the answer
would be summation of those integers. which is 15
print("-------------------")
print("The sum of integer which
are divisible by 3 between ",number1," and ",number2," is
",sum)
Comments
Post a Comment