Found 10784 Articles for Python

What is @ operator in Python?

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

309 Views

@ symbol is used to define decorator in Python. Decorators provide a simple syntax for calling higher-order functions. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.we have two different kinds of decorators in Python:Function decoratorsClass decorators A decorator in Python is any callable Python object that is used to modify a function or a class. A reference to a function  or a class  is passed to a decorator and the decorator returns a modified function or class. The modified functions or classes usually contain calls to ... Read More

How to implement Python __lt__ __gt__ custom (overloaded) operators?

Pythonista
Updated on 02-Mar-2020 09:52:46

3K+ Views

Python has magic methods to define overloaded behaviour of operators. The comparison operators (=, == and !=) can be overloaded by providing definition to __lt__, __le__, __gt__, __ge__, __eq__ and __ne__ magic methods.  Following program overloads < and > operators to compare objects of distance class. class distance:   def __init__(self, x=5,y=5):     self.ft=x     self.inch=y   def __eq__(self, other):     if self.ft==other.ft and self.inch==other.inch:       return "both objects are equal"     else:       return "both objects are not equal"   def __lt__(self, other):     in1=self.ft*12+self.inch     in2=other.ft*12+other.inch     if in1

What is vertical bar in Python bitwise assignment operator?

Pythonista
Updated on 02-Mar-2020 09:53:31

1K+ Views

Vertical bar (|) stands for bitwise or operator. In case of two integer objects, it returns bitwise OR operation of two>>> a=4 >>> bin(a) '0b100' >>> b=5 >>> bin(b) '0b101' >>> a|b 5 >>> c=a|b >>> bin(c) '0b101'

Is there a “not equal” operator in Python?

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

216 Views

In Python 2.x as well as != symbols are defined as 'not equal to' operators. In Python 3, operator is deprecated.

How to save a Python Dictionary to CSV file?

Pythonista
Updated on 24-Aug-2023 16:15:39

40K+ Views

CSV (Comma Separated Values) is a most common file format that is widely supported by many platforms and applications.Use csv module from Python's standard library. Easiest way is to open a csv file in 'w' mode with the help of open() function and write  key value pair in comma separated form.import csv my_dict = {'1': 'aaa', '2': 'bbb', '3': 'ccc'} with open('test.csv', 'w') as f:     for key in my_dict.keys():         f.write("%s, %s"%(key, my_dict[key]))The csv module contains DictWriter method that requires name of csv file to write and a list object containing field names. The writeheader() ... Read More

How to split Python tuples into sub-tuples?

Vikram Chiluka
Updated on 09-Nov-2022 07:48:02

8K+ Views

In this article, we will show you how to split python tuples into sub-tuples. Below are the various methods to accomplish this task − Using slicing Using enumerate() & mod operator Tuples are an immutable, unordered data type used to store collections in Python. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable. Using slicing Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Create a variable to ... Read More

How to Check Leap Year using Python?

Jayashree
Updated on 22-Dec-2020 07:33:54

634 Views

Leap year comes after every four years. For normal year, if it is divisible by four, it is called leap year, whereas for century year, it should be divisible by 400. The following Python program shows whether the year is leap or notExampleyr=int(input('enter year')) if yr%100==0: #century year if yr%400==0:    print ('{} is leap year'.format(yr)) else:    print ('{} is not leap year'.format(yr)) else:    if yr%4==0:       print ('{} is leap year'.format(yr)) else:    print ('{} is not leap year'.format(yr))Outputenter year2012 2012 is leap year enter year2018 2018 is not leap year 2000 is ... Read More

How to Check if a Number is Odd or Even using Python?

Vikram Chiluka
Updated on 27-Oct-2022 12:36:20

4K+ Views

In this article, we will show you how to check if a number is odd or even in python. Below are the methods to accomplish this task − Using modulo (%) operator Using Recursion Using the Binary AND (&) operator Using modulo (%) operator Python's modulo (%) operator (also called the remainder operator) is useful to determine if a number is odd or even. We obtain the remainder of the division of a number by 2. If it is 0, it is even otherwise it is odd Even Number − A number that can be divided by 2, ... Read More

How to Convert Celsius To Fahrenheit using Python?

Vikram Chiluka
Updated on 25-Oct-2022 07:28:14

15K+ Views

In this article, we will show you how to convert Celsius To Fahrenheit using Python. Celsius Celsius is a temperature measurement unit that is also known as centigrade. It is an SIderived unit that is used by the majority of countries throughout the world. It is named after the Swedish astronomer Anders Celsius. Fahrenheit Fahrenheit is a temperature scale named after the Polish-born German physicist Daniel Gabriel Fahrenheit, and it uses degrees Fahrenheit as a temperature unit. To obtain Fahrenheit equivalent of celsius, multiply by 1.8 and add 32 - f=c*1.8+32 Or we can use another formula − ... Read More

How to Swap Two Variables using Python?

Jayashree
Updated on 02-Mar-2020 07:40:44

301 Views

By using a temporary variable −>>> x=10 >>> y=20 >>> z=x >>> x=y >>> y=z >>> x,y (20, 10)Without using temporary variable>>> a,b=5,7 >>> a,b (5, 7) >>> a,b=b,a >>> a,b (7, 5)

Advertisements