python means

Python loops and lists

We should now be able to create some programs that are significantly more interesting thanks to Python Loops. If you’ve been following along, you should be aware that you can now use if-statements and boolean expressions to combine all the other things you’ve learned to have your programs do intelligent actions.

Programs must, however, perform repetitive tasks quickly. In this exercise, we’ll generate and output various lists using a for loop. You’ll begin to identify them as you complete the practice. I won’t reveal it to you now. You must resolve the issue.

You need a means to save the output of loops somewhere before you can use a for-Python loop. A list is the ideal tool for accomplishing this. A list is precisely what it sounds like—a collection of items arranged according to priority. The only difficult part is learning a new syntax. To start, this is how to create a list:


hairs = ['brown', 'blond', 'red']
eyes = ['brown', 'blue', 'green']
weights = [1, 2, 3, 4]

The [(left bracket, which “opens” the list, is what you use to begin the list. Then, just as with function parameters, add each item you wish to the list, separating each one with a comma. The list is concluded by adding a] (right bracket) at the end. Then, Python assigns the variable with the contents of this list.

Python Loops

We now will build some lists using some loops and print them out:

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

# this first kind of for- loop goes through a list
for number in the_count:
print "This is count %d" % number

# same as above
for fruit in fruits:
print "A fruit of type: %s" % fruit

# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
print "I got %r" % i

# we can also build lists, first start with an empty one
elements = []

# then use the range function to do 0 to 5 counts
for i in range(0, 6):
print "Adding %d to the list." % i
# append is a function that lists understand
elements.append(i)

# now we can print them out too
for i in elements:
print "Element was: %d" % i

While Loop

Here’s a new loop that will completely blow your mind: the while-loop. As long as a boolean expression is True, a while-loop will continue to run the code block underneath it. Have you been following the terminology, I take it? That if we write a line and end it with a : (colon), then that tells Python to start a new block of code? Then we indent and that’s the new code. This is all about structuring your programs so that Python knows what you mean. If you still don’t understand that concept, go back and continue working with if statements, functions, and the for loop until you do.

As we did when we burned boolean expressions into your brain, we’ll later have some activities that will educate your brain to read these structures. Back to while-loops. Simply put, they conduct a test similar to an if-statement, but instead of running the code block only once, they loop back up to the while at the top. Until the phrase is false, it continues in this manner.

The issue with while-loops is that they occasionally do not stop. This is excellent if your goal is to simply keep looping indefinitely. If not, you almost always want your loops to come to a conclusion.

To avoid these problems, there’s some rules to follow:

  1. Make sure that you use while- loops sparingly. Usually a for- loop is better.
  2. Review your while statements and make sure that the thing you are testing will become False at some point.
  3. When in doubt, print out your test variable at the top and bottom of the while- loop to see what it’s doing.

In this exercise, you will learn the while- loop by doing the above three things:

i = 0
numbers = []

while i < 6:
print "At the top i is %d" % i
numbers.append(i)

i = i + 1
print "Numbers now: ", numbers
print "At the bottom i is %d" % i

print "The numbers: "

for num in numbers:
print num

 

Online Training Tutorials

  • Python Tutorial – Quick Guide: Learn Python Programming OnlinePython Tutorial – Quick Guide: Learn Python Programming OnlineThis document includes a basic Python tutorial and information on learning Python, a programming language that first appeared in the late 1980s. In contrast to other widely used languages […]
  • Python DictionaryPython DictionaryWe also have a Python dictionary that you may use since once you master it, a whole universe of ultra-cool will be at your disposal. The dictionary is the greatest storage device ever. In […]
  • System Copy in SAP Business IntelligenceWhat is SAP-BI Data Archiving Process?SAP-BI Data Archiving Process: Archiving is used to store your data at a remote location to improve the performance in BI. Archiving is a process of moving the data from the sap database […]
  • sapSAP Indtroduction – SAP Largest Enterprise Software Firm in the WorldSAP AG (ISIN: DE0007164600, FWB: SAP, NYSE: SAP) is a German global software corporation that provides enterprise software applications and support to businesses of all sizes globally. […]
  • sap sd training tutorialsSAP SD (Sales & Distribution) Tutorial for BeginnersSAP SD Tutorial (Sales and Distribution) by the real time functional consultant and experts . The SAP SD module handles all the processes starting from an Order to delivery. The main […]
  • request for quotation (RFQ)What is request for quotation (RFQ) in sap?The request for quotation (RFQ) is an invitation to a vendor to indicate his terms and conditions (in particular, his price) for the supply of a material or the provision of a service by […]
  • Data ArchivingIntroduction to Data ArchivingSAP Data Archiving : As the usage of SAP database increases it’ll results in huge amounts of enterprise data, which is stored in SAP R/3. The new updated data is entered into the system […]
  • sap process flowWhat is mean by SAP Process Flow?SAP runs eight different types of processes on Application Server. How many actual processes would be run for each type is dictated by SAP instance profiles (which we will talk about […]