3907732-Assignment Answers
3907732-Assignment Answers
2: Python Basics
Topics Covered:
• Syntax and Semantics
• Variables and Data Types
• Basic Operators (Arithmetic, Comparison, Logical)
Hello World!
Question 2: Write a Python program that takes a user input and prints it.
print(x)
print(y)
print(z)
print(a)
print(type(x))
print(type(y))
print(type(z))
print(type(a))
# Your code
temp=float(input("enter the temparature:"))
def cel_to_fahren(temp):
x=(temp*9/5)+32
print("tempareture after converting to fahrenheit:",x)
cel_to_fahren(temp)
Question 10: Write a Python program to check if a variable is of a specific data type.
sum=num1+num2
print(sum)
diffrence=num1-num2
print(diffrence)
product=num1*num2
print(product)
division=num1/num2
print(division)
remainder=num1%num2
print(remainder)
floordiv=num1//num2
print(floordiv)
Question 12: Write a Python program to demonstrate comparison operators: equal to, not equal
to, greater than, less than.
print(p<q)
print(p>q)
print(p<=q)
print(p>=q)
print(p==q)
print(p!=q)
Question 13: Write a Python program to demonstrate logical operators: and, or, not.
False
True
False
the sqare of 25 is :
625
Question 16: Write a Python program to find the sum of the first n natural numbers.
if x.isdigit():
x=int(x)
if((x % 400==0) or(x % 4==0 and x % 100!=0)):
print('{0} is a leap year'.format(x))
else:
print('{0} is not a leap year'.format(x))
else:
print('invalid year')
Question 20: Write a Python program to sort a list of numbers in ascending order.
the sorted list: [1, 1, 3, 49, 74, 6363, 7283, 7382, 8293, 9292,
72772, 82827, 82828]