Working With Loops In Python
Sometimes we have to repeat ourselves before we are heard. Then again there are times where we have to perform a certain action the same way several times before we can carry on with what we want to do.
Repetition is the keyword here and for humans that is something we generally try to avoid. Yet our digital friends love repetition. They never get tired and they never get bored of doing the same thing over and over again countless times.
So it’s little wonder then that all modern programming languages give us various ways in which we can perform a certain action as many times as we need.
In python we have the for statement which gives us the power to loop over large collections of data very quickly and efficiently.
If you’re trying to imagine a use for loops then consider this:
Your application requires that the user enters a set of four numbers. One of those numbers must be the number 20 before it will continue to execute.
Because it is only four numbers you could use a bunch of if statements to check for the correct number, but that would not only be time consuming but rather inefficient.
The best way to go would be to use a loop, even for such a simple case. Check out the code below and then I will explain what’s going on:
myNumbers = [1,2,20,32]
for number in myNumbers:
print number
On line one, we create a variable called myNumbers which is actually a list, or in other languages it would be called an array.
The second line is where we start the for loop. You will notice that we create a variable called number which is accessible only within the loop and then we use the in keyword followed by the name of our list variable.
In plain English the line would read as: for each number in myNumbers.
Then on the third line we print out the loop variable, which will contain the value of the current index being accessed in our myNumbers variable.
So, in simple terms the code will access each number in our list and print it back to us.
Now I did mention that our imaginary application would also check for the correct number within a list provided by the user, and so here is the extended version of our loop which contains an if statement which checks for the number 20.
myNumber = [1,2,4,20]
for number in myNumbers:
if number == 20:
print “Found the correct number!”
Don’t worry if you haven’t got as far as learning conditional statements, all you need to keep in mind is that the code above will iterate over each number in our list variable. If it finds the number 20 then it will print the congratulatory message to the screen.
If you’re feeling adventurous you can extend the above example to include an else statement which will perform an action if the wrong number is encountered.
other blog entries
Course Directory [training on all levels]
- .NET Classes
- Agile/Scrum Classes
- Ajax Classes
- Android and iPhone Programming Classes
- Blaze Advisor Classes
- C Programming Classes
- C# Programming Classes
- C++ Programming Classes
- Cisco Classes
- Cloud Classes
- CompTIA Classes
- Crystal Reports Classes
- Design Patterns Classes
- DevOps Classes
- Foundations of Web Design & Web Authoring Classes
- Git, Jira, Wicket, Gradle, Tableau Classes
- IBM Classes
- Java Programming Classes
- JBoss Administration Classes
- JUnit, TDD, CPTC, Web Penetration Classes
- Linux Unix Classes
- Machine Learning Classes
- Microsoft Classes
- Microsoft Development Classes
- Microsoft SQL Server Classes
- Microsoft Team Foundation Server Classes
- Microsoft Windows Server Classes
- Oracle, MySQL, Cassandra, Hadoop Database Classes
- Perl Programming Classes
- Python Programming Classes
- Ruby Programming Classes
- Security Classes
- SharePoint Classes
- SOA Classes
- Tcl, Awk, Bash, Shell Classes
- UML Classes
- VMWare Classes
- Web Development Classes
- Web Services Classes
- Weblogic Administration Classes
- XML Classes
- RED HAT ENTERPRISE LINUX AUTOMATION WITH ANSIBLE
2 December, 2024 - 5 December, 2024 - Introduction to Spring 5 (2022)
16 December, 2024 - 18 December, 2024 - Ruby Programming
2 December, 2024 - 4 December, 2024 - VMware vSphere 8.0 Boot Camp
9 December, 2024 - 13 December, 2024 - Ruby on Rails
5 December, 2024 - 6 December, 2024 - See our complete public course listing
did you know? HSG is one of the foremost training companies in the United States
Our courses focus on two areas: the most current and critical object-oriented and component based tools, technologies and languages; and the fundamentals of effective development methodology. Our programs are designed to deliver technology essentials while improving development staff productivity.
An experienced trainer and faculty member will identify the client's individual training requirements, then adapt and tailor the course appropriately. Our custom training solutions reduce time, risk and cost while keeping development teams motivated. The Hartmann Software Group's faculty consists of veteran software engineers, some of whom currently teach at several Colorado Universities. Our faculty's wealth of knowledge combined with their continued real world consulting experience enables us to produce more effective training programs to ensure our clients receive the highest quality and most relevant instruction available. Instruction is available at client locations or at various training facilities located in the metropolitan Denver area.
Upcoming Classes
- RED HAT ENTERPRISE LINUX AUTOMATION WITH ANSIBLE
2 December, 2024 - 5 December, 2024 - Introduction to Spring 5 (2022)
16 December, 2024 - 18 December, 2024 - Ruby Programming
2 December, 2024 - 4 December, 2024 - VMware vSphere 8.0 Boot Camp
9 December, 2024 - 13 December, 2024 - Ruby on Rails
5 December, 2024 - 6 December, 2024 - See our complete public course listing
consulting services we do what we know ... write software
The coaching program integrates our course instruction with hands on software development practices. By employing XP (Extreme Programming) techniques, we teach students as follows:
Configure and integrate the needed development tools
MOntitor each students progress and offer feedback, perspective and alternatives when needed.
Establish an Action plan to yield a set of deliverables in order to guarantee productive learning.
Establish an Commit to a deliverable time line.
Hold each student accountable to a standard that is comparable to that of an engineer/project manager with at least one year's experience in the field.
These coaching cycles typically last 2-4 weeks in duration.
Business Rule isolation and integration for large scale systems using Blaze Advisor
Develop Java, .NET, Perl, Python, TCL and C++ related technologies for Web, Telephony, Transactional i.e. financial and a variety of other considerations.
Windows and Unix/Linux System Administration.
Application Server Administration, in particular, Weblogic, Oracle and JBoss.
Desperate application communication by way of Web Services (SOAP & Restful), RMI, EJBs, Sockets, HTTP, FTP and a number of other protocols.
Graphics Rich application development work i.e. fat clients and/or Web Clients to include graphic design
Performance improvement through code rewrites, code interpreter enhancements, inline and native code compilations and system alterations.
Mentoring of IT and Business Teams for quick and guaranteed expertise transfer.
Architect both small and large software development systems to include: Data Dictionaries, UML Diagrams, Software & Systems Selections and more