<jupyter_start><jupyter_text>Some Python basics and cool stuff The Python code (part 1)<jupyter_code># basic program
print('hello world')
# dynamic typing
x = 1
print(x)
x = 'kaczka'
x
# indentation decides scopes
for i in range(5):
    print(i)
print('after')
# a function
def foo(n):
    print('foo', n)

foo(13)
# a class
class Bar:
    def __init__(self, spam):
        self.spam = spam
        
    def spamminate(self):
        return self.spam * 2
        
ham = Bar('something')
print(ham.spam)
ham.spamminate()
# using other modules (imports)
import time
print(time.time())

import urllib
x = urllib.parse.urljoin('http://blabla', 'bar')
print(x)

from urllib.parse import urljoin
print(urljoin('http://bar', 'foo'))
import sys
sys.path
#undefining stuff
print(urllib)
del urllib
#print(urllib)<jupyter_output><empty_output><jupyter_text>Data structures  List<jupyter_code>[1, 'a', 2.2, 'b', 3+4j, 'c']
x = []
for i in range(5):
    x.append(i)
x
# list comprehension
nums = [i for i in range(5)]
print(nums)

[x**3 for x in nums if x % 2 != 0]<jupyter_output><empty_output><jupyter_text>Dictionary<jupyter_code>{1:2, 'key': 'value'}
{x: x**2 for x in [2, 5, 123]}
'a' in {'a': 1, 'b':3}<jupyter_output><empty_output><jupyter_text>Set<jupyter_code>{1,2,3}
1 in {1,2,3}
set([1,2,3,1,1,2])<jupyter_output><empty_output><jupyter_text>Tuple<jupyter_code>(1,2,3)
x = 'kaczka', 'ser'
x
x = ('kaczka', 'ser')
x
# unpacking
y, z = x
print(y)
print(z)
# ignoring while unpacking
y, __ = x
y<jupyter_output><empty_output><jupyter_text>Deque<jupyter_code>from collections import deque
x = deque([1,2,3])
x
print(x.pop())
print(x.popleft())
x<jupyter_output><empty_output><jupyter_text>The Python code (part 2)<jupyter_code># everything is an object
print(foo)
foo = lambda n: print('new foo', n)
foo(13)

foo.bar = 3
print(foo.bar)
# code is executed top to bottom
def bar():
    asdasdasdasdasd
print('there are no errors here')
#bar()
# any statement can be anywhere
def spam():
    class Ham:
        def foo(self):
            print('foo')
    Ham().foo()
spam()
# python logic evaluation
if not []:
    print('Yes, empty list evaluates to False.')

print()
for x in ('', None, 0, False, []):
    print(bool(x))

print()
for x in ('foo', 1, True, [1]):
    print(bool(x))<jupyter_output><empty_output><jupyter_text>Few more advanced things<jupyter_code># a generator
def generate_integers(n):
    for i in range(n):
        yield i

# works as any other iterable
for i in generate_integers(5):
    print(i)
gen = generate_integers(3)
next(gen)
list(generate_integers(10))
#generator comprehension
for x in (-x for x in range(3)):
    print(x)
import math        

# piping generators
integer_factorials = (math.factorial(x) for x in generate_integers(10000))
# piping generators evaluation
next(integer_factorials)
# decorators
def dekoratorek(func, *args, **kwargs):
    def decorated(*args, **kwargs):
        print('before')
        func()
        print('after')
    return decorated
    
@dekoratorek
def foo():
    print('foo')
    
foo()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15252032520325204, 'fraction_numerical': 0.020162601626016262, 'mean_word_length': 4.542342342342343, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '323020', 'license': 'MIT', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1183, 'n_words': 311, 'path': 'python/2015/python_i_ty.ipynb', 'repo_name': 'butla/presentations', 'size': '3075'}