Found 10784 Articles for Python

How can we print multiple blank lines in python?

Samual Sam
Updated on 05-Mar-2020 05:02:36

2K+ Views

We can print multiple blank lines in python by using the character the number of times we need a blank line. For example, If you need 5 blank lines, you can use −Python 2.x: print "" Python 3.x: print("")You can use features like repetition operator in python to make this easier. For example,Python 2.x: print "" * 5 Python 3.x: print("" * 5)All of these commands will print 5 blank lines on the STDOUT.

Using HTML5 Server-Sent Events in a web application

Jennifer Nicholas
Updated on 20-Dec-2019 10:49:19

159 Views

To use Server-Sent Events in a web application, you would need to add an element to the document.The src attribute of the element should point to an URL that should provide a persistent HTTP connection that sends a data stream containing the events.The URL would point to a PHP, PERL or any Python script that would take care of sending event data consistently.ExampleHere is an example showing application that would expect server time.                    /* Define event handling logic here */                                                              

How to Find HCF or GCD using Python?

Vikram Chiluka
Updated on 28-Oct-2022 11:31:48

1K+ Views

In this article, we will show you how to find the HCF (Highest Common Factor) or GCD (Greatest Common Factor) in Python. Below are the various methods to accomplish this task: Using For Loop Using Euclidean Algorithm Using While Loop Using Recursion(Naive method) Handling Negative Numbers in HCF What is H.C.F. or G.C.D? The largest positive integer that perfectly divides the two given numbers is known as the highest common factor (H.C.F.) or greatest common divisor (G.C.D.). Example - The HCF of 12 and 14, is 2. Using For Loop Algorithm (Steps) Following are the Algorithm/steps to be followed to ... Read More

What are Ordered dictionaries in Python?

Jayashree
Updated on 02-Mar-2020 10:21:48

405 Views

An OrderedDict is a dictionary subclass that remembers the order in which its contents are added, supporting the usual dict methods.If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.>>> from collections import OrderedDict >>> d = {'banana': 3, 'apple':4, 'pear': 1, 'mango': 2} >>> od=OrderedDict(d.items()) >>> od OrderedDict([('banana', 3), ('apple', 4), ('pear', 1), ('mango', 2)]) >>> od=OrderedDict(sorted(d.items())) >>> od OrderedDict([('apple', 4), ('banana', 3), ('mango', 2), ('pear', 1)]) >>> t=od.popitem() >>> t ('pear', 1) >>> od=OrderedDict(d.items()) >>> t=od.popitem() >>> t ('mango', 2)

Where are operators mapped to magic methods in Python?

Vikram Chiluka
Updated on 09-Nov-2022 08:05:07

304 Views

In this article, we will explain to you where are operators mapped to magic methods in python. Python Magic methods are special methods that begin and end with double underscores. They are also known as dunder methods. Magic methods are not intended to be invoked directly by you, but rather invocation occurs by the class on a specific action. When you use the + operator to add two numbers, the __add__() method is called internally. Many magic methods in Python are defined by built−in classes. To get the number of magic methods inherited by a class, use the dir() function. ... Read More

What is the difference between operator and method on Python set?

Jayashree
Updated on 02-Mar-2020 10:21:19

351 Views

Python's set object represents built-in set class. Different set operations such as union, intersection, difference and symmetric difference can be performed either by calling corresponding methods or by using operators.Union by method>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1.union(s2) {1, 2, 3, 4, 5, 6, 7, 8} >>> s2.union(s1)  {1, 2, 3, 4, 5, 6, 7, 8}Union by | operator>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1|s2  {1, 2, 3, 4, 5, 6, 7, 8}Intersection by method>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1.intersection(s2) {4, 5} >>> s2.intersection(s1)  {4, 5}Intersection & operator>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1&s2 {4, 5} >>> s2&s1  {4, 5}Difference method>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1.difference(s2) {1, 2, 3} >>> s2.difference(s1)  {8, 6, 7}Difference - operator>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1-s2 {1, 2, 3} >>> s2-s1  {8, 6, 7}

How will you explain Python Operator Overloading?

Jayashree
Updated on 30-Jul-2019 22:30:22

144 Views

Every class in Python, whether built-in or user defined is inherited from object class. The object class has a number of properties whose name is preceded and followed by double underscores (__). Each of these properties is a wrapper around a method of same name. Such methods are called special or magic methods.The magic methods __lt__(), __gt__(), __eq__(), __ne__(), etc. are overridden in a class to overload == and != operators respectively.

How to perform square root without using math module in Python?

Vikram Chiluka
Updated on 28-Aug-2023 12:39:09

29K+ Views

In this article, we will show you how to perform square root without using a math module in Python. Below are the various methods to accomplish this task: Using exponential operator ** Using Mathematical Logic Using exponential operator ** Without using the math module, the simplest approach to find the square root of a number in Python is to use the built-in exponential operator ** (It is an exponent operator because it calculates the power of the first operand to the power of the second operand). Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired ... Read More

How to calculate square root of a number in Python?

Vikram Chiluka
Updated on 27-Oct-2022 12:24:16

2K+ Views

In this article, we will show you how to calculate the square root of a number in Python. Below are the various methods to accomplish this task − Calculating square root using sqrt() Calculating the square root using the pow() function Calculating the square root using the exponent operator Calculating square root using the cmath module What is the square root of a number? The square root of a number is a value when multiplied by itself returns that same number. Example − 5 x 5 = 25, hence the square root of 25 is 5. However -5 ... Read More

How do you round up a float number in Python?

Vikram Chiluka
Updated on 26-Oct-2023 03:44:45

23K+ Views

In this article, we will show you how to round up a float number in Python. Round up float number using the round() function The round() function gives a floating point number with a specified number of decimals which is the rounded version of the specified number. The function will return the nearest integer because the default value for the number of decimals is 0. Syntax round(number, digits) Parameters number(required) − a number that should be rounded digits(optional) − up to the number of decimals to be rounded. 0 is the default. Python has an in-built function round() for ... Read More

Advertisements