Malhar Lathkar has Published 50 Articles

What is best way to check if a list is empty in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 16-Jun-2020 11:19:42

216 Views

The best way is to use not operator on list object. If list is empty it returns true, otherwise false.>>> L1=[] >>> not L1 True >>> L1=[1,2] >>> not L1 FalseAnother method is to check if length of list is zero which means it is empty>>> L1=[] >>> len(L1) 0 >>> L1=[1,2] >>> len(L1) 2

How to indent multiple if...else statements in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 26-Feb-2020 11:11:26

1K+ Views

Use of indented blocks is an important feature of Python. Indent level of the block is more than previous statements. Hence, if multiple if statements are present in a program in nested fashion, each subsequent indented block will have increasing level of indent.if expr1==True:     if expr2==True:     ... Read More

What is basic syntax of Python for Loops?

Malhar Lathkar

Malhar Lathkar

Updated on 26-Feb-2020 11:09:04

117 Views

Python's for loop executes the body of loop for each object in a collection such as string, list, tuple or dictionary. Its usage is as follows −Examplefor obj in seq:     stmt1     stmt2Following code snippet iterates over each number element of the list and prints its squareL1=[1, ... Read More

Can we use break statement in a Python if clause?

Malhar Lathkar

Malhar Lathkar

Updated on 26-Feb-2020 11:06:04

116 Views

Python's break keyword is used as decision control statement. It causes the remaining iterations to be abandoned and control of execution goes to next statement after the end of loop. Invariably it is executed conditionally and appears inside if block within a loop.while expr==True:     stmt1       ... Read More

Can we use continue statement in a Python if clause?

Malhar Lathkar

Malhar Lathkar

Updated on 26-Feb-2020 11:05:28

107 Views

Python's continue statement is a loop control statement. It causes starting next iteration of loop after abandoning current iteration. Invariably is is executed conditionally i.e. in if blockwhile expr==True:     stmt1     stmt2     if expr2==True:        continue     stmt3     stmt4However, it ... Read More

What are the >> and << operators in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 26-Feb-2020 10:59:01

114 Views

The symbols > are defined as left and right shift operators respectively in Python. They are bitwise operators. First operand is a bitwise representation of numeric object and second is the number of positions by which bit formation is desired to be shifted to left or right.The >> a=60 >>> ... Read More

How does == operator works in Python 3?

Malhar Lathkar

Malhar Lathkar

Updated on 26-Feb-2020 10:58:05

102 Views

The == symbol is defined as equality operator. It returns true if expressions on either side are equal and false if they are not equal>>> (10+2) == 12 True >>> 5*5 == 5**2 True >>> (10/3)==3 False >>> 'computer'=="computer" True >>> 'COMPUTER'.lower()=='computer' True

What is the purpose of the `//` operator in python?

Malhar Lathkar

Malhar Lathkar

Updated on 26-Feb-2020 10:55:31

246 Views

In Python, // is defined as floor division operator. It returns integer part of a floating point result of division. For example 10//3 returns 3>>> 10//3 3 >>> 125.5//2.5 50.0 >>> 12.5//1.5 8.0However in case of negative division, returned value is rounded towards negative infinity.>>> -10//3 -4 >>> -73//9 -9

What does these operators mean (** , ^ , %, //) ?

Malhar Lathkar

Malhar Lathkar

Updated on 26-Feb-2020 10:44:55

225 Views

In Python ** is a raised to operator. It returns x raised to y in the expression x**y>>> x=5 >>> y=3 >>> x**y 125^ is a bitwise XOR operator. Taking two bits as operands it returns 1 if one is 1 and other is 0>>> a=10 >>> bin(a)    #0001 ... Read More

What is the difference between = and == operators in Python?

Malhar Lathkar

Malhar Lathkar

Updated on 26-Feb-2020 10:43:01

1K+ Views

In Python = symbol is defined as assignment operator. It requires one variable on its left and an expression on its right. Value of the expression on right is assigned to variable on left. Expression and name of variable are not interchangeable.>>> a=10 >>> b=20 >>> c=a+b >>> a, b, c ... Read More

Advertisements