Python Tutorial: CS Assignment1

Saturday, 7 February 2026

CS Assignment1

1.      Write a program to input a number and generate the mathematical table of that number only if that number is divisible by 2.

Ans:

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

if num % 2 == 0:

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

    for i in range(1, 11):

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

else:

    print("The number is not divisible by 2. Table cannot be generated.")

2.      Akshat is a of class XI. He has written a program for manipulating string. Fill the blank with appropriate command / method

myaddress="Wazirpur 1, New Yamuna Nagar, New Delhi"

for i in range(         )      #line 1

if myaddress[i].                :         #line 2 print(myaddress[i].upper(), end="")

elif myaddress[i].                   :    # line 3

      print("*",end="")

else:

print(myaddress[i],end="")

             print()

print(len(myaddress.split(","))) #line 4

print(myaddress.replace("New","Old")) #line 5

a. Fill in the blank in Line 1 to calculate length of string

b.  Write the function in line 2 to check lower letter.

c. Write the function in line 3 to check digit.

d.      What will be the output of line 4.

OR

e.  What will be the output of line 5.

Ans

a. Fill in the blank in Line 1 to calculate length of string

len(myaddress)

b. Function in Line 2 to check lower letter

islower()

c. Function in Line 3 to check digit

isdigit()

d. Output of Line 4

print(len(myaddress.split(",")))

OR

d. Output of Line 5

print(myaddress.replace("New", "Old"))

Final Filled Code (for clarity)

myaddress = "Wazirpur 1, New Yamuna Nagar, New Delhi"

for i in range(len(myaddress)):

    if myaddress[i].islower():

        print(myaddress[i].upper(), end="")

    elif myaddress[i].isdigit():

        print("*", end="")

    else:

        print(myaddress[i], end=""

print()

print(len(myaddress.split(",")))

print(myaddress.replace("New", "Old"))

3.      Mr Arvind is software developer and works with infotech Company Ltd Noida. He has assigned to Create a dictionary with the roll number, name and marks of n students in a class and display the names of students who have marks above 75. How he can write the correct python code for the same.

Ans:

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

students = {}

for i in range(n):

    roll = int(input("Enter roll number: "))

    name = input("Enter student name: ")

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

    students[roll] = {"Name": name, "Marks": marks}

print("\nStudents scoring above 75 marks:")

for roll in students:

    if students[roll]["Marks"] > 75:

        print(students[roll]["Name"])

4.      Write a program in Python, which accepts a list L of integers and displays the sum of all such integers from the list L which end with the digit 3.

For example, if the list L is [ 123, 10, 13, 15, 23] then the program should display the sum of 123, 13, 23, i.e. 159 as follows: Sum of integers ending with digit 3 = 159

Ans:

L = eval(input("Enter a list of integers: "))

sum3 = 0

for num in L:

    if num % 10 == 3:

        sum3 += num

print("Sum of integers ending with digit 3 =", sum3)

5.      Write a program to check whether an integer input by the user is a 5-digit number and whether it is prime or composite.

Ans:

num = int(input("Enter an integer: "))

# Check whether the number is 5-digit

if 10000 <= abs(num) <= 99999:

    print("The number is a 5-digit number.")

    # Check prime or composite

    if num > 1:

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

            if num % i == 0:

                print("The number is Composite.")

                break

        else:

            print("The number is Prime.")                                                                                                          

    else:

        print("The number is neither Prime nor Composite.")

else:

    print("The number is not a 5-digit number.")

6.      Create a password validation program that asks the user for a password and checks if a given password meets the following criteria:

·         At least 8 characters long

        Contains both uppercase and lowercase letters

        Has at least one digit

        Includes at least one special character given (@,#,$, !,*,<,>)

Accept the password if it meets the above criteria otherwise continue accepting new password.

Ans:

while True:

    pwd = input("Enter password: ")

    upper = lower = digit = special = False

    if len(pwd) >= 8:

        for ch in pwd:

            if ch.isupper():

                upper = True

            elif ch.islower():

                lower = True

            elif ch.isdigit():

                digit = True

            elif ch in "@#$!*<>":

                special = True

        if upper and lower and digit and special:

            print("Password accepted")

            break

        else:

            print("Password does not meet all conditions")

    else:

        print("Password must be at least 8 characters long")

7.      Write a program to input few names in a list and create a menu driven program to:

a.       Display the words that starts with vowels

b.      Display the words that starts and ends with same vowel

c.       Display the words that ends with a consonant

d.      Display those words that starts and ends with same consonant

Ans:

names = eval(input("Enter list of names: "))

vowels = "aeiou"

while True:

    print("\nMENU")

    print("1. Words that start with vowels")

    print("2. Words that start and end with same vowel")

    print("3. Words that end with a consonant")

    print("4. Words that start and end with same consonant")

    print("5. Exit")

    choice = int(input("Enter your choice: "))

    if choice == 1:

        print("Words starting with vowels:")

        for w in names:

            if w[0].lower() in vowels:

                print(w)

    elif choice == 2:

        print("Words starting and ending with same vowel:")

        for w in names:

            if w[0].lower() in vowels and w[-1].lower() in vowels and w[0].lower() == w[-1].lower():

                print(w)

    elif choice == 3:

        print("Words ending with a consonant:")

        for w in names:

            if w[-1].lower() not in vowels:

                print(w)

    elif choice == 4:

        print("Words starting and ending with same consonant:")

        for w in names:

            if w[0].lower() not in vowels and w[-1].lower() not in vowels and w[0].lower() == w[-1].lower():

                print(w)

    elif choice == 5:

        print("Program terminated.")

        break

    else:

        print("Invalid choice. Try again.")

8.      Akash wants to store some information about his class. He wants to save this data in a dictionary where the name of the student is the key and the marks of 5 subjects along with their total marks, as the value (in a list). Help him by performing the following task as asked in order.

        Accept the name, and marks of 5 subjects out of 100 in a list for n students and store it in a dictionary as asked.

        Then, calculate the total marks out of 500 for each student and add it at the end of the list (value of each dictionary)

        Then, Display the name of the students who secured more than 400 out of 500.

Ans:

students = {}

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

for i in range(n):

    name = input("Enter student name: ")

    marks = []

    print("Enter marks of 5 subjects:")

    for j in range(5):

        print("Subject ",j+1,end=": ")

        m = int(input())

        marks.append(m)

    total = sum(marks)

    marks.append(total)

    students[name] = marks

print("\nStudents scoring more than 400 out of 500:")

for name in students:

    if students[name][-1] > 400:

        print(name)

 

x

No comments:

Post a Comment