Loops are a type of programming instruction that performs a certain series of instructions or activities based on a set of criteria and then repeats the process until the conditions are met.
Loops are a type of programming instruction that performs a certain series of instructions or activities based on a set of criteria and then repeats the process until the conditions are met.
Statements are executed in sequential order, however, there are situations when the programmers need to run a block of code multiple times. Programming languages provide control structures that allow us to execute a statement or a set of statements repeatedly.
Python has three different looping methods:
- For Loop
This is what programmers used to do when they had a piece of code they wanted to repeat a certain number of times.
-
While Loop
The loop is run until the Boolean condition is satisfied. -
Nested Loop
Programmers can nest loops within loops, such as for loop inside while or vice versa, or loop inside for loop or while inside while. -
A complex number has both real and imaginary components. 2 3j, for instance, is a complex number with 2 as the real component and 3 multiplied by j as the imaginary component.
Converting numbers to other types
Two ways exist for converting one number to the other:
- Using Arithmetic Operations: If one of the operands is a float, we can use operations like addition and subtraction to implicitly alter the type of number (automatically). For complex numbers, this approach does not work.
- Using built-in functions: To convert between different types, we can use built-in functions like int(), float(), and complex().