Category: 3. Control flow

  • Python pass

    Summary: in this tutorial, you’ll learn how to use the Python pass statement as a placeholder. Introduction to the Python pass statement Suppose that you have the following if…else statement: In the else clause, you haven’t got any code yet. But you’ll write code for this else clause later. In this case, if you run the code, you’ll get a syntax error (SyntaxError).…

  • Python continue

    Summary: in this tutorial, you’ll learn about the Python continue statement and how to use it to control the loop. Introduction to the Python continue statement The continue statement is used inside a for loop or a while loop. The continue statement skips the current iteration and starts the next one. Typically, you use the continue statement with an if statement to skip the current iteration once a condition is True.…

  • Python break

    Summary: in this tutorial, you’ll learn about the Python break statement and how to use it to exit a loop prematurely. Introduction to the Python break statement Sometimes, you want to terminate a for loop or a while loop prematurely regardless of the results of the conditional tests. In these cases, you can use the break statement: Typically, you use the break statement with the if statement to terminate a…

  • Python while

    Summary: in this tutorial, you’ll learn about the Python while statement and how to use it to run a code block as long as a condition is true. Introduction to the Python while statement Python while statement allows you to execute a code block repeatedly as long as a condition is True. The following shows the syntax of the Python while statement: The condition is an…

  • Python for Loop with Range

    Summary: in this tutorial, you’ll learn about the Python for loop and how to use it to execute a code block a fixed number of times. Introduction to Python for loop statement with the range() function In programming, you often want to execute a block of code multiple times. To do so, you use a for loop. The following…

  • Python Ternary Operator

    Summary: in this tutorial, you’ll learn about the Python ternary operator and how to use it to make your code more concise. Introduction to Python Ternary Operator The following program prompts you for your age and determines the ticket price based on it: Here is the output when you enter 18: In this example, the…

  • Python if Statement

    Summary: in this tutorial, you’ll learn how to use the Python if statement to execute a block of code based on a condition. The simple Python if statement You use the if statement to execute a block of code based on a specified condition. The syntax of the if statement is as follows: The if statement checks the condition first. If the condition…