Notes in Python fundamentals

To Subscribe, use this Key


Status Last Update Fields
Published 07/10/2024 def f(): x = 1 x = 2 f() print(x)
Published 07/10/2024 def func(): global x x = 1 x = 2 func() print(x)
Published 07/10/2024 def func(): def nested(): global x x = 1 nested() x = 2 func() print(x)
Published 07/10/2024 def func(): def nested(): nonlocal x x = 1 x = 3 nested() print("func:", x) x = 2 func() print("global:", x) …
Published 07/10/2024 def func(): def nested(): def nested2(): nonlocal x x = 100 print("nested2:", x) x = 50 neste…
Published 07/10/2024 Python. A {{c1::docstring}} is {{c2::a string literal which is the first expression in a class, function or module}} and is automatically stored in it…
Published 07/10/2024 def func(): print('first') func() def func(): print('second') func()
Published 07/10/2024 for i in range(3): print(i) if i == 1: break else: print('end')
Published 07/10/2024 print("foo", "bar", sep="baz", end="qux")
Published 07/10/2024 get my_string's length
Published 07/10/2024 create a string object from obj
Published 07/10/2024 convert my_string to upper case, returning a new string
Published 07/10/2024 text = "ababab" print(text.replace("a", "c"))
Published 07/10/2024 replace the first two occurrences of "foo" in my_string by "bar", returning a new string
Published 07/10/2024 def func(): global x x = 1 func() print(x)
Published 07/10/2024 lst = ["a", "b", "c", "d", "e"] print(lst[0], lst[-1])
Published 07/10/2024 my_list = ['a', 'b', 'c', 'd', 'e'] print(my_list[1:4])
Published 07/10/2024 text = "foo bar foo bar" print(text.find("bar"))
Published 07/10/2024 get the index of the first occurrence of "foo" in my_string, starting from index 5
Published 07/10/2024 get the index of the first occurrence of "foo" in my_string, between index 10 and 20
Published 07/10/2024 text = "abababab" i = text.count("abab") print(i)
Published 07/10/2024 get the number of non-overlapping occurrences of "foo" in my_string, between indexes 5 and 13
Published 07/10/2024 t = 'AaBbCcDd' print(t[::2]) print(t[1::2])
Published 07/10/2024 t = "12345" print(t[::-1]) print(t[::-2])
Published 07/10/2024 t = "12345" print(t[1:-1])
Published 07/10/2024 print(5 // 2)
Published 07/10/2024 print(-5 // 2)
Published 07/10/2024 print(5 % 2)
Published 07/10/2024 t = (1, 2) t[1] = 3
Published 07/10/2024 s = "hello" s[1] = "u"
Published 07/10/2024 a = 1 b = a print(a is b) b = 2 print(a is b)
Published 07/10/2024 a = [1, 2] b = a print(a is b) b[0] = 3 print(a is b) print(a, b)
Published 07/10/2024 a = [1, 2] b = a print(a is b) b = 3 print(a is b) print(a, b)
Published 07/10/2024 a = [1, 2] b = a[:] print(a is b) print(a, b)
Published 07/10/2024 a = [1] b = [0, a] print(b) print(a in b) b[1] = 'test' print(b) print(a in b)
Published 07/10/2024 a = [1] b = [0, a] print(a, b) print(a in b) b[1][0] = "test" print(a, b) print(a in b) print(a is b[1])
Published 07/10/2024 a = [1] b = [a] c = [b] print(a in b) print(a in c) print(a in c[0])
Published 07/10/2024 text = "hello world" print("world" in text)
Published 07/10/2024 import copy a = [1] b = [0, a] c = copy.copy(b) print(c is b) print(c[1] is b[1]) print(b[1] is a)
Published 07/10/2024 print("foo" "bar")
Published 07/10/2024 text = " I met a traveller " words = text.split() print(words)
Published 07/10/2024 text = "a b c d" words = text.split(None, 2) print(words)
Published 07/10/2024 text = "ABBA"words = text.split("B")print(words)
Published 07/10/2024 t = "hel lo"print(list(t))
Published 07/10/2024 def func(): passprint(func())
Published 07/10/2024 x = [0, 1, 2, 3] x[1] = [] print(x)
Published 07/10/2024 x = [0, 1, 2, 3] x[1:2] = [] print(x)
Published 07/10/2024 x = [0, 1, 2, 3]x[1:3] = []print(x)
Published 07/10/2024 x = [0, 1, 2, 3] x[::2] = [] print(x)
Published 07/10/2024 x = [0, 1, 2, 3] x[::2] = [7, 8] print(x)
Published 07/10/2024 x = [0, 1, 2, 3] y = x.pop() print(x) print(y)
Published 07/10/2024 x = [0, 10, 20, 30] y = x.pop(1) print(x) print(y)
Published 07/10/2024 x = [4, 5, 6, 5] x.remove(5) print(x)
Published 07/10/2024 x = [0, 1, 2, 3]x.remove(4)print(x)
Published 07/10/2024 x = [0, 1, 2] x.append(3) print(x)
Published 07/10/2024 x = [0, 1, 2] x.append([3 ,4]) print(x) print(x[3])
Published 07/10/2024 x = [0, 1, 2] x.extend([3, 4]) print(x)
Published 07/10/2024 x = [0, 1, 2] x.extend(3) print(x)
Published 07/10/2024 for i in range(5): if i == 2: continue print(i, end=" ")
Published 07/10/2024 x = () print(type(x)) x = (1) print(type(x)) x = (1,) print(type(x))
Published 07/10/2024 data = {"a": 1, "b": 2, "c": 3} print(data["a"])
Published 07/10/2024 data = {"a": 1, "a": 2, "c": 3} print(data["a"])
Published 07/10/2024 data = {'a': 1, 'b': 2, 'c': 3} data['a'] = 4 print(data)
Published 07/10/2024 data = {"a": 1, "b": 2} data["c"] = 3 print(data)
Published 07/10/2024 data = {"a": 1, "b": 2}data[66] = 77print(data)
Published 07/10/2024 data = {x: "abc"[x] for x in range(3)} print(data)
Published 07/10/2024 if condition: x = 1 else: x = 2
Published 07/10/2024 seq = ["a", "b", "c"] x = "-".join(seq) print(x)
Published 07/10/2024 x = 2 y = 3 print(1 < x < y < 4)
Published 07/10/2024 x = (i**2 for i in range(4))print(x)
Published 07/10/2024 x = tuple(i**2 for i in range(4)) print(x)
Published 07/10/2024 x = [0, 1] print(x[2])
Published 07/10/2024 x = {"a": 0, "b": 1} print(x["c"])
Published 07/10/2024 a, b, c = [1, 2, 3, 4, 5] print(a, b, c)
Published 07/10/2024 a, b, *c = [1, 2, 3, 4, 5] print(a, b, c)
Published 07/10/2024 *a, b, c = [1, 2, 3, 4, 5] print(a, b, c)
Published 07/10/2024 a, *b, c = [1, 2, 3, 4, 5]print(a, b, c)
Published 07/10/2024 a, *b, *c = [1, 2, 3, 4, 5] print(a, b, c)
Published 07/10/2024 x = [4, 5, 6, 7] print(x) print(*x)
Published 07/10/2024 text = "foo bar baz "x = text.rsplit()print(x)
Published 07/10/2024 foo = lambda x: x+1 bar = foo(1) print(bar)
Published 07/10/2024 foo = lambda x: x+1 print(type(foo))
Published 07/10/2024 func = lambda: 1 if x else 2 x = True print(func()) x = False print(func())
Published 07/10/2024 def func(x=[]): x.append(1) return x for i in range(4): print(func())
Published 07/10/2024 def func(x=[1], y=[99]): x[0], y[0] = y[0], x[0] print(x, y) for i in range(4): func()
Published 07/10/2024 def func(x): return x print("here") print(func(1))
Published 07/10/2024 x = [[0]]*3 print(x) x[2][0] = 1 print(x) print(x[1] is x[2])
Published 07/10/2024 x = (0,) * 3 print(x) y = [1] * 3 print(y)
Published 07/10/2024 x = 1, 2 print(type(x))
Published 07/10/2024 x, *y = 1, 2, 3 print(type(x)) print(type(y))
Published 07/10/2024 x = ['a', 'b', 'c', 'd']for a, b in enumerate(x): print(a, b)
Published 07/10/2024 yield (index, item) pairs from my_stream, counting from 5
Published 07/10/2024 def out(): def inn(): nonlocal x x = 5 inn() out()
Published 07/10/2024 def out(): global x x = 5out()print(x)
Published 07/10/2024 x = ([],) x[0] = 1 print(x)
Published 07/10/2024 x = [] x[0] = 999 print(x)
Published 07/10/2024 x = ([],) x[0].append("a") x[0].append("b") print(x)
Published 07/10/2024 x = {'a': 1, 'b': 2} y = {'b': 3, 'c': 4} x.update(y) print(x)
Published 07/10/2024 for collection in [[], (), {}, [1], (1,), {"a": 1}]: print(bool(collection), end=" ")
Published 07/10/2024 x = 1 or 3 print(x) x = 1 and 3 print(x)
Published 07/10/2024 x = 0 or 3 print(x) x = 0 and 3 print(x)
Published 07/10/2024 def foo(): print("here") return False x = foo() and False print(x) y = False and foo() print(y)
Published 07/10/2024 def foo(): print("here") return False x = foo() or True print(x) y = True or foo() print(y)
Published 07/10/2024 def foo(): print("here") return 1 x = [foo(), foo(), foo()] print('x:', x)
Published 07/10/2024 def foo(): global y y += 1 y = 0 x = (foo() for i in range(3)) print(y) next(x) print(y)
Published 07/10/2024 x = not "bloop" print(x)
Published 07/10/2024 y = [bool(i) for i in ("", "0", "None", "False")] print(*y)
Published 07/10/2024 func = lambda x=1, y=1: x + yprint(func())print(func(2))print(func(2, 3))
Published 07/10/2024 x = [0, 0, 1, 0, 1] if any(x): print("yes") else: print("no")
Published 07/10/2024 x = [0, "", 0.0, False]if any(x): print("yes")else: print("no")
Published 07/10/2024 x = [1, 2, 3] if all(x): print("yes") else: print("no")
Published 07/10/2024 def func(): yield 1 yield 2 yield 3 print(type(func)) print(type(func()))
Published 07/10/2024 def func(): output = 0 while True: new = yield output output = new genr = func() print(next(genr)) print(next(genr)) print(ne…
Published 07/10/2024 def func(): output = 0 while True: new = yield output output = new genr = func() print(next(genr)) print(next(genr)) print(ge…
Published 07/10/2024 def func(): output = 0 while True: new = yield output output = new if new is not None else output genr = func() print(next(g…
Published 07/10/2024 def func(): output = 0 while True: new = yield output output = new if new is not None else output genr = func() print(genr.s…
Published 07/10/2024 class MyClass: pass MyClass.x = 1 print(MyClass.x)
Published 07/10/2024 text = "abcabcabc" y = "".join( [ char.upper() for char in text if char != "a" ] ) print(y)
Published 07/10/2024 def func(): pass func.x = 5 print(func.x)
Published 07/10/2024 def func(): print("here") return func func()()()
Published 07/10/2024 text = "Hello world" x = text.lower()[::-1].title() print(x)
Published 07/10/2024 class Foo: x = 1 foo = Foo() print(foo.x)
Published 07/10/2024 a = [1, 2, 3] b = [4, 5, 6] for i in zip(a, b): print(i)
Published 07/10/2024 a = [1, 2] b = [3, 4, 5] for i in zip(a, b): print(i)
Published 07/10/2024 a = "hello" b = "world" print(*zip(a, b)), sep='\n')
Published 07/10/2024 strings = ["a", "ab", "abc", "abcd", "abcde"] for string in strings: print(string.rjust(4, "_"))
Published 07/10/2024 strings = ["a", "aa", "aaa", "aaaa", "aaaaa"] for string in strings: print(string.center(6, "_"))
Published 07/10/2024 def wrap(func): return 1 @wrap def foo(): return 2 print(foo, type(foo))
Published 07/10/2024 def wrap(func): def wrapped(arg): return func(arg) + 1 return wrapped @wrap def foo(x): return x print(foo(1))
Published 07/10/2024 def wrap(func): def wrapped(y=2): return func(y) return wrapped @wrap def foo(x=1): return x print(foo())
Published 07/10/2024 x = [ ['a','a','a'], ['b','b','b'], ['c','c','c'], ] y = zip(*x) print(list(y))
Published 07/10/2024 a = [1, 2, 3] b = a c = [5, 6] a += c print(a, b, c, sep='\n')
Published 07/10/2024 a = [1, 2, 3] b = a c = [5, 6] a = a + c print(a, b, c, sep='\n')
Published 07/10/2024 x = {1, 2, 3} y = {2, 3, 4} print(x | y)
Published 07/10/2024 x = {1, 2, 3} y = {2, 3, 4} print(x & y)
Published 07/10/2024 x = {1, 2, 3} y = {2, 3, 4} print(x - y) print(y - x)
Published 07/10/2024 x = {1, 2, 3} y = {2, 3, 4} print(x ^ y)
Published 07/10/2024 x = {1, 2, 3}y = {}print(x & y)
Published 07/10/2024 x = {1, 2, 3} y = set() print(x & y)
Published 07/10/2024 x = ["when", "a", "knight", "won", "his", "spurs"] x.sort() print(x)
Published 07/10/2024 x = ["a", "b", 2, 1] x.sort() print(x)
Published 07/10/2024 x = ["a", "b", 2, 1] x.sort(key=str) print(x)
Published 07/10/2024 sort list_of_strings in place by the last character
Published 07/10/2024 x = {char.lower() for char in "Foo Bar" if char} print(x) print(type(x))
Published 07/10/2024 class Thing(): def __init__(self): self.x = 0 def __call__(self, arg): self.x = arg print(self.x) thing = Thing() pr…
Published 07/10/2024 class Foo: def __init__(self, x, y): self.x = x self.y = y def px(self): print(self.x) return self def py(self): …
Published 07/10/2024 def foo(func, x): return func(x) print(foo(lambda x: x * 3, 8)
Published 07/10/2024 words = ['abc', 'def', 'ghi'] t = [''.join(chars) for chars in zip(*words)] print(t)
Published 07/10/2024 for i in range(1, 3): print(i) print(i)
Published 07/10/2024 x = [1, 2, 3] y = x.reverse() print(x) print(y)
Published 07/10/2024 print(True + True) print(True * 3 + True / 2) print(5 * False) print(10 ** False)
Published 07/10/2024 print(True / False)
Published 07/10/2024 from functools import reduce foo = reduce(lambda x, y: x + y, [1, 2, 3, 4]) print(foo)
Published 07/10/2024 from functools import reduce print(reduce(lambda x, y: x+y, [1]))
Published 07/10/2024 from functools import reduce print(reduce(lambda x, y: x+y, [1], 9))
Published 07/10/2024 Python. Give some examples of built-in sequences.
Published 07/10/2024 x, y = [1, 2]print(x, y)
Published 07/10/2024 x, y = 'hi' print(x, y)
Published 07/10/2024 mdy = "OCT/04/1992" date = mdy.split("/") dmy = f"{date[1]}/{date[0]}/{date[2]}"
Published 07/10/2024 color, (x, y, z) = "red", (1, 2, 3) print(color, x, y, z)
Published 07/10/2024 start_points = [(1, 1), (2, 5), (-3, -4)] end_points = [(1, -1), (3, 6), (3, -4)] for start, end in zip(start_points, end_points): if start[0] ==…
Published 07/10/2024 name = "Ken Thompson" year = 2020 print(f"{name} turned {year - 1943} in {year}.")
Published 07/10/2024 Python. Give some examples of built-in non-sequence iterables.
Published 07/10/2024 Python. What are iterators?
Published 07/10/2024 Python. Give some examples of iterators.
Published 07/10/2024 matrix = [[1,2], [3,4]]flattened = [ item for row in matrix for item in row]print(flattened)
Published 07/10/2024 Python. List comprehensions are to lists as {{c1::generator expressions}} are to generators.
Published 07/10/2024 def func(*args): print(type(args)) print(args) print(sum(args)) func(1, 2, 3)
Published 07/10/2024 def foo(sequence): return [*sequence[1::2], *sequence[0::2]] print(foo('0123456'))
Published 07/10/2024 dict1 = {'a': 1, 'b': 2}dict2 = {'b': 3, 'c': 4}dict3 = {**dict1, **dict2, 'c': 5}print(dict3)
Published 07/10/2024 dict1 = {'a': 1, 'b': 2}dict2 = {'b': 3, 'c': 4}foo = {*dict1, *dict2}print(foo, type(foo))
Published 07/10/2024 def counter(maximum): i = 0 while i < maximum: val = (yield i) if val is not None: i = val else: …
Published 07/10/2024 def counter(maximum): i = 0 while i < maximum: yield i i += 1 it = counter(3) for _ in range(4): print(next(it))
Published 07/10/2024 def counter(maximum): i = 0 while i < maximum: yield i i += 1 it = counter(3) for _ in range(5): print(next(it, 'exhau…
Published 07/10/2024 def func(): x = 1 def inner_func(): print(x) inner_func() x = 0 func()
Published 07/10/2024 Python. In what order are namespaces searched?
Published 07/10/2024 def foo(): lst = [] for i in range(4): lst.append(lambda: i) print([f() for f in lst]) foo()
Published 07/10/2024 nums = [1, 2, 3, 4, 5] for n in nums: if n % 2 == 0: print(2**n)
Published 07/10/2024 def gen(stream): for n in stream: if n % 2 == 0: return else: yield nnums = [1, 3, 4, 1, 2]for n in gen(nums): …
Published 07/10/2024 x, y = {1: 'a', 2: 'b'} print(x, y)
Published 07/10/2024 def palindromic(sequence): for from_left, from_right in zip(sequence, sequence[::-1]): if from_left != from_right: return Fals…
Published 07/10/2024 print(reversed(x**2 for x in range(4)))
Published 07/10/2024 print(type(sorted((2, 3, 1))))
Published 07/10/2024 def foo(x, *, y): return x + y print(foo(1, 1))
Published 07/10/2024 def foo(x, *, y): return x + yprint(foo(1, y=2))
Published 07/10/2024 def foo(**attributes): print(type(attributes)) return attributes.popitem() print(foo( name='Cosmo', age=29, day='11/25/2020', …
Published 07/10/2024 def func(x, y, z): return (x - y) * z print(func(y=1, z=3, x=2))
Published 07/10/2024 def func(x, y, z): return (x - y) * z print(func(x=1, 0, 2))
Published 07/10/2024 from functools import reduce numbers = [1, 2, 3, 4, 5] product = reduce(lambda x, y: x * y, numbers) print(product)
Published 07/10/2024 w = {1, 2, 3, 4, 5} x = {1, 2, 3} y = {2, 3, 4} print(w - (x & y) == (w - x) | (w - y))
Published 07/10/2024 w = {1, 2, 3, 4, 5} x, *y = (*w,) print(x, y)
Published 07/10/2024 dict1 = {'a': 1, 'b': 2} dict2 = {'b': 3, 'c': 4} new = {**dict1, **dict2} print(type(new)) print(new)
Published 07/10/2024 Python. Why do you need to be cautious when using assert statements?
Published 07/10/2024 assert (1 == 2, 'Failed')
Published 07/10/2024 my_list = ['long_item', 'another_long_item', 'yet_another_long_item']
Published 07/10/2024 f = open("my_file.txt", 'w') f.write("blah blah") f.close()
Published 07/10/2024 foo = (3, 1, 4, 2) bar = sorted(foo) print(type(bar))
Published 07/10/2024 foo = (3, 1, 4, 2) bar = reversed(foo) print(type(bar))
Published 07/10/2024 Python. A {{c1::single leading underscore}} in a name conventionally indicates that {{c2::the name is private}}.
Published 07/10/2024 Python. To avoid a name shadowing a built-in, the convention is {{c1::to add a trailing underscore}}.
Status Last Update Fields