0% found this document useful (0 votes)
57 views16 pages

Python String and List Manipulation Guide

The document discusses Python programming concepts related to string manipulation, list manipulation, tuples, and dictionaries. It provides 12 questions to write programs on these topics, including checking if a string is a palindrome, printing a pattern without nested loops, checking if a number contains zero, manipulating lists and tuples, and manipulating dictionaries.

Uploaded by

Prabhjot Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views16 pages

Python String and List Manipulation Guide

The document discusses Python programming concepts related to string manipulation, list manipulation, tuples, and dictionaries. It provides 12 questions to write programs on these topics, including checking if a string is a palindrome, printing a pattern without nested loops, checking if a number contains zero, manipulating lists and tuples, and manipulating dictionaries.

Uploaded by

Prabhjot Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python programming….

Topic –
String manipulation,
List manipulation,
Tuples &
Dictionaries
Contents:-

String Manipulation…...

 Write A program To Input A String And Check If It Is A


Palindrome String Using A String Slice.
 Write A Program That Prints The Pattern Without Using
Nested Loop.
 Write A Program To Input An Integer And Check If It Contains
zero In It.

List manipulation……

 Write A Program To Create A Copy Of List. In The List’s Copy,


Add 10 to Its First and Last Elements. Then Display The List
 Write A Program That Input A List, Replicates It Twice And
Then Prints The Sorted List In Ascending And Descending
Order.
 Write A Program To Print Elements Of List[ ] In
Separate Lines Along With Element’s Both Indexes
(Positive Nd Negative.).


Tuples……

 Write a program to check if the elements in the first half of a tuple


are sorted in ascending order.
 Write a program to input a tuple and check if it contains all
elements as same
 Write a program to check if the minimum element of a tuple lies
at the middle position of a tuple.

Dictionaries…..

 Your school has decided to deposit scholarship amount of rs.2500/-


to some selected students. Write a program to input the selected
students” roll numbers and create a dictionary for the same.
 Write a program to create a dictionary with 10 keys 0….9
 Write a program to create a third dictionary from two dictionaries
having some keys, in way so that the values of common keys are
added in the third dictionary.
Q1. WRITE APROGRAM TO INPUT A STRING AND CHECK
IF IT IS A PALINDROME STRING USING A STRING SLICE.
Q2. WRITE A PROGRAM THAT PRINTS THE PATTERN
WITHOUT USING NESTED LOOP.
Q3. WRITE A PROGRAM TO INPUT AN INTEGER AND
CHECK IF IT CONTAINS 0 IN IT.
Q4. WRITE A PROGRAM TO CREATE A COPY OF LIST. IN
THE LIST’S COPY, ADD 10 TO ITS FIRST AND LAST
ELEMeNTS. THEN DISPLAY THE LIST.
Q5. WRITE A PROGRAM THAT INPUT A LIST,
REPLICATES IT TWICE AND THEN PRINTS THE SORTED
LIST IN ASCENDING AND DESCENDING ORDER.
Q6. WRITE A PROGRAM TO PRINT ELEMEMTS OF LIST[ ]
IN SEPARATE LINES ALONG WITH ELEMENT’S BOTH
INDEXES (POSITIVE ND NEGATIVE.).
Q7. Write a program to check if the elements in
the first half of a tuple are sorted in ascending
order.
Q8. Write a program to input a tuple and check if
it contains all elements as same.
Q9. Write a program to check if the minimum
element of a tuple lies at the middle position of a
tuple.
Q10. Your school has decided to deposit
scholarship amount of rs.2500/- to some selected
students. Write a program to input the selected
students” roll numbers and create a dictionary
for the same.
Q11. Write a program to create a dictionary with
10 keys 0….9.
Q12. Write a program to create a third
dictionary from two dictionaries having some
keys, in way so that the values of common keys
are added in the third dictionary.
Thank you……

Common questions

Powered by AI

Create a dictionary where the keys are roll numbers and all values are set to the scholarship amount, for instance Rs. 2500/-. Input roll numbers as a list or directly in the code. Iterate over these roll numbers, assigning each to a key in the dictionary with the value set to 2500 .

Use a dictionary comprehension to create a dictionary with keys as integers from 0 through 9, each initialized to zero or another default value. This can be achieved with: '{i: 0 for i in range(10)}', which systematically generates keys using 'range()' .

To create a third dictionary by summing values of common keys from two dictionaries, iterate over the keys of the first dictionary. For each key, check if it exists in the second dictionary. If it does, assign the sum of the values from both dictionaries to this key in the third dictionary. Otherwise, carry over the value from the first dictionary. Repeat similarly for the second dictionary .

To check if a string is a palindrome using string slicing, you can compare the string to its reverse. This can be done by slicing the string with the syntax [::-1], which reverses the string. For example, if 's' is the input string, checking if 's' == 's'[::-1] will determine if it is a palindrome .

To print each element of a list with its positive and negative indices, iterate over the list using a loop and employ the built-in 'enumerate()' function. For each element, the positive index is given by the loop variable, while the negative index can be calculated as the length of the list minus the loop variable minus one .

Create a copy of the list using list slicing (e.g., 'copied_list = original_list[:]') or the 'copy()' method. Then access the first and last element of 'copied_list' using indexing and increment them by 10. Finally, print the modified copy to display the changes .

To replicate a list twice and then sort it, first concatenate the list with itself, which can be achieved using the '+' operator (e.g., 'replicated_list = lst + lst'). Sorting in ascending order can be done using the 'sorted()' function directly. For descending order, pass 'reverse=True' as an argument to 'sorted()'. Considerations include ensuring that the list is not modified in place, which preserves the original list .

To verify if all tuple elements are identical, choose one element as a reference, typically the first element, then iterate through the tuple. Compare each element against the reference, returning true only if all comparisons justify the equality condition throughout the tuple .

To determine if the minimal element of a tuple is located in the middle position, first identify the minimal element using 'min()'. Next, calculate the middle index as len(tuple) // 2 (for zero-based indexing). Then, check if the element at this index is equal to the identified minimal element .

Divide the tuple into two halves using slicing. For the first half, apply the 'sorted()' function and compare it with the unsorted version. If they match, the half is sorted in ascending order. Specifically, 'if first_half == sorted(first_half):' will confirm the ordering .

You might also like