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

3907732-Assignment Answers

The document covers Python basics, including syntax, variables, data types, and basic operators. It provides a series of programming exercises with sample code for tasks such as printing strings, checking numbers, and performing arithmetic operations. Each section includes questions that guide users through fundamental programming concepts in Python.

Uploaded by

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

3907732-Assignment Answers

The document covers Python basics, including syntax, variables, data types, and basic operators. It provides a series of programming exercises with sample code for tasks such as printing strings, checking numbers, and performing arithmetic operations. Each section includes questions that guide users through fundamental programming concepts in Python.

Uploaded by

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

Lesson 1.

2: Python Basics
Topics Covered:
• Syntax and Semantics
• Variables and Data Types
• Basic Operators (Arithmetic, Comparison, Logical)

1. Syntax and Semantics


Question 1: Write a Python program to print "Hello, World!".

# Your code here


print('Hello World!')

Hello World!

Question 2: Write a Python program that takes a user input and prints it.

# Your code here


x=input("enter a sting:")
print(x)

Question 3: Write a Python program to check if a number is positive, negative, or zero.

# Your code here


num1=float(input("enter a number:"))
if (num1>0):
print("entered number is positive number")
elif(num1<0):
print("the entered number is positive number")
else:
print("entered number is zero")

entered number is positive number

Question 4: Write a Python program to find the largest of three numbers.

# Your code here


n1=float(input("enter 1st number:"))
n2=float(input("enter 2nd number:"))
n3=float(input("enter 3rd number:"))
if(n1>n2 and n1>n3):
print(n1,"is greater among three numbers")
elif(n2>n1 and n2>n3):
print(n2,"is greater among three numbers")
else:
print(n3,"is greater among three numbers")

Question 5: Write a Python program to calculate the factorial of a number.

# Your code here


x=int(input("enter the number to fing factorial:"))
def fact(x):
if (x==1 or x==0):
return 1
else:
return x*fact(x-1)
fact(x)

2. Variables and Data Types


Question 6: Create variables of different data types: integer, float, string, and boolean. Print
their values and types.

# Your code here


x=10
y=20.0
z="string"
a=True

print(x)
print(y)
print(z)
print(a)

print(type(x))
print(type(y))
print(type(z))
print(type(a))

Question 7: Write a Python program to swap the values of two variables.

# Your code here


def swap(a,b):
print("before swaping a={0} and b={1}".format(a,b))
temp=a
a=b
b=temp
print("after swapping a={0} and b={1}".format(a,b))
swap(10,20)
Question 8: Write a Python program to convert Celsius to Fahrenheit.

# 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 9: Write a Python program to concatenate two strings.

# Your code here


str1=input("enter string 1:")
str2=input("enter 2nd string:")
print("resultant string is:",str1+str2)

Question 10: Write a Python program to check if a variable is of a specific data type.

# Your code here


x=input("enter a value:")
def check(x):
if(type(x==int) or type(x==float) or type(x==bool) or
type(x==str)):
print("the variable is of specific data type")
else:
print("not a specific type")
check(x)

The variable is of a specific data type

3. Basic Operators (Arithmetic, Comparison, Logical)


Question 11: Write a Python program to perform arithmetic operations: addition, subtraction,
multiplication, and division.

# Your code here


#arithmetic operator and simple calculator
num1=float(input("enter first number:"))
num2=float(input("enter second number:"))

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.

# Your code here


p=5
q=4

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.

# Your code here


a=True
b=False
print(a and b)
print(a or b)
print(not a)

False
True
False

Question 14: Write a Python program to calculate the square of a number.

# Your code here


x=int((input("enter the number to find the sqare:")))
def sqr(x):
r=x*x
print("the sqare of {0} is :".format(x))
return r
sqr(x)

the sqare of 25 is :

625

Question 15: Write a Python program to check if a number is even or odd.


# Your code here
x=float(input("enter the number to check whether it is odd or even:"))
def evenodd(x):
if(x%2==0):
print("the entered number is even")
else:
print("the entere dnumber is not even")
evenodd(x)

Question 16: Write a Python program to find the sum of the first n natural numbers.

# Your code here


x=int(input("enter the number to find sum:"))
r=(x*(x+1))/2
print('the sum of',x,'natural numbers is',r)

the sum of 5 natural numbers is 15.0

Question 17: Write a Python program to check if a year is a leap year.

# Your code here


x=input('enter the year to check for leap year:').strip()

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')

2005 is not a leap year

Question 18: Write a Python program to reverse a string.

# Your code here


input_str=input("enter a string:")
print("the reverse of string is :",input_str[-1::-1])

the reverse of string is : gnirts

Question 19: Write a Python program to check if a string is a palindrome.

# Your code here


input_str=input("enter the string to check whether it is palindrome or
not:")
rev_str=input_str[-1::-1]
if(input_str==rev_str):
print("the input string is palindrome")
else:
print("the input sring is not palindrome")

the input string is palindrome

Question 20: Write a Python program to sort a list of numbers in ascending order.

# Your code here


input_int=(input("enter the list of numbers to sort:"))
x=list(map(int,input_int.split()))
print('the sorted list:',sorted(x))

the sorted list: [1, 1, 3, 49, 74, 6363, 7283, 7382, 8293, 9292,
72772, 82827, 82828]

You might also like