Python pt.2

conditional statement and iteration statement

Python conditional statement

  • Literally, the conditional statement can be used in the conditions of data analysis on Python.

  • The reserved word of conditional statement is if/elif/else.

  • if: The first condition is true.

  • elif: if not true in the first condition, but other conditions are true.
  • else: when every above conditions are false

  • Format with example:

    1
    2
    3
    4
    5
    6
    if a > 15:
    print('High')
    elif a = 15:
    print('Yes!')
    else:
    print('Low')
  • Conditional statement is useful in various codes. (You’ll use it a lot!!)

Python iteration statement

  1. while:
  • repeat infinitely not until the reserved word break is used (in other words, repeat until conditions are met)
  • format:
    1
    2
    3
    while [conditions (able to be without conditions)]:
    statement..
    ~
  • if you stop the while statement because the conditions are alreay met, you have to use the reserved word break.
  • if you execute the statement and move on the next statement by passing only some specific condition, you can use the reserved word continue.
  1. for:
  • repeat during the defined number of times
  • use the range function with for loop (the automatically defined number of times)
  • format:
    1
    2
    3
    for i in [list, tuple ...] (or range(#, #+1):
    statement..
    ~