Control Flow And Loops

Control Flow And Loops

If Statements and Loops :

An "if statement" is written by using the if keyword.

In this example we use two variables, a and b, which are used as part of the if statement to test whether b is greater than a. As a is 50, and b is 60, we know that 60 is greater than 50, and so we print to screen that "b is greater than a".

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

#Returns - b is greater than a        

Indentation

Python relies on indentation to define scope in the code. Other programming languages often use curly-brackets for this purpose.

Elif

The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition".

a = 50
b = 50
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")

# Returns - a and b are equal        

Else

The else keyword catches anything which isn't caught by the preceding conditions.

a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

#Returns - a is greater than b        

And

The and keyword is a logical operator, and is used to combine conditional statements

#Test if a is greater than b, AND if c is greater than a
a = 200
b = 33
c = 500
if a > b and c > a:
  print("Both conditions are True")

# Returns - Both conditions are True        

Or

Test if a is greater than b, OR if a is greater than c

a = 200
b = 33
c = 500
if a > b or a > c:
  print("At least one of the conditions is True")

#Returns - At least one of the conditions is True        

Not

The not keyword is a logical operator, and is used to reverse the result of the conditional statement

# Test if a is NOT greater than b

a = 33
b = 200
if not a > b:
  print("a is NOT greater than b")

# Return - a is NOT greater than b        

Nested If

You can have if statements inside if statements, this is called nested if statements.

x = 41

if x > 10:
  print("Above ten,")
  if x > 20:
    print("and also above 20!")
  else:
    print("but not above 20.")

# Returns - 
Above ten,
and also above 20!        

The pass Statement

if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.

a = 33
b = 200

if b > a:
  pass

## having an empty if statement like this, would raise an error without the pass statement        

Loops

Python has two primitive loops

  • while loops
  • for loops

The while Loop

With the while loop we can execute a set of statements as long as a condition is true.

Note: remember to increment i, or else the loop will execute infinite times.

i = 1
while i < 6:
  print(i)
  i += 1

# In Above example loop will execute till i is less than 6
#Return -
1
2
3
4
5        

The break Statement

With the break statement we can stop the loop even if the while condition is true

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

# Loop will execute till i = 3        

The continue Statement

With the continue statement we can stop the current iteration, and continue with the next

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

# Note that except 3 all the number less than 7 will print         

The else Statement

With the else statement we can run a block of code once when the condition no longer is true

i = 1
while i < 6:
  print(i)
  i += 1
else:
  print("i is no longer less than 6")

# Return - 
1
2
3
4
5
i is no longer less than 6        

For Loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

fruits = ["orange", "pineapple", "Graphes"]
for x in fruits:
  print(x) 

# Return - 
orange
pineapple
Graphes        

The break and continue statements will work same in for loop as they work in while loop

range() Function

for x in range(6):
  print(x) 

# It will return the number from 0 to 5         

The range() function defaults to 0 as a starting value,it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6)

要查看或添加评论,请登录

社区洞察

其他会员也浏览了