23
loading...
This website collects cookies to deliver better user experience
python
in your terminal. Any Python code that starts with >>>
symbols indicated that it was typed into a REPL.type()
, dir()
, and help()
.>>> name = "Lennart"
>>> type(name)
<class 'str'>
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> help(str.title)
Help on method_descriptor:
title(self, /)
Return a version of the string where each word is titlecased.
More specifically, words start with uppercased characters and all remaining
cased characters have lower case.
Number
.>>> x = 4
>>> y = 5.0
>>> z = 42j
>>> type(x)
<class 'int'>
>>> type(y)
<class 'float'>
>>> type(z)
<class 'complex'>
set
and dict
. Unpacking is a way of quickly getting information from a tuple:>>> favourite_food = ("Italian", "Pizza")
>>> cuisine, name = favourite_food
>>> cuisine
'Italian'
>>> name
'Pizza'
list
, set
or dict
) in it. A set can only contain unique items.>>> names = ["Luke", "Leia", "Malak", "Luke"]
>>> set(names)
{'Leia', 'Luke', 'Malak'}
.add
or .discard
etc.) but I definitely found .update
interesting since you can add multiple values at once:>>> chars = {"James", "Naomi", "Amos"}
>>> addition = {"Alex", "Julie"}
>>> chars.update(addition)
>>> chars
{'Julie', 'James', 'Naomi', 'Alex', 'Amos'}
set
operations -- union and intersection:>>> chars = {"James", "Naomi", "Amos"}
>>> favourite_chars = {"James"}
>>> chars | addition
{'Julie', 'James', 'Naomi', 'Alex', 'Amos'}
>>> chars & favourite_chars
{'James'}
keys()
, values()
, items()
:>>> chars = { "expanse": "Holden", "star_wars": "Luke" }
>>> chars.keys()
dict_keys(['expanse', 'star_wars'])
>>> chars.values()
dict_values(['Holden', 'Luke'])
>>> chars.items()
dict_items([('expanse', 'Holden'), ('star_wars', 'Luke')])
>>> for franchise, char in chars.items():
... print(f"The char {char} exists in the franchise {franchise}")
...
The char Holden exists in the franchise expanse
The char Luke exists in the franchise star_wars
items()
returns a list of tuples one can use tuple unpacking to get both values.