Approach:
Q. How do you display the contents of a file in Python?
Python Program to Read the Contents of a File
- Take the file name from the user.
- Use readline() function for the first line first.
- Use a while loop to print the first line and then read the remaining lines and print it till the end of file.
- Exit.
Q. How do you write to a text file in Python?
There are 6 access modes in python.
- Read Only (‘r’) : Open text file for reading.
- Read and Write (‘r+’) : Open the file for reading and writing.
- Write Only (‘w’) : Open the file for writing.
- Write and Read (‘w+’) : Open the file for reading and writing.
- Append Only (‘a’) : Open the file for writing.
- Open a file in read mode which contains a string.
- Use for loop to read each line from the text file.
- Again use for loop to read each word from the line splitted by ‘ ‘.
- Display each word from each line in the text file.
Q. What does split do in Python?
The split() method in Python returns a list of the words in the string/line , separated by the delimiter string. This method will return one or more new strings. All substrings are returned in the list datatype.
Q. How do you join two lists in Python?
Join Two Lists
- Join two list: list1 = [“a”, “b” , “c”] list2 = [1, 2, 3] list3 = list1 + list2. print(list3)
- Append list2 into list1: list1 = [“a”, “b” , “c”] list2 = [1, 2, 3] for x in list2: list1.append(x)
- Use the extend() method to add list2 at the end of list1: list1 = [“a”, “b” , “c”] list2 = [1, 2, 3] list1.extend(list2)
Q. How we can clone a list?
To clone a list, one can iterate through the original list and use the clone method to copy all the list elements and use the add method to append them to the list. Approach: Create a cloneable class, which has the clone method overridden. Create a list of the class objects from an array using the asList method.
Q. Which functions can you use to add elements to a list?
Elements can be added into the list at a particular position by making use of the insert() function. It takes in 2 arguments as inputs, the first specifies the position to be inserted in and second specifies the element to be inserted. So the string, ‘value’ is inserted at the position 4 (index 3) in sample_list.
Q. What are the two ways to add something to a list?
Here are four different ways that data can be added to an existing list.
- append() Method. Adding data to the end of a list is accomplished using the .
- insert() Method. Use the insert() method when you want to add data to the beginning or middle of a list.
- extend() Method.
- The Plus Operator (+)
Q. Can you put a function in a list Python?
Functions can be stored as elements of a list or any other data structure in Python.
Q. What is the difference between append and insert method of list?
The only difference between append() and insert() is that insert function allows us to add a specific element at a specified index of the list unlike append() where we can add the element only at end of the list.
Q. What are the two arguments of insert method of list?
The insert() method takes two parameters: index – the index where the element needs to be inserted. element – this is the element to be inserted in the list.
Q. What is the difference between append () and extend ()?
Python append() method adds an element to a list, and the extend() method concatenates the first list with another list (or another iterable). Whereas extend() method iterates over its argument adding each element to the list, extending the list.
Q. What is the difference between append and extend functions of list Explain with examples?
append adds its argument as a single element to the end of a list. The length of the list itself will increase by one. extend iterates over its argument adding each element to the list, extending the list. The length of the list will increase by however many elements were in the iterable argument.
Q. How do I append to a list?
How to append one list to another list in Python
- Use list. extend() to combine two lists. Use the syntax list1. extend(list2) to combine list1 and list2 .
- Use list. append() to add a list inside of a list. Use the syntax list1. append(list2) to add list2 to list1 .
- Other solutions. Use itertools.chain() to combine many lists.
Q. What are nested lists give an example?
A nested list is a list that appears as an element in another list. In this list, the element with index 3 is a nested list. If we print( nested[3] ), we get [10, 20] .
Q. Which is a good example of nesting?
A good example of nesting is the relationship between the DL (definition list) tag, the DT (definition term) tag, and the DD (definition description) tag. The DL tag specifies a definition list and the DT and DD tags specify the terms and descriptions of the items within the definition list.
Q. What is called nested list?
A nested list is simply a list that occurs as an element of another list (which may of course itself be an element of another list, etc.). Common reasons nested lists arise are: They’re matrices (a list of rows, where each row is itself a list, or a list of columns where each column is itself a list).