-
Notifications
You must be signed in to change notification settings - Fork 25
Home
Welcome to the DDGSpring2016 wiki! This wiki contains a few simple examples for using Python and this library. These are not meant to actually teach you the full Python language, but hopefully will suffice to get you through the simple tasks for this course. If you're interested in fully learning Python, there are a whole bunch of great tutorials on the web!
On the sidebar to the right, you will find a few pages containing some documentation and examples.
Here are a few general notes about Python that I think are most important for a beginner to know:
-
Python is an interpreted language. This means programs are interpreted each time you run them, they do not need to be compiled. You may notice some
.pycfiles floating around your directories, these are automatically generated/managed by Python, and you can safely ignore them. -
In Python code, unlike nearly every other modern language, whitespace is significant -- changing the indentation of a program changes its meaning. Colons are used to denote the beginning of a loop or if-statement, and the subsequent indented region makes up the body of the loop. Brackets are never used.
for i in range(10): print("Hello from inside the loop") if i == 3: print("Iteration 3 is my favorite iteration") else: print("I wish this was iteration 3...") print("Hello from outside the loop")
-
Python is a dynamically typed language. You never need to give your variables a type (simply
x = 3instead of something likeint x = 3), and you are free to later reassign a variable to a new value of a different type (x = "cat"is valid). -
This codebase is designed using a simple form object-oriented programming, where the code is organized in to classes and each class contains the variables and logic for some component of the program. For instance, there is a
Vertexclass representing a vertex in a mesh, which contains a variable for the vertex position, as well as defining methods for operating on a vertex. If you don't already know object-oriented programming, don't worry -- you shouldn't need to learn it to complete the homework assignments.However, you should know that the
selfkeyword is crucial for writing object-oriented code in Python. When writing code inside a class, that class' variables are accessed usingself.variableName, and similarly class methods are called usingself.methodName(parameter1, parameter2). When defining a class method, it must takeselfas its first parameterdef myMethod(self, parameter1):. -
In Python, variables defined inside loops and if-statements are still valid after the end of the loop/if-statement.
for i in range(10): x = i*i print(x) #(valid code)