in this week we've learned the following things :
In Python, the break
statement is a control statement used to exit a loop prematurely. It allows you to terminate the current loop and resume execution of the program after the loop. Here's a simple explanation of how the break
statement works:
Imagine you have a loop (e.g., for or while) that is designed to run a set of instructions repeatedly until a certain condition is met. The break
statement is used to interrupt the loop and stop its execution when a specific condition is satisfied. Once the break statement is encountered, the loop immediately exits, and the program continues with the code outside of the loop.
In Python, the continue
statement is a control statement used within loops (such as for or while) to skip the current iteration and proceed to the next one. Here's a simple explanation of how the continue
statement works:
When a continue
statement is encountered inside a loop, it immediately stops the current iteration of the loop and moves on to the next iteration. This means that any code following the continue
statement within the current iteration is skipped, and the loop continues with the next iteration.
The while
statement in Python is used for creating loops that continue executing a block of code as long as a specified condition is True. It repeatedly checks the condition before each iteration and stops when the condition becomes False. The while
loop allows you to automate repetitive tasks and control the flow of your program based on changing conditions.
In a while
statement, the else block will always be executed when the condition in the while
statement becomes False.
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
- Without list comprehension you will have to write a for statement with a conditional test inside.
- With list comprehension you can do all that with only one line of code.
The condition is like a filter that only accepts the items that valuate to True.
The iterable can be any iterable object. You can use the range() function to create an iterable.
- A function in mathematics is a process that maps an input to an output. In Python,
functions
serve a similar purpose while also being used to organize code for reusability. - Python
functions
should ideally have a specific purpose and be designed for reusability. - While Python provides common built-in functions, developers can also define their own custom
functions
.
By default in Python, when a function is declared, its parameters are positioned in the order in which they were defined. For the function to be invoked correctly, the parameters must be called in the same order.
- The function will terminate the execution of the program and simultaneously return a specific value when the return statement is used with the assigned expression.
- The return value of a function can be stored in a variable, which distinguishes it from a void function, which has no return value. It is this defining feature that determines whether a value is returned or not.