Write a Python program to:
- Create a list of integers.
- Add an element to the end of the list.
- Remove the third element from the list.
- Create a list of integers [1, 2, 3, 4, 5].
- Add 6 to the end of the list.
- Remove the first element from the list.
- Find the length of the list.
- Extract a sublist containing elements from index 2 to 4.
- Check if 3 is present in the list.
-
String Concatenation: Write a Python program that takes two strings as input from the user and concatenates them together. Then, print the concatenated string.
-
String Length: Write a Python program that takes a string as input from the user and prints its length.
-
String Slicing: Write a Python program that takes a string as input from the user and prints the first three characters, the last three characters, and every second character of the string.
-
String Reversal: Write a Python program that takes a string as input from the user and prints its reverse.
-
Counting Occurrences: Write a Python program that takes a string and a character as input from the user and counts the number of occurrences of that character in the string.
-
String Capitalization: Write a Python program that takes a string as input from the user and prints it in uppercase and lowercase.
-
Substring Check: Write a Python program that takes two strings as input from the user and checks if the second string is a substring of the first string. Print "Substring found" if it is, otherwise print "Substring not found".
-
Replacing Substrings: Write a Python program that takes a string as input from the user and replaces all occurrences of a specified substring with another substring.
-
String Splitting: Write a Python program that takes a sentence as input from the user and splits it into words. Then, print each word on a separate line.
-
Removing Whitespaces: Write a Python program that takes a string as input from the user and removes all leading and trailing whitespaces from it.
- Write a Python program that takes a list of strings as input and joins them into a single string using a space as the separator. Then, print the joined string.
- Write a Python program that takes a list of strings and a separator as input from the user and joins them into a single string using the provided separator. Then, print the joined string.
- Write a Python program that takes a string as input from the user and joins its characters into a single string using a hyphen ('-') as the separator. Then, print the joined string.
- Write a Python program that takes a list of numbers as input and joins them into a single string, converting each number to a string in the process. Use a comma (,) as the separator. Then, print the joined string.
- Write a Python program that takes a sentence as input from the user and joins its words into a single string using an underscore ('_') as the separator. Then, print the joined string.
- Write a Python program that takes several string fragments as input from the user and joins them into a single string. Use a space as the separator. Then, print the joined string. Dictionary Operations:
Write a Python program to:
- Create a dictionary with student names as keys and their scores as values.
- Remove a student from the dictionary.
- Add a new student and score to the dictionary.
- Check if a student is present in the dictionary.
- Print all keys and values in the dictionary.
- Create a dictionary with keys 'apple', 'banana', and 'orange' having corresponding values 10, 20, and 30.
- Add a new key 'grape' with value 15.
- Remove the key 'banana' from the dictionary.
- Check if 'orange' is present in the dictionary.
- Print all keys in the dictionary.
- Print all values in the dictionary.
-
Create a dictionary representing a person with keys "name", "age", and "city" with corresponding values.
-
Access and print the value associated with the "age" key.
-
Adding and Updating Dictionary Entries:
-
Add a new key-value pair to the dictionary representing the person's "gender".
-
Update the value associated with the "age" key to a new value.
-
Removing Entries from Dictionary:
-
Remove the "city" key from the dictionary.
-
Use the pop() method to remove the value associated with the "gender" key and store it in a variable.
-
Checking Key Existence:
-
Check if the dictionary contains a key "city". Print "City found" if it does, otherwise print "City not found".
- Iterate over the dictionary and print each key-value pair in the format "key: value".
- Clear all entries from the dictionary using the clear() method and print the dictionary to verify it's empty.
- Create a nested dictionary representing information about multiple persons, where each person has their own dictionary of attributes (name, age, city, etc.).
- Access and print the name of one of the persons.
Write a Python program to:
- Create a tuple of strings.
- Concatenate two tuples.
- Find the index of a specific element in the tuple.
- Count the occurrences of a particular element in the tuple.
- Slice the tuple and print a subset of elements.
- Create a tuple of strings ("apple", "banana", "cherry", "date").
- Access the third element of the tuple.
- Find the index of "banana" in the tuple.
- Count the occurrences of "apple" in the tuple.
- Concatenate the tuple with ("elderberry", "fig").
- Check if "grape" is present in the tuple.
Write a Python program to:
- Create two sets of integers.
- Find the union, intersection, and difference of the two sets.
- Check if one set is a subset of another.
- Add and remove elements from a set.
- Find the length of a set.
- Create two sets of integers {1, 2, 3, 4, 5} and {4, 5, 6, 7, 8}.
- Find the union of the two sets.
- Find the intersection of the two sets.
- Find the difference between the first set and the second set.
- Add 9 to the first set.
- Remove 2 from the second set.
- Check if {1, 2, 3} is a subset of the first set.
Write a Python program to:
- Create two frozensets with some common elements.
- Find the union of the two frozensets.
- Find the intersection of the two frozensets.
- Find the difference between the two frozensets.
- Check if one frozenset is a subset of another.