Basic#
This page focuses on the basic python
datatypes, their properties and methods. Check official python documentation on it.
str#
Here is the list of str
methods:
Method |
Description |
---|---|
|
Converts the first character to uppercase and the rest to lowercase. |
|
Converts the string to lowercase, more aggressive than |
|
Centers the string in a field of given width, padding with spaces. |
|
Returns the number of non-overlapping occurrences of a substring. |
|
Encodes the string to the specified encoding. |
|
Checks if the string ends with the specified suffix. |
|
Replaces all tab characters with spaces. |
|
Returns the lowest index of a substring, or -1 if not found. |
|
Formats the string using placeholders. |
|
Formats the string using a mapping object. |
|
Like |
|
Checks if all characters are alphanumeric. |
|
Checks if all characters are alphabetic. |
|
Checks if all characters are ASCII. |
|
Checks if all characters are digits. |
|
Checks if all characters are decimal characters. |
|
Checks if the string is a valid Python identifier. |
|
Checks if all characters are lowercase. |
|
Checks if all characters are numeric. |
|
Checks if all characters are printable. |
|
Checks if all characters are whitespace. |
|
Checks if the string follows title case. |
|
Checks if all characters are uppercase. |
|
Joins elements of an iterable with the string as the separator. |
|
Left-aligns the string in a field of given width. |
|
Converts the string to lowercase. |
|
Removes leading characters (default: whitespace). |
|
Returns a translation table for |
|
Splits the string into a tuple at the first occurrence of a separator. |
|
Replaces occurrences of a substring with another string. |
|
Returns the highest index of a substring, or -1 if not found. |
|
Like |
|
Right-aligns the string in a field of given width. |
|
Splits the string into a tuple at the last occurrence of a separator. |
|
Splits the string from the right. |
|
Removes trailing characters (default: whitespace). |
|
Splits the string into a list using a separator. |
|
Splits the string at line breaks. |
|
Checks if the string starts with the specified prefix. |
|
Removes leading and trailing characters (default: whitespace). |
|
Swaps uppercase characters to lowercase and vice versa. |
|
Converts the string to title case. |
|
Translates the string using a given translation table. |
|
Converts the string to uppercase. |
|
Pads the string with zeros on the left to fill the given width. |
For more details check:
Specific page about str datatype.
Corresponding section of the official documentation.
list#
To read more check specific page.
Is a collection datatype that allows items to be added to and removed from it. Lists are ordered.
The following example uses all the named properties of the list to move the element one at a time.
show_lst = [i for i in range(20)]
for i in range(10):
print(show_lst)
show_lst.append(show_lst.pop(0))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1]
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2]
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4]
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4, 5]
[7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4, 5, 6]
[8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4, 5, 6, 7]
[9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4, 5, 6, 7, 8]
dict#
A data type that implements key/value relationships between objects. Modern implementations make it orderly.
Find out more at the specific page.
The following example corresponds to each symbol in ASCII code. Note that the elements follow the order in which they were generated.
print({chr(i): i for i in range(ord("a"), ord("z")+1)})
{'a': 97, 'b': 98, 'c': 99, 'd': 100, 'e': 101, 'f': 102, 'g': 103, 'h': 104, 'i': 105, 'j': 106, 'k': 107, 'l': 108, 'm': 109, 'n': 110, 'o': 111, 'p': 112, 'q': 113, 'r': 114, 's': 115, 't': 116, 'u': 117, 'v': 118, 'w': 119, 'x': 120, 'y': 121, 'z': 122}
tuple#
Is ordred collection of the elements that can’t be changed.
my_tuple = ("a", "c", "b")
# You can acess particular element
print("Value at index 0 -", my_tuple[0])
# You can't change value of the particular element
try: my_tuple[0] = 3
except Exception as e: print("Got exception:", e)
Value at index 0 - a
Got exception: 'tuple' object does not support item assignment
Note To create an element tuple, you must use the syntax (<element>,)
(comma after element). It was necessary to make a distinction with the operators ()
that define the order of the operations.
The following example shows two almost identical expressions, but one uses tuples and the other uses only arithmetic operations.
print("No commas -", (3+5) * 3)
print("With commas -", (3+5,) * 3)
No commas - 24
With commas - (8, 8, 8)
set#
Is mutable unordred collection. The main feature of this type is that it can contain only unique elements. Find out more on the specific page.
The following example shows the creation of a set of random characters. Even though 100 items have been generated, only unique items are in the collection and they are in alphabetical order, which shows that the collection is unordered - it doesn’t follow the order specified by the developer.
from random import randint
{chr(randint(97, 107)) for i in range(100)}
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'}
frozenset?
Init signature: frozenset(self, /, *args, **kwargs)
Docstring:
frozenset() -> empty frozenset object
frozenset(iterable) -> frozenset object
Build an immutable unordered collection of unique elements.
Type: type
Subclasses:
frozenset#
set
in Python is a mutable data type, so you cannot use it in many cases (e.g. as a key for dict
or as an element of another set
). To fix this, Python provides a special data type frozenset
which is like set
but immutable.
So in the following example I try to use frozenset
and set
as elements of another set
:
With
frozenset
all is well;Regular
set
causes an error.
regular_set = {1,3}
frozen_set = frozenset([1,2,3,4])
print("Using a frozenset:", {1,2,3, frozen_set})
try:
print("Using a regular set:", {1, 2, 3, regular_set, 1})
except Exception as e:
print("Got exception:", e)
Using a frozenset: {1, 2, 3, frozenset({1, 2, 3, 4})}
Got exception: unhashable type: 'set'