Wednesday, July 29, 2026

Week - I Programs


*Week - I Programs*

1) Write a program to find the largest element among three Numbers

CODE:

Method 1: Using if-else statements

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

b = int(input("Enter second number: "))

c = int(input("Enter third number: "))

 

if a >= b and a >= c:

    largest = a

elif b >= a and b >= c:

    largest = b

else:

    largest = c

 

print("The largest number is:", largest) 

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

Method 2: Using max() function

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

b = int(input("Enter second number: "))

c = int(input("Enter third number: "))

 

largest = max(a, b, c)

print("The largest number is:", largest)


 OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

Method 3: Using Ternary operator

 

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

b = int(input("Enter second number: "))

c = int(input("Enter third number: "))

 

largest = a if (a > b and a > c) else (b if b > c else c)

print("The largest number is:", largest)

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

Method 4: Using sorted() function

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

b = int(input("Enter second number: "))

c = int(input("Enter third number: "))

 

numbers = [a, b, c]

largest = sorted(numbers)[-1]

print("The largest number is:", largest)

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

Method 5: Defining a function

def find_largest(a, b, c):

    if a >= b and a >= c:

        return a

    elif b >= a and b >= c:

        return b

    else:

        return c

 

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

b = int(input("Enter second number: "))

c = int(input("Enter third number: "))

 

print("The largest number is:", find_largest(a, b, c))

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

2) Write a Program to display all prime numbers within an interval

CODE:

Method 1: Basic Loop

 

start = int(input("Enter the start of interval: "))

end = int(input("Enter the end of interval: "))

 

print(f"Prime numbers between {start} and {end} are:")

 

for num in range(start, end + 1):

    if num > 1:

        for i in range(2, num):

            if num % i == 0:

                break

        else:

            print(num, end=" ")

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

 

Method 2: Optimized Prime Check

 

import math

 

start = int(input("Enter the start of interval: "))

end = int(input("Enter the end of interval: "))

 

print(f"Prime numbers between {start} and {end} are:")

 

for num in range(start, end + 1):

    if num > 1:

        is_prime = True

        for i in range(2, int(math.sqrt(num)) + 1):

            if num % i == 0:

                is_prime = False

                break

        if is_prime:

            print(num, end=" ")

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

 

Method 3: Using a function

def is_prime(n):

    if n <= 1:

        return False

    for i in range(2, int(n**0.5) + 1):

        if n % i == 0:

            return False

    return True

 

start = int(input("Enter the start of interval: "))

end = int(input("Enter the end of interval: "))

 

print(f"Prime numbers between {start} and {end} are:")

 

for num in range(start, end + 1):

    if is_prime(num):

        print(num, end=" ")


OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

3) Write a program to swap two numbers without using a temporary variable.

 

CODE:

 

Method 1: Addition and Subtraction

 

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

b = int(input("Enter second number: "))

 

a = a + b

b = a - b

a = a - b

 

print("After swapping: a =", a, ", b =", b)

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

Method 2: Multiplication and Division

 

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

b = int(input("Enter second number: "))

 

a = a * b

b = a / b

a = a / b

 

print("After swapping: a =", int(a), ", b =", int(b))

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

Method 3: Pythonic way (Tuple unpacking)

 

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

b = int(input("Enter second number: "))

 

a, b = b, a

 

print("After swapping: a =", a, ", b =", b)

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

 4) Demonstrate the following Operators in Python with suitable examples.

i) Arithmetic Operators ii) Relational Operators iii) Assignment Operators iv)

Logical Operators v) Bit wise Operators vi) Ternary Operator vii) Membership

Operators viii) Identity Operators

 

CODE:

Demonstration of Different Operators in Python

 

1. Arithmetic Operators

a = 15

b = 4

print("Arithmetic Operators:")

print("a + b =", a + b)   # Addition

print("a - b =", a - b)   # Subtraction

print("a * b =", a * b)   # Multiplication

print("a / b =", a / b)   # Division

print("a % b =", a % b)   # Modulus

print("a ** b =", a ** b) # Exponentiation

