Wk 2: Background in Python and Unix Flashcards
Python tool for debugging online
pythontutor.com
What is dynamic typing of variables?
you don’t have to declare type of variable
Why does python allow for dynamic typing?
Everything in python is an object
What happens when you assign a different type to a variable that’s already been created?
Python creates a new object, and then points your variable at the new one (garbage collector takes care of original)
IS operator
tests if two variables point to the same object
assign a value to a variable
X = 5, X = “hello”, X = 4.5
Some string methods in python
capitalize(), lower() – return copies of the original (but don’t modify the original)
How to call lower() method on variable x?
x.lower()
How to assign output of x.lower to x?
x = x.lower()
Loop syntax example in python
for i in range (0,10): print (i)
Range(x,y) - x is inclusive? y is inclusive?
x is inclusive, y is exclusive
loop syntax increment by 2
for i in range (0,10,2): print (i)
while loop equivalent of “for i in range (0,10,2)”
i = 0; while i < 12: print(i); i+=3
Print var1 if var1 == 2
if var1 == 2:
print(var1)
If / else if / else syntax
if x==2; elif x==3: else:
define a function
def func_name(args): xxxx; return val
what’s the downside of not having to define the type of the args or the return value
will get the error at run time if a value is passed that isn’t the expected type
what does a function return if there is not return statement
returns None
What’s the scope of a variable set at the top of the code
Global - it’s available throughout all code, and the functions
What’s the scope of a variable set inside of a function
only available in that function, unless it has the same name as a prev set global var