Quantcast
Channel: Cloud Training Program
Viewing all articles
Browse latest Browse all 1890

Python Loops and Control Statements Q & A: Day 3 Live Session Review

$
0
0

This blog will cover the Q&As of Python for Data Science (AI/ML) and Data Engineer Training covering Python Looping and Control Statements. This blog will help you to clear your concepts with Looping and Control Statements
We also covered hands-on Lab 8Lab 9, and Lab 10 out of our 25+ extensive labs.

The previous week, In Day 1 & 2 Live Session, we have covered the basic concepts of Python, Introduction, Features, Classes, Objects, and Data Structure Basics.

So here are some Q & As of Day 3 during the live session from Module 3: Python Looping and Control Statements.

Loop

Loops in Python allow us to execute a group of statements several times. The first statement in a function is executed first, followed by the second, and so on.
There may be a situation when you need to execute a block of code several times. Programming languages provide various control structures that allow for more complicated execution paths. The following diagram illustrates a loop statement −

Python: Loop

Q1: What is a while Loop?
Ans: A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true.
The syntax of a while loop in the Python programming language is −

while expression:
 statement(s)

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the loop.
Example:

while Loop

In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.

Q2: What is a Loop?
Ans: It has the ability to iterate over the items of any sequence, such as a list or a string.
The syntax of a for loop in Python programming language is −

for iterating_var in sequence:
 statements(s)

If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted.
Example:

Loop

Q3: What is a Nested Loop?
Ans: Python programming language allows to use of one loop inside another loop. The following section shows few examples to illustrate the concept.
The syntax of a Nested For loop in Python programming language is −

for iterating_var in sequence:
  for iterating_var in sequence:
    statements(s)
  statements(s)

The syntax for a Nested While loop statement in the Python programming language is as follows −

while expression:
  while expression:
    statement(s)
  statement(s)

A final note on loop nesting is that you can put any type of loop inside of any other type of loop. For example, a for loop can be inside a while loop or vice versa.
Example:

python: Nested While loop

Q4: How does For Loop differ from a While Loop?
Ans: Main Differences are :

  • Initialization, conditional checking, and increment or decrement are done while iteration in the “for” loop is executed. while on the other hand, only initialization and condition checking in the syntax can be done.
  • For loop is used when we are aware of the number of iterations at the time of execution. on the other hand, in the “while” loop, we can execute it even if we are not aware of the number of iterations.
  • If you forgot to put the conditional statement in for loop, it will reiterate the loop infinite times but if you forgot to put the conditional statement in while loop, it will show an error to you.
  • The syntax in the for loop will be executed only when the initialization statement is on the top of the syntax but in the case of the while loop, it doesn’t matter whether the initialization statement finds anywhere in the syntax.
  • The iteration will be executed if the body of the loop executes. on the contrary, the iteration statement in the while loop can be written anywhere in the syntax.

Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

Q5: What is a break Statement?
Ans: It terminates the current loop and resumes execution at the next statement, just like the traditional break statement in C.
The most common use for a break is when some external condition is triggered requiring a hasty exit from a loop. The break statement can be used in both while and for loops.

If you are using nested loops, the break statement stops the execution of the innermost loop and starts executing the next line of code after the block.
The syntax for a break statement in Python is as follows − break
Example:

python: break Statement

Q6: What is a continue Statement?
Ans: It returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.The continue statement can be used in both while and for loops.
The syntax for a continue statement in Python is as follows − continue
Example:

python: continue

Q7: What is a pass Statement?
Ans: It is used when a statement is required syntactically but you do not want any command or code to execute. The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example)
The syntax for a pass statement in Python is as follows − pass
Example:

python: pass Statement

General Questions

Q8: What are Keywords in Python?
Ans: Keywords in python are reserved words that have special meaning. They are generally used to define types of variables. Keywords cannot be used for variable or function names. There are the following keywords in python-

Keywords in Python

Q9: What is a namespace in Python?
Ans: A namespace is a naming system used to make sure that names are unique to avoid naming conflicts.
There 4 types of namespaces in python

  • Built-in namespace– These namespaces contain all the built-in objects in python and are available whenever python is running
  • Global namespace– These are namespaces for all the objects created at the level of the main program.
  • Enclosing namespace– These namespaces are at the higher level or outer function.
  • Local namespace– These namespaces are at the local or inner function.

Related References

Next Task For You…

Python’s growth is very promising in the near future. Gaining the right skills through the right platform will get you to the perfect job.

We are launching our course Python For Data Science (AI/ML) & Data Engineers (Python For Beginners) which will you help and guide you towards your first steps to Python. Join our Waitlist for the FREE CLASS now.

Free Class Python

The post Python Loops and Control Statements Q & A: Day 3 Live Session Review appeared first on Cloud Training Program.


Viewing all articles
Browse latest Browse all 1890

Trending Articles