Loops


Python.

Python supports Iterative Statements (Loops) and allow us to execute a statment or group of statements multiple times.

In general, programs are executed sequentially: the first statement in a program is executed first, followed by the second, and so on. There can be situations in which you may need to execute a block of code several times, and that's where loops become beneficial.

This module is all about learning Python Loops. Topics to be covered:


Challenges

While loops will repeat whatever is in the loop block while a given condition is True. It checks the conditional every iteration before executing the loop block. As soon as the condition is False, the program control passes to the line immediately following the loop.

If it fails to turn False, the loop continues to run and will not stop unless forced to stop. Such loops are called infinite loops, and are not ideal in a compute program.

Syntax of a Python while loop:

while EXPRESSION:
	# statement(s) to run will EXPRESSION evaluates to True

Example of a Python while loop:

count = 0
while count < 5:
	count = count + 1
  print("Iteration number {}".format(count))

print("End of while loop")

Challenge:

Use python to write a Count Down program

  1. Create a new file with the file extension .py
  2. Write python to accept an integer from the user
  3. Write a program that then counts down from the given number to 1, printing each number on a newline
  4. The program should stop when it reaches 1 and then print "Blast Off!"
# Example Running of the program
python yourScript.py
Enter A Number: 5
5
4
3
2
1
Blast Off!
  1. Test your code with python yourFile.py
  2. Verify your solution with verify yourFile.py

A list is a built-in data type in Python. A Python list is a sequence of comma seperated items, enclosed in square brackets. Every item in a list can be made up of any type of variable we have learned about so far, and they don't necessarily need to be the same type. Examples of Python Lists:

list1 = ["Thomas", "Jefferson", "President", 3]
list2 = [1, 2, 3, 4, 5]
list3 = ["a", "b", "c", "d", "e"]

Challenge:

Use python to create 3 different lists

  1. Create a new file with the file extension .py
  2. Write python to create 3 python lists (a list of strings, a list of integers, and a list of floats)
  3. Test your solution with python yourFile.py
  4. Verify your solution with verify yourFile.py

Python also offers a built-in range() function that returns an iterator object, essentially a list of numbers. This object contains integers from start to stop and this can be used to run a for loop.

range() Syntax:

range(start, stop, step)
# Start is the starting value of the range. Optional. Default is 0.
# Stop is where range will stop, runs until stop-1.
# Step is how we want to increment the values in the range. Optional, default is 1.

Challenge:

Use python to see the different outputs of the range() command

  1. Create a new file with the file extension .py
  2. Write python to create 3 different range() calls, trying to use different start, stop, and step values.
  3. Test your solution with python yourFile.py
  4. Verify your solution with verify yourFile.py

The for loop in Python provides the ability to loop over items in an iterable sequence such as a list or string.

Syntax of a Python For Loop:

for VARIABLE in SEQUENCE:
	# statement(s)

Example of a Python For Loop with a List:

numbers = [1, 5, 6, 2, 7, 8, 2, 9, 10]
total = 0

for number in numbers:
	total = total + number
  
print("Total: ", total)

Examples of a Python For Loop with different range() Parameters:

'''
Fun tip!
You can use end='' to change how python prints, the default is end='\n' to print a special secret newline character
You won't need to use this for this challenge, we just didn't want you to have to scroll alot to read the example output :)
'''

for number in range(5):
	print(number, end=' ')
print() # Print an empty line

for number in range(10, 20):
	print(number, end=' ')
print() # Print an empty line
  
for number in range(1, 10, 2):
	print(number, end=' ')
print() # Print an empty line

Running the code above will output the following:

0 1 2 3 4 
10 11 12 13 14 15 16 17 18 19 
1 3 5 7 9 

Challenge:

Use python to write a program that prints even number

  1. Create a new file with the file extension .py
  2. Write python to accept a positive integer from the user
  3. Write python, using a for() loop, that iterates through the range starting at 0 to the given number
  4. Print only even numbers up until the user specified number
# Example Running of the program
python yourScript.py
Enter A Number: 10
2
4
6
8
10
  1. Test your code with python yourFile.py
  2. Verify your solution with verify yourFile.py


30-Day Scoreboard:

This scoreboard reflects solves for challenges in this module after the module launched in this dojo.

Rank Hacker Badges Score