Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Python Articles
Page 823 of 852
Python Program for Merge Sort
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using the concept of merge sortHere we place the maximum element at the end. This is repeated until the array is sorted.Now let’s observe the solution in the implementation below −Example#merge function def merge(arr, l, m, r): n1 = m - l + 1 n2 = r- m # create arrays L = [0] * (n1) R = [0] * (n2) # Copy data to arrays ...
Read MoreDifferent messages in Tkinter - Python
Tkinter is the GUI module of python. It uses various message display options which are in response to the user actions or change in state of a running program. The message box class is used to display variety of messages like confirmation message, error message, warning message etc.Example-1The below example shows display of a message with the background colour, font size and colour etc customizable.import tkinter as tk main = tk.Tk() key = "the key to success is to focus on goals and not on obstacles" message = tk.Message(main, text = key) message.config(bg='white', font=('times', 32, 'italic')) message.pack() ...
Read MoreCreate a stopwatch using python
A stopwatch is used to measure the time interval between two events usually in seconds to minutes. It has various usage like in sports or measuring the flow of heat, current etc in an industrial setup. Python can be used to creat a stopwatch by using its tkinter library.This library will have the GUI features to create a stopwatch showing the Start, Stop and Reset option. The key component of the program is using the lable.after() module of tkinter.label.after(parent, ms, function = None) where parent: The object of the widget which is using this function. ms: Time in miliseconds. function: ...
Read MoreWhat is the best way to handle list empty exception in Python?
List is an ordered sequence of elements. Individual element in list is accessed using index starting with 0 and goes up to length-1. If index goes beyond this range, IndexError exception is encountered.In following example, an infinite loop is used to pop one element at a time. As loop tries to go even after last element is popped, IndexError exception will be encountered. We trap it using try – except mechanism.a=[1,2,3] while True: try: b=a.pop() print (b) except (IndexError): break
Read MoreHow to remove tabs and newlines using Python regular expression?
The following code removes tabs and newlines from given stringExampleimport re print re.sub(r"\s+", " ", """I find Tutorialspoint helpful""")OutputThis gives outputI find Tutorialspoint helpful
Read MoreHow to compare two strings using regex in Python?
We can compare given strings using the following codeExampleimport re s1 = 'Pink Forest' s2 = 'Pink Forrest' if bool(re.search(s1,s2))==True: print 'Strings match' else: print 'Strings do not match'OutputThis gives the outputStrings do not match
Read MoreHow to divide a string by line break or period with Python regular expressions?
The following code splits given string by a period and a line break as followsExampleimport re s = """Hi. It's nice meeting you. My name is Jason.""" result = re.findall(r'[^\s\.][^\.]+', s) print resultOutputThis gives the following output['Hi', "It's nice meeting you", 'My name is Jason']
Read MoreHow to create file of particular size in Python?
To create a file of a particular size, just seek to the byte number(size) you want to create the file of and write a byte there.For examplewith open('my_file', 'wb') as f: f.seek(1024 * 1024 * 1024) # One GB f.write('0')This creates a sparse file by not actually taking up all that space. To create a full file, you should write the whole file:with open('my_file', 'wb') as f: num_chars = 1024 * 1024 * 1024 f.write('0' * num_chars)
Read MoreHow can I do "cd" in Python?
You can change directory or cd in Python using the os module. It takes as input the relative/absolute path of the directory you want to switch to.For example>>> import os >>> os.chdir('my_folder')
Read MoreWhat are common practices for modifying Python modules?
If you are modifying a module and want to test it in the interpreter without having to restart the shell everytime you save that module, you can use the reload(moduleName) function. reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "import moduleName". It is intended for conversational use, where you have edited the source file for a module and want to test it without leaving Python and starting it again.For example>>> import mymodule >>> # Edited mymodule and want to reload it in this script >>> reload(mymodule)Note that the moduleName is the actual name of the ...
Read More