Summery: Python Tutorial for Beginners. Hope You Enjoy…

Hello guys! This is the part two of python tutorial for beginners article series. If you haven’t read the part one of the tutorial you can read it from here.

In the previous article we’ve talked about the very basics in python language. In this article we are going to dig little deeper in python. So let’s get started.

Conditionals and loops

These Conditional are used when we want make decisions within our code. So in summery if…elif…else statement is used for decision making.

if statement

a = 10
b = 20
if a > b:
   print("a is greater")

As you can see in the above code in python “if statement” is written by using the “if ” keyword.

In the example we are using two variables “a” and “b” which are equal to 10 and 20 respectively. Third line is the place where you can find the beginning of if statement. It says that if a is greater than b display “a is greater” in the terminal. Since 10 is not greater than 20 we won’t be able to see a output in the terminal.

By the way if you have only one condition in the if statement you can put it on the same line as shown below.

if a > b: print("a is greater than b")

elif Statement

a = 10
b = 20
if a > b:
   print("a is greater")
elif a < b:
   print("a is smaller")
elif a == b:
   print("a is equal to b")
else:
   print("Done")

elif statement helps you to check multiple expressions and execute a block of code. For example in the above code at first it will check if a is greater than b if it’s true it will display “a is greater” in the terminal. Obviously 10 is not greater than 20 therefore code will jump to the next line. In there it will check if a is smaller than b. Since it is true it will print out the statement “a is smaller” and exit the program without going through the rest of the code lines.

else Statement

a = 10
b = 20
if a > b:
   print("a is greater")
else:
   print("Done")

if the conditional expression in the if statement resolves to false then this else statement comes into play. For example in the above code a is not greater than b therefore the code within else statement gets execute. Therefore in the terminal the only thing we can see is the word “Done”.

Loops

for Loop

for loop is used for iterating over a sequence.

foods = ["Soya", "Rice", "meet"]
for f in foods:
    print(f)
Output

In the above example “f” act as the variable for 1st f=rice 2nd f=Soya and 3rd f=meet. This how you can iterate over a sequence using for loop.

While Loop

while loop is used to execute a set of instructions as long as a condition is true. Let’s look at the below statement.

s = 1
while s < 6:
  print(s)
  s += 1
Output

So in the above code we have a variable named as “s” and it is equal to 1. Next line it says that while “s” smaller than 6 which means while 1 is smaller than 6 print “s” and add 1 to “s”. As soon as the “s” become equal to 6 the loop will stop.

The continue and break Statements

i = 0
while i < 6:
  i += 1 
  if i == 3:
    continue
  print(i)

Continue is use if the statement is true skip the code it and jump to beginning. In the above code there is variable named “i” and it is equal to zero. next we have a while loop which says as long as “i” smaller than 6 “i” equal + 1 (i = i+1). During the 3rd iteration the condition in the 4th line become true. Then it will jump to the next line of the code which says ”continue” therefore during this iteration it will not print the number “3” on the terminal. It will continue to iterate by printing 4, 5 and 6.

Output

If we remove the 5th line from the above code which contains the “continue” statement the output will be only number 3.

Outpput
i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1

Using the break statement we can stop the stop the loop even if the condition is true.

Output

See you from the part 03 of this tutorial.

Also Read: Python Tutorial for Beginners (Part 03)


Leave a Reply

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