Control Structures In Python

In this tutorial, we’ll learn control structures in Python. Control structures tutorial consist of While, For loop, and condition statement. Loops and condition statements almost use in all Python projects so, you should know these 2 subjects. If you want to look at control structures in a different programming language, you can read Control Structures In R.

What Are Loops And Conditional Statements?

Loop, used to automate repetitive processes in the program. Instead of trading one by one, you can automate your transactions with a small loop block.

Condition statements used to create different solutions for questions, except this, you can use for compare statement, so you have 2 value and you want to compare these 2 value, you can use conditional statements.

If – Elif – Else Statements

Now, we going to learn condition statements, we can use if – elif – else for condition statements. Below, I gave syntax these functions.

if condition:
  # Code
elif condition:
  # Code
  # Code

First, the if statement is run. If the condition part is true, the code block runs, if it is false, it moves to another condition. Secondly, elif condition works. The same logic applies here as well. If “elif statement” is working code blocks runs, if doesn’t work passes to another conditional statement. In else, the last code block here will work if no conditional statement has worked.

Conditional Statement Practices In Real Projects

Now, we going to look at conditional statements in real projects, so we going to create a new project with conditional statements. We will ask the user for a value and print whether this value is less than 10 or greater.

Now, we finished the first project, let’s pass on another project, we going to create a basic login system. In this project, we ask username and password after that we will compare it with the username and password we have, if we can log in, we going to print “Welcome”, if not, we will print an error message.

Logical Operators

Above, we have seen “and” logical operator, now we going to look at all logical operators but first, let’s we look at what is the logical operator?

What Are Logical Operators?

Used to link two or more expressions together. For example, we can use the “and” operator for two statements that should be true.

And -> connects two statements both statements must be true. (True and True)
Or -> connects two statements if one of the two statements is true, it is sufficient. (True or False)
Not -> converts the structure it brings to negative. (not True = false , not False = True)
While And For Loop

We finished conditions, now we’ll look at loops in python. First, we going to learn syntax after that we use these loops in real projects.

While Loop Syntax

while condition:

If the condition added to the condition section is true, the code block will run continuously until the condition section is false.

For Loop Syntax

for i in varaible:
  # Code

in here, loops run as many times as the value in the given variable.

Loops Practices In Real Projects

We’ll print the message we receive from the user on the screen 10 times, for this, you can type print 10 times or you can use the while loop (For loop can also be used)

Now let’s print all the elements of a list to test the “For Loop” loop. If you don’t know the lists, no problem I’ll show list logic in the script.

Break and Continue Statement

With break and continuation structures you can control your loops. For example, you can end loops, and skip certain structures.


Leave a Reply

Your email address will not be published. Required fields are marked *