Data Types
- The function type() yields information on the type of a variable
>>> type(8 / 5)
<class 'float'>
>>> a = 8
>>> b = 5
>>> sum = a + b
>>> type(sum)
<class 'int'>
>>> greeting = "Welcome!"
>>> type(greeting)
<class 'str'>
>>>
- The function dir() yields information on the methods of a specific data type or object
>>> greeting = "Welcome!"
>>> dir(greeting)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__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', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>
Methods
- The function help() yields further information on how to use these methods (press "q" to quit)
>>> greeting = "Welcome!"
>>> dir(greeting)
[..., 'count', ...]
>>> help(greeting.count)
Help on built-in function count:
count(...) method of builtins.str instance
S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of
substring sub in string S[start:end]. Optional
arguments start and end are interpreted as in slice
notation.
(END)
>>> greeting = "Welcome!"
>>> greeting.count('e')
2
>>> greeting.count('e', 2)
1
>>> greeting.count('e', 2, 4)
0
>>> greeting.count('come')
1
Python Strings
- In Python operations with strings of characters can be done in a very convenient way
Given are two strings A = "ABCD" and B = "EFG"
|
Problem |
Python |
Result |
|
Selection of a character |
A[0], B[2] |
"A", "G" |
|
Concatenation |
B + A |
"EFGABCD" |
|
Substring |
A[1:3] |
"BC" |
|
Prefix |
A[:2] |
"AB" |
|
Suffix |
A[1:] |
"BCD" |
Note: Python starts counting from zero!
Python Lists
- Lists contain series of arbitrary values (to be precise: objects)
- Lists are defined by writing the individual values separated by commas inside square brackets
- Lists can contain different data types
>>> l = [1, 2, "ABC", 5]
- One can define empty lists:
>>> l = []
- With append one can add elements to a list
- Lists can be concatenated with the operator +
- Lists can be accessed via index operations in the same way as strings
>>> l = []
>>> l.append(1)
>>> l.append(2)
>>> print(l)
[1, 2]
>>> m = [3, 4, 5]
>>> n = l + m
>>> print(n)
[1, 2, 3, 4, 5]
>>> print(n[2])
3
>>>
- Python has more useful built-in functions
>>> l = [170, 50, 3, 244]
>>> print(min(l))
3
>>> print(max(l))
244
>>> print(sorted(l))
[3, 50, 170, 244]
>>> print(sum(l))
467
>>> print(len(l))
4
>>>