1. Write the outputs of the following loops: a for count in range(5): print( cou
ID: 3665057 • Letter: 1
Question
1. Write the outputs of the following loops:
a for count in range(5): print( count +1, end= " ")
b for count in xrange(1, 4): print (count, end = " " )
c for count in range(1,6,2): print (count, end = " " )
d for count in range(6,1,-1): print (count, ends= " ")
2. Assume that the variable amount refers to 24.325. Write the outputs of
the following statements:
a print “Your salary is $%0.2f” % amount
b print “The area is %0.1f” % amount
c print “%7f” % amount
3. Assume that x is 3 and y is 5. Write the values of the following
expressions:
a x == y
b x > y - 3
c x <= y - 2
d x == y or x > 2
e x != 6 and y > 10
f x > 0 and x < 100
4. Assume that the variable data refers to the string “myprogram.exe”.
Write the values of the following expressions:
a data[2]
b data[-1]
c len(data)
d data[0:8]
e “gram” in data and “pro” in data
5. Write the encrypted text of each of the following words using a Caesar
cipher with a distance value of 3:
a python
b hacker
c wow
6. Translate each of the following numbers to decimal numbers:
a (11001)2
b (100000)2
c (11111)2
7. 1 Assume that the variable data refers to the list [5, 3, 7]. Write the
values of the following expressions:
a data[2]
b data[-1]
c len(data)
d data[0:2]
e 0 in data
f data + [2, 10, 5]
g tuple(data)
8. What roles do the parameters and the return statement play in a function definition?
9. 1 Give three examples of real-world objects that behave like a dictionary
Explanation / Answer
And here is the output:
So what does the program do? First it sees the line a = 0 and sets the variable a to zero. Then it sees while a < 5: and so the computer checks to see if a < 5. The first time the computer sees this statement, a is zero, and zero is less than 5. In other words, while a is less than five, the computer will run the indented statements.
Here is another example of the use of while:
Notice how print 'Total Sum =',s is only run at the end. The while statement only affects the lines that are tabbed in (a.k.a. indented). The != means does not equal sowhile a != 0 : means until a is zero run the tabbed in statements that are afterwards.
Now that we have while loops, it is possible to have programs that run forever. An easy way to do this is to write a program like this:
This program will output Help, I'm stuck in a loop. until the heat death of the universe or you stop it. The way to stop it is to hit the Control (or Ctrl) button and `c' (the letter) at the same time. This will kill the program. (Note: sometimes you will have to hit enter after the Control C.)
Examples[edit]
Fibonacci.py
Output:
Password.py
Sample run:
For Loops[edit]
The next type of loop in Python is the for loop. Unlike in most languages, for requires some __iterable__ object like a Set or List to work.
The output:
The output looks very familiar, but the program code looks different. The first line uses the range function. The range function uses two arguments like thisrange(start,finish). start is the first number that is produced. finish is one larger than the last number. Note that this program could have been done in a shorter way:
Here are some examples to show what happens with the range function:
Another way to use the range() function in a for loop is to supply only one argument:
The above code acts exactly the same as:
with 0 implied as the starting point. The output is
The code would cycle through the for loop 10 times as expected, but starting with 0 instead of 1.
The next line for count in onetoten: uses the for control structure. A for control structure looks like for variable in list:. list is gone through starting with the first element of the list and going to the last. As for goes through each element in a list it puts each into variable. That allows variable to be used in each successive time the for loop is run through. Here is another example to demonstrate:
The output is:
Notice how the for loop goes through and sets item to each element in the list. (Notice how if you don't want print to go to the next line add a comma at the end of the statement (i.e. if you want to print something else on that line). ) So, what is for good for? The first use is to go through all the elements of a list and do something with each of them. Here a quick way to add up all the elements:
with the output simply being:
Or you could write a program to find out if there are any duplicates in a list like this program does:
and for good measure:
How does it work? Here is a special debugging version:
with the output being:
Note: The reason there are so many print statements is because print statements can show the value of each variable at different times, and help debug the program. First the program starts with a old list. Next the program sorts the list. This is so that any duplicates get put next to each other. The program then initializes a prev(ious) variable. Next the first element of the list is deleted so that the first item is not incorrectly thought to be a duplicate. Next a for loop is gone into. Each item of the list is checked to see if it is the same as the previous. If it is a duplicate was found. The value of prev is then changed so that the next time the for loop is run through prev is the previous item to the current. Sure enough, the 7 is found to be a duplicate.
The other way to use for loops is to do something a certain number of times. Here is some code to print out the first 9 numbers of the Fibonacci series:
with the surprising output:
Everything that can be done with for loops can also be done with while loops but for loops give an easy way to go through all the elements in a list or to do something a certain number of times.
range Versus xrange[edit]
Above, you were introduced to the range function, which returns a list of all the integers in a specified range. Supposing you were to write an expression like range(0, 1000000): that would construct a list consisting of a million integers! That can take up a lot of memory.
Often, you do indeed need to process all the numbers over a very wide range. But you might only need to do so one at a time; as each number is processed, it can be discarded from memory before the next one is obtained.
To do this, you can use the xrange function instead of range. For example, the following simple loop
will print the million integers from 0 to 999999, but it will get them one at a time from the xrange call, instead of getting them all at once as a single list and going through that.
This is an example of an iterator, which yields values one at a time as they are needed, rather than all at once. As you learn more about Python, you will see a lot more examples of iterators in use,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.