DEV Community

Guru prasanna
Guru prasanna

Posted on

1

Python Day-16 Loop-Slicing & step operator,Pattern Formation,Task

Slicing operator & Step operator:

Example:

name = 'abcdefghijklmn'

name[2:8] --> Slicing Operator-->Used for extracting portions of a sequence.

name[2:8:3] --> Step Operator-->The step operator defines the interval between indices. A positive step moves forward, while a negative step moves backward.

1) Syntax to get following output:

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Method:1(Using 2 variables)

start, end = 1, 6
while end>1:
    for num in range(start,end):
        print(num, end=' ')
    print()
    end-=1
Enter fullscreen mode Exit fullscreen mode

Method:2(Using single variable)

end = 6
while end>1:
    for num in range(1,end):
        print(num, end=' ')
    print()
    end-=1
Enter fullscreen mode Exit fullscreen mode

Method:3(Without using variables)

for end in range(6,1,-1):
    for num in range(1,end):   
        print(num, end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Method:4

for row in range(5):
    for col in range(5-row):
        print(col+1, end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
Enter fullscreen mode Exit fullscreen mode

2) Syntax to get following output:

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

for row in range(2,7):
    for col in range(1,row):
        print(col,end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode

3) Syntax to get following output:
2 4 6 8 10
2 4 6 8
2 4 6
2 4
2

for row in range(5):
    for col in range(5-row):
        print((col+1)*2, end=' ')
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

2 4 6 8 10 
2 4 6 8 
2 4 6 
2 4 
2 
Enter fullscreen mode Exit fullscreen mode

4) Syntax to get following output:

1 2 3 4 5
2 4 6 8
3 6 9
4 8
5

for row in range(5):
    for col in range(5-row):
        print((col+1)*(row+1), end=' ') 
    print()
Enter fullscreen mode Exit fullscreen mode

Output:

1 2 3 4 5 
2 4 6 8 
3 6 9 
4 8 
5 
Enter fullscreen mode Exit fullscreen mode

Task:
Draw this "kolam" without taking your hand out from paper:

Image description

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay