r/Python • • Sep 18 '20

Resource PowerShell vs Python Reference

https://blog.ironmansoftware.com/powershell-vs-python/
198 Upvotes

14 comments sorted by

View all comments

2

u/WadeEffingWilson Sep 19 '20 edited Sep 19 '20

A few essential things that are left out:

  • PS has switch/case conditional statements whereas python does not. You can approximate the functionality with if/else statements

PS example:

switch ($var) {
    case1 { $true_condition1 }
    case2 { $true_condition2 }
    default { $nothing_matched }
    }

Python example:

if var == case1:
    true_condition1
elif var ==  case2:
    true_condition2
else:
    nothing_matched  
  • PS doesn't have the slice vector notation capability like python (wish it did)

PS example:

# getting elements 0-5
@($array[0],$array[1],$array[2],$array[3],$array[4],$array[5])

Python example:

# getting elements 0-5
array[:5]  
  • PS's feature set can be extended by using inline C#

4

u/[deleted] Sep 19 '20

In Python, you can emulate switch cases more effectively than if/else statements using a dictionary acting as a dispatch table.

Because functions in Python are objects, they can be stored and called from anywhere an object could be.

Meaning I can do things like

table = { case_1: true_condition }

def true_condition(): print(“Hello World”)

2

u/WadeEffingWilson Sep 19 '20

Interesting! I never thought to use it that way. Thanks!

1

u/i5513 Sep 19 '20

But switch on powershell is like awk on Linux, It is not as simple like other switch statements in others program languages