Learn Programming in Python from Scratch More Advanced Stuff

Words
422
Reading
2 min
Listen
Play
9y

In this post we will learn something useful :).

Before proceeding with it, we need to know our basics (primitive types and the concepts of variables in Python),
if you have to remind yourself consult: https://steemit.com/science/@ai-enthusiast/learn-programming-in-python-from-scratch-getting-started

Learning objectives

  1. Get familiar with Control Flow (Get to know different types of loops and if-else statements)
  2. Put it all together, in the example which we'll use later to build upon

Thanks to omarf, who suggested changes :D

Control Flow:

Understanding Control Flow(order in which the commands are executed) is essential to programming. There are just a few ways to change it. However the way they're combined, leads to complex programs that are actually useful :D. Without these ways, order in which commands are executed is from the first line up to the last one.

Operators:

Before we proceed, I must point some other things in Operators. You can also do this:
operat.png
operats.png

Now, let's continue:

Loops:

When you have a piece of code you want executed multiple times, instead of copy/pasting the code, which makes it hard to read and prone to error, you would want to use loops. There are two types of loop, FOR loop (which you will use usually when you know in advance what number of iterations(passes through a loop) will be there, and WHILE loop (which will run, until some condition is met). Anything you write inside of a loop is considered it's 'body';IF-Else.png
loops.png

Scope

Make special attention to the indentation. In Python, if you indent something that means it belongs to the outer scope (area in which it is visible).
scope.png
In other programming languages you wouldn't be able to acces a Variable defined in some other scope (you can, but not directly). In Python you can access it, basically directly.

IF-ELSE:

When you want you your program to have branches, meaning only one branch(path) gets executed, you would use IF-ELSE statements. Statement number == 1 returns a Truth value (True or False). If it returns True the body of the IF gets executed. If it returns False, body of ELSE gets executed.
IF-Else.png
There's also an ELIF statement, which tests another condition if the condition in IF statement is not met.
ELIF.png

Putting it together:

We will write our first complete program. Let's say we're teachers and we want to review our students based on grades.
If they got a good grade, we will encourage them. If they got a bad one, well...
program2.png

out.png

Sucks to be Mya :D

See you in the next post.

Learn Programming in Python from Scratch More Advanced Stuff | Ecency