Ernesto's Notes
  • Home
  • Glossary
  • ⛓️Blockchain
    • Solidity Compiler
    • Foundry
    • Gas Optimization
    • Resources
  • 💻Programming
    • Computational Complexity
    • JavaScript
      • Syntax
    • Python
      • Syntax
      • PIP
      • Poetry
    • ZSH
    • Git
  • 🚀Startups
    • Talking to Users
Powered by GitBook
On this page
  • For-Loops
  • Iterate Through a List in Reverse Order
  • Iterate Through a List
  • While-Loops
  • Iterate Through a Sequence of Floats
  • Range
  • Parameters
  • Minimal Example
  • Comprehensive Examples
  • Enumerate
  • Parameters
  • Minimal Example
  • Comprehensive Example

Was this helpful?

  1. Programming
  2. Python

Syntax

Overview of Python Syntax

For-Loops

Iterate Through a List in Reverse Order

We can use the range(start, stop, step) function to start the iteration from the end of the list. In the example below, we iterate in reverse order to reverse the order of integers in a list.

myList = [1, 2, 3, 0, 0, 0]
reversedList = []
lastIndex = len(myList) - 1

for i in range(lastIndex, -1, -1):
    reversedList.append(myList[i])
    
print("reversedList = ", reversedList)

Iterate Through a List

myList = [1, 2, 3, 0, 0, 0]

for i in myList:
    print(i)

While-Loops

Iterate Through a Sequence of Floats

start = 0.0
stop = 1.0
step = 0.1
float_range = []

while start < stop:
    float_range.append(start)
    start += step

print(float_range)

Range

The range() function in Python creates an immutable sequence of integers, which can very helpful for iterating through loops. Note that the stop is exclusive, meaning it will not be included in the sequence.

Parameters

Parameter
Description

start

Optional - Integer to specify the start position (defaults to 0).

stop

Required - Integer to specify the stop position.

step

Optional - Integer to specify the sequence increment (defaults to 1).

In the examples below, a for-loop is used with the range() function to iterate through a sequence of integers and print each item in the sequence.

Minimal Example

Here, range(4) creates a sequence of integers from 0 to 4, in increments of 1, but excluding 4.

for i in range(4):
    print(i)

# Result:
# 0
# 1
# 2
# 3

Comprehensive Examples

Here, range(3, 6) creates a sequence of integers from 3 to 6, in increments of 1, but excluding 6.

for i in range(3, 6):
    print(i)

# Result:
# 3
# 4
# 5

Here, range(4, 20, 4) creates a sequence of integers from 4 to 20, in increments of 4, but excluding 20.

for i in range(4, 20, 4):
    print(i)

# Result:
# 4
# 8
# 12
# 16

Enumerate

Parameters

Parameter
Description

iterable

Required - The iterable to be enumerated (e.g., list, strings, dict, ...).

start

Optional - Integer to specify the start position (defaults to 0).

In the examples below, a for-loop is used with the enumerate() function to iterate through a sequence of values and print each index and item from the sequence.

Minimal Example

Here, enumerate(myList) adds a counter to each item in the list, starting at 0.

myList = ["apple", "banana", "cherry"]

for index, value in enumerate(myList):
    print(index, value)
    
# Result:
# 0 apple
# 1 banana
# 2 cherry

Comprehensive Example

Here, enumerate(myList, 2) adds a counter to each item in the list, starting at 2.

myList = ["apple", "banana", "cherry"]

for index, value in enumerate(myList, 2):
    print(index, value)

# Result:
# 2 apple
# 3 banana
# 4 cherry
PreviousPythonNextPIP

Last updated 1 year ago

Was this helpful?

The enumerate() function in Python adds a counter to an and returns it in a form of an enumerate object. This object can then be used directly in for loops or be converted into a list of tuples, where each tuple contains a pair of the index and the value from the iterable.

💻
iterable