Software Development Exam  >  Software Development Notes  >  Basics of Python  >  Assignment: Patterns

Assignment: Patterns | Basics of Python - Software Development PDF Download

Multiple Choice Questions (MCQs)


Q.1. Which of the following statements about patterns in Python is correct?
(a) Patterns are used for formatting text in Python.
(b) Patterns are used to define algorithms in Python.
(c) Patterns are used for data visualization in Python.
(d) Patterns are used to create graphical user interfaces in Python.

Ans. (a)

Q.2. What is the output of the following code?

import re


pattern = r"[A-Za-z]+"

text = "Hello, World!"


result = re.findall(pattern, text)

print(result)

(a) ['Hello', 'World']
(b) ['Hello', 'World!']
(c) ['Hello,', 'World']
(d) ['Hello,', 'World!']

Ans. (a)

Q.3. Which module in Python is commonly used for working with patterns?
(a) math
(b) datetime
(c) re
(d) os

Ans. (c)

Q.4. What does the 're.search()' function in Python return?
(a) The first occurrence of the pattern in the text.
(b) The index of the first occurrence of the pattern in the text.
(c) True if the pattern is found in the text, otherwise False.
(d) The number of occurrences of the pattern in the text.

Ans. (a)

Q.5. Which of the following regular expressions matches any digit in Python?
(a) \d
(b) \w
(c) \s
(d) \D

Ans. (a)

Higher Order Thinking Skills (HOTS)


Q.1. Write a Python function to check if a string contains at least one uppercase letter.

import re


def contains_uppercase(text):

    pattern = r"[A-Z]"

    if re.search(pattern, text):

        return True

    else:

        return False

Q.2. Write a Python program to count the number of occurrences of a given word in a text using regular expressions.

import re


def count_occurrences(word, text):

    pattern = rf"\b{word}\b"

    occurrences = re.findall(pattern, text)

    return len(occurrences)

Q.3. Write a regular expression to match a valid email address format.

pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b"

Q.4. Write a Python program to find all the phone numbers (in the format XXX-XXX-XXXX) from a given text using regular expressions.

import re


def extract_phone_numbers(text):

    pattern = r"\b\d{3}-\d{3}-\d{4}\b"

    phone_numbers = re.findall(pattern, text)

    return phone_numbers

Q.5. Write a Python program to replace all occurrences of a word with another word in a text using regular expressions.

import re


def replace_word(original_word, new_word, text):

    pattern = rf"\b{original_word}\b"

    modified_text = re.sub(pattern, new_word, text)

    return modified_text

Fill in the Blanks


1. In Python, regular expressions are handled using the ______ module.

Ans. re

2. The ______ function in the re module is used to find all occurrences of a pattern in a text.

Ans. re.findall()

3. The ______ character in a regular expression matches any single character except a newline.

Ans. dot (.)

4. The ______ character in a regular expression matches zero or more occurrences of the previous pattern.

Ans. asterisk (*)

5. The ______ character in a regular expression matches one or more occurrences of the previous pattern.

Ans. plus (+)

True or False


1. Regular expressions can only be used to search for patterns in strings. (True/False)

Ans. False

2. The re.findall() function returns a list of all occurrences of a pattern in a text. (True/False)

Ans. True

3. The metacharacter '*' in regular expressions matches exactly one occurrence of the previous pattern. (True/False)

Ans. False

4. The 're' module in Python is part of the standard library. (True/False)

Ans. True

5. Regular expressions are case-sensitive by default in Python. (True/False)

Ans. True

Hands-On Questions


Q.1. Write a Python program that extracts all email addresses from a given text and stores them in a list.

import re


def extract_email_addresses(text):

    pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b"

    email_addresses = re.findall(pattern, text)

    return email_addresses

Q.2. Write a Python function that takes a list of words and returns a new list containing only the words that start with a vowel.

import re


def filter_vowel_words(words):

    pattern = r"\b[aeiouAEIOU]\w+\b"

    vowel_words = [word for word in words if re.match(pattern, word)]

    return vowel_words

Q.3. Write a Python program that removes all punctuation marks from a given text using regular expressions.

import re


def remove_punctuation(text):

    pattern = r"[^\w\s]"

    cleaned_text = re.sub(pattern, "", text)

    return cleaned_text

Q.4. Write a Python function that checks if a given string is a valid URL format using regular expressions.

import re


def is_valid_url(url):

    pattern = r"^https?://[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b"

    if re.match(pattern, url):

        return True

    else:

        return False

Q.5. Write a Python program that counts the number of words in a text using regular expressions.

import re


def count_words(text):

    pattern = r"\b\w+\b"

    words = re.findall(pattern, text)

    return len(words)

The document Assignment: Patterns | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development
49 videos|38 docs|18 tests
49 videos|38 docs|18 tests
Download as PDF
Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Download the FREE EduRev App
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

Summary

,

Sample Paper

,

Assignment: Patterns | Basics of Python - Software Development

,

ppt

,

practice quizzes

,

mock tests for examination

,

Semester Notes

,

Viva Questions

,

shortcuts and tricks

,

Assignment: Patterns | Basics of Python - Software Development

,

Objective type Questions

,

Exam

,

past year papers

,

Assignment: Patterns | Basics of Python - Software Development

,

Important questions

,

Previous Year Questions with Solutions

,

Extra Questions

,

study material

,

pdf

,

video lectures

,

Free

,

MCQs

;