Found 10784 Articles for Python

Ternary Operator in Python?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

11K+ Views

Many programming languages support ternary operator, which basically define a conditional expression.Similarly the ternary operator in python is used to return a value based on the result of a binary condition. It takes binary value(condition) as an input, so it looks similar to an “if-else” condition block. However, it also returns a value so behaving similar to a function.Syntax[on_true] if [expression] else [on_false]Let’s write one simple program, which compare two integers -a. Using python if-else statement ->>> x, y = 5, 6 >>> if x>y:    print("x") else:    print("y") yb. Using ternary operator>>> x, y = 5, 6 >>> ... Read More

ipaddress - IPv4/IPv6 manipulation library in Python

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

580 Views

Internet Protocol is currently in the process of moving from version 4 to version 6. This is necessitated because version 4 doesn’t provide enough addresses to handle the increasing number of devices with direct connections to the internet.An IPv4 address is composed of 32 bits, represented into four eight bit groups called as "octets". This is a "dotted decimal" format where each eight-bit octet can have a decimal value 0 to 255.For example: 192.168.1.1IPv4 address with CIDR notation: 192.168.1.1/24 where 24 means first three octets identify the network and last octet identifies node.An IPv6 address is 128 bits long. It ... Read More

urllib.robotparser - Parser for robots.txt in Python

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

517 Views

Web site owners use the /robots.txt file to give instructions about their site to web robots; this is called The Robots Exclusion Protocol. This file is a simple text-based access control system for computer programs that automatically access web resources. Such programs are called spiders, crawlers, etc. The file specifies the user agent identifier followed by a list of URLs the agent may not access.For example#robots.txt Sitemap: https://example.com/sitemap.xml User-agent: * Disallow: /admin/ Disallow: /downloads/ Disallow: /media/ Disallow: /static/This file is usually put in the top-level directory of your web server.Python's urllib.robotparser module provides RobotFileParser class. It answers questions about whether ... Read More

urllib.parse — Parse URLs into components in Python

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

7K+ Views

This module provides a standard interface to break Uniform Resource Locator (URL) strings in components or to combine the components back into a URL string. It also has functions to convert a "relative URL" to an absolute URL given a "base URL."This module supports the following URL schemes -fileftpgopherhdlhttphttpsimapmailtommsnewsnntpprosperorsyncrtsprtspusftpshttpsipsipssnewssvnsvn+sshtelnetwaiswswssurlparse()This function parses a URL into six components, returning a 6-tuple. This corresponds to the general structure of a URL. Each tuple item is a string. The components are not broken up in smaller parts (for example, the network location is a single string), and % escapes are not expanded. The return ... Read More

html.parser — Simple HTML and XHTML parser in Python

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

2K+ Views

The HTMLParser class defined in this module provides functionality to parse HTML and XHMTL documents. This class contains handler methods that can identify tags, data, comments and other HTML elements.We have to define a new class that inherits HTMLParser class and submit HTML text using feed() method.from html.parser import HTMLParser class parser(HTMLParser): pass p = parser() p.feed('')We have to override its following methodshandle_starttag(tag, attrs):HTML tags normally are in pairs of starting tag and end tag. For example and . This method is called to handle the start of a tag.Name of the tag converted to lower case. The attrs ... Read More

Functools — Higher-order functions and operations on callable objects in Python

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

192 Views

Function in Python is said to be of higher order. It means that it can be passed as argument to another function and/or can return other function as well. The functools module provides important utilities for such higher order functions.partial() functionThis function returns a callable 'partial' object. The object itself behaves like a function. The partial() function receives another function as argument and freezes some portion of a function’s arguments resulting in a new object with a simplified signature.The built-in int() function converts a number to a decimal integer. Default signature of int() isint(x, base = 10)The partial() function can ... Read More

Copy - Shallow and deep copy operations in Python

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

389 Views

In Python a variable is just a reference to object. Hence when it is assigned to another variable, it doesn’t copy the object, rather it acts as another reference to same object. This can be verified by using id() function>>> L1 = [1, 2, 3] >>> L2 = L1 >>> id(L1), id(L2) (2165544063496, 2165544063496)Result of above code shows that id() for both list objects is same, which means both refer to same object. The L2 is said to be a shallow copy of L1. Since both refer to same object, any change in either will reflect in other object.>>> L1 ... Read More

Bisect - Array bisection algorithm in Python

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

466 Views

Performing sort operations after every insertion on a long list may be expensive in terms of time consumed by processor. The bisect module ensures that the list remains automatically sorted after insertion. For this purpose, it uses bisection algorithm. The module has following functions:bisect_left()This method locates insertion point for a given element in the list to maintain sorted order. If it is already present in the list, the insertion point will be before (to the left of) any existing entries. The return value caan be used as the first parameter to list.insert()bisect_right()This method is similar to bisect_left(), but returns an ... Read More

Array — Efficient arrays of numeric values in Python

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

210 Views

Array is a very popular data structure in C/C++, Java etc. In these languages array is defined as a collection of more than one elements of similar data type. Python doesn't have any built-in equivalent of array. It's List as well as Tuple is a collection of elements but they may of different types.Python's array module emulates C type array. The module defines 'array' class. Following constructor creates an array object:array(typecode, initializer)The typecode argument determines the type of array. Initializer should be a sequence with all elements of matching type.Following statement creates an integer array object:>>> import array >>> arr ... Read More

Function Decorators in Python?

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

465 Views

Python developers can extend and modify the behavior of a callable functions, methods or classes without permanently modifying the callable itself by using decorators. In short we can say they are callable objects which are used to modify functions or classes.Function decorators are functions which accepts function references as arguments and adds a wrapper around them and returns the function with the wrapper as a new function.Let’s understand function decorator bye an example:Code1@decorator def func(arg):    return "value"Above code is same as:Code2def func(arg):    return "value" func = decorator(func)So from above, we can see a decorator is simply another function ... Read More

Advertisements