Home > Backend Development > Python Tutorial > Day - Slicing,for loop and nested loop

Day - Slicing,for loop and nested loop

Mary-Kate Olsen
Release: 2024-12-05 12:05:15
Original
765 people have browsed it

Day - Slicing,for loop and nested loop

Slicing:

Slicing is a programming technique used in Python to extract a portion of a sequence. By specifying a range of indices, you can retrieve specific parts of the sequence without altering the original data.

Example:

name=[2,8]

Step operator:

A step operator refers to the ability to specify an increment for iteration in loops. In Python, this is often used with the range() function, which allows specifying a step to control how the loop variable changes after each iteration.

Example:

name[2:8:3]
3 is a step operator.

Program using two variable:

start,end= 1,6
while end>1:
    for num in range(start,end):
        print(num, end=" ")
    print()
    end-=1   
Copy after login

Same program using one variable:

end= 6
while end>1:
    for num in range(1,end):
        print(num, end=" ")
    print()
    end-=1  
Copy after login

*Same program without using variable or using nested loop:
*

for end in range(6,1,-1):
    for num in range(1,end):
        print(num, end=" ")
    print()  
Copy after login

Output:

1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 

Copy after login

Nested loop:

A nested for loop is a loop inside another loop.

Syntax:

for outer in outer_iterable:
    for inner in inner_iterable:
Copy after login

1.The outer loop runs first.
2.For every iteration of the outer loop, the inner loop runs completely.
3.When the inner loop finishes, the outer loop proceeds to its next iteration.

for row in range(2,7):
    for col in range(1,row):
        print(col, end=' ')
    print()
Copy after login
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
Copy after login
for row in range(5):
    for col in range(5-row):
        print(col+1, end=' ')
    print()
Copy after login
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
Copy after login
for row in range(5):
    for col in range(5-row):
        print((col+1)*2, end=' ')
    print()
Copy after login
2 4 6 8 10 
2 4 6 8 
2 4 6 
2 4 
2 
Copy after login
for row in range(5):
    for col in range(5-row):
        print((col+1)*(row+1), end=' ')
    print()
Copy after login
1 2 3 4 5 
2 4 6 8 
3 6 9 
4 8 
5 
Copy after login

Task:

(https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3v84djylxrixjnllx8hq.jpg)

for row in range(5):
    for col in range(5-row):
        print((col+1)*3, end=" ")
    print()

Copy after login
3 6 9 12 15 
3 6 9 12 
3 6 9 
3 6 
3 
Copy after login
for row in range(5):
    for col in range(row+1):
        print(5-col, end=' ')
    print()
Copy after login
5 
5 4 
5 4 3 
5 4 3 2 
5 4 3 2 1 
Copy after login

The above is the detailed content of Day - Slicing,for loop and nested loop. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template