Python Programming
1. Input a welcome message and display it.
Input:-
print("Welcome to python")
Output:- Welcome to python
2. Input two numbers and display the larger/smaller number.
Input:-
num1=int(input("Enter First Number"))
num2=int(input("Enter Second Number"))
if (num1>num2):
print("The Larger number is", num1)
else:
print ("The Larger number is", num2)
Output:-
Enter First Number28
Enter Second Number69
The Larger number is 69
Program- display the larger number /smaller number in list
lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Larger number in the list is :", max(lst), "\nSmaller number in the
list is :", min(lst))
Output:-
How many numbers: 5
Enter number 44
Enter number 98
Enter number 66
3Enter number 1
Enter number 0
Larger number in the list is : 98
Smaller number in the list is : 0
3. Input three numbers and display the larger/smaller number.
Input:-
number1 = int(input('Enter First number : '))
number2 = int(input('Enter Second number : '))
number3 = int(input('Enter Third number : '))
def largest(num1, num2, num3):
if (num1 > num2) and (num1 > num3):
largest_num = num1
elif (num2 > num1) and (num2 > num3):
largest_num = num2
else:
largest_num = num3
print("The largest of the 3 numbers is : ", largest_num)
def smallest(num1, num2, num3):
if (num1 < num2) and (num1 < num3):
smallest_num = num1
elif (num2 < num1) and (num2 < num3):
smallest_num = num2
else:
smallest_num = num3
print("The smallest of the 3 numbers is : ", smallest_num)
largest(number1, number2, number3)
smallest(number1, number2, number3)
Output:-
Enter First number : 25
Enter Second number : 36
Enter Third number : 100
The largest of the 3 numbers is : 100
The smallest of the 3 numbers is : 25
4. Generate the following patterns using nested loops:
**
***
****
*****
Input:-
def pypart(n):
# outer loop to handle number of rows
# n in this case
for i in range(0, n):
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range(0, i+1):
# printing stars
print("* ",end="")
# ending line after each row
print("\r")
# Driver Code
n=5
pypart(n)
Output:-
*
**
***
****
*****
5. Generate the following patterns using nested loops:
1
12
123
1234
12345
Input:-
rows = int(input("Enter number of rows: "))
for i in range(rows):
for j in range(i+1):
print(j+1, end=" ")
print("\n")
Output:-
Enter number of rows: 6
1
12
123
1234
12345
123456
6. Generate the following patterns using nested loops:
A
BB
CCC
DDDD
EEEEE
Input:-
rows = int(input("Enter number of rows: "))
ascii_value = 65
for i in range(rows):
for j in range(i+1):
alphabet = chr(ascii_value)
print(alphabet, end=" ")
ascii_value += 1
print("\n")
Output:-
Enter number of rows: 6
A
BB
CCC
DDDD
EEEEE
FFFFFF
7. Generate the following patterns using nested loops:
12345
1234
123
12
1
Input:-
rows = int(input("Enter number of rows: "))
for i in range(rows, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print("\n")
Output:-
Enter number of rows: 5
12345
1234
123
12
1
8. Program to find the sum of series: 1 + 1/2 + 1/3 + ….. + 1/N
Input:-
n=int(input("Enter the number of terms:"))
x=int(input("Enter the value of x:"))
sum1=1
for i in range(2,n+1):
sum1=sum1+((x**i)/i)
print("The sum of series is",round(sum1,2))
Output:-
Enter the number of terms:4
Enter the value of x:2
The sum of series is 9.67
9. Fibonacci series with recursion
Input:-
a=int(input("Enter the first number of the series "))
b=int(input("Enter the second number of the series "))
n=int(input("Enter the number of terms needed "))
print(a,b,end=" ")
while(n-2):
c=a+b
a=b
b=c
print(c,end=" ")
n=n-1
Output:-
Enter the first number of the series 0
Enter the second number of the series 1
Enter the number of terms needed 4
>
0112>
10.Fibonacci series without recursion
a=int(input("Enter the first number of the series "))
b=int(input("Enter the second number of the series "))
n=int(input("Enter the number of terms needed "))
print(a,b,end=" ")
while(n-2):
c=a+b
a=b
b=c
print(c,end=" ")
n=n-1
output:-
Enter the first number of the series 2
Enter the second number of the series 2
Enter the number of terms needed 3
>
224>
11. Palindrome number
def is_palindrome(n):
return str(n) == ''.join(reversed(str(n)))
# Get the number from the user
n = int(input("Enter number: "))
# Check if the number is a palindrome
if is_palindrome(n):
print("The number is a palindrome!")
else:
print("The number is not a palindrome.")
Output:- Enter number: 123321
The number is a palindrome!