Python Articles

Page 823 of 852

Python Program for Merge Sort

Pavitra
Pavitra
Updated on 20-Dec-2019 781 Views

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 More

Different messages in Tkinter - Python

Pradeep Elance
Pradeep Elance
Updated on 19-Dec-2019 521 Views

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 More

Create a stopwatch using python

Pradeep Elance
Pradeep Elance
Updated on 19-Dec-2019 1K+ Views

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 More

What is the best way to handle list empty exception in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 19-Dec-2019 2K+ Views

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 More

How to remove tabs and newlines using Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 19-Dec-2019 1K+ Views

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 More

How to compare two strings using regex in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 19-Dec-2019 2K+ Views

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 More

How to divide a string by line break or period with Python regular expressions?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 16-Dec-2019 418 Views

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 More

How to create file of particular size in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Dec-2019 5K+ Views

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 More

How can I do "cd" in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Dec-2019 15K+ Views

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 More

What are common practices for modifying Python modules?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 11-Dec-2019 382 Views

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
Showing 8221–8230 of 8,519 articles
« Prev 1 821 822 823 824 825 852 Next »
Advertisements