Skip to content

Instantly share code, notes, and snippets.

@molcay
Last active November 21, 2016 09:12
Show Gist options
  • Save molcay/fb3e1fc4076958aefd51a61d301efc7c to your computer and use it in GitHub Desktop.
Save molcay/fb3e1fc4076958aefd51a61d301efc7c to your computer and use it in GitHub Desktop.
Python Notes
'''
Q: Give the values of each local variable in function f at the end of its execution (i.e. the output of the print statements at the end):
'''
def f():
a = [ 3, {'x':3, 'y':4}, [1,2,3], (10,20), "xyz" ]
b = a
c = b[:] # shallow copy
(a0,a1,a2,a3,a4) = a # equivalent of a0=a[0], a1=a[1] ... a4=a[4]
a5 = a[2][:]
a2.append("new")
c[0] = 30
c[1]['x'] = 40
a[4] = "hello"
a[0] = 50
a3 = "world"
#print(locals().items())
for (k,v) in locals().items():
print("{0} = {1}".format(k,v))
# kodu denerken aşağıdaki yorum satırları kaldırılmalı
#f()
'''
Q: What will be the output of the following code:
'''
a = [1,2,3]
b = a
c = [1,2,3]
print(a==c)
print(a is b)
print(a is c)
a.append(4)
print(a==c)
print(a is b)
b = [1,2,3,4]
print(a == b)
print(a is b)
'''
Q: Write the value of a after each line and give an equivalent Python conditional expression:
1 var x=5, y=8, a; // a is undefined
2 a = x>y ? "yes" : (y%2 == 0 ? "no" : "may be") // a is "no"
3 a = x<y ? "yes" : (y%2 == 0 ? "no" : "may be") // a is "yes"
4 a = x==y ? "yes" : (x%2 == 0 ? "no" : "may be") // a is "may be"
'''
x = 5
y = 8
#for second line:
a = "yes" if x>y else ("no" if y%2==0 else "may be")
#if (x > y):
# a = "yes"
#else:
# if (y % 2 == 0): a = "no"
# else: a = "may be"
#for third line:
a = "yes" if x<y else ("no" if y%2==0 else "may be")
#if (x < y):
# a = "yes"
#else:
# if (y % 2 == 0): a = "no"
# else: a = "may be"
#for fourth line:
a = "yes" if x==y else ("no" if x%2==0 else "may be")
#if (x == y):
# a = "yes"
#else:
# if (x % 2 == 0): a = "no"
# else: a = "may be"
'''
Q: Give the JS equivalent of the following Python code:
1 a = 5 if x>y else (4 if x<y else 3)
'''
# a = x > y ? 5 : (x < y ? 4 : 3) # burası JS kodu
'''
Q: Rewrite the following function by replacing all conditional statements by conditional expressions:
1 def f(x,y):
2 if x>y: a = x
3 else: a = y
4 if a>0: return g(a*2)
5 else: return g(a/4)
'''
def f(x,y):
a = x if x > y else y
return g(a*2) if a > 0 else g(a/4)
# kodu denerken aşağıdaki yorum satırları kaldırılmalı
#def g(a):
# print(a)
"Lazy Evaluation in Booleans"
'''
Q: For boolean expressions, Python and Javascript use lazy evaluation (or sometimes called short circuting in this context). See this discussion. Give the value of a after each line in the following Python code:
1 a = 5 or (1/0) # a is now ... 5
2 a = (0 or "abc") and (0 or "") # a is now ... ""
3 a = (0 or "") and (0 or "abc") # a is now ... ""
4 a = ("" or []) and ("abc" or 0) # a is now ... []
5 a = 0 or "abc" or [1,2] # a is now ... "abc"
6 a = [] or (5 and [1,2]) or "xyz" # a is now ... [1,2]
'''
a = 5 or (1/0) # a is now ... 5
a = (0 or "abc") and (0 or "") # a is now ... ""
a = (0 or "") and (0 or "abc") # a is now ... ""
a = ("" or []) and ("abc" or 0) # a is now ... []
a = 0 or "abc" or [1,2] # a is now ... "abc"
a = [] or (5 and [1,2]) or "xyz" # a is now ... [1,2]
"------------------------------------------------------------"

This gist is about Python perpective from Programming Language Consept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment