In [1]:
2 + 2
Out[1]:
4
In [2]:
name = "world"
print("hello " + name)
hello world
In [3]:
list(range(12))
Out[3]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
In [4]:
[n * 2 for n in range(12)]
Out[4]:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
In [5]:
[n for n in range(12) if n > 5]
Out[5]:
[6, 7, 8, 9, 10, 11]
In [6]:
for i in range(4):
    print(i)
0
1
2
3
In [7]:
for word1 in ["abe", "sø", "citron"]:
    for word2 in ["kat", "uhyre", "græs"]:
        print(word1 + word2)
abekat
abeuhyre
abegræs
søkat
søuhyre
søgræs
citronkat
citronuhyre
citrongræs
In [8]:
len(['a', 'b', 'c', 'd'])
Out[8]:
4
In [9]:
len('hej')
Out[9]:
3
In [10]:
for word1 in ["abe", "sø", "citron"]:
    for word2 in ["kat", "uhyre", "græs"]:
        if len(word1 + word2) > 6:
            print(word1 + word2)
abeuhyre
abegræs
søuhyre
citronkat
citronuhyre
citrongræs
In [11]:
data = {
    "a": [11,22,33],
    "b": {
        "c": 456
    }
}
print(data)
{'a': [11, 22, 33], 'b': {'c': 456}}
In [12]:
import json
print(json.dumps(data))
{"a": [11, 22, 33], "b": {"c": 456}}
In [13]:
json.loads('{"d":[7,8],"e":"f"}')
Out[13]:
{'d': [7, 8], 'e': 'f'}
In [14]:
data["a"]
Out[14]:
[11, 22, 33]
In [15]:
data["a"][0]
Out[15]:
11
In [16]:
data["a"][2]
Out[16]:
33
In [17]:
data.get("a", "hej")
Out[17]:
[11, 22, 33]
In [18]:
data.get("x", "hej")
Out[18]:
'hej'
In [19]:
list(data.keys())
Out[19]:
['a', 'b']
In [20]:
list(data.values())
Out[20]:
[[11, 22, 33], {'c': 456}]
In [21]:
import requests
fortunes = requests.get('https://raw.githubusercontent.com/JKirchartz/fortune/master/jung').text
print(fortunes[0:500])
The word 'happiness' would lose its meaning if it were not balanced by sadness.
 -- Carl Jung
%
One looks back with appreciation to the brilliant teachers, but with gratitude to those who touched our human feelings. The curriculum is so much necessary raw material, but warmth is the vital element for the growing plant and for the soul of the child.
 -- Carl Jung
%
Knowing your own darkness is the best method for dealing with the darknesses of other people.
 -- Carl Jung
%
Everything that irritat
In [22]:
"hej med dig".split(" ")
Out[22]:
['hej', 'med', 'dig']
In [23]:
import random
random.choice([4,5,6,7])
Out[23]:
7
In [24]:
print(random.choice(fortunes.split('%\n')))
The greatest and most important problems of life are all fundamentally insoluble. They can never be solved but only outgrown.
 -- Carl Jung

In [25]:
words = fortunes.split(' ')
words[0:10]
Out[25]:
['The',
 'word',
 "'happiness'",
 'would',
 'lose',
 'its',
 'meaning',
 'if',
 'it',
 'were']
In [26]:
from collections import Counter
Counter(words).most_common(10)
Out[26]:
[('the', 100),
 ('--', 75),
 ('Carl', 75),
 ('of', 71),
 ('is', 58),
 ('a', 52),
 ('to', 45),
 ('and', 35),
 ('not', 30),
 ('in', 30)]