Python Questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Name-Kartik Singh

Roll no-31
Problem Statement 1:
Write commands or scripts for creating, accessing, iterating, updating
and removing various
types in python mentioned below:
I. Number
Objective: To get basic idea about how numbers can be, updated and
removed in python.
Description: Three numeric types used to represent numbers
integer,float,complex numbers
Solution:
# three numeric data type to represent numbers integetr,float,complex
numbers
a=10
print(type(a))
print(a)
b=9.7
print(b)
print(type(b))
c=6+8j
print(type(c))
print(c)
#arithmetic operations
b1=int(b)
print(a+b1)
print(a**b1)
print(c*7)
print(float(a)*b)
OUTPUT:
<class 'int'>
10
Name-Kartik Singh
Roll no-31
9.7
<class 'float'>
<class 'complex'>
(6+8j)
19
1000000000
(42+56j)
97.0
II. String
Objective: To get basic idea about how strings can be created,
accessed, updated and removed in
python.
Description: String crated using double, triple quotes, finding the length
of string using length
function
Solution:
str1="This is python class"#single ,double,triple quotes can be used
str2='''multiline
string
span over multiple lines'''
print(str2)
# accessing string
print(str1)
#finding length of string
print("lenghth of string is",len(str1))
#String is an immutable sequence data type hence not possible to
update it
OUTPUT:
multiline
string
Name-Kartik Singh
Roll no-31
span over multiple lines
This is python class
length of string is 20
III. List
Objective: To get basic idea about how lists can be created,
accessed ,updated and removed in
python.
Description:[] denotes list, using for loop to access elements of list,
updating list using
append()and deleting elements inside list using pop(),remove() and del
Solution:
# creating list
list1=["Aditya","Aggarwal",4,9.0,[7,8]]
# accessing list
print(list1)
print(list1[3])
# iterating through the list
for i in list1:
print(i)
# updating and removing elements from the list
list1.append("BCA")
print(list1)
del list1[1]
list1.remove("BCA")
print(list1)
print(list1.pop())
print(list1)
del list1
print(list1)
OUTPUT:
Name-Kartik Singh
Roll no-31
['Aditya', 'Aggarwal', 4, 9.0, [7, 8]]
9.0
Aditya
Aggarwal
4
9.0
[7, 8]
['Aditya', 'Aggarwal', 4, 9.0, [7, 8], 'BCA']
['Aditya', 4, 9.0, [7, 8]]
[7, 8]
['Aditya', 4, 9.0]
Traceback (most recent call last):
File "d:\bca2\code_1.py", line 61, in <module>
print(list1)
NameError: name 'list1' is not defined
IV. Tuple
Objective: To get basic idea about how tuples can be created,
accessed ,updated and removed in
python.
Description: Creating tuple, accessing tuple elements using [] ,deleting
tuple using del
Solution:
# creating tuple
t1=("Aditya","Neha","Kanika","Riya",1,2,3,4.7)
print("tuple 1",t1)
print(t1[0])
print(t1[-1])
# tuples cannot be modified as it is immutable ordered collection of
data items
del t1
Name-Kartik Singh
Roll no-31
print(t1)
OUTPUT:
tuple 1 ('Aditya', 'Neha', 'Kanika', 'Riya', 1, 2, 3, 4.7)
Aditya
4.7
Traceback (most recent call last):
File "d:\bca2\code_1.py", line 51, in <module>
print(t1)
NameError: name 't1' is not defined
V. Set
Objective: To get basic idea about how set can be created,
accessed ,updated and removed in
python.
Description: Creating set using set(),modifying set using add() and
update()and removing
elements from set using remove function
Solution:
# creating set
s1=set("hello")
print("set1",s1)
#modifying set
s1.add(10)
s1.add('w')
s1.remove('h')
print(s1)
s2=set("python")
print("set2",s2)
s1.update(s2)#updating with another set
# set operations
print(s1.union(s2))
Name-Kartik Singh
Roll no-31
print(s1.intersection(s2))
print(s1.difference(s2))
print(s1.symmetric_difference(s2))
OUTPUT:
set1 {'l', 'h', 'o', 'e'}
{'o', 10, 'w', 'e', 'l'}
set2 {'t', 'o', 'p', 'y', 'n', 'h'}
{'t', 'o', 10, 'p', 'y', 'n', 'h', 'w', 'e', 'l'}
{'t', 'o', 'p', 'n', 'y', 'h'}
{10, 'l', 'w', 'e'}
{'w', 'e', 10, 'l'}
VI. Dictionary
Objective: To get basic idea about how dictionary can be created,
accessed, updated and
removed in python.
Description: Creating dictionary, accessing the key: value pairs using for
loop, get(), deletion
using pop(),popitem(),del
CODE:
# creating dictionary
dict1={1:"Aditya",2:"Neha",3:"Kanika",4:"Riya",5:1,6:3,7:4.7}
print("dictionary 1",dict1)
print(dict1[1])
print(dict1.get(2))
for key in dict1:
print("key",key,"values",dict1[key])
#updating dictionary
dict1[5]="Trip"
print(dict1)
del dict1[5]
Name-Kartik Singh
Roll no-31
print(dict1.pop(7))
print(dict1)
print(dict1.popitem())
print(dict1)
print(dict1.keys())
print(dict1.values())
OUTPUT:
dictionary 1 {1: 'Aditya', 2: 'Neha', 3: 'Kanika', 4: 'Riya', 5: 1, 6: 3, 7: 4.7}
Aditya
Neha
key 1 values Aditya
key 2 values Neha
key 3 values Kanika
key 4 values Riya
key 5 values 1
key 6 values 3
key 7 values 4.7
{1: 'Aditya', 2: 'Neha', 3: 'Kanika', 4: 'Riya', 5: 'Trip', 6: 3, 7: 4.7}
4.7
{1: 'Aditya', 2: 'Neha', 3: 'Kanika', 4: 'Riya', 6: 3}
(6, 3)
{1: 'Aditya', 2: 'Neha', 3: 'Kanika', 4: 'Riya'}
dict_keys([1, 2, 3, 4])
dict_values(['Aditya', 'Neha', 'Kanika', 'Riya'])
Name-Kartik Singh
Roll no-31
Problem Statement 2:
Write a program to check whether a person is eligible for voting or
not. Input will be taken by user.
Objective: Got a Basic Idea about the use of IF-ELSE
Description: Age taken from user as an Input then using the IF-ELSE
statement checked weather the age is greater than 18 or not. If yes
then he/she is eligible to vote else not.

