← Back to Articles

For, While, If and If Else

Jamie Z 2024-02-01

Learning Goals

  • Deepen understanding of Loops and Selection statements
  • Understanding Global and Local scope with variables

What is a FOR loop?

  • The for loop is a common control structure in Python that allows you to repeat a block of code a specific number of times
numbers = [1, 2, 3, 4, 5]
for num in numbers:
    print(num)
  • Here, a sequence can be any iterable object, such as an array, or even a range of numbers

Range

  • You can also use the range function to generate a sequence of numbers, and use that sequence in a for loop. For example:
for i in range(5):
    print(i)
  • In this example, the range function generates a sequence of numbers from 0 to 4, and the for loop iterates over that sequence. Note that the range function generates numbers up to, but not including, the specified end value.

What is a WHILE loop?

  • A while loop is a structure in Python that repeats a block of code until a certain condition is met
count = 1
while count <= 5:
    print(count)
    count += 1
  • It's important to be careful when using while loops, as it's possible to create an infinite loop if the condition never becomes false. To avoid this, you should make sure that the condition you're testing in the loop will eventually become false, or you should use a break statement to exit the loop early if a certain condition is met.
Calculating Factorial ```python n = int(input("Enter a number: ")) factorial = 1 i = 1 while i <= n: factorial *= i i += 1 print("The factorial of", n, "is", factorial) ```

💡

# What is an IF statement?
  • The if and if-else statements are used in Python to perform conditional execution of code. They allow you to specify one or more conditions, and depending on whether the conditions are met or not, the code inside the if or if-else block is executed.
  • The basic syntax of an if statement is as follows:
if condition():
	#code to be executed if condition is true
	pass
  • Here, condition is an function that evaluates to either True or False. If the condition is True, the code inside the if block is executed. If the condition is False, the code inside the if block is skipped.

What is an IF ELSE statement?

  • If the condition is True, the code inside the first block (after the if) is executed, and if the condition is False, the code inside the second block (after the else) is executed.
if condition():
    # code to be executed if condition is True
else:
    # code to be executed if condition is False
Is even? ```python num = int(input("Enter a number: ")) if num % 2 == 0: print(num, "is even.") else: print(num, "is odd.") ```
# Activity

Research and create your own python program that:

  • Asks for password
  • Accepts a user input
  • Ask for username
  • Stores the username
  • If the Password is less than 10 letters, output to the user that it needs to be longer
  • If the Password is more than 15 letters, output to the user that it needs to be shorter
  • If the Password is between 10 and 15 letters, outputs to the user a string that says “Hello. Your password meets the minimum length requirement.”