0% found this document useful (0 votes)
6 views18 pages

Python Lab Assignment: Basic Programs

Uploaded by

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

Python Lab Assignment: Basic Programs

Uploaded by

Rishi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Lab Assignment 3

1) WAP to print 'Hello World'.


Code:
print('Hello World')
2) WAP to input a number and check whether it is Odd or Even.
Code:
num = int(input('Enter a number: '))
if num % 2 == 0:
print('Even')
else:
print('Odd')
3) WAP to check whether a year is a Leap Year.
Code:
year = int(input('Enter year: '))
if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):
print('Leap Year')
else:
print('Not Leap Year')
4) WAP to check whether a number is Prime or not.
Code:
num = int(input('Enter a number: '))
if num > 1:
for i in range(2, num):
if num % i == 0:
print('Not Prime')
break
else:
print('Prime')
else:
print('Not Prime')
5) WAP to check whether a number is Armstrong or not.
Code:
num = int(input('Enter a number: '))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print('Armstrong')
else:
print('Not Armstrong')
6) WAP to swap two numbers.
Code:
a = int(input('Enter first number: '))
b = int(input('Enter second number: '))
a, b = b, a
print('After swapping: a =', a, ', b =', b)

\
7) WAP to create a Menu-driven Calculator.
Code:
print('Menu Driven Calculator')
print('1. Addition')
print('2. Subtraction')
print('3. Multiplication')
print('4. Division')
choice = int(input('Enter choice: '))
a = int(input('Enter first number: '))
b = int(input('Enter second number: '))
if choice == 1:
print('Result =', a+b)
elif choice == 2:
print('Result =', a-b)
elif choice == 3:
print('Result =', a*b)
elif choice == 4:
print('Result =', a/b)
else:
print('Invalid Choice')
8) WAP to initialize different variables and check their datatype using type().
Code:
a=5
b = 3.14
c = 'Python'
d = True
print(type(a))
print(type(b))
print(type(c))
print(type(d))
9) WAP to input three values and check if the triangle is valid or not.
Code:
a = int(input('Enter first side: '))
b = int(input('Enter second side: '))
c = int(input('Enter third side: '))
if a+b>c and a+c>b and b+c>a:
print('Valid Triangle')
else:
print('Invalid Triangle')
10) WAP to check whether a five-digit number is a Palindrome.
Code:
num = int(input('Enter a 5-digit number: '))
if str(num) == str(num)[::-1]:
print('Palindrome')
else:
print('Not Palindrome')
11) WAP to find the Factorial of a number.
Code:
num = int(input('Enter a number: '))
fact = 1
for i in range(1, num+1):
fact *= i
print('Factorial:', fact)
12) WAP to print Multiplication Table of a number.
Code:
num = int(input('Enter a number: '))
for i in range(1, 11):
print(num, 'x', i, '=', num*i)
14) Write a Python program to count the number of even and odd numbers
from a series of numbers.

Sample numbers: numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)

Expected Output:

Number of even numbers: 5

Number of odd numbers: 4

Code:

numbers = input("Enter numbers without spaces: ")

even_count = 0

odd_count = 0

for ch in numbers:

num = int(ch) # convert each character to number

if num % 2 == 0:

even_count += 1

else:

odd_count += 1

print("Number of even numbers:", even_count)

print("Number of odd numbers:", odd_count)


15) Write a Python program that prints all the numbers from 0 to 6 except 3
and 6

Note: Use 'continue' statement.

Expected Output: 01245


Code:

for i in range(7): # loop from 0 to 6

if i == 3 or i == 6:

continue

print(i, end="")
16) Print First 10 natural numbers using while loop
Code:

i=1

while i <= 10:

print(i)

i+=1
17)Print the following pattern

12

123

1234

12345
Code:

for i in range(1, 6):

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

print(j, end=””)

print()
18) Display numbers from -10 to -1 using for loop
Code:

for num in range(-10, 0):

print(num)
19)Print the following pattern
*

**

***

****

*****

Code:

n = int(input("Enter number of rows: "))

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

print("*" * i)

You might also like