Programming Articles - Page 1759 of 3363

Custom length Matrix in Python

Pradeep Elance
Updated on 26-Aug-2020 06:56:44

207 Views

Sometimes when creating a matrix using python we may need to control how many times a given element is repeated in the resulting matrix. In this articled we will see how to create a matrix with required number of elements when the elements are given as a list.Using zipWe declare a list with elements to be used in the matrix. Then we declare another list which will hold the number of occurrences of the element in the matrix. Using the zip function we can create the resulting matrix which will involve a for loop to organize the elements.Example Live DemolistA = ... Read More

Creating DataFrame from dict of narray-lists in Python

Pradeep Elance
Updated on 26-Aug-2020 06:55:14

633 Views

Pandas is a very widely used python library for data processing and data analysis. In this article we will see how we we can create pandas dataframe from given python dictionaries and lists.From dictionary with listsDictionaries are key value pairs. If we take a python dictionary which has key and a list as a value then we can directly use the DataFrame method on the given dictionary to create the pandas data frame.Example Live Demoimport pandas as pd # Dictionary for Exam Schedule Exam_Schedule = { 'Exam Day': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], 'Exam Subject': ['Chemisry', 'Physics', 'Maths', 'English', 'Biology'], ... Read More

Creating a Pandas dataframe column based on a given condition in Python

Pradeep Elance
Updated on 26-Aug-2020 06:51:46

330 Views

Pandas creates data frames to process the data in a python program. In this article we will see how we can add a new column to an existing dataframe based on certain conditions.The Given Data FrameBelow is the given pandas DataFrame to which we will add the additional columns. It describes the Days and Subjects of an examination.Example Live Demoimport pandas as pd # Lists for Exam subjects and Days Days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] Sub = ['Chemisry', 'Physics', 'Maths', 'English', 'Biology'] # Dictionary for Exam Schedule Exam_Subjects = {'Exam Day': Days,           ... Read More

Create and write on excel file using xlsxwriter module in Python

Pradeep Elance
Updated on 26-Aug-2020 06:43:31

1K+ Views

Python’s wide availability of libraries enables it to interact with Microsoft excel which is a very widely used data processing tool. In this article we will see how we can use the module named xlsxwriter to create and write into a excel file. It cannot write into existing excel file.Writing to Each CellWe can write to each cell of an excel sheet by writing to the name of the cell. In the below example we create a workbook and then ass a worksheet to it. Finally write to the cells of the worksheet using the write() method.Exampleimport xlsxwriter # ... Read More

Create a Pandas Dataframe from a dict of equal length lists in Python

Pradeep Elance
Updated on 26-Aug-2020 06:39:17

338 Views

The Dataframe in pandas can be created using various options. One of the option is to take a dictionary and convert it to a Dataframe. In this article we will see how to take three lists of equal length and convert them to a pandas dataframe using a python dictionary.Uisng Lists and DictionaryIn this approach we have the lists declared individually. Then each of them is used as a value for the appropriate key inside a dictionary definition. Finally the pandas method called pd.Dataframe is applied to the dictionary.Example Live Demoimport pandas as pd # Lists for Exam schedule Days ... Read More

Python to create a digital clock using Tkinter

Pradeep Elance
Updated on 26-Aug-2020 06:35:01

3K+ Views

Python Tkinter can be used to create all kinds of GUI programs for the web and desktop. In this article we will see how to create a digital clock displaying hour, minute and seconds live.We use the time module to import the method strftime which displays the time in Hour, minute and seconds format. We create a canvas to hold these values. We refresh the values of strftime after every 200 milli seconds. We define a recursive function to achieve this.Exampleimport time from tkinter import * canvas = Tk() canvas.title("Digital Clock") canvas.geometry("350x200") canvas.resizable(1, 1) label = Label(canvas, font=("Courier", 30, 'bold'), ... Read More

Progressbar widget in Python Tkinter

Pradeep Elance
Updated on 26-Aug-2020 06:28:14

1K+ Views

The progressbar is a common GUI element which is used to show the progress of certain task. In tis article we will see how to create a progressbar using the Python tkinter GUI library.In the below program we have imported the progressbar sub-module of tkinter.ttk module. Then used the style object to create the style options and supply the value for the length of the button as well as value of the progress.Exampleimport tkinter as tk from tkinter.ttk import Progressbar from tkinter import ttk canv = tk.Tk() canv.title("Tkinter Progressbar") canv.geometry('250x100') style = ttk.Style() style.theme_use('default') style.configure("grey.Horizontal.TProgressbar", background='blue') bar = Progressbar(canv, length=180, ... Read More

Argument Parsing in Python

Pradeep Elance
Updated on 26-Aug-2020 06:25:32

2K+ Views

Every programming language has a feature to create scripts and run them from the terminal or being called by other programs. When running such scripts we often need to pass on arguments needed by the script for various functions to be executed inside the script. In this article we will see what are the various ways to pass arguments into a python script.Using sys.argvThis is a inbuilt module sys.argv can process arguments that are passed on with the script. By default the first argument which is considered at sys.argv[0] is the file name. the rest of the arguments are indexed ... Read More

Add style to Python tkinter button

Pradeep Elance
Updated on 26-Aug-2020 06:22:39

4K+ Views

Tkinter has great support for creating the GUI programs based on python. It offers different ways of styling a button on the Tkinter canvas based on its font, size, colour etc. In this article we will see how to apply style to specific buttons or all buttons in general on the canvas.Applying to Specific ButtonsLets consider the case when we have two buttons in the canvas and we want to apply some styling only to the first button. We use the W.TButton as part of the configuration along with the font and the foreground colour.Examplefrom tkinter import * from tkinter.ttk ... Read More

Find four factors of N with maximum product and sum equal to N - Set-2 in Python Program

Arnab Chakraborty
Updated on 25-Aug-2020 12:05:51

171 Views

Suppose we have a number N, we have to find all factors of N and return the product of four factors of N such that: The sum of the four factors is same as N. The product of the four factors is maximum. All four factors can be equal to each other to maximize the product.So, if the input is like N = 60, then the output will be All the factors are -> 1 2 3 4 5 6 10 12 15 20 30 60 and product is 50625, as we 15 has been selected four times to make ... Read More

Advertisements