print("a // b =", a // b) # Floor Division

print()

 

2. Relational (Comparison) Operators

print("Relational Operators:")

print("a > b is", a > b)

print("a < b is", a < b)

print("a == b is", a == b)

print("a != b is", a != b)

print("a >= b is", a >= b)

print("a <= b is", a <= b)

print()

 

3. Assignment Operators

print("Assignment Operators:")

x = 10

print("x =", x)

x += 5   # x = x + 5

print("x += 5 ", x)

x -= 3   # x = x - 3

print("x -= 3 ", x)

x *= 2   # x = x * 2

print("x *= 2 ", x)

x /= 4   # x = x / 4

print("x /= 4 ", x)

x %= 5   # x = x % 5

print("x %= 5 ", x)

print()

 

4. Logical Operators

p = True

q = False

print("Logical Operators:")

print("p and q is", p and q)

print("p or q is", p or q)

print("not p is", not p)

print()

 

5. Bitwise Operators

m = 6   # (110 in binary)

n = 3   # (011 in binary)

print("Bitwise Operators:")

print("m & n =", m & n)   # AND

print("m | n =", m | n)   # OR

print("m ^ n =", m ^ n)   # XOR

print("~m =", ~m)         # NOT

print("m << 1 =", m << 1) # Left shift

print("m >> 1 =", m >> 1) # Right shift

print()

 

6. Ternary Operator

num = 10

result = "Even" if num % 2 == 0 else "Odd"

print("Ternary Operator:")

print("Number is", result)

print()

 

7. Membership Operators

list1 = [1, 2, 3, 4, 5]

print("Membership Operators:")

print("3 in list1 ", 3 in list1)

print("7 not in list1 ", 7 not in list1)

print()

 

8. Identity Operators

x = [1, 2, 3]

y = [1, 2, 3]

z = x

print("Identity Operators:")

print("x is y ", x is y)         # False (different objects with same content)

print("x is z ", x is z)         # True  (same object)

print("x is not y ", x is not y) # True

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 


5) Write a program to add and multiply complex numbers

 

CODE:

 Method 1: Using complex type

a = complex(2, 3)   # 2 + 3i

b = complex(4, 5)   # 4 + 5i

 

# Addition and Multiplication

print("Addition:", a + b)

print("Multiplication:", a * b)

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

Method 2: Taking input as complex numbers

a = complex(input("Enter first complex number (e.g., 2+3j): "))

b = complex(input("Enter second complex number (e.g., 4+5j): "))

 

print("Addition:", a + b)

print("Multiplication:", a * b)

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

6) Write a program to print multiplication table of a given number.

 

CODE:

 Method 1: For loop

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

print(f"Multiplication Table of {num}")

 

for i in range(1, 11):

    print(f"{num} x {i} = {num * i}")

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

Method 2: While loop

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

print(f"Multiplication Table of {num}")

 

i = 1

while i <= 10:

    print(f"{num} x {i} = {num * i}")

    i += 1

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

Method 3: Function

def multiplication_table(n):

    for i in range(1, 11):

        print(f"{n} x {i} = {n * i}")

 

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

print(f"Multiplication Table of {num}")

multiplication_table(num)


OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

 

Method 4: List Comprehension

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

table = [f"{num} x {i} = {num * i}" for i in range(1, 11)]

print("\n".join(table))

 

OUTPUT: ----------------------------- Try on your own👏----------------------------- 

 

Sunday, July 19, 2026

Lets Learn Python

 

 

S.No

Task

One-Line Python Code

1

Print Hello World

print("Hello, World!")

2

Print Your Name

print("My name is Maddy")

3

Add Two Numbers

print(10 + 20)

4

Multiply Two Numbers

print(5 * 8)

5

Find Square of a Number

print(7 ** 2)

6

Find Cube of a Number

print(4 ** 3)

7

Divide Two Numbers

print(20 / 5)

8

Find Remainder

print(17 % 3)

9

Print Current Year

print(2026)

10

Print a Welcome Message

print("Welcome to Python Programming")

11

Take Input and Print It

print(input("Enter your name: "))

12

Print the Data Type

print(type(100))

13

Convert String to Integer

print(int("50"))

14

Convert Integer to Float

print(float(25))

15

Find Maximum Number

print(max(5, 9, 3, 12))

16

Find Minimum Number

print(min(5, 9, 3, 12))

17

Find Absolute Value

print(abs(-25))

18

Generate a Random Number

from random import randint; print(randint(1,10))

19

Print the Length of a String

print(len("Python"))

20

Print Today's Motivation

print("Believe in yourself. Happy Coding!")

 

 

 

 

 

 

 

 

 

 Predict the Output Question

Question 1: Hello Python

print("Hello")
print("Python")

Predict the Output:


Question 2: Addition

a = 10
b = 20
print(a + b)

Predict the Output:


Question 3: Multiplication

x = 5
print(x * 4)

Predict the Output:


Question 4: String Repetition

print("Hi" * 3)

Predict the Output:


Question 5: Variable Update

num = 8
num = num + 2
print(num)

Predict the Output:


Question 6: Integer Division

