Introduction to Programming - 3 Flashcards

1
Q

What are loops in Python?

A

allow you to execute a block of code repeatedly based on a condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Loop is a general term for a programming construct which allows us to

A

do things many times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is loops good practically? - (2)

A

might be making lots of stimuli for an experiment or analyzing lots of different datasets.

..or that thing you just did where you printed out the same ‘count’ statement more or less six times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

2 types of loops in Python - (2)

A
  1. For loop
  2. While loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is For loop?

A

For loops in Python are used to iterate over a sequence (such as a list, tuple, string, or range) and execute the same block of code for each element in the sequence

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Loop you will commonly encounter is a

A

for loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

General structure of for loop

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does this general structure mean? - (4)

A

What this means is that LOOPVARIABLE (which can be named whatever you want) will be set to the first item in LIST_OF_THINGS_TO_ITERATE_OVER. The code which is indented (just a comment in this case) will then run.

Once all of the code which is indented has been run, the loop will go back to the top,

LOOPVARIABLE will be set to the second item in LIST_OF_THINGS_TO_ITERATE_OVER and all of the indented code will run again.

This will repeat for each element in LIST_OF_THINGS_TO_ITERATE_OVER.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Example of for loop using structure

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What will be the output?

A

Hello!
Hello 1
Hello!
Hello 2
Hello!
Hello 3
Hello!
Hello 4
Hello!
Hello 5
Hello!
Hello 6
Hello!
Hello 7
Hello!
Hello 8
Hello!
Hello 9
Hello!
Hello 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In the for loop

The for keyword…

It is followed by…

The keyword in…

Finaly there is :

The code inside te for loop is … - (6)

A

The for keyword starts the loop.

It is followed by the variable which is created to run the loop.

The keyword in is then used, followed by the expression which gives the list of items over which to iterate.

As we will see later, this is not always a list or tuple - there are other possibilities.

Finally, there is a colon : which shows that the loop is starting.

The code inside the loop is indented. In other words it is shifted to the right - like this line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In loops, Python works out where your loop ends by - (2)

A

looking at the indentation of the text – i.e. it uses spaces. At first this can be confusing but it becomes second nature after a while.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

To ident code inside loop you can either use

A

4 spaces or tab

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Create a for loop that produces 1-10

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Iterating over a list refers to the process of

A

accessing each element in the list one by one in a sequential manner.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The simplest form of a loop involves

A

setting up a list and then performing some code for each entry in the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

The simplest form of a loop involves setting up a list and then performing some code for each entry in the list. For example, code below shows..

A

we print each element in a list followed by itself squared

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Output of this code and explain - (4):

A

Meaning:

print(x,x**2) is idented within for loop so we print each element in a list followed by itself squared:

In print(x.5) is not idented within for loop and identation is different from print(x,x2) so left the for loop

print(x,x**.5) square root the last element of the list (‘x’) which is 40 and prints “Done”

Output:
10 100
20 400
30 900
40 1600
6.324555320336759
Done

19
Q

Example of for loop written out in detail/full

A
20
Q

For loops do not have to be on a single line as we can write:

A
21
Q

Explain this code and give its output:

A

Meaning:
The loop iterates over each element in the list my_list.

For each iteration, it prints “Hello”.

It computes the square of the current element (x) and stores it in y.

It then prints both the current element (x) and its square (y).

Output:
Hello
10 100
Hello
20 400
Hello
30 900
Hello
40 1600

22
Q

Long-handed version of the for loop code

A
23
Q

You can run any code you want inside of a loop. One thing which you should not do however, is to - (2)

A

delete an item in a list whilst you are looping over the list.

This will cause hard to find problems in your code and almost certainly not do what you want

24
Q

Write a code that has a list 10-100 stored in my_list = [] var

For loop that loops over that list and prints out value of x and its x squared and its square root on each iteration

A
25
Q

What is output of the code?

A
26
Q

What is nested for loops? - (3)

A

situation where one for loop is contained within another for loop.

This means that one loop runs inside another loop.

Each time the outer loop runs once, the inner loop can potentially run multiple times, depending on its own iteration behavior.

27
Q

For nested for loops it is

A

important to follow the indentation carefully reading this code.

28
Q

Meaning of this nested for loop example - (13)

A

As we can see, the first loop uses x as its loop variable.

On line 4, this will initially be set to the first value of my_first_list which is 1 (this is represented by x = 1 in the output comments).

We then progress to line 5 where we print out the statement Before Y loop starts.

On line 7, another loop is constructed.

The second loop uses y as its loop variable.

You should always use a different loop variable for each nested loop.

At this point, y will be set to the first value in my_second_list, which is 50 (this is represented by # y = 50 in the output comments).

We then execute the code within the loop on line 8 (the print function call) which prints out the values of both x and y.

At this point, we run out of code, so we go back to the innermost loop (line 7) and update y to 60.

We run the print again, then update y to 70 and run the print again.

We have now finished the “inner” y loop and execute line 10 which prints out the string After Y loop ends.

We then go back to line 4 and update the x variable to 2.

We now continue to line 5 again, which causes us to go print the Before Y loop starts string and then go around the y loop again, making y equal to 50, 60, and 70 in turn in the same way that we did previously

29
Q

Long-handed version of this for loop

A
30
Q

Fix the code to simulate all possible sums of two 6-sided dice, Which number is the most common? - (2)

A

It determines which number is the most common by printing the sum of each pair of throws and then analyzing the results.

The most common number should be 7 since there are more combinations of dice throws that sum to 7 than any other number

31
Q

What is range() in Python? - (2)

A

built-in function used to generate a sequence of numbers.

It’s commonly used in loops to iterate over a specific range of numbers.

32
Q

3 ways to use/call range() function - (3)

A
  1. range(stop)
  2. range(start,stop)
  3. range(start,stop,setp)
33
Q

What is range(stop)

A

This generates a sequence of numbers starting from 0 up to (but not including) the specified stop value.

34
Q

What is range(start,stop)?

A

This generates a sequence of numbers starting from start up to (but not including) the stop value.

35
Q

What is range(start,stop,step) - (2)

A

This generates a sequence of numbers starting from start up to (but not including) the stop value, with a specified step size between each number.

The step parameter is optional and defaults to 1 if not provided

36
Q

In python, all stop-style parameters are exclusive; i.e. - (3)

A

they are not included in the list which is returned.

This means that if we ask for range(0, 4), we will get the numbers 0, 1, 2, 3.

Forgetting how this works is a common source of bugs.

37
Q

What will this output give you?

x = range(5)
print(type(x))
print(x)

A

<class ‘range’>
range(0, 5)
print the generator

38
Q

What will this code give you?

x = list(range(5))
print(type(x))
print(x)

0 it is convert to list and print

A

<class ‘list’>
[0, 1, 2, 3, 4]

39
Q

What will this code give you?

x = list(range(1, 500))
print(x)

A

[1, 2, 3, 4, 5, …., 499]

40
Q

The step parameter in range… - (2)

A

sets the increment or decrement.

Setting this to be negative lets us count backwards.

41
Q

What will these code give? - (3)

A

[1, 3]
[5, 4, 3, 2, 1]
[4, 3, 2, 1, 0]

42
Q

range function generates a sequence of integer numbers within a specified range.

If you want to use floating point numbers then

A

use the arange function from the numpy module – we will cover this in a future session.

42
Q

Exercise:
Write python code, using a loop, to count between -10 and 10 (inclusive) and for each number print out the number and its cube (i.e. the number to the power 3).

A