Skip to content

List Of Contents : Break, Continue, While Loops, List Comprehension, Functions

Notifications You must be signed in to change notification settings

pritaaa/Python-part-2

Repository files navigation

Python-part-2

in this week we've learned the following things :

1. Break

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:

image

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.

2. Continue

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:

image

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.

3. While

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.

image

ELse-while

In a while statement, the else block will always be executed when the condition in the while statement becomes False.

else-while

4. List Comprehension

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.

list-1

  • With list comprehension you can do all that with only one line of code.

list-2

- Condition

The condition is like a filter that only accepts the items that valuate to True.

condition

- Iterable

The iterable can be any iterable object. You can use the range() function to create an iterable.

iterable

5. Functions

  • 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. image

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.

Return

  • 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. image

About

List Of Contents : Break, Continue, While Loops, List Comprehension, Functions

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published