I was always amazed by how easily things can be done using python. Some of the tedious tasks can be done in a single line of code using python. I have gathered some of my favorite one-liners from python. I have listed out 50 of them below with an example for each.
from collections import Counter
s1 = 'below'
s2 = 'elbow'
print('anagram') if Counter(s1) == Counter(s2) else print('not an anagram')
decimal = int('1010', 2)
"Hi my name is Allwin".lower()
# 'hi my name is allwin'
"Hi my name is Allwin".casefold()
# 'hi my name is allwin'
Python provides a built-in module called “platform” that enables us to fetch some of the information about our system such as OS, processor, system version, etc
Let us look at some of the methods one by one with an example for each
The machine() method returns the machine type of the user. An empty string is returned if the value cannot be determined.
If you wish to push the code in your local computer system to a particular branch in remote, then this article is for you. Follow the steps below.
2. Initialise git. This command will add a .git file to your local directory
3. Add the files to be committed to the remote code
git add .
4. Add a commit message
git commit -m "Initial commit"
5. Add it to the remote repository
git remote add origin firstname.lastname@example.org:/path/to/my_project.git
6. Push it to the master
git push origin master
7. If you wish to push it to some other branch instead of master, run the following command.
git push origin HEAD:<your_branch_name>
Flask is a micro-framework written in python. Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine. It is called a micro framework because it does not require tools or libraries. Flask supports various extensions that can add additional functionalities. Few applications that use flask are
Some of the major disadvantages of the flask framework are,
These disadvantages are based on comparing flask with Django. …
The string module has some of the built-in constant variables. We can use this instead of declaring the constants again in our code. Some of the string constants that will be discussed here are,
Let us look at these constants one by one with a code snippet printing each of these string constants.
The ascii_letters constant is the concatenation of both lowercase and uppercase alphabets as a string. Let us import the string module and print this constant.
The calendar module in python enables us to print calendars on the terminal and other cool functions with the calendar.
Import the calendar module to access all these functions.
The prmonth() method helps us to print a month’s calendar. It requires the year and the month in the form of integers as arguments.
The math module provides us various functions. Some of them are discussed in this article. Import the math function to begin.
The ten methods in this article are
The ceil() method returns an integer that is greater than or equal to the given float value. Similarly, the floor() method returns an integer that is lesser or equal to the given float value. If an integer value is given the same value is returned.
Python has a built-in library called statistics. This module provides functions for calculating mathematical statistics of numeric (Real-valued) data.
Unless explicitly noted, these functions support
Some of the most common methods supported by this module are,
Let us look at the above methods one by one with an example for each.
The first method is the mean(). This method returns the sample arithmetic mean of data which can be a sequence or iterable.
The arithmetic mean is the sum of the data divided by the number of…
This blog post focuses on finding various information about the nature of the browser and os from which the HTTP requests are made. The following information can be obtained from the requests.
Let us get started. There are actually two ways to obtain this information.
'HTTP_USER_AGENT'header data from the request object
Let us get started.
'HTTP_USER_AGENT'data from the header
In Django, each type of HTTP request is received at the backend via a request object. This request object…
This article is about copying the contents of one file to another using python. This is possible with the help of a built-in python library called shutil. This library just copies the contents of the source file to the destination file in a single line of code.
The shutil module has a method called copyfile(). This method takes the source file name and the destination file name as the parameters. The copy of the source file is made in the same directory as the program unless a complete path is given as the destination file name.