



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




An array is a fixed number of elements of the same type stored sequentially in memory. The size of the array is referred to as its dimension. To declare an array in Python, we can use the following syntax:
python arr = [element1, element2, element3, ...]
A set is a collection of objects that is unordered and unindexed. In Python, sets are written with curly brackets {}.
Set Operations
Union : The union of two sets A and B is the set consisting of all elements in A plus all elements in B, denoted as A ∪ B or A + B. Example: If A = {a, b, c, d} and B = {b, c, e, f, g}, then A ∪ B = {a, b, c, d, e, f, g}.
Intersection : The intersection of two sets A and B is the set consisting of all elements that occur in both A and B, denoted as A ∩ B, A · B, or AB. Example: If A = {a, b, c, d} and B = {b, c, e, f, g}, then A ∩ B = {b, c}.
Difference : The set consisting of all elements of a set A that do not belong to a set B is called the difference of A and B, denoted as A - B. Example: If A = {a, b, c, d} and B = {b, c, e, f, g}, then A - B = {a, d}.
Complement : The complement of a set A, denoted as A^c or A¯, is the set of all elements that are in the universal set S but are not in A.
The provided code demonstrates the implementation of set operations on array data structures in Python without using built-in functions.
List of students who play both cricket and badminton : The code iterates through the groupB list and checks if each element is present in the groupA list. If a match is found, the element is added to the resultlistCB list.
List of students who play either cricket or badminton but not both : The code first checks if the length of groupA is less than the length of groupB. If so, it iterates through groupB and checks if each element is not present in groupA. If not found, the element is added to the resultlistCBN list. The same process is repeated for groupA elements.
Number of students who play neither cricket nor badminton : The code iterates through the groupC list and checks if each element is not present in both groupA and groupB. If the element is not found in either group, it is added to the resultlistCBNF list.
List of students who play cricket and football but not badminton : The code first creates a resultlistCF list by iterating through groupC and checking if each element is present in groupA. The length of resultlistCF is then stored in lenCF. Finally, the code iterates through resultlistCF and checks if each element is not present in groupB. If not found, the element is added to the resultlistCFNB list.
What is a linear data structure? A linear data structure is a data structure where data elements are arranged in a sequential manner, and each member element is connected to its previous and next element. Such data structures are easy to implement as computer memory is also sequential.
What are the set operations? The main set operations are union, intersection, difference, and symmetric difference.
Write an array with three elements. python a = [1, 2, 3]
How can Python programming be useful to implement set theory? Grouping objects into a set can be useful in programming, and Python provides a built-in set type to do so. Sets are distinguished from other object types by the unique operations that can be performed on them, such as union, intersection, and difference.
String Manipulation Operations
To display the word with the longest length in a given string, we can use the following approach:
Split the input string into a list of words. Find the length of each word and keep track of the longest word. Print the longest word.
Example: python input_string = "The quick brown fox jumps over the lazy dog" words = input_string.split() longest_word =
input_string.find(substring) if index == -1: print(f"The substring '{substring}' was not found in the string.") else: print(f"The substring '{substring}' first appears at index {index}.")
Output: The substring 'fox' first appears at index 16.
To count the occurrences of each word in a given string, we can use the following approach:
Split the input string into a list of words. Create a dictionary to store the word counts. Iterate through the list of words and update the counts in the dictionary. Print the word counts.
Example: ```python input_string = "The quick brown fox jumps over the lazy dog. The dog barks." word_counts = {} words = input_string.split() for word in words: if word in word_counts: word_counts[word] += 1 else: word_counts[word] = 1
for word, count in word_counts.items(): print(f"The word '{word}' appears {count} times.") ```
Output: The word 'The' appears 2 times. The word 'quick' appears 1 times. The word 'brown' appears 1 times. The word 'fox' appears 1 times. The word 'jumps' appears 1 times. The word 'over' appears 1 times. The word 'the' appears 2 times. The word 'lazy' appears 1 times. The word 'dog.' appears 1 times. The word 'dog' appears 1 times. The word 'barks.' appears 1 times.
In this document, we have covered various string manipulation operations in Python, including:
Displaying the word with the longest length in a given string. Determining the frequency of occurrence of a particular character in a string. Checking if a given string is a palindrome. Finding the index of the first appearance of a substring in a string. Counting the occurrences of each word in a given string.
These operations can be useful in a wide range of applications, from text processing to data analysis.
String Operations in Python
To display the word with the longest length in a given string:
Take the input string from the user. Split the string into a list of words. Initialize variables m and word to keep track of the length of the longest word and its index, respectively. Iterate through the list of words and update m and word if a longer word is found. Print the word with the longest length.
Example: python str1 = input('Enter the string: ') list1 = str1.split() m = 0 word = 0 for i in range(len(list1)): if m < len(list1[i]): m = len(list1[i]) word = i print('The word with the longest length:', list1[word])
To determine the frequency of occurrence of a particular character in a given string:
Take the input string and the character from the user. Initialize a counter variable to keep track of the number of occurrences. Iterate through the string and increment the counter whenever the character is found. Print the frequency of the character in the string.
Example: python str1 = input('Enter the string: ') char = input('Enter the character: ') counter = 0 for i in range(len(str1)): if char == str1[i]: counter += 1 print('The character', char, 'is present', counter, 'times in the string', str1)
To count the occurrences of each word in a given string:
Take the input string from the user. Split the string into a list of words. Create a set to remove duplicate words and then convert it back to a list. Iterate through the list of unique words and count the occurrences of each word in the original list. Store the word and its count in a list and print the result.
Example: python str1 = input('Enter the input: ') list1 = str1.split() list2 = set(list1) # Delete duplicates list3 =
String Operations in Python
Python provides a wide range of string operations and methods. Some of the commonly used string operations are:
join(): Joins the elements of an iterable to the end of the string. ljust(): Returns a left-justified version of the string. lower(): Converts a string to lowercase. lstrip(): Returns a left-trimmed version of the string.
Reversing a String
To reverse a string in Python, you can use the slice notation [::-1]. For example:
python txt = 'Hello World'[::-1] print(txt) # Output: 'dlroW olleH'
Counting Character Frequency in a String
To count the number of occurrences of each character in a string, you can use a dictionary. Here's an example:
python def char_frequency(str1): dict = {} for n in str1: keys = dict.keys() if n in keys: dict[n] += 1 else: dict[n] = 1 return dict
Removing a Character from a String
To remove a specific character from a string, you can use the replace() method or the slice notation. Here's an example using the charRemoveAt() function:
str = 'India is my country' print(charRemoveAt(str, 7)) # Output: 'India my country' ```