Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Weekly Python Assignment 3 - Group 1

Instructions: For each exercise below, type the requested code into the cell below the exercise, then run your code to verify that it works correctly.

1. Set a variable x equal to 0. Create a while loop that runs as long as the variable x is less than 5. Inside the loop, print the value of x, then increment it by 1. When the loop is finished, print "The loop is finished".

In [1]:

x = 0

while x < 5:

print(x)

x = x + 1

printLoop is done, x =',x)

0

1

2

3

4

Loop is done, x 5

2. Set a variable my_string equal to 'hello', and a variable x equal to 0. Create a while loop that runs as long as my_string is equal to 'hello'. Inside the loop, increment x by 1 and print the value of my_string. When x is equal to 5, change the value of 'hello' to 'goodbye'. When the loop has completed, print the value of my_string.

In [5]:

my_string = 'hell' x = 0

while 4 < 5:

print(my_string)

x x + 1 if x == 5: break print (' goodbye) print(x) hello hello hello hello hello goodbye

5

3. Set a variable x equal to 0. Create a while loop with a test condition of True. Inside the loop, increment x by

1 and print its value. When x is greater than 6, break out of the loop. Then print the value ofx.

In [6]:

x 0

while True:

print(x)

x = x + 1

if x > 6:

break

print C Loop is done. x=,, x)

0

1

2

3

4

5

6

Loop is done. x= 7

4. Set a variable x equal to 0. Create a while loop using this variable that runs exactly 10 times. To test it, print the value of x each time the loop executes. When it has finished, print "The loop has run x times'* - but don't print the letter x, print the value of x.

In ⑻:

x = 0

while True:

print(x)

x = x + 1

if x > 10:

break

print (' Loop has nm', x - 1,' times') 0

1

2

3

4

5

6

7

8

9

10

Loop has run 10 times

5. Create a variable my_condition and set it equal to True. Create a while loop that runs as long as my_condition is equal to True. Inside the loop, print the value of my_condition, and then set my_condition to False. When the loop is finished, print the value of my_condition.

In []:

x 0

while True:

print(x)

x = x + 1

if x > 3:

break

print C Loop is done. x=,, x)

6. Create a for loop the prints the values 1, 2, and 3, one each time the loop executes.

In [11]:

for i in [1, 2, 3]:

print (i)

1

2

3

7. Create a for loop the prints the strings 'cat', 'dog', and 'fish', one value each time the loop executes

In [12]:

for i in ['cat', 'dog', 'fish']:

print (i)

cat

dog

fish

8. Create a for loop that loops through each of the values [0,1, 3, 5, 6, 8, 12, 15, 20] and prints each value.

In [13]:

for i in[0, 1,3, 5,6, 8, 12,15, 20]: print (i)

0

1

3

5

6

8

12

15

20

9. Repeat exercise 8, but only print the values that are less than 10.

In [16]:

for i in [0, 1, 3, 5, 6, 8,12,15, 20]: if i < 10 :

print (i) 0

1

3

5

6

8

10. Repeat exercise 8, but only print the values that are even.

In [62]:

for i in[0, 1,3,5,6,8,12,15,20]: if (i % 2)二二 0:

print (i) 0

6

8

12

20

11. Repeat exercise 8, but only print the first even value and then break out of the loop.

In [27]: my_even_number = 6

for i in [0, 1, 3, 5, 6, 8,12,15, 20]:

print (i)

if i == my_even_number:

print (' my_even_number :', my_even_number) break

0

1

3

5

6

my_even_number : 6

12. Use a for loop to add up the even numbers from 2 to 20, inclusive.

In [28]:

total = 0

for number in [0,1, 3, 5, 6, 8, 12, 15, 20]: total total + number

print(total)

70

13. Write nested for loops. In the outer loop, go through each odd number 1,3,5 and 7. In the inner loop, go through the even numbers 2,4,6,8. Inside the inner loop, print the sum of the current odd and even numbers. Hint: your first sum should be 1 +2 = 3, and your second sum should be 1 +4 = 5.

In [30]:

for i in [1, 3, 5, 7]: for j in [2, 4, 6, 8]: print(i + j)

3

5

7

9

5

7

9

11

7

9

11

13

9

11

13

15

14. Write nested for loops. The outer loop should use the range function and a variable i to go through the numbers from 1 to 5. The inner loop should also use the range function with a variable j to go from 1 to 5. Inside the inner loop, print the values of i and j.


In [68]:

for i in [0, 1, 2, 3, 4, 5]: for j in [0, 1, 2, 3, 4, 5]: print (i and j) 0

0

0

0

0

0

0

1

2

3

4

5

0

1

2

3

4

5

0

1

2

3

4

5

0

1

2

3

4

5

0

1

2

3

4

5

15. Repeat exercise 14, but only use odd numbers for both i and j.

In [73]:

for i in [0, 1, 2, 3, 4, 5]: for j in [0, 1, 2, 3, 4, 5]:

if i % 1 二二 0

print (i and j)

Input In [73]

if i % 1 = 0

SyntaxError: invalid syntax

16. Create a list with elements 'a', 'b', 'c' and'd'. Print the element 'b'.

In [40]:

1 La, b , c , d J

print( i [1])

b

17. Create a list with elements 2, 4, 6 and 8. Write a for loop that prints the square of each element in the list

In [44]:

1 = list ([2, 4, 6, 8])

print (i)

2 4, 6, 8]

18. Create an empty list called myjist. Create a for loop that runs from 1 to 10 with an index variable i. Append each of value of i to the list. When the loop has ended, print myjist.

In [50]:

for my_list in [1, 2, 3, 4, 5, 6, 7, 8, 9,10]: print (my_list)

print (J my_list,)

1

2

3

4

5

6

7

8

9

10

my_list

19. Create a list with elements 1 to 5. Create a for loop that executes for each even number from 2 to 10. Inside the loop, test if the loop variable is in the list If it is, print "x is in the list" except do not print the letter x, print the current value of the loop variable.

In []:

listl =[1,2, 3, 4, 5]

list2 =[2,4,6,8,10]:

20. Create an empty list called my_table. Then create a for loop that goes from 0 to 9 with an index variable i. Inside the for loop, create an empty list called my_row and append it to mytable. Also inside the loop, create another for loop that goes from 0 to 9 with an index variable j. Append each value of j to my_row. When both loops are finished, print my_table.

In [61]:

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] j = [o.l, 2, 3, 4, 5, 6, 7, 8, 9] my_row = 口

my_list. append([]) my_row. append([j]) print(my_list) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,[]]

21. Using the my_table list from the previous exercise, print the table row by row using a for loop so that the result looks like a spreadsheet table, with 10 identical rows and each column having the same number in all 10 rows. Hint: if you add the option end=" to the print function, it will print but not move to the next line.

So:

print(a, end=")

printCb)

prints:

a b

In []:

22. For the list [1, 2, 5, 3, 6, 8, 10, 9, 13, 16, 17, 14, 18, 19, 21, 20, 12], print the minimum value, the maximum value, the length of the list, the first two elements of the list, and the fourth through seventh elements. Then sort the list and print the sorted list. Finally, print the sum of all the values in the list

In [57]:

listl 二[1, 2, 5, 3, 6, 8, 10, 9, 13, 16, 17, 14, 18, 19, 21, 20, 12]

print (min(listl))

print(max(listl))

print(sum(listl))

print(listl[0:1])

1

21

194

[2]

In []: