Assignment 13: More on Decision Control

  1. Write a Python script to check whether a given number is a three-digit number or not.

  2. Write a Python script to check whether a given number is positive, negative or zero.

  3. Write a Python script to check whether a given quadratic equation has real and distinct roots, real and equal roots, or imaginary roots.

  4. Write a Python script to check whether a given year is a leap year or not.

  5. Write a Python script to print the greater among three numbers. Print the number only once even if some numbers are the same.

num=int(input("Enter the nuber : "))
if num<=99 or num>=1000 :
  print("Not Three digit number !!")
else:
  print("Yes, Three Digit number !!!")
Enter the nuber : 67676
Not Three digit number !!
num=int(input("Enter the nuber : "))
if num>99 and num<1000 :
  print("Yes, Three Digit number !!!")
else:
  print("Not Three digit number !!")
Enter the nuber : 153
Yes, Three Digit number !!!
a,b,c = input("Enter the three numbers : ").split()
a=int(a)
b=int(b)
c=int(c)
Enter the three numbers : 1 2 3
a,b,c
(1, 2, 3)
n = int(input("Enter a number: "))
if 100 <= abs(n) <= 999:
    print("Three-digit number")
else:
    print("Not a three-digit number")

n = float(input("Enter a number: "))
if n > 0:
    print("Positive")
elif n < 0:
    print("Negative")
else:
    print("Zero")

a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))
D = b**2 - 4*a*c
if D > 0:
    print("Real and distinct roots")
elif D == 0:
    print("Real and equal roots")
else:
    print("Imaginary roots")

year = int(input("Enter a year: "))
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
    print("Leap year")
else:
    print("Not a leap year")

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
print(max(a, b, c))
Enter a number: 5
Not a three-digit number
Enter a number: 538
Positive
Enter a: 83
Enter b: 3903
Enter c: 8
Real and distinct roots
Enter a year: 388
Leap year
Enter first number: 820923
Enter second number: 83
Enter third number: 8374
820923.0

Assignment 15: While Loop

  1. Write a Python script to print MSC 5 times on the screen.

  2. Write a Python script to print the first 10 natural numbers.

  3. Write a Python script to print the first 10 natural numbers in reverse order.

  4. Write a Python script to print the first 10 odd natural numbers.

  5. Write a Python script to print the first 10 odd natural numbers in reverse order.

while condition:

\(~~\)body

i = 1
while i <= 5:
    print("MySirG")
    i += 1

i = 1
while i <= 10:
    print(i, end=" ")
    i += 1
print()

i = 10
while i >= 1:
    print(i, end=" ")
    i -= 1
print()

i = 1
count = 0
while count < 10:
    print(i, end=" ")
    i += 2
    count += 1
print()


i = 19
while i >= 1:
    print(i, end=" ")
    i -= 2
print()
MySirG
MySirG
MySirG
MySirG
MySirG
1 2 3 4 5 6 7 8 9 10 
10 9 8 7 6 5 4 3 2 1 
1 3 5 7 9 11 13 15 17 19 
19 17 15 13 11 9 7 5 3 1 
i=0
while i<10:

  print(i)
  i+=1
0
1
2
3
4
5
6
7
8
9

Assignment 16: More on While Loop

  1. Write a Python script to print the first 10 even natural numbers.

  2. Write a Python script to print the first 10 even natural numbers in reverse order.

  3. Write a Python script to print the squares of the first 10 natural numbers.

  4. Write a Python script to print the cubes of the first 10 natural numbers.

  5. Write a Python script to print the first 10 multiples of 5.

i = 2
while i <= 20:
    print(i, end=" ")
    i += 2
print()

i = 20
while i >= 2:
    print(i, end=" ")
    i -= 2
print()

i = 1
while i <= 10:
    print(i**2, end=" ")
    i += 1
print()

i = 1
while i <= 10:
    print(i**3, end=" ")
    i += 1
print()

i = 1
while i <= 10:
    print(5*i, end=" ")
    i += 1
print()
2 4 6 8 10 12 14 16 18 20 
20 18 16 14 12 10 8 6 4 2 
1 4 9 16 25 36 49 64 81 100 
1 8 27 64 125 216 343 512 729 1000 
5 10 15 20 25 30 35 40 45 50 
value = 5.65
print(value)
5.65
print("{:.4e}".format(value))
5.6500e+00
value1 = 10
value2 = 15
print("{0},{1}".format(value1,value2))
10,15

for condition:

\(~~\)body

for i in range(10):
  print(i)
0
1
2
3
4
5
6
7
8
9
list1 = [0,10,1]
list1[1]
10
dummy_llist = []
dummy_llist.append(0.0)

vector1

vector2

length of vectors

for to iterate over the list

\(~~~\) dummy_vector in that append the multiplication

x=[1,2,3]
y=[4,5,6]
n=len(x)
dummy=[]
for i in range(n):
  num=x[i]*y[i]
  dummy.append(num)
print(dummy)
[4, 10, 18]

Write a program to multiply two matrices by using your knowledge of lists and for loop

[[1,0],[0,1]]
[[1, 0], [0, 1]]
[[1,0],[0,1]][0]
[1, 0]
[[1,0],[0,1]][0][0]
1

Take two list of lists

Use for loop to iterate over the row

use another for loop to iterate over the columns

Do the same dot of the list and sum it up

now append the final value to your dummy_list

once you create the first row of the multiplication matrix put it within the final dummy matrix

sum([0,1,2])
3
dummy_matrix = []

n = int(input("Give the dimension of the matrix"))

for i in range(n):
  row_of_the_dummy =[]
  for j in range(n):
    a_ij = int(input("enter the number"))
    row_of_the_dummy.append(a_ij)
  dummy_matrix.append(row_of_the_dummy)
Give the dimension of the matrix2
enter the number1
enter the number0
enter the number0
enter the number1
dummy_matrix
[[1, 0], [0, 1]]

Assignment 17: While Loop with User Input

  1. Write a Python script to print MySirG N times on the screen.

  2. Write a Python script to print the first N natural numbers.

  3. Write a Python script to print the first N natural numbers in reverse order.

  4. Write a Python script to print the first N odd natural numbers.

  5. Write a Python script to print the first N odd natural numbers in reverse order.

N = int(input("Enter N: "))

i = 1
while i <= N:
    print("MySirG")
    i += 1

i = 1
while i <= N:
    print(i, end=" ")
    i += 1
print()

i = N
while i >= 1:
    print(i, end=" ")
    i -= 1
print()

i = 1
count = 0
while count < N:
    print(i, end=" ")
    i += 2
    count += 1
print()

i = 2*N - 1
while i >= 1:
    print(i, end=" ")
    i -= 2
print()
Enter N: 5
MySirG
MySirG
MySirG
MySirG
MySirG
1 2 3 4 5 
5 4 3 2 1 
1 3 5 7 9 
9 7 5 3 1