Found 10784 Articles for Python

Adding value to sublists in Python

Pradeep Elance
Updated on 23-Dec-2019 10:35:02

724 Views

Sometimes we need to introduce an additional value to an already existing list. In this article we will see how the new value or values can be inserted into an already existing list by combining with each item of the existing list.Using For LoopIf we take a list which has items of the same length, we can use this method to introduce new values in each of the item of the list. In the below example we are taking a list ofExample Live DemoList = [[10, 20], [14, 8], ['Mon', 'Tue']] print("Given List: " + str(List)) s = "Rise" t = ... Read More

Turtle graphics using Python

Pradeep Elance
Updated on 23-Dec-2019 10:33:05

22K+ Views

Turtle is a Python library to draw graphics. After we import Turtle we can give commands like forward, backward, right, left etc. This commands will draw different shapes when we. When We combine Search commands we can create many nice graphics in the below example we will see some simple scenarios and then some Complex ones where nice graphics is created.Simple Turtle Commandsforward(10)  It moves the turtle (arrow) forward by 10 pixels.backward(5)  It moves the turtle (arrow) backward by 5 pixels right(35)  It moves the turtle (arrow) clockwise by an angle of 35 degrees.left(55)  It moves the turtle (arrow) counter-clockwise by ... Read More

Python - Get the Index of first element greater than K

Pradeep Elance
Updated on 23-Dec-2019 10:25:05

2K+ Views

The values of items in a python list are not necessarily in any sorted order. More over there may be situation when we are interested only in certain values greater than a specific value. In this article we will see how we can get theUsing EnumerationUsing enumeration we get both the index and value of the elements in the list. Then we apply the greater than condition to get only the first element where the condition is satisfied. The next function goes through each list element one by one.Example Live DemoList = [21, 10, 24, 40.5, 11] print("Given list: " + ... Read More

Python - Get sum of tuples having same first value

Pradeep Elance
Updated on 23-Dec-2019 10:22:16

346 Views

Tuples are python collections or arrays which are ordered but unchangeable. If we get a number of tuples where the first element is the same, then we may have a scenario when we need to add the second elements of those tuples whose first elements are equal.Using map and for loopIn this method we will first consider a list made up of tuples. Then convert them to dictionary so that we can associate the elements in the tuple as key value pair. Then we apply the for loop with summing the value for each key o the dictionary. Finally use ... Read More

Python - geometry method in Tkinter

Pradeep Elance
Updated on 23-Dec-2019 10:11:32

6K+ Views

Python has capability to create GUI applications using the Tkinter library. The library provides many methods useful for GUI applications. The geometry method is a fundamental one which decides the size, position and some other attributes of the screen layout we are going to create.Example - 1In the below program we create a window of size 22x200 pixels using the geometry method. Then we add a button to it and decide button position in the window using the side and pady options.Examplefrom tkinter import * base = Tk() base.geometry('200x200') stud = Button(base, text = 'Tutorialspoint', font =('Courier', 14, 'bold')) stud.pack(side ... Read More

Python - Check if two lists have any element in common

Pradeep Elance
Updated on 23-Dec-2019 10:09:16

4K+ Views

During manipulating data using python lists, we come across a situation where we need to know if two lists are entirely different from each other or they have any element in common. This can be found out by comparing the elements in the two lists with the below approaches decribed.Using InIn a for loop we use the in clause to check in an element is present in the list or not. We will stretch this logic to compare the elements of the lists by choosing an element from first list and checking its presence in the second list. So we ... Read More

Getting screens height and width using Tkinter Python

Pradeep Elance
Updated on 23-Dec-2019 10:00:44

335 Views

Tkinter is the library which gives GUI programming capability to python programs. As part of GUI creation we need to create screen layouts of different sizes and depth. In this program we will see how to calculate the size of a screen in terms of pixels as well as in mm. We can also get the depth of the screen in pixels. There are various methods available as part of Tkinter which we use for this.Examplefrom tkinter import * # creating tkinter window base = Tk() #screen's length and width in pixels and mm length_1= base.winfo_screenheight() width_1= base.winfo_screenwidth() length_2 = ... Read More

Python program to find the smallest number in a list

Pavitra
Updated on 23-Dec-2019 09:30:14

246 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given al list, we need to display the smallest number available in the listHere we can either sort the list and get the smallest element or use the built-in min() function to get the smallest element.Now let’s observe the concept in the implementation below −Example Live Demolist1 = [101, 120, 104, 145, 99] # sorting using built-in function list1.sort() print("Smallest element is:", list1[0])OutputSmallest element is: 99All the variables are declared in the local scope and their references are seen in the figure ... Read More

Python program to find the second maximum value in Dictionary

Pavitra
Updated on 11-Jul-2020 11:28:51

563 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given two integers, we need to print the second maximum value in the dictionaryNow let’s observe the concept in the implementation below−Approach 1 − Using sorted() function by negative indexesExample Live Demo#input example_dict ={"tutor":3, "tutorials":15, "point":9, "tutorialspoint":19} # sorting the given list and get the second last element print(list(sorted(example_dict.values()))[-2])Output15Approach 2 − Here we use sort method on the list and then access the second largest elementExample Live Demolist1 = [11, 22, 1, 2, 5, 67, 21, 32] # using built-in sort method list1.sort() # ... Read More

Python program to find the second largest number in a list

Pavitra
Updated on 11-Jul-2020 11:30:42

3K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to display the second largest number in a list.There are three approaches to solve the problem−Approach 1 − We use the set() function & remove() functionExample Live Demolist1 = [11, 22, 1, 2, 5, 67, 21, 32] # to get unique elements new_list = set(list1) # removing the largest element from list1 new_list.remove(max(new_list)) # now computing the max element by built-in method? print(max(new_list))Output32Approach 2 − We use sort() method and negative indexesExample Live Demolist1 = [11, 22, 1, 2, ... Read More

Advertisements