top of page
Search

Flow control in python

  • Writer: Nanditha Mahesh
    Nanditha Mahesh
  • Jun 24, 2024
  • 4 min read

Python program control flow is regulated by various types of  conditional statements, loops and function calls. Python Training in Bangalore By default, the instructions in a computer program are executed in a sequential manner, from top to bottom, or from start to end. However, such sequentially executing programs can perform only simplistic tasks. We would like the program to have a decision-making ability, so that it performs different steps depending on different conditions.

Boolean Values

Boolean values in Python are True and False. They are used to represent truth values and are the result of comparisons or logical operations Most programming languages including Python provide functionality to control the flow of execution of instructions. Normally, there are two type of control flow statements in any programming language and Python also supports them.While the integer, floating-point, and string data types have an unlimited number of possible values, (Boolean is capitalized because the data type is named after mathematician George Boole.) When typed as Python code, the Boolean values True and False lack the quotes you place around strings, and they always start with a capital T or F, with the rest of the word in lowercase.

Explanation:

  1. We define two variables, x and y.

  2. We use an if statement to check if x is less than y and print a message if true.

  3. We check if x is even by evaluating the expression (x % 2 == 0) and store the result in is_even.

  4. We determine if x is odd using the not operator on is_even and store the result in is_odd.

  5. We print the boolean values of is_even and is_odd.

  6. We use an if statement with a boolean expression combining is_even and a comparison y > 15 to print a message if both conditions are true.

Comparison operators In Python are used to compare two values. They return boolean values (True or False). Here’s a list of theDifferent B/W operators available in Python:

The Difference Between the == and = Operators

You might have noticed that the == operator (equal to) has two equal signs,

while the = operator (assignment) has just one equal sign. It’s easy to confuse these two operators with each other. Just remember these points:

• The == operator (equal to) asks whether two values are the same as each other.

• The = operator (assignment) puts the value on the right into the variable on the left.

To help remember which is which, notice that the == operator (equal to) consists of two characters, just like the != operator (not equal to) consists of two characters.

Boolean Operators

The three Boolean operators (and, or, and not) are used to compare Booleanvalues. Like comparison operators, they evaluate these expressions down to a Boolean value. Let’s explore these operators in detail, starting with the and operator.Python Training Course Certification in Bangalore

Binary Boolean Operators

The and or operators always take two Boolean values (or expressions),so they’re considered binary operators. The and operator evaluates an expres-sion to True if both Boolean values are True; otherwise, it evaluates to False.Enter some expressions using and into the interactive shell to see it in action.

Mixing Boolean and Comparison Operators

Since the comparison operators evaluate to Boolean values, you can use them in expressions with the Boolean operators.Recall that the and, or, and not operators are called Boolean operators because they always operate on the Boolean values True and False. While expressions like 4 < 5 aren’t Boolean values, they are expressions that evalu-ate down to Boolean values. Try entering some Boolean expressions that use comparison operators into the interactive shell.

Elements of Flow Control

Top Python Training in Bangalore Flow control statements often start with a part called the condition, and all are followed by a block of code called the clause. Before you learn about Python’s specific flow control statements, I’ll cover what a condition and a block are.

Conditions

The Boolean expressions you’ve seen so far could all be considered con-ditions, which are the same thing as expressions; condition is just a more specific name in the context of flow control statements. Conditions always evaluate down to a Boolean value, True or False. A flow control statement decides what to do based on whether its condition is True or False, and almost every flow control statement uses a condition.

Blocks of Code

Lines of Python code can be grouped together in blocks. You can tell when a block begins and ends from the indentation of the lines of code. There are three rules for blocks.

1. Blocks begin when the indentation increases.

2. Blocks can contain other blocks.

3. Blocks end when the indentation decreases to zero or to a containing block’s indentation.

 Conclusion

 In 2024 Python is a dynamic and influential language that can significantly enhance your programming skills and career prospects. Through Nearlearn, you have built a strong foundation in Python, setting the stage for further growth and exploration in the world of programming. Continue to practice, experiment, and innovate with Python to fully harness its capabilities and achieve your professional goals.







 
 
 

Recent Posts

See All
21 Number game in Python

Let's create the "21 Number Game" in Python. This is a common game where two players take turns adding 1, 2, or 3 to a running total,...

 
 
 

Comments


© 2035 by Skyline

Powered and secured by Wix

bottom of page