Solution:
age = int(input("Enter your age: "))
if(age>=18):
print("Eligible")
else:
print("Not eligible")

Output:
Enter your age: 18
Eligible
Enter your age: 16
Not eligible
Name-Kartik Singh
Roll no-31
Problem Statement 3:
Write a program to check whether a number is even or odd. Input will
be taken by the user.
Objective: Get to know about how to verify certain conditions inside IF-
ELSE.
Description: Use of if-else. If the condition is true the statements in the
if block get executed, if the condition is false the statements in the else
block get executed

Solution:
num = int(input("Enter number to be checked:"))
if(num%2)==0:
print("Even")
else:
print("Odd")

Output:
Enter number to be checked: 7
Odd
Enter number to be checked: 2
Even
Name-Kartik Singh
Roll no-31
Problem Statement 4:
Write a program to check whether an year is leap year or not input
will be taken by user
Objective: Got a Basic Idea about the use of nested IF-ELSE
Description: Year taken from user as an Input then using the nested IF-
ELSE statement certain conditions are Checked to verify if the year is
leap or not.

Solution:
def CheckLeap(Year):
if((Year % 400 == 0) or (Year % 100 != 0) and (Year % 4 == 0)):
print("Given Year is a leap Year");
else:
print ("Given Year is not a leap Year")
Year = int(input("Enter the number: "))
CheckLeap(Year)

Output:
Enter the number: 2023
Given Year is not a leap Year
Enter the number: 2024
Given Year is a leap Year
Name-Kartik Singh
Roll no-31

Problem Statement 5:
Write a program to accept the cost price of a mobile and display GST
to be paid according to the following criteria:
Cost Price in Rs. GST
>100000 18%
>50000 and <=100000 15%
<50000 10%

Objective: To calculate the gst based on the cost price of the mobile
Description: A menu driven program using if-elif(if –else-if) ladder.
Solution:
CP = int (input("Enter cost price") )
if CP>100000:
GST=(18/100)*CP
if 50000<CP<=100000:
GST=(15/100)*CP
if CP<50000:
GST=(10/100)*CP
print (GST)