print(15 // 2)

Predict the Output:


Question 7: Modulus Operator

print(15 % 4)

Predict the Output:


Question 8: Comparison Operator

a = 10
b = 20
print(a < b)

Predict the Output:


Question 9: Simple If Statement

x = 7

if x > 5:
    print("Python")
print("Programming")

Predict the Output:


Question 10: String Concatenation

name = "Alice"
print("Hello " + name)

Predict the Output:


Question 11: Addition and Multiplication

a = 2
b = 3
print(a + b * 2)

Predict the Output: ______________________


Question 12: String with Number

age = 18
print("Age =", age)

Predict the Output: ______________________


Question 13: String Concatenation

first = "Data"
second = "Science"
print(first + second)

Predict the Output: ______________________


Question 14: Exponent Operator

print(2 ** 3)

Predict the Output: ______________________


Question 15: Floating-Point Division

print(9 / 2)

Predict the Output: ______________________


Question 16: Equality Comparison

x = 10
y = 10
print(x == y)

Predict the Output: ______________________


Question 17: Logical Operator

print(True and False)

Predict the Output: ______________________


Question 18: Simple If-Else

marks = 45

if marks >= 50:
    print("Pass")
else:
    print("Fail")

Predict the Output: ______________________


Question 19: Variable Swapping

a = 5
b = 8

a = b
print(a)

Predict the Output: ______________________


Question 20: Mixed Arithmetic

x = 6
y = 4
print((x + y) * 2)

Predict the Output: ______________________



 

S.No

Program

Concepts Covered

1

Print "Hello, World!"

print() function

2

Read and Print User Details (Name, Age, Branch)

input(), variables

3

Add Two Numbers

Variables, arithmetic operators

4

Find the Area of a Rectangle

Input, multiplication

5

Convert Temperature (Celsius to Fahrenheit)

Formula, arithmetic

6

Check Whether a Number is Even or Odd

if-else, modulus operator

7

Find the Largest of Two Numbers

Conditional statements

8

Print Multiplication Table of a Number

for loop

9

Calculate the Sum of Numbers from 1 to N

Loops, accumulator

10

Simple Calculator (+, -, *, /)

if-elif-else, operators








1. Hello World

print("Hello, World!")

Sample Output

Hello, World!

 

2. Read and Print User Details

name = input("Enter your name: ")

age = input("Enter your age: ")

branch = input("Enter your branch: ")

 

print("Name:", name)

print("Age:", age)

print("Branch:", branch)

Sample Output

Enter your name: Ravi

Enter your age: 19

Enter your branch: CSE

 

Name: Ravi

Age: 19

Branch: CSE

 

3. Addition of Two Numbers

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

b = int(input("Enter second number: "))

 

sum = a + b

 

print("Sum =", sum)

Sample Output

Enter first number: 15

Enter second number: 20

 

Sum = 35

 

4. Area of a Rectangle

length = float(input("Enter length: "))

breadth = float(input("Enter breadth: "))

 

area = length * breadth

 

print("Area =", area)

Sample Output

Enter length: 8

Enter breadth: 5

 

Area = 40

 

5. Celsius to Fahrenheit

celsius = float(input("Enter temperature in Celsius: "))

 

fahrenheit = (celsius * 9/5) + 32

 

print("Temperature in Fahrenheit =", fahrenheit)

Sample Output

Enter temperature in Celsius: 30

 

Temperature in Fahrenheit = 86.0

 

6. Even or Odd

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

 

if num % 2 == 0:

    print("Even Number")

else:

    print("Odd Number")

Sample Output

Enter a number: 27

 

Odd Number

 

7. Largest of Two Numbers

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

b = int(input("Enter second number: "))

 

if a > b:

    print("Largest =", a)

else:

    print("Largest =", b)

Sample Output

Enter first number: 25

Enter second number: 18

 

Largest = 25

 

8. Multiplication Table

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

 

for i in range(1, 11):

    print(num, "x", i, "=", num * i)

Sample Output

5 x 1 = 5

5 x 2 = 10

...

5 x 10 = 50

 

9. Sum of Numbers from 1 to N

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

 

sum = 0

 

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

    sum += i

 

print("Sum =", sum)

Sample Output

Enter a number: 10

 

Sum = 55

 

10. Simple Calculator

a = float(input("Enter first number: "))

b = float(input("Enter second number: "))

 

op = input("Enter operator (+, -, *, /): ")

 

if op == "+":

    print("Result =", a + b)

elif op == "-":

    print("Result =", a - b)

elif op == "*":

    print("Result =", a * b)

elif op == "/":

    if b != 0:

        print("Result =", a / b)

    else:

        print("Division by zero is not allowed.")

else:

    print("Invalid Operator")

Sample Output

Enter first number: 20

Enter second number: 4

Enter operator (+, -, *, /): /

 

Result = 5.0

 

Week - I Programs

*Week - I Programs* 1) Write a program to find the largest element among three Numbers CODE: Method 1: Using if-else statements a = int(inpu...