Found 10784 Articles for Python

What is the #define Preprocessor in C++?

Jennifer Nicholas
Updated on 18-Jun-2020 13:05:52

478 Views

The #define creates a macro, which is the association of an identifier or parameterized identifier with a token string. After the macro is defined, the compiler can substitute the token string for each occurrence of the identifier in the source file.#define identifier token-stringThis is how the preprocessor is used. The #define directive causes the compiler to substitute token-string for each occurrence of identifier in the source file. The identifier is replaced only when it forms a token. That is, identifier is not replaced if it appears in a comment, in a string, or as part of a longer identifier.example#include #define ... Read More

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

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

2K+ 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:         stmt1     else:          if expr3==True:         stmt2 else:     if expr3==True:        stmt3     else:        stmt4

How to indent an if...else statement in Python?

Malhar Lathkar
Updated on 18-Jun-2020 13:04:11

659 Views

One of the characteristic features of Python is use of uniform indent to denote a block of statements. A block is initiated by − symbol As soon as − symbol is typed and enter pressed, any Python aware editor will take cursor to next line with increased indent. All lines entered subsequently will follow same level of indent. To signal end of block, indent level must be reduced by pressing backspace.Using above procedure give − after if statement and write statements in true block. Then dedent by backspace and write else − In another block of increased indent enter statements ... Read More

Can we use pass statement in a Python if clause?

Malhar Lathkar
Updated on 18-Jun-2020 13:01:38

96 Views

In Python, pass keyword is a dummy statement. It is used where a statement is necessary to fulfill syntax requirement but the actual implementation of processing logic is yet to be finalized. It can be used in if as well as else blockif expr==True:    pass else:    pass

Can we use continue statement in a Python if clause?

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

116 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 can't be used in an if block if it is not a part of loop. If used, interpreter will throw syntax error.

Can we use break statement in a Python if clause?

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

131 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         stmt2         if expr2==True:        break     stmt3     stmt4   However it can't be used in an if block if it is not a part of loop. 

How to exit from a Python if clause?

Malhar Lathkar
Updated on 30-Jul-2019 22:30:21

476 Views

It is not possible to exit from an if block of Python code. The break keyword does appear in if block but it has to inside a loop. It is however possible to exit from entire program from inside if block by sys.exit()

What does colon ':' operator do in Python?

Malhar Lathkar
Updated on 18-Jun-2020 12:59:27

8K+ Views

The : symbol is used for more than one purpose in PythonAs slice operator with sequence −The − operator slices a part from a sequence object such as list, tuple or string. It takes two arguments. First is the index of start of slice and second is index of end of slice. Both operands are optional. If first operand is omitted, it is 0 by default. If second is omitted, it is set to end of sequence.>>> a=[1, 2, 3, 4, 5] >>> a[1:3] [2, 3] >>> a[:3] [1, 2, 3] >>> a[2:] [3, 4, 5] >>> s='computer' >>> s[:3] ... Read More

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

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

272 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 are the >> and << operators in Python?

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

142 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 >>> bin(a) '0b111100' >>> b=a> b 240 >>> bin(b) '0b11110000'You can see two bits on right set to 0On the other hand >> operator shifts pattern to right. Most significant bits are set to 0>>> a=60 >>> bin(a) '0b111100' >>> b=a>>2 >>> b 15 >>> bin(a) '0b111100'

Advertisements