Flow of Information

The information in a code flows in a direction based on the loops and conditional statements written.

Control Statements

These statements determine the order in which the code is executed. Two major ways of writing control statements are using iteration and transfer statements.

Iteration

By: (Sumedha, Aditya, Ishraq, Grayson, Anthony)

What is iteration?

We use iteration when we need to repeat the same code for a multiple number of times. Iteration is the looping of data in a code sequence. In Python, iteration is done using While loops and For loops.

While loops

While loops are used when the looping depends on a condition. As long as the condition is true, the code will be executed. When the condition becomes false, the loop stops.

In a while loop, we need to specify the index value (where the loop starts) and the increment value (what the loop increases by).

i = 0
while i < 5:
    print(i)
    i += 1
0
1
2
3
4

Hack 1:

Write a code for printing even numbers from 1 to 10 using a while loop.

i=2
while i<10:
    if i%2==0:
        print (i)
        print("Aditya is amazing")
        i += 2

2
Aditya is amazing
4
Aditya is amazing
6
Aditya is amazing
8
Aditya is amazing

Transfer Functions

Statements which transfer control from one part of a program to another. Examples are: break, continue, and pass statements.

Break Funtion

To stop a code from printing after a certain value, use the break function. We use an if statement to decide where the output will stop. The break function is placed before the increment and after the if statement.

i = 1
while i < 5:
  print(i)
  if i == 2:
    break
  i += 1

Continue Function

To skip an output value and continue on to the end value, use the continue function. We use an if statement to decide which value should be skipped. The continue function is placed after both the increment and the if statement.

i = 0
while i < 5:
  i += 1 
  if i == 2:
    continue
  print(i)

Hack 2:

Write a code to list the numbers from 1 to 20 while skipping the multiples of 2 using a while loop.

i = 0
while i<20:
    if i%2 != 0:
        print(i)
    i+=1
1
3
5
7
9
11
13
15
17
19

Pass Function

The pass function is like a placeholder after empty code has been written. When a pass statement is added, there will be no effect, but there will be no error message. If there is no pass statement, then an error message will appear.

for x in []:
    pass

Else Statements

If a condition isn’t true anymore, then we can write a code using an else statement which stops the iteration from continuing.

i = 1
while i < 5:
  print(i)
  i += 1
else:
  print("i is no longer less than 5")
1
2
3
4
i is no longer less than 5

For Loops

For Loops are used to iterate over a group of values such as lists, strings, dictionaries, tuples, etc.

gemstones = ["aquamarine", "beryl", "citrine"]
for x in gemstones:
  print(x)
aquamarine
beryl
citrine

Looping through Strings

We can iterate through strings by using a for loop that shows each character in the string. Example below:

for x in "beryl":
    print(x)

Break Function

Using a break function, we can stop the loop from iterating when the output is a certain value. This is the same as in while loops.

gemstones = ["aquamarine", "beryl", "citrine"]
for x in gemstones:
  print(x)
  if x == "beryl":
    break

Continue Function

Using a continue function, we can skip an output value and continue the iteration till the end of the sequence. This is similar to while loops.

gemstones = ["aquamarine", "beryl", "citrine"]
for x in gemstones:
    if x == "beryl":
        continue
    print(x)

Range Function

Using a range function, we can iterate through a specific range of values. This function starts with zero and increments by 1, which it does automatically, and we specify exactly which values to iterate through.

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

We can define the starting value if it is other than zero by adding a second parameter.

for x in range(2,8):
    print(x)

We can also define the increment value by adding a third parameter.

for x in range(1,8,2):
    print(x)

Else Statements

We use else statements to signal when a code is done iterating through a set of values. It is used to code for a message which is printed to show when the iteration is complete.

for x in range(8):
    print(x)
else:
    print("The code is finished!")

Nested Loops

Nested Loops are loops within loops. This is used to return an output which is a combination of the outer and inner loops.

for x in [1, 2]:
    print(x)
    for y in [2, 3, 4, 5]:
        print(y)
for i in range(4,6):
    print (i)
    for x in range(1,11):
        print (i, "X", x, "=", i * x)

Hack 3

Write a code using a for loop that outputs numbers from 1 to 100 and skips multiples of 4 and breaks at 75.

Upper 90%: Add multiple parameters to specify your values. Use continue to skip the proper values. Double check outputs.

# Your code here
i = 0
for i in range(1, 100):
    if i%4 == 0:
        pass
    else:
        print(i)
    if i==75:
        break
    
1
2
3
5
6
7
9
10
11
13
14
15
17
18
19
21
22
23
25
26
27
29
30
31
33
34
35
37
38
39
41
42
43
45
46
47
49
50
51
53
54
55
57
58
59
61
62
63
65
66
67
69
70
71
73
74
75

Homework

Create a function that takes a list of numbers and arranges the in ascending order

For example this function should be able to take the list [35,4,6,90,2,55,13,8] and arrange it into [2,4,6,8,13.35,55,90]

Extra Credit

Make it so this function can also sort strings by their length For example [Car,4,11,twenty,27,trees] should sort into [Car,4,trees,twenty,11,27]

def shortestToLongestSorter(items):
    # Separate numbers and strings
    nums = [item for item in items if isinstance(item, (int, float))]
    strings = [item for item in items if isinstance(item, str)]

    # Sort numbers and strings separately
    nums.sort()
    strings.sort(key=lambda x: len(str(x)))

    # Merge sorted numbers and strings back into a single list
    sorted_list = nums + strings

    return sorted_list

# Test cases
numbers_list = [35, 4, 6, 90, 2, 55, 13, 8]
strings_list = ["Car", 4, 11, "twenty", 27, "trees"]

sorted_numbers = shortestToLongestSorter(numbers_list)
sorted_strings = shortestToLongestSorter(strings_list)

print("Sorted Numbers List:", sorted_numbers)
print("Sorted Strings List:", sorted_strings)

Sorted Numbers List: [2, 4, 6, 8, 13, 35, 55, 90]
Sorted Strings List: [4, 11, 27, 'Car', 'trees', 'twenty']