0% found this document useful (0 votes)
9 views

IP Python Programs

The document contains a series of Python programming questions and their corresponding answers, covering various topics such as loops, conditionals, string manipulation, and data structures. Each question is followed by a code snippet that demonstrates the solution. The questions range from printing patterns and calculating sums to checking for palindromes and validating passwords.

Uploaded by

Sarita Saini
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

IP Python Programs

The document contains a series of Python programming questions and their corresponding answers, covering various topics such as loops, conditionals, string manipulation, and data structures. Each question is followed by a code snippet that demonstrates the solution. The questions range from printing patterns and calculating sums to checking for palindromes and validating passwords.

Uploaded by

Sarita Saini
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Question 1: Print the first 10 natural numbers using for loop.

Answer :
for i in range(1,11):
print(i)

Question 2: Python program to print all the even numbers within the given
range.
Answer :
given_range = 10
for i in range(given_range):
if i%2==0:
print(i)

Question 3: Python program to calculate the sum of all numbers from 1 to a


given number.
Answer :
given_number = 10
sum = 0
for i in range(1,given_number+1):
sum+=i
print(sum)

Question 4: Python program to calculate the sum of all the odd numbers within
the given range.
Answer :
given_range = 10
sum = 0
for i in range(given_range):
if i%2!=0:
sum+=i
print(sum)

Question 5: Python program to print a multiplication table of a given number


Answer :
given_number = 5
for i in range(11):
print (given_number," x",i," =",5*i)

Question 6: Python program to display numbers from a list using a for loop.
Answer :
list = [1,2,4,6,88,125]
for i in list:
print(i)

Question 7: Python program to count the total number of digits in a number.


Answer :
given_number = 129475
given_number = str(given_number)
count=0
for i in given_number:
count += 1
print(count)

Question 8: Python program to check if the given string is a palindrome.


Answer :
given_string = "madam"
reverse_string = ""
for i in given_string:
reverse_string = i + reverse_string
if(given_string == reverse_string):
print("The string", given_string,"is a Palindrome.")
else:
print("The string",given_string,"is NOT a Palindrome.")

Question 9: Python program that accepts a word from the user and reverses it.
Answer :
given_string = input()
reverse_string = ""
for i in given_string:
reverse_string = i + reverse_string
print(reverse_string)

Question 10: Python program to check if a given number is an Armstrong number


Answer :
given_number = 153
given_number = str(given_number)
string_length = len(given_number)
sum = 0
for i in given_number:
sum += int(i)**string_length
if sum == int(given_number):
print("The given number",given_number,"is an Amstrong number.")
else:
print("The given number",given_number,"is Not an Amstrong number.")

Question 11: Python program to count the number of even and odd numbers from a
series of numbers.
Answer :
num_list = [1,3,5,6,99,134,55]
for i in num_list:
if i%2==0:
print(i,"is an even number.")
else:
print(i,"is an odd number.")

Question 12: Python program to display all numbers within a range except the
prime numbers.
Answer :
import math
def is_not_prime(n):
flag = False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
flag = True
return flag
range_starts = 10
range_ends = 30
print("Non-prime numbers between",range_starts,"and", range_ends,"are:")
for number in filter(is_not_prime, range(range_starts, range_ends)):
print(number)

Question 13: Python program to get the Fibonacci series between 0 to 50.
Answer :
num = 50
first_value,second_value = 0, 1
for n in range(0, num):
if(n <= 1):
next = n
if nextnum:
break
print(next)
Question 14: Python program to find the factorial of a given number.
Answer :
given_number= 5
factorial = 1
for i in range(1, given_number + 1):
factorial = factorial * i
print("The factorial of ", given_number, " is ", factorial)

Question 15: Python program that accepts a string and calculates the number of
digits and letters.
Answer :
user_input = input()
digits = 0
letters = 0
for i in user_input:
if i.isdigit():
digits=digits+1
elif i.isalpha():
letters=letters+1
print(" The input string",user_input, "has", letters, "letters and",
digits,"digits.")

Question 16: Write a Python program that iterates the integers from 1 to 25.
Answer :
given_range = 25
for i in range(given_range+1):
if i % 4 == 0 and i % 5 == 0:
print("fizzbuzz")
continue
if i % 4 == 0 and i%5!=0:
print("fizz")
continue
if i % 5 == 0 and i % 4!= 0:
print("buzz")
else:
print(i)

Question 17: Python program to check the validity of password input by users.
Answer :
password = input()
has_valid_length = False
has_lower_case = False
has_upper_case = False
has_digits = False
has_special_characters = False
if (len(password) >= 8) and (len(password)<=16):
has_valid_length = True
for i in password:
if (i.islower()):
has_lower_case = True
if (i.isupper()):
has_upper_case = True
if (i.isdigit()):
has_digits = True
if(i=="@" or i=="$" or i=="_"or i=="#" or i=="^" or i=="&" or i=="*"):
has_special_characters = True
if (has_valid_length==True and has_lower_case ==True and has_upper_case == True
and has_digits == True and has_special_characters == True):
print("Valid Password")
else:
print("Invalid Password")

