Pythonista has Published 42 Articles

How to use else conditional statement with for loop in python?

Pythonista

Pythonista

Updated on 27-Feb-2020 05:22:06

159 Views

The else block in a loop (for as well as while) executes after all iterations of loop are completed and before the program flow exits the loop body. The syntax is as follows −Syntaxwhile expr==True:     #statements to be iterated while expr is true. else:    #this statement(s) will ... Read More

How to generate prime numbers using Python?

Pythonista

Pythonista

Updated on 26-Feb-2020 12:46:08

5K+ Views

A prime number is the one that is not divisible by any other number except 1 and itself.In Python % modulo operator is available to test if a number is divisible by other. Assuming we have to find prime numbers between 1 to 100, each number (let us say x) in ... Read More

How to iterate by sequence index in Python?

Pythonista

Pythonista

Updated on 26-Feb-2020 12:45:35

385 Views

Sequence objects in Python are an ordered collection of items. Each item in the sequence (list, tuple and string) is accessible by index starting with 0.To traverse elements in a list>>> L1=[10, 20, 30, 40, 50] >>> for i in range(len(L1)): print (L1[i]) 10 20 30 40 50To slice ... Read More

What keyboard command we have to stop an infinite loop in Python?

Pythonista

Pythonista

Updated on 26-Feb-2020 12:44:52

302 Views

Any loop is formed to execute a certain number of times or until a certain condition is satisfied. However, if the condition doesn't arise, loop keeps repeating infinitely. Such an infinite loop needs to be forcibly stopped by generating keyboard interrupt. Pressing ctrl-C stops execution of infinite loop>>> while True: ... Read More

What does the >> operator do in Python?

Pythonista

Pythonista

Updated on 26-Feb-2020 07:38:32

308 Views

It is a bitwise right shift operator. The bit pattern is shifted towards right by number of places stipulated by operand on right. Bits on left are set to 0For example a=60 (00111100 in binary), a>>2 will result in 15 (00001111 in binary)>>> a=60 >>> bin(a)result #39;0b111100' >>> b=a>>2 >>> ... Read More

What is function of ^ operator in Python

Pythonista

Pythonista

Updated on 26-Feb-2020 07:37:55

283 Views

In Python, ^ is called EXOR operator. It is a bitwise operator which takes bits as operands. It returns 1 if one operand is 1 and other is 0.Assuming a=60 (00111100 in binary) and b=13 (00001101 in binary) bitwise XOR of a and b returns 49 (00110001 in binary)>>> a=60 ... Read More

Explain function of % operator in Python.

Pythonista

Pythonista

Updated on 26-Feb-2020 07:37:20

276 Views

In Python % is an arithmetic operator that returns remainder of division operation. It is called modulo or remainder operator and operates upon numeric operands except complex numbers>>> a=10 >>> a%3 1 >>> a%5 0 >>> b=12.5 >>> b%2.5 0.0 >>> b%2 0.5

What is behavior of ++ and -- operators in Python?

Pythonista

Pythonista

Updated on 26-Feb-2020 07:36:49

141 Views

In C/C++ and Java etc, ++ and -- operators are defined as increment and decrement operators. In Python they are not defined as operators.In Python objects are stored in memory. Variables are just the labels. Numeric objects are immutable. Hence they can't be incremented or decremented.However, prefix ++ or -- ... Read More

How do we define dictionary in Python?

Pythonista

Pythonista

Updated on 25-Feb-2020 11:23:19

117 Views

Dictionary object is an unordered collection of key-value pairs, separated by comma and enclosed in curly brackets. Association of value with key is marked by : symbol between them.>>> D1={'a':1, 'b':2, 'c':3}Key can appear in a dictionary object only once, whereas single value can be assigned to multiple keys. Key ... Read More

How can I parse a numeric string to its corresponding float value?

Pythonista

Pythonista

Updated on 25-Feb-2020 11:22:35

67 Views

Python’s number conversion function float() converts an integer to float with fractional part as 0. It also parses a string with valid representation of float number to a float object.>>> float('1.11') 1.11 >>> float(1) 1.0 >>> float('1') 1.0 >>> float('1.1e-2') 0.011

Advertisements