# Python Programming - Getting Started ###### tags: `Python`, `VPython` ## Variables and Objects Variables can be created anywhere in a Python program, the equal sign (`=`) is used to assign a value to a variable. The type of the variable is determined by the assignment statement. ``` Python a = 3 # an integer b = -2.4 # a floating-point number c = "ABC" # a string Earth = sphere(pos=vector(0,0,0), radius=6.4e6) # a 3D VPython object values = [a, b, c, 100, "abc", Earth] # a list of values ``` Basic VPython objects such as sphere() and box() have a set of "attributes" such as color, and you can define additional attributes such as mass or velocity. Other objects, such as vector(), have built-in attributes but you cannot create additional attributes. The value of a variable or object can be changed. If a variable is not “defined” (assigned a value), trying to use it will give you an error. Run the following code: ```python= print(n) # try to access an undefined variable x = 10 y = 2 print(x + y) z = x / y # copying the value of ( x / y ) to z print(z) x = x * z # copying the value of ( x * z ) to x y = y - z # copying the value of ( y - z ) to y print(x, y) # print multiple objects, output results are delimited by a space ``` ## String In programming, we call a text **string**. * Character * A unit of a text. * A letter, a numerical digit, or a symbol. * String * A series of characters. * For example, `"Hello"` consists of five characters that are `'H'`, `'e'`, `'l'`, `'l'`, and `'o'`. * String representation * Single quotes * `'ABC'` * `'123456890'` * Double quotes * `"ABC"` * `"1234567890"` * No different between single quotes and double quotes ## Operators ### String Operators | Operator | Meaning | Example | | -------- | -------------------- | ------- | | + | String concatenation | | | += | String appending | | ### Arithmetic Operators | Operator | Meaning | Example | | -------- | -------------------------------- | ------- | | + | Addition | x + y | | - | Subtraction | x - y | | * | Multiplication | x * y | | / | Division | x / y | | % | Modulus | x % y | | ** | Exponent | x ** y | | // | Floor division(integer division) | x // y | Run the following code: ``` python= x = 11 y = 7 z = x % y print(z) # 4 z = y ** 2 print(z) # 49 z = 2 ** 0.5 print(z) # 1.4142135623730951 z = x / y print(z) # 1.5714285714285714 z = x // y print(z) # 1 ``` ## Data Conversion | Usage | Meaning | | ------------- | ------------------------------------------------------ | | int(object) | Convert an object to an integer | | float(object) | Convert an object to a floating number (a real number) | | str(object) | Convert an object to a string | ## Reference [Complete Documentation for VPython](https://www.glowscript.org/docs/VPythonDocs/index.html) [The Python Tutorial](https://docs.python.org/3/tutorial/index.html)