- List of Experiments
- Week 1
- Week 2
- Week 3
- Week 4
- Week 5
- Week 6
- Week 7
- Week 8
Python Array Conversion and Comparison (Lists & Tuples)
Aim: i) Write a program to convert a list and tuple into arrays.
Program:
from array import array
list=[1,2,3,4,5]
print("The original list valves : " + str(list))
res=array("i",list)
tuple=(6,7,8,9,10)
print("The original tuple values : " + str(tuple))
res1=array("i",tuple)
# Printing array converted result from array
print("List after conversion to array : " + str(res))
print("type",type(res))
# Printing array converted result from tuple
print("List after conversion to array : " + str(res1))
print("type",type(res1))
Output:
The original list valves : [1, 2, 3, 4, 5]
The original tuple values : (6, 7, 8, 9, 10)
List after conversion to array : array(‘i’, [1, 2, 3, 4, 5])
type <class ‘array.array’>
List after conversion to array : array(‘i’, [6, 7, 8, 9, 10])
type <class ‘array.array’>
Aim: ii) Write a program to find common values between two arrays.
Program:
from array import array
array1 = [0,1,2,3,4] #first array
array2 = [4,5,3,9,8] # second array
result=[] # result to be stored
for i in range(len(array1)):
for j in range(len(array2)):
if array1[i]==array2[j]:
result.append(array1[i])
print ("The Common Values are" ,result)
Output:
The Common Values are [3, 4]
Sample Viva Questions and Answers
1. What is the purpose of the first program?
The first program converts a list and a tuple into arrays.
2. Which module is imported to work with arrays in Python?
The array
module is imported to work with arrays.
3. How do you create an array from a list in the program?
An array is created from a list using the array()
function, specifying the type code (e.g., "i"
for integers) and the list itself.
4. What is the output of the program when converting the list [1, 2, 3, 4, 5]
to an array?
The output will be: List after conversion to array : array('i', [1, 2, 3, 4, 5])
.
5. What type of data does the type code "i"
represent in the array?
The type code "i"
represents signed integers in the array.
6. What is the purpose of the second program?
The second program finds and prints the common values between two arrays.
7. How are the common values between the two arrays identified in the program?
Common values are identified using nested loops that compare each element of the first array with each element of the second array.
8. What will be the output of the second program given the arrays [0, 1, 2, 3, 4]
and [4, 5, 3, 9, 8]
?
The output will be: The Common Values are [3, 4]
.
9. Why is an empty list result
initialized in the second program?
The empty list result
is initialized to store the common values found between the two arrays.
10. Can you explain the time complexity of the nested loops used to find common values?
The time complexity of the nested loops is O(n * m), where n is the length of the first array and m is the length of the second array, as each element of the first array is compared with each element of the second array.
📌 Python Programming Lab – Complete Guide
Welcome to the Python Programming Lab! This comprehensive lab series is designed to provide a solid foundation in Python programming through practical exercises and hands-on learning. From basic calculations to advanced concepts like file handling, modules, and GUI programming, this lab covers it all. Let’s dive into what you’ll be learning throughout this lab course.
🔍 Week 1: Introduction to Python Basics
In the first week, you will explore fundamental Python concepts aimed at building a strong foundation for your programming journey.
- Getting Started with Python: Visit the Python Official Website to explore documentation and use the
help()
function in the interpreter. - Python as a Calculator: Learn how to perform basic arithmetic operations like addition, subtraction, multiplication, and division.
- Calculating Compound Interest: Write a program to calculate compound interest using the formula with given principal, rate, and time.
- Distance Calculation: Compute the distance between two points using the distance formula.
- Reading User Details: Write a program to collect and print user details like name, address, email, and phone number.
🔍 Week 2: Python Loops and Conditional Statements
This week focuses on using loops and conditional statements effectively.
- Pattern Generation: Print a triangle pattern using nested loops.
- Character Identification: Write a program to identify whether the input is a digit, lowercase, uppercase, or special character.
- Fibonacci Sequence Generation: Use a
while
loop to generate the Fibonacci series. - Prime Numbers Identification: Find all prime numbers within a specified range using the
break
statement.
🔍 Week 3: Data Structures and Functions
Dive into data structures and creating functions to enhance your programming skills.
- Converting Lists and Tuples to Arrays: Understand how to convert different data structures.
- Finding Common Values: Compare two arrays to find common values.
- Calculating GCD: Create a function to calculate the greatest common divisor (GCD) of two numbers.
- Palindrome Checker: Write a function that checks if a given string is a palindrome.
🔍 Week 4: Advanced Functions and String Manipulation
This week emphasizes sorting, handling duplicate elements, and manipulating strings effectively.
- Checking Sorted Lists: Write a function to check if a list is sorted.
- Handling Duplicates: Create functions to detect and remove duplicates from lists.
- Dictionary Inversion: Write a program to swap keys and values in a dictionary.
- String Manipulation: Add commas between characters, remove words from strings, and convert sentences to title case without using built-in functions.
- Binary String Generation: Use recursion to generate all binary strings of a specified length.
🔍 Week 5: Working with Matrices and Modules
Learn to work with matrices and create custom modules for various applications.
- Matrix Operations: Define, print, add, and multiply square matrices using Python.
- Creating Modules: Build modules using geometrical shapes and their operations.
- Exception Handling: Implement exception handling for robust error management.
🔍 Week 6: Object-Oriented Programming and Validation
This week focuses on using classes, inheritance, and validation techniques.
- Drawing Shapes with Classes:
- Create classes for rectangles, points, and circles, and draw them on a canvas.
- Add colors and attributes to these shapes to enhance visualization.
- Method Resolution Order (MRO): Demonstrate MRO in multiple inheritance scenarios.
- Data Validation: Write programs to validate phone numbers and email addresses.
🔍 Week 7: File Handling and Text Processing
Master file handling techniques and analyzing text data.
- File Merging: Combine the contents of two files into a new file.
- Word Search: Create a function to find specific words in a file.
- Word Frequency Analysis: Identify the most frequent words in a text file.
- Text Statistics: Count words, vowels, blank spaces, lowercase, and uppercase letters in a file.
🔍 Week 8: Python Libraries and GUI Programming
Explore advanced Python libraries and build simple graphical user interfaces.
- NumPy, Plotly, and Scipy: Learn how to install and use these powerful libraries for data analysis and visualization.
- Digital Logic Gates: Implement logic gate operations such as AND, OR, NOT, and EX-OR.
- Adder Circuits: Create programs for Half Adders, Full Adders, and Parallel Adders.
- GUI Programming: Build a simple window wizard with text labels, input fields, and buttons using Python’s GUI tools.
🌟 Conclusion:
The Python Programming Lab R23 provides you with a strong foundation in Python programming through a wide range of exercises, from basic concepts to advanced topics. By the end of this lab, you will have gained valuable skills in data processing, file handling, OOP, modules, and GUI development. Start coding and enhance your Python skills! 🚀