Last active
January 4, 2016 15:59
-
-
Save horstjens/8644373 to your computer and use it in GitHub Desktop.
python3 Schulung
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 + 1 | |
100 – 1 | |
100 / 7 | |
100 / 7.0 | |
100 % 7 | |
100 * 4 | |
100 ** 2 | |
100 ** 0.5 | |
(100+1) * 2 | |
100 + 1 * 2 | |
'python' + 'cool' | |
'python' * 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
random.randint(1,6) | |
random.randint(1,100) | |
random.random() | |
random.choice([1,2,3]) | |
random.choice(“abcdefg“) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
for x in range(66): | |
turtle.circle(x*2) | |
turtle.left(13) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#iterate over 2 lists | |
#multiply the numbers of 1 – 10 with each other | |
print('---- start ----') | |
for a in range(1,11): | |
print('---',a,'---') | |
for b in range(1,11): | |
print(a,' x ',b,' = ',a * b) | |
print('---- end ---') | |
#task: make a division table | |
# make pretty format | |
# "bla bla bla {:.2f} bla bla bla".format(result) | |
# division: / |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
result = 0 | |
trials = 0 | |
while result != 6: | |
trials = trials + 1 | |
result = random.randint(1,6) | |
print(trials, result) | |
if trials > 100: | |
break | |
print("i do not loop anymore") | |
#task: create random numbers between 1 and 100, loop until result is 33 or result is 66 | |
#task: ask the user how large the random numbers should be | |
#task: ask the user for up to three exit numbers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print('i will square your numbers') | |
counter = 0 | |
history = [] # empty list | |
while True: | |
x = input("enter a new positive number") | |
i = int(x) | |
if i < 0: | |
print('this was a negative number') | |
break | |
# check if the number is new | |
if i in history: | |
print('old number. try again.') | |
continue | |
# it is a new, positive number | |
print('the square of', i, 'is', i*i) | |
history.append(i) # add x to history | |
print('---- end ----') | |
print(history) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# buchstabencounter | |
# zählt buchstaben eines Strings | |
eingabe = "" | |
while len(eingabe) < 1: | |
eingabe = input("bitte gib einen langen Text ein:") | |
alphabet = {} # leeres dictionary | |
for buchstabe in eingabe: | |
if buchstabe in alphabet: | |
alphabet[buchstabe] += 1 # häufigkeit um eins erhöhen | |
else: | |
alphabet[buchstabe] = 1 # brandneuer buchstabe | |
print("bin fertig") | |
sortedkeys = list(alphabet.keys()) | |
sortedkeys.sort() | |
print("Häufigkeit der Buchstaben, alphabetisch sortiert") | |
for k in sortedkeys: | |
print(k, alphabet[k]) | |
# task: herausfinden welche buchstaben NICHT vorkommen | |
alles="abcdefghijklmnopqrstuvwxyzöäüß" | |
print("Buchstaben die nicht vorgekommen sind:") | |
for buchstabe in alles: | |
if buchstabe not in eingabe: | |
print("{} kam nicht vor".format(buchstabe)) | |
# task: buchstaben sortieren nach Häufigkeit | |
print("Buchstaben sortiert nach Häufigkeit:") | |
for buchstabe in sorted(alphabet, key=alphabet.get, reverse=True): | |
print(buchstabe, alphabet[buchstabe]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print('i will divide two numbers') | |
a = input('enter first number') | |
b = input('enter second number') | |
print('i try now to divide',a,'by',b) | |
try: | |
result = int(a) / int(b) # 3 mögliche Fehler: int(a), int(b), division durch 0. am besten in 3 verschiedene try-blöcke aufteilen | |
print(result) # sollte nicht im try block stehen, weil hier nichts schiefgehen kann | |
except: | |
print('sorry i could not calculate that') | |
print('bye-bye') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gutenmorgen(wer="Niemand"): | |
if wer=="Horst": | |
print("guten Morgen, Horst") | |
elif wer == "Sara": | |
print("Cafe e pronto, amore") | |
else: | |
print("einen schönen guten Morgen, oh Fremdling") | |
name=input("Bitte gib Deinen Namen ein:") | |
gutenmorgen(name) # call the function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spaceliste = [" Raumschiff ", "Donald Duck", " Micky Mouse", "Donald ", " ente "] | |
print(spaceliste) # hat unnötige spaces vor und hinter den Wörtern | |
shortliste = [x.strip() for x in spaceliste] | |
print(shortliste) # bereinigte liste |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bug(object): | |
"""Eine Programmfehler mit diversen Eigenschaften und Methoden""" | |
anzahl = 0 # dies ist ein class attribut | |
def __init__(self, nummer): | |
self.nummer = nummer | |
Bug.anzahl += 1 # class attribute verändern | |
self.seriennummer = Bug.anzahl # zugriff auf class attribute | |
self.autor = "Horst" | |
self.beschreibung = "unbekannter Programmierfehler" | |
def report(self): | |
txt = "ich bin Bug Nr. {}\n".format(self.nummer) | |
txt+= "meine Seriennummer = {}\n".format(self.seriennummer) | |
txt+= "Autor: {}\n{}".format(self.autor, self.beschreibung) | |
return txt # kein self.txt weil nicht dauerhaft gepeichert | |
bugliste = [Bug(111), Bug(3524), Bug(33), Bug(5055), Bug(33)] | |
#Seriennummer ist unique, nummer nicht, da von Mensch vergeben | |
for bug in bugliste: | |
print(bug.report()) | |
print("es wurden bisher {} bugs erzeugt".format(Bug.anzahl)) | |
# wäre auch gegangen mit len(bugliste) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Datenbank(object): | |
"""eine Klasse ohne Instanzen zum Austasch von Daten""" | |
bugdict = {} # key: serial number, value instanz (Bug) | |
class Bug(object): | |
"""Eine Programmfehler mit diversen Eigenschaften und Methoden""" | |
anzahl = 0 # dies ist ein class attribut | |
def __init__(self, nummer): | |
Bug.anzahl += 1 # class attribute verändern | |
self.seriennummer = Bug.anzahl # zugriff auf class attribute | |
Datenbank.bugdict[self.seriennummer] = self # ich als Instanz lebe in Datenbank.bugliste | |
self.nummer = nummer | |
self.autor = "Horst" | |
self.beschreibung = "unbekannter Programmierfehler" | |
def report(self): | |
txt = "---------\n" | |
txt += "ich bin Bug Nr. {}\n".format(self.nummer) | |
txt += "meine Seriennummer = {}\n".format(self.seriennummer) | |
txt += "Autor: {}\n{}\n".format(self.autor, self.beschreibung) | |
return txt # kein self.txt weil nicht dauerhaft gepeichert | |
class DocumentationBug(Bug): | |
"""eine spezielle Art von Bug, eine Kind-Klasse der Klasse Bug""" | |
def __init__(self, nummer): | |
Bug.__init__(self, nummer) # erzeuge ein Elternobjeket | |
self.dokument = "handbuch.txt" | |
self.kapitel = 0 | |
self.seite = 0 | |
def report(self): | |
txt = Bug.report(self) | |
txt += "dokument: {} kapitel: {} seite: {}\n".format(self.dokument, | |
self.kapitel, self.seite) | |
return txt | |
Bug(1234) | |
Bug(4567) | |
DocumentationBug(6666) | |
DocumentationBug(999) | |
Bug(919) | |
#Seriennummer ist unique, nummer nicht, da von Mensch vergeben | |
#docbugs = [DocumentationBug(333)] | |
for serialnr in Datenbank.bigdict: | |
print(Datenbank.bugdict[seriealnr].report()) | |
Datenbank.bugdict[serialnr].beschreibung = "abc def ghi jkl" | |
print("es wurden bisher {} bugs erzeugt".format(Bug.anzahl)) | |
# wäre auch gegangen mit len(bugliste) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment