Assignment 1 - Python
Q1. Explain the 3 differences between List and tuples.
Q2. You are working as a Data Scientist and have been given a programming task.
Create two lists, l1 and l2, where:
• l1 = [1, 2, 3, 4, 5, 6]
• l2 = [6, 5, 4, 3, 2, 1]
Compare the elements at the same index position in both lists.
For each comparison:
• Print "L1 element value is greater than L2 element value" if the element
from l1 is greater.
• Print "L1 element value is less than L2 element value" if the element from
l1 is smaller.
Q3. Write a user-defined Python function that accepts a list of numbers as input
from the user and returns a dictionary. In the dictionary, each number from the
list should be used as the key and the square of that number should be stored as
the corresponding value.
Eg: Input from user: [2, 4, 6, 8]
Expected output: {2: 4, 4: 16, 6: 36, 8: 64}
Q4. You are working as an Analyst in a company and the management has asked
you to develop a Python-based calculator.
Write a user-defined function named Calculator that accepts three parameters:
• int1 (first number)
• int2 (second number)
• task (operation to be performed)
The task parameter will be a string that can take the values 'add', 'sub', 'mul', or
'div'. Based on the value of task, perform addition, subtraction, multiplication or
division on the two numbers (int1 and int2).
Ensure that the task parameter is case-insensitive, meaning inputs such as 'ADD',
'Add' or 'aDd' should all be treated as valid commands.
Implement the required logic inside the Calculator function so that it performs the
appropriate operation and returns the computed result.
Eg: Calculator(2,5,ADD) should return 7
Calculator(7,1,SuB) should return 6
Q5 Write a Function to check if the year number is a leap year or not. Print all
Leap Years of 21st Century.
Q6. Write a python code to print the following pattern.
1
12
123
1234
12345
Q7. You are working as a Data Analyst in an organization. As part of a security
validation task, you are required to analyze a list of user passwords.
Write a Python program that processes a list of passwords and classifies each
password as Strong or Weak based on the following conditions:
• A password is considered Strong if:
o It contains at least 8 characters
o It includes both letters and numbers
• A password is considered Weak if it does not meet the above criteria.
The program should iterate through the list of passwords using loops and apply
appropriate conditional checks.
Handle invalid inputs (such as non-string values) without terminating the
program.
Eg: Input: ["Admin123", "pass", "welcome2024", 12345, "abc"]
Output:
Admin123 -> Strong
pass -> Weak
welcome2024 -> Strong
12345 -> Invalid Input
abc -> Weak
Q8 . Write a Function to take a list from user and return 2 lists:
- 1st list should contain all even indexed numbers.
- 2nd list should contain all odd indexed numbers.
Q9. Write a Python function named longest_word(sentence) that accepts a sent-
nce as input and returns the longest word present in the sentence. If two or more
words have the same maximum length, the function should return the first occurr
ing word.
Eg: longest_word("Python programming is fun") # Output: 'programming'
Q10. Write a Python program that takes a list and counts the frequency of each
element using a dictionary. The keys of the dictionary should be the list elements,
and the values should be the counts
Eg: I/P: [1, 2, 2, 3, 4, 4, 4, 5, 5]
o/p: {1: 1, 2: 2, 3: 1, 4: 3, 5: 2}