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

Practice Questions Python

The document contains a series of Python programming exercises covering various topics such as loops, conditionals, string manipulation, and mathematical computations. It includes tasks like skipping numbers, reversing strings, calculating sums, generating patterns, and checking for prime numbers. Additionally, it outlines exercises for list operations such as finding the largest element, removing duplicates, and checking for palindromes.

Uploaded by

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

Practice Questions Python

The document contains a series of Python programming exercises covering various topics such as loops, conditionals, string manipulation, and mathematical computations. It includes tasks like skipping numbers, reversing strings, calculating sums, generating patterns, and checking for prime numbers. Additionally, it outlines exercises for list operations such as finding the largest element, removing duplicates, and checking for palindromes.

Uploaded by

gguy3911
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab-2

--> Write a program to skip the numbers divisible by 4 less


than 20.
a = 1
while a <= 20:
if a % 4 == 0:
a=a+1
continue
print(a)
a += 1
----------------------------------------

-->To Find Reverse String


str = input("Enter any string: ")
rev = " "
for char in str:
rev = char+rev
print("Reversed string:", rev)
----------------------------------------

--> Sum of first 20 Natural Numbers:


sum=0
for i in range(1,21):
sum=sum+i
print("Sum of first 20 natural number is:",sum)
-----------------------------------------------

--> Program to print number 10 down to 1 using for loop


for i in range (10,0,-1):
print(i)
----------------------------------------------------
--> Sum of even numbers between 1 to 50
sum=0
for i in range(2,50,2):
sum=sum+i
print("Sum of first 50 even numbers is :",sum)
------------------------------------------------

--> Program to find the square of element in list.


list=[1,2,3,4,5]
for i in list:
print(f"Square of {i} is:{i*i}")
------------------------------------------------

--> Program to find the largest word in list


list=["Himalayan","Whitehouse","International","College"]
strg=""
for i in list:
if len(i)>len(strg):
strg=i
print("Largest string is:",strg)
----------------------------------------------------
--> Program to find the particular element in list.
list=[10,15,20,25,30]
a=int(input("Enter any number:"))
found=False
for i in list:
if a==i:
found=True
else:
found=False
if found:
print(f"{a} is found in list")
else:
print(f"{a} is not found in list")
-------------------------------------------

--> Program to concatenate all string in list


list=["Welcome", "To","Himalayan",
"Whitehouse","International","College"]
a=""
for i in list:
a=a+i+" "
print(a)
----------------------------------------------

-->pyramid Shape pattern


rows = 5
for i in range(1, rows + 1):
print(' ' * (rows - i) + '*' * (2 * i - 1))
---------------------------------------------

--> Inverted right angle triangle


rows=5
for i in range (rows,0,-1):
for j in range (1,i+1):
print("*",end="")
print()
-------------------------------------------------

--> Multiplication table using nested loop


for i in range (1,11):
for j in range(1,6):
print(i*j,end=" ")
print()
-------------------------------------------------

--> Program to check prime or not


a = int(input("Enter any number: "))
is_prime = True
if a <= 1:
is_prime = False
else:
for i in range(2, a):
if a % i == 0:
is_prime = False
break
if is_prime:
print(f"{a} is a prime number")
else:
print(f"{a} is not a prime number")

------------------------------------
a=input("Enter numbers")
list=[int(i) for i in a.split()]
for j in list:
print(j**2)

------------------------------------
#Write a program in python to print Fibonacci series of 10 numbers.

a=0

b=1

print(a)

print(b)

for i in range(8):

c=a+b

print(c)

a=b

b=c

#Write a program in python to find reverse of input number.

n=int(input("Enter any number"))

rev=0

while n!=0:

r = n % 10

rev=rev*10+r

n=n//10

print("Reverse Number is:",rev)

#Write a program in python to find reverse of input string.

str = input("Enter any string: ")

rev = " "

for char in str:

rev = char +rev

print("Reversed string:", rev)

#Write a program in python to check positive and negative number.

a=int(input("Enter any number:"))

if a>0:

print("Input number is positive")

elif a<0:

print("Input number is Negative number")

else:

print("Input number is zero(0)")


#Write a program in python to input check input number is Armstrong number or not.

n=int(input("Enter any number:"))

d=len(str(n))

a=n

m=0

while n!=0:

r=n%10

m=m+r**d

n=n//10

if a==m:

print("Is Armstrong number")

else:

print("Is not a Armstrong number")

#While a program to print the following pattern.

12

123

1234

12345

for i in range(1,6):

for j in range(1,i+1):

print(j, end=" ")

print()
#While a program to print the following pattern.

22

333

4444

55555

for i in range(1,6):

for j in range(1,i+1):

print(i, end=" ")

print()

#While a program to print the following pattern.

**

***

****

*****

for i in range(1,6):

for j in range(1,i+1):

print("*", end=" ")

print()
#While a program to print the following pattern.

AB

ABC

ABCD

ABCDE

n=5

a = 65

for i in range(0, n):

for j in range(0, i+1):

print(chr(a), end=" ")

a += 1

a = 65

print()

 Write a program that takes a list of numbers and finds both the largest and the smallest elements in the list.

 Given a list with potential duplicate values, write a program to remove the duplicates while preserving the
original order of the elements.

 Write a program that checks if a given list is a palindrome (i.e., it reads the same backward as forward).

 Write a program to count how many even and odd numbers are in a list of integers.

 Write a program to reverse a list and print the reversed list.

 Write a program that calculates and prints the average of all the numbers in a list.

 Write a program that asks the user for a number and checks if the number exists in a predefined list.

You might also like