Found 10784 Articles for Python

Any & All in Python?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

846 Views

Python provides two built-ins functions for “AND” and “OR” operations are All and Any functions.Python any() functionThe any() function returns True if any item in an iterable are true, otherwise it returns False. However, if the iterable object is empty, the any () function will return False.Syntaxany(iterable)The iterable object can be a list, tuple or dictionary.Example 1>>> mylst = [ False, True, False] >>> x = any(mylst) >>> x TrueOutputOutput is True because the second item is True.Example 2Tuple – check if any item is True>>> #Tuple - check if any item is True >>> mytuple = (0, 1, 0, ... Read More

Optimization Tips for Python Code?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

232 Views

Though we all know python is not as fast or efficient as other complied languages. However, there are many big companies which shows us that python code can handle much bigger workload which shows it’s not that slow. In this section, we are going to see some of the tips that one should keep in mind so that a correct python program runs even faster and more efficient.Tip 1: Go for built-in functionsThough we can write efficient code in python, but it’s very hard to beat built-in functions(which are written in C). Below image shows the list of python built-in ... Read More

Python Input Methods for Competitive Programming?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

1K+ Views

In this we are going to see different ways of I/O methods for competitive programming in Python. In competitive programming it is important to read the input as fast as possible so as take advantage over others.Suppose you’re in a codeforces or similar online jude (like SPOJ) and you have to read numbers a, b, c, d and print their product. There are multiple ways to do, let’s explore them – one by oneOne way to doing it is either through list comprehension and map function.Method 1: Using a list comprehensiona, b, c, d = [int(x) for x in input().split()] ... Read More

Timeit in Python with Examples?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

294 Views

Python provides many ways to measure the time of execution for a piece of python code. One way is to use the python inbuilt time module and save the time before and after the execution of the program?Python timeitWhen some program is running, many processes also run in the background to make that code executable. The time module doesn’t count background processes execution time, however if you need precise time performance measurements timeit is the module to go for it.The timeit module runs the code approximately 1 million times (default value) and take into account the minimum amount of time ... Read More

File Objects in Python?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

4K+ Views

In python, whenever we try read or write files we don’t need to import any library as it’s handled natively.The very first thing we’ll do is to use the built-in open function to get a file object.The open function opens a file and returns a file object. The file objects contains methods and attributes which latter can be used to retrieve information or manipulate the file you opened.What is a file?Before we do any operation on file, let’s first understand what is file? File is a named location on disk to store related information, as the file is having some ... Read More

XML parsing in Python?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

2K+ Views

Python XML parser parser provides one of the easiest ways to read and extract useful information from the XML file. In this short tutorial we are going to see how we can parse XML file, modify and create XML documents using python ElementTree XML API.Python ElementTree API is one of the easiest way to extract, parse and transform XML data.So let’s get started using python XML parser using ElementTree:Example1Creating XML fileFirst we are going to create a new XML file with an element and a sub-element.#Import required library import xml.etree.ElementTree as xml def createXML(filename):    # Start with the ... Read More

Detection of a specific color(blue here) using OpenCV with Python?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

2K+ Views

For many people, image processing may seem like a scary and daunting task but it is not as hard as many people thought it is. In this tutorial we’ll be doing basic color detection in openCv with python.How does color work on a computer?We represent colors on a computers by color-space or color models which basically describes range of colors as tuples of numbers.Instead of going for each color, we’ll discuss most common color-space we use .i.e. RGB(Red, Green, Blue) and HSV (Hue, Saturation, Value).RGB basically describes color as a tuple of three components. Each component can take a value ... Read More

How to use contains() in android LinkedBlockingDeque?

Samual Sam
Updated on 30-Jun-2020 14:13:06

73 Views

Before getting into example, we should know what LinkedBlockingDeque is. It is implemented by Collection interface and the AbstractQueue class. It provides optional boundaries based on linked nodes.It going to pass memory size to constructor and helps to provide memory wastage in android.This example demonstrate about How to use contains() in android LinkedBlockingDequeStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have taken text view to show ... Read More

Linear Regression using Python?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

574 Views

Linear regression is one of the simplest standard tool in machine learning to indicate if there is a positive or negative relationship between two variables.Linear regression is one of the few good tools for quick predictive analysis. In this section we are going to use python pandas package to load data and then estimate, interpret and visualize linear regression models.Before we go down further down, let’s discuss what is regression first?What is Regression?Regression is a form of predictive modelling technique which helps in creating a relationship between a dependent and independent variable.Types of RegressionLinear RegressionLogistic RegressionPolynomial RegressionStepwise RegressionWhere is Linear ... Read More

Difference between various Implementations of Python?

AmitDiwan
Updated on 12-Aug-2022 12:08:24

167 Views

Most developers know about python, irrespective of what python is implemented in their system. Therefore, what do I mean by “python”, is it a python the abstract interface? Do we mean CPython, the common Python implementation (not the Cython)?. Or we mean something else entirely? Or we mean Jython or IronPython or PyPy. While the technologies mentioned above are commonly-named and commonly-referenced, however some of them serve completely different purposes. We can think of python as a specification for a language that can be implemented in many different ways. In this tutorial we’ll go through the following implementation of python ... Read More

Advertisements