Output:
Enter cost price: 700000
126000.0
Name-Kartik Singh
Roll no-31
Problem Statement 6:
Write a Program to check whether the last digit of a number is
divisible by 7 or not input will be taken by user.
Objective: Find out how access the last digit of a number
Description: Number is taken as the input from user then mod the
number by to obtain its last digit then checked if it is divisible by 7 or
not.
Solution:
num = int (input("Enter the number: ") )
n= num%10
if int(n)%7==0:
print("Divisible")
else:
print("Not Divisible")

Output:
Enter the number: 244
Not Divisible
Enter the number: 777
Divisible
Name-Kartik Singh
Roll no-31
Problem Statement 7:
WAP to print first 10 integers and their squares.
Objective: Got a Basic Idea about the use of for loop
Description: Started a for loop from 1 to 10 and printed the number
with their squares.
Solution:
print("no. Square")
for x in range(10):
print(x,x*x)

Output:
no. Square
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
Name-Kartik Singh
Roll no-31
Problem Statement 8:
WAP to print the table of the number entered by the user.
Objective: Got a basic Idea about the use of for loop on user entered
inputs
Description: Number taken from user as an input and printed the table
of that number using the for loop.
Solution:
num=int(input("enter Number"))
for i in range(1, 11):
print(num, 'x', i, '=', num*i)

Output:
enter Number 7
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
Name-Kartik Singh
Roll no-31
7 x 10 = 70
Problem Statement 9:
WAP to print even numbers falls between two numbers these two
numbers will be taken from user.
Objective: Got a Basic Idea about the use of for loop under various
conditions.
Description: Number is taken from user as an input and between those
two numbers even numbers are printed using the while loop and IF
statement
Solution:
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
for num in range(start, end + 1):
if num % 2 == 0:
print(num)

Output:
Enter the start of range: 2
Enter the end of range: 7
246
Name-Kartik Singh
Roll no-31
Problem Statement 10:
WAP to check whether the number is prime or not.
Objective: Got a Basic Idea about the use of IF-ELSE with loops.
Description: Number taken from user as an input and checked if it has
only 2 factors or not. If yes then it is Prime Number else not Prime
Solution:
num = int(input("Enter a number: "))
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")

Output:
Enter a number: 7
7 is a prime number
Enter a number: 2
2 is a prime number
Enter a number: 4
4 is not a prime number
Name-Kartik Singh
Roll no-31
Name-Kartik Singh
Roll no-31
Problem Statement 11:
WAP to reverse the number inputted by user
Objective: Got a Basic Idea about the reversing a number.
Description: Number is taken from user as a input and one by store its
last digit in the string
Solution:
num = int(input("Enter a number: "))
print(str(num)[ : :-1])

Output:
Enter a number: 7224
4227
Name-Kartik Singh
Roll no-31
Problem Statement 12:
WAP to find Fibonacci, Factorial, Palindrome and Armstrong of
inputted number.
Fibonacci:
n = int(input("enter number: "))
num1 = 0
num2 = 1
num3 = 1
for i in range(0,n):
print(num1)
num1 = num2
num2 = num3
num3 = num1 + num2

Output:
enter number: 9
0
1
1
2
3
5
Name-Kartik Singh
Roll no-31
8
13
21
Factorial:
n = int(input("enter number: "))
fact = 1
for i in range(1, n+1):
fact = fact * i
print("The factorial of n is : ", end="")
print(fact)
Output:
enter number: 7
The factorial of n is : 5040

Palindrome:
num = int(input("Enter a number: "))
a=(str(num)[ : :-1])
if int(a)==num:
print("Yes it is a palindrome")
else:
print("No it is not a palindrome")
Output:
Name-Kartik Singh
Roll no-31
Enter a number: 15151
Yes it is a palindrome
Enter a number: 1515
No it is not a palindrome

Armstrong:
Objective: Got to know about finding if a Number is Armstrong or not
using basics(loops,if-else).
Description: Number taken from user as a input and checked if the sum
of cubes of its digits is equals to the original number. If yes then the
Number is Armstrong Numbers else not.

number=int(input("enter the number"))


temp=0
rem=0
ams=number
while number>0:
temp=number%10
rem=(temp*temp*temp)+rem
number=number//10
if ams==rem:
print("Amstrong")
else:
Name-Kartik Singh
Roll no-31
print("Not amstrong")
Output:
enter the number: 153
Amstrong

You might also like