Python For

Posted onby admin
  1. Python For Loop
  2. Python For Loop
  3. Python If

MicroPython employs many advanced coding techniques, and lots of tricks to maintain a compact size while still having a full set of features.

Some of the more notable items are:

  • highly configurable due to many compile-time configuration options
  • support for many architectures (x86, x86-64, ARM, ARM Thumb, Xtensa)
  • extensive test suite with over 590 tests, and more than 18,500 individual testcases
  • code coverage at 98.4% for the core and at 96.3% for the core plus extended modules
  • fast start-up time from boot to loading of first script (150 microseconds to get to boot.py, on PYBv1.1 running at 168MHz)
  • a simple, fast and robust mark-sweep garbage collector for heap memory
  • a MemoryError exception is raised if the heap is exhausted
  • a RuntimeError exception is raised if the stack limit is reached
  • support for running Python code on a hard interrupt with minimal latency
  • errors have a backtrace and report the line number of the source code
  • constant folding in the parser/compiler
  • pointer tagging to fit small integers, strings and objects in a machine word
  • transparent transition from small integers to big integers
  • support for 64-bit NaN boxing object model
  • support for 30-bit stuffed floats, which don't require heap memory
  • a cross-compiler and frozen bytecode, to have pre-compiled scripts that don't take any RAM (except for any dynamic objects they create)
  • multithreading via the '_thread' module, with an optional global-interpreter-lock (still work in progress, only available on selected ports)
  • a native emitter that targets machine code directly rather than the bytecode virtual machine
  • inline assembler (currently Thumb and Xtensa instruction sets only)

Python For Loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

Python for Everybody. This web site is building a set of free materials, lectures, book and assignments to help students learn how to program in Python. You can take this course and receive a certificate at: Coursera: Python for Everybody Specialization; edX: Python for Everybody; FutureLearn: Programming for Everybody (Getting Started with Python). Welcome to Google's Python Class - this is a free class for people with a little bit of programming experience who want to learn Python. The class includes written materials, lecture videos, and lots of code exercises to practice Python coding. Using Python on a Macintosh¶ Author. Bob Savage Python on a Macintosh running Mac OS X is in principle very similar to Python on any other Unix platform, but there are a number of additional features such as the IDE and the Package Manager that are worth pointing out. Python For Loop can be used to iterate a set of statements once for each item of a sequence or collection. The sequence or collection could be Range, List, Tuple, Dictionary, Set or a String. In this tutorial, we will learn how to implement for loop for each of the above said collections. Join 575,000 other learners and get started learning Python for data science today! Welcome to the LearnPython.org interactive Python tutorial. Whether you are an experienced programmer or not, this website is intended for everyone who wishes to learn the Python programming language.

This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

Example

Print each fruit in a fruit list:

fruits = ['apple', 'banana', 'cherry']
for x in fruits:
print(x)
Try it Yourself »

The for loop does not require an indexing variable to set beforehand.

Looping Through a String

Even strings are iterable objects, they contain a sequence of characters:

Example

Loop through the letters in the word 'banana':

Try it Yourself »

The break Statement

With the break statement we can stop the loop before it has looped through all the items:

Example

Exit the loop when x is 'banana':

fruits = ['apple', 'banana', 'cherry']
for x in fruits:
print(x)
if x 'banana':
break
Try it Yourself »

Example

Analysis

Exit the loop when x is 'banana', but this time the break comes before the print:

fruits = ['apple', 'banana', 'cherry']
for x in fruits:
if x 'banana':
break
print(x)
Try it Yourself »

The continue Statement

With the continue statement we can stop the current iteration of the loop, and continue with the next:

Example

Do not print banana:

fruits = ['apple', 'banana', 'cherry']
for x in fruits:
if x 'banana':
continue
print(x)
Try it Yourself »

The range() Function

Python For Loop

To loop through a set of code a specified number of times, we can use the range() function,

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

Example

Using the range() function:

Try it Yourself »

Note that range(6) is not the values of 0 to 6, but the values 0 to 5.

The range() function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6):

Example

Using the start parameter:

Try it Yourself »

The range() function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter: range(2, 30, 3):

Example

Increment the sequence with 3 (default is 1):

Try it Yourself »

Else in For Loop

The else keyword in a for loop specifies a block of code to be executed when the loop is finished:

Python For

Example

Print all numbers from 0 to 5, and print a message when the loop has ended:

for x in range(6):
print(x)
else:
print('Finally finished!')
Try it Yourself »

Note: The else block will NOT be executed if the loop is stopped by a break statement.

Example

Break the loop when x is 3, and see what happens with the else block:

for x in range(6):
if x 3: break
print(x)
else:
print('Finally finished!')
Try it Yourself »

Nested Loops

A nested loop is a loop inside a loop.

The 'inner loop' will be executed one time for each iteration of the 'outer loop':

Python For Loop

Example

Print each adjective for every fruit:

adj = ['red', 'big', 'tasty']
fruits = ['apple', 'banana', 'cherry']
for x in adj:
for y in fruits:
print(x, y)
Try it Yourself »

The pass Statement

for loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass statement to avoid getting an error.

Example

Try it Yourself »

Python If