- List of Experiments
- Week 1
- Week 2
- Week 3
- Week 4
- Week 5
- Week 6
- Week 7
- Week 8
Python Program for Phone Number and Email Validation
Aim: Write a python code to read a phone number and email-id from the user and validate it for correctness
Program:
import re # Importing re module
n=input('Enter Mobile number :') # Reading input from the user
m=(input('Enter Email address:'))
r=re.fullmatch('[7-9]{1}[0-9]{9}',n)# calling fullmatch function by passing pattern and n
s=len(re.findall("[w._%+-]{1,20}@[w.-]{2,20}.[A-Za-z]{2,3}",m))
''' passssword constraints
1 to 20 lowercase and/or uppercase letters, numbers, plus . _ % +
An @ symbol
2 to 20 lowercase and uppercase letters, numbers and plus
A period symbol
2 to 3 lowercase and uppercase letter'''
if r!=None: # checking whether it is none or not
print('Valid Number')
else:
print('Not a valid number')
if s!=None:
print("Email Matches")
else:
print("Not a valid mail id")
Output:
Enter Mobile number :7730815575
Enter Email address:cmrtpoint@gmail.com
Valid Number
Email Matches
Sample Viva Questions and Answers
1. What is the purpose of the program?
The purpose of the program is to read a mobile number and an email address from the user and validate them for correctness using regular expressions.
2. Which module is imported to handle regular expressions in Python?
The re
module is imported to work with regular expressions.
3. How does the program validate the mobile number?
The program uses the re.fullmatch()
function with a regular expression pattern to check if the mobile number starts with a digit between 7 and 9 and is followed by 9 more digits.
4. What is the regular expression pattern used for validating the mobile number?
The pattern used is '[7-9]{1}[0-9]{9}'
, which ensures that the first digit is between 7 and 9, followed by 9 digits (0-9).
5. What will be the output if the user enters a valid mobile number like “9876543210”?
The output will be Valid Number
, indicating that the mobile number is valid.
6. How does the program validate the email address?
The program uses re.findall()
to check if the email address matches a specific pattern, but the pattern used in the code is incorrect.
7. What is the issue with the regular expression pattern used for validating the email address?
The pattern "[w._%+-]{1,20}@[w.-]{2,20}.[A-Za-z]{2,3}"
contains a typo; it should use "\w"
instead of "[w]"
to match word characters.
8. What will be the output if the user enters a valid email address like “example@mail.com”?
If the email validation pattern were correct, the output would be Email Matches
. However, due to the incorrect pattern, it may not match even valid emails.
9. How can you improve the email validation pattern?
You can improve the email validation pattern to something like r'^[\w._%+-]+@[\w.-]+\.[A-Za-z]{2,3}$'
, which correctly matches valid email formats.
10. What will happen if the user enters an invalid mobile number like “1234567890”?
The output will be Not a valid number
, as the number does not start with a digit between 7 and 9.
📌 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! 🚀