Found 10783 Articles for Python

Catching the ball game using Python

Pradeep Elance
Updated on 10-Jul-2020 09:55:19

919 Views

Python can also be used to create computer games. In this article we will see how the ball catching game can be created using python. In this game a ball keeps falling from the top of a canvas window and a bar is present at the bottom of the window. Two buttons are provided to move the bar in the left and right direction. Using mouse button presss we move the bar at the bottom to catch the falling ball. At different times the ball falls from different positions .ApproachThe approach to build the game is described in the following ... Read More

Calculate difference between adjacent elements in given list using Python

Pradeep Elance
Updated on 10-Jul-2020 09:49:06

1K+ Views

In this article we will see how we create a new list from a given list by subtracting the values in the adjacent elements of the list. We have various approaches to do that.With append and rangeIn this approach we iterate through list elements by subtracting the values using their index positions and appending the result of each subtraction to a new list. We use the range and len function to keep track of how many iterations to do.Example Live DemolistA= [25, 97, 13, 62, 14, 102] print("Given list:", listA) list_with_diff = [] for n in range(1, len(listA)):    list_with_diff.append(listA[n] ... Read More

Binding function in Python Tkinter

Pradeep Elance
Updated on 10-Jul-2020 09:47:21

2K+ Views

In python tkinter is a GUI library that can be used for various GUI programming. Such applications are useful to build desktop applications. In this article we will see one aspect of the GUI programming called Binding functions. This is about binding events to functions and methods so that when the event occurs that specific function is executed.Binding keyboard eventIn the below example we bind the press of any key from the keyboard with a function that gets executed. Once the Tkinter GUI window is open, we can press any key in the keyboard and we get a message that ... Read More

Associating a single value with all list items in Python

Pradeep Elance
Updated on 10-Jul-2020 09:44:13

247 Views

We may have a need to associate a given value with each and every element of a list. For example − there are the name of the days and we want to attach the word day as a suffix in them. Such scenarios can be handled in the following ways.With itertools.repeatWe can use the repeat method from itertools module so that the same value is used again and again when paired with the values from the given list using the zip function.Example Live Demofrom itertools import repeat listA = ['Sun', 'Mon', 'Tues'] val = 'day' print ("The Given list : ... Read More

Standard errno system symbols in Python

Pradeep Elance
Updated on 09-Jul-2020 13:54:07

351 Views

Every programming language has a error handling mechanism in which some errors are already coded into the compiler. In Python we have love which are associated with some standard pre-determined error codes. In this article we will see how to to get the error numbers as well as error codes which are inbuilt. And then take an example of how error code can be used.Error codesIn this program just list out the inbuilt error numbers and error codes. Memorial we use the error no module along with the OS module.Example Live Demoimport errno import os for i in sorted(errno.errorcode):    print(i, ... Read More

Python module to Generate secure random numbers

Pradeep Elance
Updated on 09-Jul-2020 13:52:01

210 Views

In this article we will see how we can generate secure Random numbers which can be effectively used as passwords. Along with the Random numbers we can also add letters and other characters to make it better.with secretsThe secrets module has a function called choice which can be used to generate the password of required length using a for loop and range function.Example Live Demoimport secrets import string allowed_chars = string.ascii_letters + string.digits + string.printable pswd = ''.join(secrets.choice(allowed_chars) for i in range(8)) print("The generated password is: ", pswd)OutputRunning the above code gives us the following result −The generated password is: $pB7WYwith ... Read More

Python Get the numeric prefix of given string

Pradeep Elance
Updated on 09-Jul-2020 13:49:25

291 Views

Suppose we have a string which contains numbers are the beginning. In this article we will see how to get only the numeric part of the string which is fixed at the beginning.With isdigitThe is digit function decides if the part of the string is it digit or not. So we will use takewhile function from itertools to join each part of the string which is a digit.Example Live Demofrom itertools import takewhile # Given string stringA = "347Hello" print("Given string : ", stringA) # Using takewhile res = ''.join(takewhile(str.isdigit, stringA)) # printing resultant string print("Numeric Pefix from the string: ", ... Read More

Python Get a list as input from user

Pradeep Elance
Updated on 09-Jul-2020 13:46:54

9K+ Views

In this article we will see you how to ask the user to enter elements of a list and finally create the list with those entered values.With format and inputThe format function can be used to fill in the values in the place holders and the input function will capture the value entered by the user. Finally, we will append the elements into the list one by one.ExamplelistA = [] # Input number of elemetns n = int(input("Enter number of elements in the list : ")) # iterating till the range for i in range(0, n):    print("Enter element No-{}: ... Read More

Python Generate successive element difference list

Pradeep Elance
Updated on 09-Jul-2020 13:44:31

346 Views

In this as its elements article we will see how to find the difference between the two successive elements for each pair of elements in a given list. The list has only numbers as its elements.With IndexUsing the index of the elements along with the for loop, we can find the difference between the successive pair of elements.Example Live DemolistA = [12, 14, 78, 24, 24] # Given list print("Given list : ", listA) # Using Index positions res = [listA[i + 1] - listA[i] for i in range(len(listA) - 1)] # printing result print ("List with successive difference in elements ... Read More

Python Generate random string of given length

Pradeep Elance
Updated on 09-Jul-2020 13:42:12

477 Views

In this article we will see how how to generate a random string with a given length. This will be useful in creating random passwords or other programs where randomness is required.With random.choicesThe choices function in random module can produce strings which can then be joined to create a string of given length.Example Live Demoimport string import random # Length of string needed N = 5 # With random.choices() res = ''.join(random.choices(string.ascii_letters+ string.digits, k=N)) # Result print("Random string : ", res)OutputRunning the above code gives us the following result −Random string : nw1r8With secretsThe secrets module also has choice method which ... Read More

Advertisements