Skip to content

Hujingwen2023221402008Forloops #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions docs/for-loop/HuJingwen2023221402008 .md
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Beginner's Tutorial on For Loops

For loops are a fundamental concept in programming that allows you to execute a block of code a certain number of times. In this tutorial, we'll be using Python as our preferred programming language to demonstrate how for loops work.

## What is a For Loop?

A for loop is used for iterating over a sequence (such as a list, tuple, dictionary, set, or string) or other iterable objects. For loops provide a way to execute a block of code repeatedly, making it easier to perform repetitive tasks.

## Basic Syntax

The basic syntax of a for loop in Python is as follows:

python

for element in iterable:

# Code block to be executed for each element


Here, ` element` is the variable that takes the value of each item in the `iterable` one by one, and the `# Code block to be executed for each element` is the block of code that will be executed for each iteration of the loop.

## Example: Iterating Over a List

Let's start with a simple example. Suppose you have a list of fruits, and you want to print each fruit name.

Python

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

for fruit in fruits:

print(fruit)

Output:

apple
banana
cherry


## Supporting Concepts

### 1. Lists
A list is a collection of items that are ordered and changeable.

Lists are written with square brackets [].

Python

my_list = [1, 2, 3, 4, 5]

### 2. Ranges
The range() function generates a sequence of numbers, which is often used in for loops to iterate a specific number of times.

Python

for i in range(5):

print(i)

Output:

0
1
2
3
4

### 3. Nested Loops
You can also nest for loops, which means placing a loop inside another loop.

Python
for i in range(3):

for j in range(2):

print(f"i: {i}, j: {j}")

Output:

i: 0, j: 0

i: 0, j: 1

i: 1, j: 0

i: 1, j: 1

i: 2, j: 0

i: 2, j: 1

### 4. Loop Control Statements
break: Exits the loop prematurely.
continue: Skips the rest of the code inside the loop for the current iteration only.

Python

for i in range(5):

if i == 3:

break

print(i)

Output:

0

1

2

Python

for i in range(5):

if i == 3:

continue

print(i)

Output:

0

1


2

4

## Practice Exercise
Try writing a for loop that iterates over a list of numbers and prints only the even numbers.

Python
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers:

if number % 2 == 0:

print(number)

Output:

2

4

6

8

10

## Conclusion

For loops are a powerful tool in programming for repeating tasks over a set of elements or a specific number of times. Practice using them with different types of iterables and in various scenarios to become more comfortable with their usage. With the knowledge of for loops, you can start building more complex programs that involve processing collections of data or performing repetitive operations efficiently.