Found 10402 Articles for Python

Counters in Python?

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

4K+ Views

A Counters is a container which keeps track to how many times equivalent values are added. Python counter class is a part of collections module and is a subclass of dictionary.Python CounterWe may think of counter as an unordered collection of items where items are stored as dictionary keys and their count as dictionary value.Counter items count can be positive, zero or negative integers. Though there is no restrict on its keys and values but generally values are intended to be numbers but we can store other object types too.InitializingCounter supports three forms of initialization. Its constructor can be called ... Read More

Time Functions in Python?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

15K+ Views

Python provides library to read, represent and reset the time information in many ways by using “time” module. Date, time and date time are an object in Python, so whenever we do any operation on them, we actually manipulate objects not strings or timestamps.In this section we’re going to discuss the “time” module which allows us to handle various operations on time.The time module follows the “EPOCH” convention which refers to the point where the time starts. In Unix system “EPOCH” time started from 1 January, 12:00 am, 1970 to year 2038.To determine the EPOCH time value on your system, ... Read More

Transpose a matrix in Python?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

3K+ Views

Transpose a matrix means we’re turning its columns into its rows. Let’s understand it by an example what if looks like after the transpose.Let’s say you have original matrix something like -x = [[1, 2][3, 4][5, 6]]In above matrix “x” we have two columns, containing 1, 3, 5 and 2, 4, 6.So when we transpose above matrix “x”, the columns becomes the rows. So the transposed version of the matrix above would look something like -x1 = [[1, 3, 5][2, 4, 6]]So the we have another matrix ‘x1’, which is organized differently with different values in different places.Below are couple ... Read More

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

889 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

675 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

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

235 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

Akshitha Mote
Updated on 12-Dec-2024 19:22:29

796 Views

What is a Copy Operation in Python? Copying in Python refers to the process of creating a duplicate of existing data. The simplest way to create a reference to an object is by using the assignment operator (=), but this does not create an actual copy—it only makes the new variable point to the same object in memory. To create an independent copy, Python provides two methods: shallow copy and deep copy, which can be achieved using the copy module. The Assignment operator doesn't create a new object; instead, it binds a new variable to the same memory address ... Read More

Advertisements