Found 10476 Articles for Python

Tuple multiplication in Python

AmitDiwan
Updated on 11-Mar-2021 09:53:50

2K+ Views

When it is required to perform tuple multiplication, the 'zip' method and the generator expression can be used.The zip method takes iterables, aggregates them into a tuple, and returns it as the result.Generator is a simple way of creating iterators. It automatically implements a class with '__iter__()' and '__next__()' methods and keeps track of the internal states, as well as raises 'StopIteration' exception when no values are present that could be returned.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = (23, 45, 12, 56, 78) my_tuple_2 = (89, 41, 76, 0, 11) print("The first tuple is : ") ... Read More

Check if element is present in tuple in Python

AmitDiwan
Updated on 11-Mar-2021 09:52:02

3K+ Views

When it is required to check if an element is present in the tuple or not, a simple loop can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They are important contains since they ensure read-only access.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = (23, 45, 12, 56, 78, 0) print("The first tuple is : ") print(my_tuple_1) N = 12 print("The value of 'N' has been initialized") my_result = False ... Read More

N element incremental tuples in Python

AmitDiwan
Updated on 11-Mar-2021 09:51:21

210 Views

When it is required to create 'N' element incremental tuples, generator expression and 'tuple' method can be used.Below is a demonstration of the same −ExampleLive DemoN = 3 print("The value of 'N' has been initialized") print("The number of times it has to be repeated is : ") print(N) my_result = tuple((elem, ) * N for elem in range(1, 6)) print("The tuple sequence is : ") print(my_result)OutputThe value of 'N' has been initialized The number of times it has to be repeated is : 3 The tuple sequence is : ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, ... Read More

How do I create an automatically updating GUI using Tkinter in Python?

pawandeep
Updated on 11-Mar-2021 09:48:42

16K+ Views

GUI window has many controls such as labels, buttons, text boxes, etc. We may sometimes want the content of our controls such as labels to update automatically while we are viewing the window.We can use after() to run a function after a certain time. For example, 1000 milliseconds mean 1 second. The function which we call continuously after a certain amount of time will update the text or any updation you want to happen.We have a label on our window. We want the text of the label to update automatically after 1 second. To keep the example easy, suppose we ... Read More

Create a tuple from string and list in Python

AmitDiwan
Updated on 11-Mar-2021 09:48:58

179 Views

When it is required to create a tuple from a string and a list, the tuple method can be used.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).Below is a demonstration of the same −ExampleLive Demomy_list_1 = ['Hey', 'there', 'How', 'are', 'you'] my_list_2 = 'Jane' print("The first list is :") print(my_list_1) print("The string is :") print(my_list_2) my_result = tuple(my_list_1 + [my_list_2]) print("The aggregated tuple is : ") print(my_result)OutputThe first list is : ['Hey', 'there', 'How', 'are', 'you'] The string list is : Jane ... Read More

Filter Tuples by Kth element from List in Python

AmitDiwan
Updated on 11-Mar-2021 09:47:37

238 Views

When it is required to filter the tuples by the 'K'th element from a list, list comprehension and 'in' operators can be used.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).A list of tuple basically contains tuples enclosed in a list. The 'in' operators checks to see if the specific data is present in the iterable/data or not.The list comprehension is a shorthand to iterate through the list and perform operations on it.Below is a demonstration of the same −ExampleLive Demomy_list = [(1, 21), (25, 'abc', ... Read More

How to develop a game in Python?

pawandeep
Updated on 25-Feb-2022 08:06:02

528 Views

We can develop games in Python using PyGame. PyGame is a Python module that is used to develop games. This module includes computer graphics and sound libraries used in video game development.Install PyGameTo develop game in Python, we need PyGame. Therefore, we need to install PyGame.We should Python and pip preinstalled on our system before installing PyGame.Open the terminal and type the following command to install PyGame.py -m pip install -U pygame --userImport PyGameThe PyGame module needs to be imported in Python IDE before writing the program for the game. There is some common code that includes displaying the pygame ... Read More

Tuple Division in Python

AmitDiwan
Updated on 11-Mar-2021 09:46:49

3K+ Views

When it is required to perform tuple division in Python, the 'zip' method and generator expressions can be used.The zip method takes iterables, aggregates them into a tuple, and returns it as the result.Generator is a simple way of creating iterators. It automatically implements a class with '__iter__()' and '__next__()' methods and keeps track of the internal states, as well as raises 'StopIteration' exception when no values are present that could be returned.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = ( 7, 8, 3, 4, 3, 2) my_tuple_2 = (9, 6, 8, 2, 1, 4) print ("The ... Read More

How to convert list to dictionary in Python?

pawandeep
Updated on 11-Mar-2021 09:46:37

2K+ Views

The list is a linear data structure containing data elements.Example1, 2, 3, 4, 5, 6Dictionary is a data structure consisting of key: value pairs. Keys are unique and each key has some value associated with it.Example1:2, 3:4, 5:6Given a list, convert this list into the dictionary, such that the odd position elements are the keys and the even position elements are the values as depicted in the above example.Method 1 − Iterating over the listExample Live Demodef convert(l):    dic={}    for i in range(0, len(l), 2):       dic[l[i]]=l[i+1]    return dic ar=[1, 'Delhi', 2, 'Kolkata', 3, ... Read More

How to install matplotlib in Python?

pawandeep
Updated on 31-Aug-2023 01:39:31

177K+ Views

Matplotlib is a Python library that helps to plot graphs. It is used in data visualization and graphical plotting.To use matplotlib, we need to install it.Step 1 − Make sure Python and pip is preinstalled on your systemType the following commands in the command prompt to check is python and pip is installed on your system.To check Pythonpython --versionIf python is successfully installed, the version of python installed on your system will be displayed.To check pippip -VThe version of pip will be displayed, if it is successfully installed on your system.Step 2 − Install MatplotlibMatplotlib can be installed using pip. ... Read More

Advertisements