Question 18: Python program to convert the month name to a number of days.
# given list of month name
month = ["January", "April", "August","June","Dovember"]
Answer :
for i in month:
if i == "February":
print("The month of February has 28/29 days")
elif i in ("April", "June", "September", "November"):
print("The month of",i,"has 30 days.")
elif i in ("January", "March", "May", "July", "August", "October",
"December"):
print("The month of",i,"has 31 days.")
else:
print(i,"is not a valid month name.")

Question 19 : Create a function named get_results_range(result_dict) that


accepts the following dictionary and returns the difference between the best
and worst exam results.
exam_results = {
'Dominic Vargas': 67,
'Marion Riley': 89,
'Candice White': 45,
'Doreen Goodwin': 82,
'Janet Hunter': 98,
'Jaime Page': 32,
'Neil Fernandez': 76,
'Jana Frank': 28,
'Adrienne Perkins': 98,
'Rosa Mccarthy': 34
}
Answer :
def get_results_range(result_dict):
max = 0
min = 100
for v in result_dict.values():
if v > max:
max = v
if v < min:
min = v
return max – min

Question 20 : write python code to print given pattern


*
**
***
****
*****
Answer :
(i)
star='*'
for num in range(1,6,1):
print(star)
star=star+'*'
(ii)
for num in range(1,6):
print('*' * num)

Question 21 : write python code to print given pattern


*
**
* *
* *
*****
Answer :
n= int(input("enter a number:"))
for i in range(1,n+1):
if i>2 and i<n:
print('*' + " " *(i-2) + "*")
else:
print('*'*i)

Question 22 : write python code to print given pattern


* * * * *
* *
* *
* *
* * * * *
Answer :
n=int(input("Enter a number:"))
for i in range(1,n+1):
if i>1 and i<n:
print('*' + ' '*(n-2)+'*')
else:
print('*'*n)

Question 23 : write python code to print given pattern


1
0 1
0 1 0
1 0 1 0
1 0 1 0 1
Answer :
n3=1
for n1 in range(1,6):
for n2 in range(1,n1+1):
print(n3%2,end=' ')
n3=n3+1
print()

Question 24 : write a python code to remove all duplicate values from a list
“a” and display only distinct values after deletion into another list “b”.
Answer :
b=[]
a=[10,12,15,11,9,10,12,11]
for num in a:
if(num not in b):
b.append(num)
else:
pass
print(b)

Question 25 : write a python code to remove all duplicate values from a list
“a” and display only distinct values after deletion.
a=[10,12,15,11,9,10,12,11]
Answer :
a=[10,12,15,11,9,10,12,11]
for num in a:
n=a.count(num)
for b in range(0,n-1):
a.remove(num)
print(a)

Question 26 : from a list “a”, find out frequency of each value in given list
and display each element of “b” with frequency of it in “a”, into another
dictionary “c” and also display distinct values in another list “b”.
Assume list “a” ->
a=['red','pink','green','blue','pink','green','red','orange','green','blue','pi
nk']
Answer :
a=['red','pink','green','blue','pink','green','red','orange','green','blue','pi
nk']
b=[]
c={}
for num in a:
if(num not in b):
b.append(num)
c[num]=a.count(num)
print(c)
print(b)

Question 27 : Write python code to take name of user and print name in given
pattern.
M
Ma
Man
Mani
Manis
Manish
Answer :
name=input("Enter a name:")
for i in range(1,len(name)+1):
print(name[0:i])
Question 28 : Write python code to print following pattern.
5
45
345
2345
12345
Answer :
for num in range(5,0,-1):
for num2 in range(num,6,1):
print(num2,end='')
print()

Question 29 : Write python code to print following pattern.


1
12
123
1234
12345
Answer :
for i in range(1,6,1):
for num2 in range(1,i+1,1):
print(num2,end='')
print()

Question 30 : Write python code to print following pattern.


1
2
3
4
5
10
9
8
7
6
Answer:
for i in range(1,11):
print(i if (i<6) else(16-i))

Question 31 : Write python code to print following pattern.


10
9
8
7
6
1
2
3
4
5
Answer:
for num in range(10,0,-1):
print(num if(num>5) else 6-num)

Question 32 : Write python code to take user’s name as input and display
characters in ascending order in a list.
Answer:
list2=[]
name=input("Enter name:")
list1=list(name)
for i in range(0,len(list1)):
list2.append(min(list1))
list1.remove(min(list1))
print(list2)

Question 33 : Write python code to take 10 numbers from user and display
smallest value.
Answer:
small=0
for num in range(1,11,1):
n=float(input("Enter number : "))
if (num==1):
small=n
if small>n:
small=n
print(small)

Question 34 : Write python code to take name of user and print name in given
pattern.
M
aM
naM
inaM
sinaM
hsinaM
Answer :
name=input("Enter a name:")
for i in range(0,len(name)):
print(name[i::-1])
Question 35 : Write python code to take name of user and print name in given
pattern.
hsinaM
sinaM
inaM
naM
aM
M
Answer :
name=input("Enter a name:")
for i in range(len(name)-1, -1, -1):
print(name[i::-1])

Question 36 : Write python code to take name of user and print name in given
pattern.
Manish
Manis
Mani
Man
Ma
M
Answer :
name=input("Enter a name:")
for i in range(len(name), 0, -1):
print(name[:i])

You might also like