True
tuple
## TypeError: ‘tuple’ object does not support item assignment
po staremu:
set
po staremu:
x = {2, 3, 4, -3, 5, 2}
y = {5, 6, 7}
z = {'a', 'b'}
w = {3, 4}
print(x.isdisjoint(y))
print(x.isdisjoint(z))
print(w.issubset(x))
print(x.issubset(w))
print(w <= x)
print(w < x)
print(w.issuperset(x))
print(x.issuperset(w))
print(w >= x)
print(w > x)
False
True
True
False
True
True
False
True
False
False
x = {2, 3, 4, -3, 5, 2}
y = {5, 6, 7}
z = {'a', 'b'}
w = {3, 4}
print(x.union(y))
print(x | y)
print(x.intersection(y))
print(x & y)
print(x.difference(y))
print(x - y)
print(x.symmetric_difference(y))
print(x ^ y)
print(x.copy()) # płytka kopia
{2, 3, 4, 5, 6, 7, -3}
{2, 3, 4, 5, 6, 7, -3}
{5}
{5}
{2, 3, 4, -3}
{2, 3, 4, -3}
{2, 3, 4, 6, 7, -3}
{2, 3, 4, 6, 7, -3}
{2, 3, 4, 5, -3}
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127
print(tel)
tel['jack']
del tel['sape']
tel['irv'] = 4127
print(tel)
{'jack': 4098, 'sape': 4139, 'guido': 4127}
{'jack': 4098, 'guido': 4127, 'irv': 4127}
https://docs.python.org/3.10/library/stdtypes.html#mapping-types-dict
po staremu:
d = {"one": 1, "two": 2, "three": 3, "four": 4}
print(d)
print(list(d))
print(list(d.values()))
d["one"] = 42
print(d)
del d["two"]
print(d)
d["two"] = None
print(d)
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
['one', 'two', 'three', 'four']
[1, 2, 3, 4]
{'one': 42, 'two': 2, 'three': 3, 'four': 4}
{'one': 42, 'three': 3, 'four': 4}
{'one': 42, 'three': 3, 'four': 4, 'two': None}
d = {"one": 1, "two": 2, "three": 3, "four": 4}
print(d.get("two"))
print(d.items())
print(d.keys())
print(d.pop("three"))
print(d)
print(d.popitem())
print(d)
2
dict_items([('one', 1), ('two', 2), ('three', 3), ('four', 4)])
dict_keys(['one', 'two', 'three', 'four'])
3
{'one': 1, 'two': 2, 'four': 4}
('four', 4)
{'one': 1, 'two': 2}
Inne nazwy: - string, napisy, łańcuchy znaków
Abstrakcyjnie:
Tablica znaków ASCII https://upload.wikimedia.org/wikipedia/commons/5/5c/ASCII-Table-wide.pdf
chr()
zamienia liczbę całkowitą na znakord()
zamienia znak na liczbę całkowitą odpowiadającą pozycji w tabeli znakówlen()
- długość napisustr()
- rzutuje argument na napisMądra definicja z wikipedii:
Relację leksykograficzną \(\preccurlyeq\) między ciągami \(\alpha, \beta \in X^*\) ustala się następująco:
jeśli istnieje wskaźnik \(j\) taki, że \(\alpha(j) \neq \beta(j),\) to znajdujemy najmniejszy \(i\) o tej własności. Wówczas
jeśli taki \(j\) nie istnieje, to
Przykłady:
printf
Zaczerpnięty z języka C - stare.
https://docs.python.org/3.10/library/stdtypes.html#old-string-formatting
Dodatkowe:
https://gist.github.com/pjastr/02d01dba3d5f5c3e60ed74cb32c913ed https://gist.github.com/pjastr/cbe8418eb4798b92d7fcba4f48d32845 https://gist.github.com/pjastr/e7d5fcbebd578c1df122d307e0051707 https://gist.github.com/pjastr/00c1df223918f975d678d8455b4f5b0a
format
https://docs.python.org/3.10/library/string.html#formatstrings
https://gist.github.com/pjastr/27fe6c13cd8bcba63be561a05af030a0
https://gist.github.com/pjastr/d40fe6eaf21a9595bcf6b43e7b020fbd
https://gist.github.com/pjastr/8a630b4a575e6a389a7dc3c5e8dc65a0
https://gist.github.com/pjastr/d42d937c7b00b80b5dfe309b4ac0e854
https://gist.github.com/pjastr/dbc9a0c1e8bf612b68e0bc7789daf53b
https://gist.github.com/pjastr/adfd7371dcbe4e4034bfb12dcfe30129
https://gist.github.com/pjastr/2980fb68a484dcb5ff595774eec4195c
Dodatkowe przykłady:
https://docs.python.org/3.9/library/string.html#format-examples https://gist.github.com/pjastr/d42d937c7b00b80b5dfe309b4ac0e854 https://gist.github.com/pjastr/90f1accf7f54d8c74ac036d59a24a9dd https://gist.github.com/pjastr/adfd7371dcbe4e4034bfb12dcfe30129 https://gist.github.com/pjastr/2980fb68a484dcb5ff595774eec4195c
f-Strings
https://docs.python.org/3.10/reference/lexical_analysis.html#f-strings
podział stałych https://docs.python.org/3.10/library/string.html?highlight=string#module-string
funkcje wbudowane dot. napisów https://docs.python.org/3.10/library/stdtypes.html#string-methods