Docsity

Python Programming Questions (List, Tuple , Strings, Loops), Assignments of Programming Languages

Partial preview of the text

Download Python Programming Questions (List, Tuple , Strings, Loops) and more Assignments Programming Languages in PDF only on Docsity!

COMSATS UNIVERSITY ISLAMABAD

ABBOTTABAD CAMPUS

Topic: Lab Assignment

Submitted By:

Muhammad Sulaiman

Registration No:

SP25-BDA-

Subject:

Programming Language for Data Analytics (Python)

Submitted To:

Dr Ihsan Ullah

Date:

15 May 2025

Lab Assignment # 2 Question 1 Create a tuple of 10 integers. Use a for loop to print each number. If the number is even, print its square; if odd, print its cube. Question 2 Define a list of 5 strings (e.g., fruit names). Use a while loop to print each string in uppercase, one by one. Question 3 Given a list of numbers from 10 to 1, use a for loop to print only those numbers which are divisible by 2 or 5.

Question 6 Given a tuple of words, use a for loop to print the length of each word. Also, print whether its length is even or odd. Question 7 Take a list of numbers from 1 to 20. Use a for loop to count how many numbers are divisible by 3 but not by 2. Print the final count. Question 8 Given a list of characters (e.g., ['p', 'y', 't', 'h', 'o', 'n']), use a while loop to print them in reverse order, one per line.

Question 9 Create a list of 10 random numbers. Use a for loop and an if condition to replace all numbers < 5 with 0, and print the updated list. (Hint: search for randint(a, b), you will need to import random module) Question 10 Given a tuple of strings, for each string: if its index is even, print the string as is; if odd, print the string reversed.

 Print the square of the number if it is greater than 5,  Otherwise, print the cube of the number. Question 3 Given a tuple: myTuple = (2, 5, 9, 12, 15, 17, 20), Use a while loop to:  Print only the odd numbers  Stop the loop if a number greater than 15 is encountered. Question 4 Create a list: myList = [12, -3, 0, 7, 19, -8, 6, 21]. Use a for loop to:  Replace each negative number with 0

 Replace each number greater than 10 with 10  Then print the updated list. Question 5 You are given a list of integers where every number occurs twice, except one number that appears only once. Write a program using a for loop to find and print that unique number. Example list: [4, 1, 2, 1, 2, 4, 3]