Variable comparison: PowerShell vs Python
PowerShell and Python have an almost similar way to declare variables. You can directly assign the value to the variable and the assigned values become that variable datatype.
The only difference is, in PowerShell variables are declared with $ sign and Name while in Python you can directly assign variable with the name. For example,
PowerShell Declaration:
Python Declaration:
Datatype Check:
To check the datatype in the PowerShell, use Gettype() method.
To check the dataype in the Python, use type() method.
Assigning Multiple Values:
Assigning multiple values in PowerShell and Python both have the same ways.
PowerShell:
$a, $b, $c = 'Cat','Dog','Tiger'
Python:
a,b,c = 'cat','Dog','Tiger'
Case Sensitivity
PowerShell isn’t case sensitive to the variable name but the Python is. So variable name str isn’t the same as Str in python.
PowerShell:
Python:
Even the output from the variable is case sensitive in the Python language. For example,
PowerShell:
Python:
Global Variable Declaration:
PowerShell and Python both uses global keyword to declare variable as a global variable but in Python you can’t assign value to the global variable while declaration. You need to assign value to the global variable separately.
PowerShell:
$x = 5
Write-Output "Old X value : $x"
function changeX(){
$Global:x = 10
}
changeX
Write-Output "New X value : $x"
Python:
x = 5
print("Old X Value " + str(x))
def changeX():
global x
x = 10
changeX()
print("New X Value : "+str(x))
When you check the above output, both the output will be the same.
In the Python example, you might have noticed that we have first declared the global variable x and then assigned value while this is not the case with PowerShell, you can directly declare and assign the global variable value at the same time.