VS Code
Visual Studio Code
VS Code Intro
-
Generic IDE - Integrated Development Environment
-
Tons of plugins
-
Open Source
-
Developed by Microsoft
-
Introspection (understands Python)
-
Running, Debugging
-
Refactoring
VS Code Project or Single file
- Open File
- Open Folder
Install the Python Extension
-
Usually VS Code will suggest you to install the plugin when you open a Python file with .py extension.
-
If not, click on the icon on the left and search for "python" (by Microsoft)
VS Code examples
- Run program
- Debug program
- Set breakpoint
- Set conditional breakpoint
def fib(n):
if int(n) != n or n <= 0:
raise ValueError("Bad parameter")
if n == 1:
return 1
if n == 2:
return 1
return fib(n-1) + fib(n-2)
print(3, fib(3)) # 2
print(30, fib(30)) # 832040
fib(0.5)
- Set argv
import sys
def main():
if len(sys.argv) != 3:
exit("Needs 2 arguments: width length")
width = int( sys.argv[1] )
length = int( sys.argv[2] )
if length <= 0:
exit("length is not positive")
if width <= 0:
exit("width is not positive")
area = length * width
print("The area is ", area)
main()
- Refactor "z" to "operator"
import random
def count():
for x in range(1000):
v = random.choice("abcd")
print(x)
print(v)
def add(x, y):
return x + y
def multiply(x, y):
return x * y
def calc(x, y, z):
if z == "+":
return x + y
if z == "*":
return x * y
if z == "-":
return x - y
if z == "/":
return x / y
raise Exception(f"Unknown operator {z}")
import mylib
mylib.count()
print(mylib.calc( 2, 3, "+"))
print(mylib.calc( 2, 3, "*"))