Question

In: Computer Science

In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times,...

  1. In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times, each time using a different syntax to define the list.
  1. Write a while loop that prints the numbers from 1 to 10.
  1. Convert your previous loop into a for loop.
  1. Rewrite your for loop to only have one number in the range.
  1. Write a for loop to print out each item of your list of 10 numbers, all on the same line, separated by & (ampersand).

In the previous question, you may notice that your line of numbers ends with the &. See if you can change this to only print the last number instead of the &. Hint: You’ll need to be clever about printing the last number from within the loop to make this work.

Solutions

Expert Solution

CODE:

#method 1 to declare a list
numbers = [4,5,6,7,8,9,0,4,6,1]
#method 2 to declare a list which contains 10 numbers each number being 10
numbers = [10]*10
#method 3 to append 10 numbers in the list
numbers = []
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)
numbers.append(10)

#counting 1 to 10 using while loop
j = 0
while(j<10):
#loop runs 10 times
j += 1
#prints j on each loop
print(j)
print()
#counting 1 to 10 using for loop
for i in range(10):
#printing the numbers
print((i+1))
print()
#printing the numbers in the list
for j,i in enumerate(numbers):
#if the current number is not the last element in the list
#then it is printed with an & at the end
if(j!= len(numbers)-1):
print(i,end = "&")
else:
#otherwise just the number and then a new line
print(i)

____________________________________________

CODE IMAGES:

___________________________________________________

OUTPUT:

______________________________________________

Feel free to ask any questions in the comments section

Thank You!


Related Solutions

In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times,...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times, each time using a different syntax to define the list. Write a while loop that prints the numbers from 1 to 10. Convert your previous loop into a for loop. Rewrite your for loop to only have one number in the range. Write a for loop to print out each item of your list of 10 numbers, all on the same line, separated by...
Short Python 3 questions a. Create a dictionary that maps the first n counting numbers to...
Short Python 3 questions a. Create a dictionary that maps the first n counting numbers to their squares. Assign the dictionary to the variable squares b. Given the list value_list, assign the number of nonduplicate values to the variable distinct_values
for python 3 a. Ask the user to enter 10 numbers. Each number is stored in...
for python 3 a. Ask the user to enter 10 numbers. Each number is stored in a list called myList. Compute and print out the sum and average of the items in the list. Print the numbers that are divisible by 2 from myList. b. Instead of using a list to store the numbers, create a loop to accept the numbers from the user, and find the average of all the numbers. Hint: use an accumulator to store the numbers...
Write a Python program that has a list of 5 numbers [2, 3, 4, 5, 6)....
Write a Python program that has a list of 5 numbers [2, 3, 4, 5, 6). Print the first 3 elements from the list using slice expression. a. Extend this program in a manner that the elements in the list are changed to (6, 9, 12, 15, 18) that means each element is times 3 of the previous value. b. Extend your program to display the min and max value in the list.
create a python function (only) that generates 10 random numbers within a specific range. Every time...
create a python function (only) that generates 10 random numbers within a specific range. Every time a number is generated, it is appended to a list (you need to initialize a list). Then the function counts how many times a number is repeated in the created list (you can choose any number to count). The function returns the list, the number you have chosen and its count. --define the function here ---
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
python coding Suppose a list of positive numbers is given like the following list (remember this...
python coding Suppose a list of positive numbers is given like the following list (remember this is only an example and the list could be any list of positive numbers) exampleList: 15 19 10 11 8 7 3 3 1 We would like to know the “prime visibility” of each index of the list. The “prime visibility” of a given index shows how many numbers in the list with indexes lower than the given index are prime. For instance, in...
In Python, Given a list of numbers, return a list where all adjacent == elements have...
In Python, Given a list of numbers, return a list where all adjacent == elements have been reduced to a single element, so [1,2,2,3,3,2,2,4] returns [1,2,3,2,4]. You may create a new list or modify the passed in list (set function does not work in this case).
Write a Python program to add, multiply and divide any two numbers.
Write a Python program to add, multiply and divide any two numbers.
in python please Q1) Create a Singly link list and write Python Programs for the following...
in python please Q1) Create a Singly link list and write Python Programs for the following tasks: a. Delete the first node/item from the beginning of the link list b. Insert a node/item at the end of the link list c. Delete a node/item from a specific position in the link list Q2) Create a Singly link list and write a Python Program for the following tasks: a. Search a specific item in the linked list and return true if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT