Q 01 Write A Python Program To Check Whether A Given Number Enter by User Is Positive or Negative
Q 01 Write A Python Program To Check Whether A Given Number Enter by User Is Positive or Negative
positive or negative.
Code
a = float(input("Enter a number: "))
if a > 0:
print("Positive number")
else:
print("Negative number")
Output:
Q 02: Write a Python program to read the value of an integer m and display the value of n
is 1 when m is larger than 0, 0 when m is 0 and -1 when m is less than 0.
Code:
m = int(input("Please enter an integer:"))
if m > 0:
n=1
print('n=1')
elif m == 0:
print('n=0')
elif m < 0:
print('n=-1')
else:
print('invalid choice')
Output:
Q 03: Write a Python program to accept a coordinate point in a XY coordinate system and
determine in which quadrant the coordinate point lies.
Code
x, y = map(int, list(input("Insert the value for variable X and Y : ").split(" ")))
if x > 0 and y > 0:
print("point (", x, ",", y, ") lies in the First quadrant")
elif x < 0 and y > 0:
print("point (", x, ",", y, ") lies in the Second quadrant")
elif x < 0 and y < 0:
print("point (", x, ",", y, ") lies in the Third quadrant")
elif x > 0 and y < 0:
print("point (", x, ",", y, ") lies in the Fourth quadrant")
elif x == 0 and y == 0:
print("point (", x, ",", y, ") lies at the origin")
elif y == 0 and x != 0:
print("point (", x, ",", y, ") on x-axis")
elif x == 0 and y != 0:
print("point (", x, ",", y, ") on at y-axis")
Output:
Q 04: Write a program for the basic calculator. Take two numbers form the user and
perform the task based on the user choice in the available menu. After the choice the
system performs the specific task according to the user selection. Use the Switch and
Nested ElseIf Statement to perform the above task
Code
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
choice = input("Enter choice(1/2/3/4): ")
if choice in ('1', '2', '3', '4'):
x = float(input("Enter first number: "))
y = float(input("Enter second number: "))
if choice == '1':
print(x, "+", y, "=", x + y)
elif choice == '2':
print(x, "-", y, "=", x-y)
elif choice == '3':
print(x, "*", y, "=", x*y)
elif choice == '4':
print(x, "/", y, "=", x/y)
else:
print("Invalid Input")
Output:
Q 05: Write a Python program to find those numbers which are divisible by 7 and multiple
of 5, between 1500 and 2700 (both included).
Code:
lower=1500
upper=2700
for a in range (lower,upper):
if(a%7==0 and a%5==0):
print (a)
Output:
Output:
Q 09: Write a python code to print the following patterns based on user enter the row (any
number).
Code:A
rows = int(input("Enter number of rows"))
for i in range(0, rows):
for j in range(0, i + 1):
print("*", end=' ')
print("\r")
Output:
Code:B
rows = int(input("Enter number of rows"))
m = (2 * rows) - 2
for i in range(0, rows):
for j in range(0, m):
print(end=" ")
m=m-1
for j in range(0, i + 1):
print("* ", end=' ')
print(" ")
Output:
Code:C
n = int(input('Enter the value of n: '))
for i in range(1,2*n):
for j in range(1,2*n):
if i==j or i+j==2*n:
print('*', end='')
else:
print(' ', end='')
print()
Output:
Code:D
rows = int(input("Enter number of rows: "))
for x in range(rows):
print(" " * (rows - x), "*" * (2*x + 1))
for x in range(rows - 2, -1, -1):
print(" " * (rows - x), "*" * (2*x + 1))
Output: