Found 10807 Articles for Python

Global and Local Variables in Python?

Arjun Thakur
Updated on 31-Aug-2023 02:52:46

11K+ Views

There are two types of variables: global variables and local variables. The scope of global variables is the entire program whereas the scope of local variable is limited to the function where it is defined.Example def func(): x = "Python" s = "test" print(x) print(s) s = "Tutorialspoint" print(s) func() print(x) OutputIn above program- x is a local variable whereas s is a global variable, we can access the local variable only within the function it is defined (func() above) and ... Read More

Counters in Python?

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

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

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

500 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

enum - Support for enumerations in Python

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

283 Views

Enumeration is a set of identifiers (members) given unique and constant values. Within an enumeration, the members can be compared by identity. Enumeration object can also terated over.The enum module defines following classesEnum : Base class for creating enumerated constants.IntEnum : Base class for creating enumerated constants that are also subclasses of int.Enumerations are created using the class syntax#enumexample.py from enum import Enum class langs(Enum): Python = 1 Java = 2 Cpp = 3 Ruby = 4Enumeration members have human readable string representation and repr representation.>>> from enumexample import langs >>> print (langs.Python) langs.Python >>> print (repr(langs.Python)) Each enum member has ... Read More

urllib.robotparser - Parser for robots.txt in Python

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

472 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

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

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

Advertisements