Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scientific Programming in Python -- Cheat Sheet

Similar presentations


Presentation on theme: "Scientific Programming in Python -- Cheat Sheet"— Presentation transcript:

1 Scientific Programming in Python -- Cheat Sheet
2018/1/26 2018/1/26 Operations and variables a = → 2**10 a/b : division a += 1 a *= 2 a // b : floor division (e.g. 10 // 3 is 3) Strings Built-in functions a = “this” b = “string” a + b → “this string” “A” * 3 → “AAA” a[0] → “t” a[1] → “h” a[0:2] → “th” a[-1] → “s” dir(x) : names, functions, variables available in x type(x) : type of x int(x) : convert into integer float(x) : convert into float str(x) : convert into string list(x) : convert into a list tuple(x) : convert into tuple print(x) : print the content of x bool(x) : convert into boolean del x : delete x from scope Boolean type Print True, False ∅, {}, [], (), None, “” are all False. All other variables or values are True print(“{0} isn’t equal to {1}”.format(1, 2)) print(“{0:f}”.format(1.2345) ➥conversion to float Other Module sequence = “this is amazing” sequence.split() “this” in sequence → True “that” in sequence → False import math from math import pi import math as mymath from math import pi as PI Files

2 list (mutable, ordered)
2018/1/26 tuple (immutable) list (mutable, ordered) t1 = (1, 2, 3) t2 = 1, 2, 3 t3 = tuple(“aa”) t3 = tuple([1, 2, 3]) t1.count(1) l1 = [1, 2, 3] l2 = list(t1) l1[2] = 10 l4 = [1] * 5 → [1, 1, 1, 1, 1] l1 + l1 → [1, 2, 3, 1, 2, 3] l1.append([4, 5]) → [1, 2, 3, [4, 5]] l1.extend([4, 5]) → [1, 2, 3, 4, 5] ⚠️ extend is an inplace operator dictionary (mutable) set (mutable, unique unordered) d1 = {} d2 = {“a”:1, “b”:2} d3[“a”] → 1 d2.items() d2.keys() d2.values() s1 = {“a”, “b”, “c”, “c”} → {“a”, “b”, “c”} Types of collections Function def myfunc(x, y, *args, **kargs): x + y args[0] kargs.keys() return x + y + args[0] + 1 Conditional structures if a == 1: # do something pass elif a == 2: else: For loop for i in range(10): print(i) List comprehensions [x for x in range(10) if x%2==0]


Download ppt "Scientific Programming in Python -- Cheat Sheet"

Similar presentations


Ads by Google