Found 10783 Articles for Python

Python - Display the Contents of a Text File in Reverse Order?

AmitDiwan
Updated on 15-Sep-2022 12:27:46

4K+ Views

We will display the contents of a text file in reverse order. For that, let us first create a text file amit.txt with the following content  Display the contents of a text file in Reverse Order with Slicing Example Let us now read the contents of the above file in reverse order − # The file to be read with open("amit.txt", "r") as myfile: my_data = myfile.read() # Reversing the data by passing -1 for [start: end: step] rev_data = my_data[::-1] # Displaying the reversed data print("Reversed data = ", rev_data) Output Reversed ... Read More

Difference Between Del and Remove() on Lists in Python?

AmitDiwan
Updated on 15-Sep-2022 12:26:34

8K+ Views

Let us understand what is Del and Remove() in a Python List before moving towards the difference. Del Keyword in Python List The del keyword in Python is used to remove an element or multiple elements from a List. We can also remove all the elements i.e. delete the entire list. Example Delete an element from a Python List using the del keyword #Create a List myList = ["Toyota", "Benz", "Audi", "Bentley"] print("List = ", myList) # Delete a single element del myList[2] print("Updated List = ", myList) Output List = ['Toyota', 'Benz', 'Audi', 'Bentley'] Updated ... Read More

Replace All Occurrences of a Python Substring with a New String?

AmitDiwan
Updated on 15-Sep-2022 12:24:19

191 Views

In this article, we will implement various example to replace all occurrences of a substring with a new string. Let’s say we have the following string − Hi, How are you? How have you been? We have to replace all occurrences of substring “How “ with “Demo”. Therefore, the output should be − Hi, Demo are you? Demo have you been? Let us now see some examples − Replace All Occurrences of a Python Substring with a New String using replace() Example In this example, we will use the replace() method to replace all occurrences of a Python Substring with ... Read More

How to Merge Elements in a Python Sequence?

AmitDiwan
Updated on 15-Sep-2022 12:19:18

2K+ Views

Python Sequences includes Strings, Lists, Tuples, etc. We can merge elements of a Python sequence using different ways. Merge Elements in a Python List Example The join() method is used to merge elements − # List myList = ['H', 'O', 'W', 'A', 'R', 'E', 'Y', 'O', 'U'] # Display the List print ("List = " + str(myList)) # Merge items using join() myList[0 : 3] = [''.join(myList[0 : 3])] # Displaying the Result print ("Result = " + str(myList)) Output List = ['H', 'O', 'W', 'A', 'R', 'E', 'Y', 'O', 'U'] Result = ['HOW', 'A', 'R', ... Read More

Python - Check If All the Characters in a String Are Alphanumeric?

AmitDiwan
Updated on 15-Sep-2022 12:17:18

421 Views

To check if all the characters in a string are alphanumeric, we can use the isalnum() method in Python and Regex also. First, let us understand what is alphanumeric. What is Alphanumeric? Alphanumeric includes both letters and numbers i.e., alphabet letter (a-z) and numbers (0-9). Examples: A, a, k, K, 8, 10, 20, etc. Let us see an example of an Alphanumeric string 8k9q2i3u4t Let us see an example of a string wherein all the characters aren’t Alphanumeric - $$###k9q2i3u4t Check If All the Characters in a String Are Alphanumeric using isalnum() We will use the built-in isalnum() ... Read More

What Does the // Operator Do?

AmitDiwan
Updated on 15-Sep-2022 12:15:37

408 Views

In Python, the // is the double slash operator i.e. Floor Divison. The // operator is used to perform division that rounds the result down to the nearest integer. The usage of the // operator is quite easy. We will also compare the results with single slash division. Let us first see the syntax: Syntax of // (double slash) Operator The a and b are 1st and 2nd number: a // b Example of // (double slash) Operator Let us now see an example to implement the double slash operator in Python - a = 37 b = 11 ... Read More

What are Pickling and Unpickling in Python?

AmitDiwan
Updated on 15-Sep-2022 12:13:28

7K+ Views

To serialize and de-serialize a Python object structure, we have the Pickle module in Python. The pickle module implements binary protocols for serializing and de-serializing a Python object structure. Pickling is the process through which a Python object hierarchy is converted into a byte stream. To serialize an object hierarchy, you simply call the dumps() function. Unpickling is the inverse operation. A byte stream from a binary file or bytes-like object is converted back into an object hierarchy. To de-serialize a data stream, you call the loads() function. Pickling and unpickling are alternatively known as serialization. What can be pickled ... Read More

Numpy Array advantage over a Nested List

AmitDiwan
Updated on 15-Sep-2022 12:11:12

1K+ Views

In this article, we will learn about the advantages of a Numpy array with a Nested List in Python. The Numpy array definitely has advantages over a Nested. Let’s see the reasons − The array in Numpy executes faster than a Nested List. A Nested List consumes more memory than a Nested List. Numpy Array NumPy is an N-dimensional array type called ndarray. It describes the collection of items of the same type. Items in the collection can be accessed using a zero-based index. Every item in an ndarray takes the same size of block in the memory. ... Read More

Explain how Python is an interpreted language

AmitDiwan
Updated on 15-Sep-2022 11:59:15

4K+ Views

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. Python is processed at runtime by the interpreter. You do not need to compile your program before executing it. This is similar to PERL and PHP. Steps of Execution Step1 − A Python source code is written by the coder. File extension: .py Step 2 − The Python source code a coder writes is compiled into python bytecode. In this process, a file with the extension .pyc gets created. Step 3 − The Virtual Machine executes the .pyc extension file. Consider the Virtual Machine is the runtime engine of ... Read More

How to use the slicing operator in Python?

AmitDiwan
Updated on 15-Sep-2022 11:54:39

2K+ Views

The slice operator is used to slice a string. The slice() function can also be use for the same purpose. We will work around the slice operator with some examples What is Slicing? Slicing in Python gets a sub-string from a string. The slicing range is set as parameters i.e. start, stop and step Syntax Let us see the syntax # slicing from index start to index stop-1 arr[start:stop] # slicing from index start to the end arr[start:] # slicing from the beginning to index stop - 1 arr[:stop] # slicing from the index start to index ... Read More

Advertisements