Assignment
Name-Meenakshi Singh
Subject-Python
Registration no.-40822210034
#Conditional statement(if,elif,else)
1. Write a python program to check if a number is positive ,negative ,or zero.\
num = float(input("Input a number: "))
if num > 0:
print("It is positive number")
elif num == 0:
print("It is Zero")
else:
print("It is a negative number")
Output:
[Link] a program that determines if a given year is a leap year or not.
year=int(input("Enter year to be checked:"))
if(year%4==0 and year%100!=0 or year%400==0):
print("The year is a leap year!")
else:
print("The year isn't a leap year!")
Output:
[Link] a python function to find the largest among three numbers using if-elif-
else statements.
def find_largest_number(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3:
return num2
else:
return num3
a = 10
b = 25
c = 15
largest = find_largest_number(a, b, c)
print("The largest number among", a, ",", b, "and", c, "is", largest)
Output :
4. Create a program that checks if a given string is a palindrome or not using
conditional statements.
a=input("Enter string:")
b=a[-1::-1]
if(a==b):
print("Pallindrome string")
else:
print("Not Pallindrome string")
Output :
5. Write a python script to determine if a student's grade is "A,"B,"C,"D," or
"F," based on their score.
score = float(input("Enter the student's score: "))
while score < 0 or score > 100:
print("Invalid score. Please enter a score between 0 and 100.")
score = float(input("Enter the student's score: "))
if 90 <= score <= 100:
grade = "A"
elif 80 <= score < 90:
grade = "B"
elif 70 <= score < 80:
grade = "C"
elif 60 <= score < 70:
grade = "D"
else:
grade = "F"
print(f"The student's grade is {grade}")
Output :
#Loops(for and while):
6. Write a program to print the first 10 natural numbers using a while loop.
num = 1
while num <= 10:
print(num,end=" , ")
num += 1
Output:
[Link] a python script to calculate the factorial of a given number number
using a for loop.
n = int (input ("Enter a number: "))
factorial = 1
if n >= 1:
for i in range (1, n+1):
factorial=factorial *i
print("Factorial of the given number is: ", factorial)
Output:
[Link] a program thats finds the sum of all even numbers between 1 and 100
using a for loop.
result=0
for x in range(1,101):
if(x % 2 == 0):
result=result+x
print("Sum of even number is ",result)
Output:
[Link] a python function to print the Fibonacci series up to n terms using a for
loop.
def fibo(n):
if(n==1):
return 0
if (n==2):
return 1
return (fibo(n-1)+ fibo(n-2))
n=int(input("Enter no. of terms:"))
for i in range(1,n+1):
print(fibo(i))
Output:
[Link] a program to find the prime numbers in a given range using a for loop.
for number in range (1,20):
if number > 1:
for i in range (2, number):
if (number % i) == 0:
break
else:
print (number,end=",")
Output:
#Nested Loops and patterns:
[Link] a python program to print a right-angled triangles of stars.
rows = int(input("Please Enter the Total Number of Rows : "))
for i in range(1, rows + 1):
for j in range(1, i + 1):
print('*', end = ' ')
print()
Output:
[Link] a program to print a square pattern of stars.
side = int(input("Please Enter any Side of a Square: "))
for i in range(side):
for i in range(side):
print('*', end = ' ')
print()
Output :
[Link] a python script to print the Pascal's triangle.
rows = int(input("Enter the number of rows: "))
k=1
for i in range(1, rows + 1):
for a in range(1, (rows - i) + 1):
print(" ", end="")
for j in range(0, i):
if j == 0 or i == 0:
k=1
else:
k = k * (i - j) // j
print(k, end=" ")
print()
Output :
[Link] a program to print a diamond pattern of star.
n=int(input("Enter the number of rows for the diamond pattern:"))
if n%2==0:
n+=1
for i in range(1,n,2):
spaces = " " * ((n-i)//2)
stars = "*" * i
print(spaces + stars + spaces)
for i in range(n,0,-2):
spaces = " " * ((n-i)//2)
stars = "*" * i
print(spaces + stars + spaces)
Output :
[Link] a python script to print the multiplication table of a given number
using number using nested loops.
num=int(input("Enter the number : "))
mul=1
while True:
for i in range(1,11):
mul=i*num;
print(num,"x",i,"=",mul)
if(i==10):
break
Output :
#Control Flow and break/Continue:
16. Write a program to find the sum of all numbers between 1 and 100 that are
divisible by 7 but not by
sum=0
for i in range(1,100):
if(i%7==0 and i%5!=0):
sum=sum+i
print("Sum of number divisible by 7 but not divisible by 5 :",sum)
Output :
[Link] a python function to find the first non-repeated character in a string
using a loop and break statement.
String = "prepinsta"
for i in String:
count = 0
for j in String:
if i == j:
count+=1
if count > 1:
break
if count == 1:
print(i,end = " ")
Output :
[Link] a program to simulate a guessing [Link] a random number
and ask the user to guess [Link] hints and use a loop to continue until the user
guesses correctly or decides to quit.
attempt = 5
for i in range(5):
user_input = int(input('Enter Number: '))
if user_input == 7:
print('You won!')
break
else:
print(f'Try again! {attempt} left.')
attempt -= 1
continue
Output :
[Link] a program thats prints the numbers from 1 to 50 but skips multiples of
3 using the 'continue'statement.
for i in range(1,51):
if(i%3==0):
continue
else:
print(i,end=" ")
Output:
[Link] a python script to find the factorial of a number using a for loop and
the 'break' statement if the number is negative.
num=int(input("Enter the number : "))
i=1
mul=1
while(i<=num):
mul=mul*i
i+=1;
if(i<0):
break
print("Factorial of ",num," is :",mul)
Output :
21. Write a program to perform the multiplication of two matrices, let there are
two matrix, matrix A of size 3x2 and matrix B of size 2x3, and the resultant
matrix is C, perform the C=AxB in matrix multiplication. Take the input from
the user
# Input matrix A (3x2)
rows_A = 3
cols_A = 2
matrix_A = []
print("Enter elements of matrix A:")
for i in range(rows_A):
row = []
for j in range(cols_A):
element = int(input(f"Enter element at position ({i + 1}, {j + 1}): "))
[Link](element)
matrix_A.append(row)
# Input matrix B (2x3)
rows_B = 2
cols_B = 3
matrix_B = []
print("Enter elements of matrix B:")
for i in range(rows_B):
row = []
for j in range(cols_B):
element = int(input(f"Enter element at position ({i + 1}, {j + 1}): "))
[Link](element)
matrix_B.append(row)
# Initialize matrix C with zeros
matrix_C = [[0 for _ in range(cols_B)] for _ in range(rows_A)]
# Perform matrix multiplication
for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
matrix_C[i][j] += matrix_A[i][k] * matrix_B[k][j]
# Print the result matrix C
print("Matrix C (result of A x B):")
for row in matrix_C:
print(row)
Output :
22. Write a program in python to find out biggest number among three.
a, b, c = 5, 13, 2
if a > b and a > c:
print(f"Maximum is {a}")
elif b > c and b > a:
print(f"Maximum is {b}")
elif c > a and c > b:
print(f"Maximum is {c}")
else:
print(f"Maximum is {a}")
Output :
[Link] a program to check the number is prime or not.
num=int(input("enter the number"))
i=1
count=0
while(i<=num):
if(num%i==0):
count=count+1
i=i+1
if(count==2):
print("prime number")
else:
print("not a prime number")
Output :
24. Write a program to check a number is even or odd.
num = int(input ("Enter the number:"))
if (num % 2) == 0:
print ("The number is even")
else:
print ("The number is odd")
Output :
25. Write a program to reverse a number in python.
num = 12345
rev_num = 0
while num != 0:
rev_num = rev_num * 10
rev_num = rev_num + (num%10)
num = num // 10
print(rev_num)
Output :
'
[Link] the data type in python with example.
• Data types means the type of data stored into a variable or memory.
• Types of data type :-
1. Built-In data type
2. User-Defined data type
1. Built-In data type :- Those data type which is provided by
python itself.
• None Type
• Numeric type
• Sequence type
• Sets
• Mapping
2. User-Defined data type :- Those data type which is defined by
the user is called user-defined data type.
• Array
• Module
• Class
• Examples of data type:-
1. Integer:- 2,3,5 etc.
2. Float :- 2.5,3.9 etc.
3. Complex :- 12i+9j
4. Boolean:- True/False
5. Sequence :- String , List etc.
27. Explain the List, set type in python language with example.
List:-A list is a data structure in Python that is a mutable, or changeable, ordered
sequence of elements.
A list is an ordered data structure with elements separated by a comma and
enclosed within square brackets.
ex- colors=['red','green','blue']
Sets:-Sets are used to store multiple items in a single variable.
we create sets by placing all the elements inside curly braces {} , separated by comma.
Ex-student_id = {112, 114, 116, 118, 115}
print('Student ID:', student_id)
28. Explain the Array in python with example.
Array:- An array is a special variable, which can hold more than one value at a time.
ex:-cars = ["Ford", "Volvo", "BMW"]
29. Explain the comments in python with an empxale.
Comment:-Comment are nothing but ignorable part of python program. That are
ignored by the interpreter.
*There are 2-type of comment
[Link] line comment(# #)
[Link]-line comment(''' ''')
30. Write a program to compare to string in python.
string1 = "hello"
string2 = "world"
if string1 == string2:
print("The strings are equal.")
else:
print("The strings are not equal.")
Output :
31. Write a program to print this pattern using the loop in python language
***
*****
n=3
for i in range(1, n+1):
for j in range(n - i):
print(' ', end='')
for k in range(2 * i - 1):
print('*', end='')
print()
32. Write a program to print this pattern using loop and conditional sentence in
python language
12
123
rows = 3
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=' ')
print('')
Output :
33. Write a program to print this pattern using loop and conditional sentence in
python language.
111
22
rows = 3
for i in range(1, rows + 1):
for j in range(1, rows + 2 - i):
print(i, end=" ")
print()
Output :
34. Write a program to print this pattern using loop and conditional sentence in
python language.
23
456
rows = 3
number = 1
for i in range(1, rows + 1):
for j in range(i):
print(number, end=" ")
number += 1
print()
Output :