01. Jupyter Notebook¶
ehemalige Veranstaltungen ia3.netz + ia3.data sowie ia4.Netz im Studiengang Interaktive Medien
(c) 2020/2021 Hochschule Augsburg - Fakultät für Informatik - Prof.Dr.Nik Klever
In [1]:
8+9
Out[1]:
17
In [5]:
a = 2*7
In [6]:
a
Out[6]:
14
In [9]:
print(a)
14
In [10]:
type(a)
Out[10]:
int
In [11]:
import os
In [12]:
getpid()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-12-c344a67d685c> in <module>() ----> 1 getpid() NameError: name 'getpid' is not defined
In [13]:
os.getpid()
Out[13]:
7016
In [14]:
os.getcwd()
Out[14]:
'C:\\Users\\nikkl'
In [15]:
x=os.getpid()
print(type(x))
<class 'int'>
In [16]:
if True:
print(True)
True
In [17]:
if True:
print(False)
File "<ipython-input-17-e69bf44c797e>", line 2 print(False) ^ IndentationError: expected an indented block
In [18]:
if True:
print(1)
print(4)
print("tab")
1 4 tab
In [19]:
help
Out[19]:
Type help() for interactive help, or help(object) for help about object.
In [22]:
help()
Welcome to Python 3.7's help utility! If this is your first time using Python, you should definitely check out the tutorial on the Internet at https://docs.python.org/3.7/tutorial/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as "spam", type "modules spam". help> quit You are now leaving help and returning to the Python interpreter. If you want to ask for help on a particular object directly from the interpreter, you can type "help(object)". Executing "help('string')" has the same effect as typing a particular string at the help> prompt.
In [23]:
dir()
Out[23]:
['In', 'Out', '_', '_1', '_10', '_13', '_14', '_19', '_21', '_4', '_6', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_exit_code', '_i', '_i1', '_i10', '_i11', '_i12', '_i13', '_i14', '_i15', '_i16', '_i17', '_i18', '_i19', '_i2', '_i20', '_i21', '_i22', '_i23', '_i3', '_i4', '_i5', '_i6', '_i7', '_i8', '_i9', '_ih', '_ii', '_iii', '_oh', 'a', 'exit', 'get_ipython', 'os', 'quit', 'x']
In [24]:
dir(a)
Out[24]:
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
In [25]:
a.bit_length()
Out[25]:
4
In [26]:
dir(os)
Out[26]:
['DirEntry', 'F_OK', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']
In [27]:
help(os.getcwd)
Help on built-in function getcwd in module nt: getcwd() Return a unicode string representing the current working directory.
In [28]:
dir(os.getcwd)
Out[28]:
['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__text_signature__']
In [30]:
text = "Dies" + " ist " + " ein " + "Satz"
In [31]:
text.find("ist")
Out[31]:
5
In [32]:
text[5:]
Out[32]:
'ist ein Satz'
In [33]:
text.replace("ist","war")
Out[33]:
'Dies war ein Satz'
In [34]:
text
Out[34]:
'Dies ist ein Satz'
In [35]:
text = text.replace("ist","war")
In [36]:
text
Out[36]:
'Dies war ein Satz'
In [39]:
textliste = text.split()
textliste
Out[39]:
['Dies', 'war', 'ein', 'Satz']
In [40]:
"-".join(textliste)
Out[40]:
'Dies-war-ein-Satz'
In [41]:
"Dies ist ein Satz"
Out[41]:
'Dies ist ein Satz'
In [42]:
'Dies ist ein Satz'
Out[42]:
'Dies ist ein Satz'
In [43]:
"Nik's Text"
Out[43]:
"Nik's Text"
In [44]:
'Nik\'s Text'
Out[44]:
"Nik's Text"
In [46]:
langertext = """Dies ist ein
Text über mehrere
Zeilen
"""
In [47]:
str(langertext)
Out[47]:
'Dies ist ein\nText über mehrere\nZeilen\n'
In [48]:
print(langertext)
Dies ist ein Text über mehrere Zeilen
In [49]:
repr(langertext)
Out[49]:
"'Dies ist ein\\nText über mehrere\\nZeilen\\n'"
In [50]:
print(str(langertext))
Dies ist ein Text über mehrere Zeilen
In [51]:
print(repr(langertext))
'Dies ist ein\nText über mehrere\nZeilen\n'
In [52]:
datei = open("test.txt","w")
datei.write(langertext)
datei.close()
In [53]:
datei = open("test.txt","r")
gesamterInhalt = datei.read()
datei.close()
print(gesamterInhalt)
Dies ist ein Text über mehrere Zeilen
In [54]:
a = 4*3.5
type(a)
Out[54]:
float
In [55]:
try:
dateiname = "irgendeinName.irgendwas"
datei = open(dateiname,"r")
except IOError, e:
print(dateiname,"nicht verfügbar")
File "<ipython-input-55-692b18f84830>", line 4 except IOError, e: ^ SyntaxError: invalid syntax
In [58]:
try:
dateiname = "irgendeinName.irgendwas"
datei = open(dateiname,"r")
except IOError as e:
print(e.errno,e.strerror)
print(dateiname,"nicht verfügbar")
2 No such file or directory irgendeinName.irgendwas nicht verfügbar
In [59]:
a = int(3.141)
In [60]:
a
Out[60]:
3
In [61]:
a = int(3.6789)
a
Out[61]:
3
In [62]:
a = float("3.141")
a
Out[62]:
3.141
In [63]:
a = float("3.45€")
a
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-63-bc731ef2a1a1> in <module>() ----> 1 a = float("3.45€") 2 a ValueError: could not convert string to float: '3.45€'