Found 10476 Articles for Python

Interpreting stat() results using Python

Pradeep Elance
Updated on 10-Jul-2020 11:12:28

433 Views

The stat() method is part of OS module which describes various OS related operations on files and directories. For example, if we want to know various user defined flags for a file or size of the file in bytes.Functions in os.stat() moduleBelow is a list of some sample functions available in stat() and their meaning.st_size − It represents the size of the file in bytes.st_atime − It represents the time of most recent access. It is expressed in seconds.st_ctime − It represents the time of most recent metadata change on Unix and creation time on Windows. It is expressed in seconds.st_blocks ... Read More

Importing Data in Python

Pradeep Elance
Updated on 10-Jul-2020 11:10:27

16K+ Views

When running python programs, we need to use datasets for data analysis. Python has various modules which help us in importing the external data in various file formats to a python program. In this example we will see how to import data of various formats to a python program.Import csv fileThe csv module enables us to read each of the row in the file using a comma as a delimiter. We first open the file in read only mode and then assign the delimiter. Finally use a for loop to read each row from the csv file.Exampleimport csv with ... Read More

GET and POST requests using Python Programming

Pradeep Elance
Updated on 10-Jul-2020 10:59:24

3K+ Views

Python can be used to access webpages as well as post content to the webpages. There are various modules like httplib, urllib, httplib2 etc but the requests module is simplest and can be used to write simpler yet powerful programs involving GET and POST methods.GET methodThe GET method is part of the python requests module which is used to obtain data from a web URL. In the below example we reach out to our own website and find out various responses through the get method. We get the encoding, response time and also the header and part of the body.Example Live ... Read More

Catching the ball game using Python

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

2K+ 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

Yaswanth Varma
Updated on 24-Jul-2025 18:06:26

1K+ Views

Difference Between Adjacent List Elements  The given task is to calculate the difference between the adjacent elements in the given list using Python, i.e, we need to subtract the current element from the next element and repeat this for each element in the list (except the last one). Scenario Following is an example scenario: Input: [10, 15, 12, 18] Output: [5, -3, 6] Explanation: Difference between 15 and 10: 15 - 10 = 5 Difference between 12 and 15: 12 - 15 = -3 Difference between 18 and 12: 18 - 12 = 6 Using List ... 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

344 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

505 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

303 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

409 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

Advertisements