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).
  1. 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

import random
#first way to create LIST
#declare an empty list
Num=[]
for i in range(0,10): #loop to add numbers into list
#generate a number between 1 to 100 with step value 1 and append
#it to Num[] list
Num.append(random.randrange(1,100,1))
#print the elements of Num
print(Num)

#second way to create List
#declare and initialize a list by fixed numbers
Numbers = [2,34,54,67,4,21,99,87,45,55]
print(Numbers) #print the list

#third way to create a list
Number=[]
for i in range(0,10): #loop to add numbers into list
Number.append(random.choice(Numbers)) #fill the list by randomly choosen from Numbers List
print(Number)

#print the numbers from 1 to 10 using while loop
print("Print the numbers using WHILE loop : ",end=' ')
i=1
while (i<=10):
print(i,end=' ') #print the numbers
i=i+1#increase the value of i
print("")

#print the numbers from 1 to 10 using FOR loop
print("Print the numbers using FOR loop : ",end=' ')
for i in range(1,11):
print(i,end=' ')

#print the elements of first LIST by using FOR loop
print("")
print("Print the elements of list by using FOR loop : ",end=' ')
for i in range(0,len(Num)-1):
print(Num[i],end='& ')
print(Num[i+1])

#print the elements of second LIST by using FOR loop
print("")
print("Print the elements of list by using FOR loop : ",end=' ')
for i in range(0,len(Numbers)-1):
print(Numbers[i],end='& ')
print(Numbers[i+1])

#print the elements of third LIST by using FOR loop
print("")
print("Print the elements of list by using FOR loop : ",end=' ')
for i in range(0,len(Number)-1):
print(Number[i],end='& ')
print(Number[i+1])
  

  

OUTPUT


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.
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT