Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
|
coding:python [2025/11/12 22:16] 127.0.0.1 Externe Bearbeitung |
coding:python [2026/03/14 12:39] (aktuell) jango ↷ Links angepasst, weil Seiten im Wiki verschoben wurden |
||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | In Python ist das korrekte Einrücken von Zeilen sehr wichtig! | ||
| + | <code python> | ||
| + | # Ganzzahlen (integers) | ||
| + | zahl = 42 | ||
| + | print(zahl) | ||
| + | |||
| + | # Gleitkommazahlen (float) | ||
| + | pi = 3.14 | ||
| + | print(pi) | ||
| + | |||
| + | # Zeichenketten (strings) | ||
| + | name = " | ||
| + | print(" | ||
| + | |||
| + | # Boolesche Werte (boolean) | ||
| + | wahr = True | ||
| + | falsch = False | ||
| + | print(wahr) | ||
| + | </ | ||
| + | |||
| + | <code ruby> | ||
| + | # Arithmetische Operatoren | ||
| + | a = 5 | ||
| + | b = 2 | ||
| + | |||
| + | summe = a + b | ||
| + | differenz = a - b | ||
| + | produkt = a * b | ||
| + | quotient = a / b | ||
| + | rest = a % b | ||
| + | potenz = a ** b | ||
| + | |||
| + | print(summe, | ||
| + | # Ausgabe: 7 3 10 2.5 1 25 | ||
| + | </ | ||
| + | |||
| + | <code ruby> | ||
| + | # Bedingte Anweisung (if-else) | ||
| + | alter = 18 | ||
| + | |||
| + | if alter >= 18: | ||
| + | print(" | ||
| + | else: | ||
| + | print(" | ||
| + | |||
| + | # Schleife (for-Schleife) | ||
| + | fruits = [" | ||
| + | |||
| + | for fruit in fruits: | ||
| + | print(fruit) | ||
| + | |||
| + | # Schleife (while-Schleife) | ||
| + | counter = 0 | ||
| + | |||
| + | while counter < 5: | ||
| + | print(counter) | ||
| + | counter += 1 | ||
| + | </ | ||
| + | |||
| + | <code ruby> | ||
| + | def greet(name): | ||
| + | print(" | ||
| + | |||
| + | greet(" | ||
| + | # Ausgabe: Hallo, Bob! | ||
| + | </ | ||
| + | |||
| + | =====Strings===== | ||
| + | |||
| + | https:// | ||
| + | |||
| + | <code python> | ||
| + | s = "Hallo welt, ciao" | ||
| + | print len(s) | ||
| + | print s[0:11] # from:to | ||
| + | </ | ||
| + | |||
| + | ACHTUNG: Strings sind immutable!!! (unveränderbar) Man muss Kopien erzeugen | ||
| + | |||
| + | <code python> | ||
| + | a = " | ||
| + | b = " | ||
| + | print(a + b) | ||
| + | |||
| + | s = "Hello world" | ||
| + | s1 = s[5] + " | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | s1 = ' | ||
| + | s2 = ' | ||
| + | print(' | ||
| + | print(' | ||
| + | |||
| + | // assoziativ | ||
| + | test = {' | ||
| + | s = ' | ||
| + | print(s.join(test)) | ||
| + | test = {" | ||
| + | s = ', ' | ||
| + | print(s.join(test)) | ||
| + | </ | ||
| + | |||
| + | =====Arrays===== | ||
| + | |||
| + | In Python nennt man Arrays auch Listen. | ||
| + | |||
| + | <code python> | ||
| + | cars = [] | ||
| + | vw = [] | ||
| + | vw.append(" | ||
| + | cars.append(vw) | ||
| + | audi = [] | ||
| + | audi.append(" | ||
| + | cars.append(audi) | ||
| + | print(cars[0]) | ||
| + | </ | ||
| + | |||
| + | =====Funktionen===== | ||
| + | |||
| + | Build In Funktionen: https:// | ||
| + | |||
| + | <code python> | ||
| + | def find(str, ch): | ||
| + | index = 0 | ||
| + | while index < len(str): | ||
| + | if str[index] == ch: | ||
| + | return index | ||
| + | index = index + 1 | ||
| + | return -1 | ||
| + | </ | ||
| + | |||
| + | Lambda | ||
| + | |||
| + | <code python> | ||
| + | numbers = (1, 2, 3, 4) | ||
| + | result = map(lambda x: x*x, numbers) | ||
| + | print(result) | ||
| + | |||
| + | # converting map object to set | ||
| + | numbersSquare = set(result) | ||
| + | print(numbersSquare) | ||
| + | </ | ||
| + | |||
| + | =====Parameter===== | ||
| + | <code python> | ||
| + | import sys, getopt | ||
| + | |||
| + | def main(argv): | ||
| + | | ||
| + | | ||
| + | try: | ||
| + | opts, args = getopt.getopt(argv," | ||
| + | | ||
| + | print ' | ||
| + | sys.exit(2) | ||
| + | for opt, arg in opts: | ||
| + | if opt == ' | ||
| + | print ' | ||
| + | | ||
| + | elif opt in (" | ||
| + | | ||
| + | elif opt in (" | ||
| + | | ||
| + | print 'Input file is "', | ||
| + | print ' | ||
| + | |||
| + | if __name__ == " | ||
| + | | ||
| + | </ | ||
| + | |||
| + | =====Dateien===== | ||
| + | |||
| + | <code python> | ||
| + | f = open(" | ||
| + | content = f.read() | ||
| + | for line in f.readlines(): | ||
| + | print(line) | ||
| + | f.write(" | ||
| + | f.close() | ||
| + | </ | ||
| + | |||
| + | =====Datenstrukturen===== | ||
| + | |||
| + | ====Set==== | ||
| + | |||
| + | Sets, auch als Mengen bezeichnet, sind eine ungeordnete Sammlung einzigartiger Elemente in Python. Sie eignen sich gut, um Duplikate zu entfernen und mathematische Operationen wie Vereinigung, | ||
| + | |||
| + | <code python> | ||
| + | fruits = {' | ||
| + | fruits.add(' | ||
| + | print(fruits) | ||
| + | </ | ||
| + | |||
| + | ====Dictionary==== | ||
| + | |||
| + | Dictionaries sind eine assoziative Datenstruktur, | ||
| + | |||
| + | <code python> | ||
| + | student = {' | ||
| + | student[' | ||
| + | print(student[' | ||
| + | </ | ||
| + | |||
| + | ====Listen==== | ||
| + | |||
| + | Listen sind eine grundlegende Datenstruktur in Python, die verwendet wird, um eine geordnete Sammlung von Elementen zu speichern. Eine Liste kann Elemente verschiedener Datentypen enthalten und ermöglicht den Zugriff auf die Elemente über deren Position (Index) in der Liste. | ||
| + | |||
| + | <code python> | ||
| + | # Listen erstellen | ||
| + | my_list = [1, 2, 3, 4, 5] | ||
| + | empty_list = [] | ||
| + | |||
| + | my_list.append(6) | ||
| + | my_list.insert(0, | ||
| + | |||
| + | my_list.remove(3) | ||
| + | popped_element = my_list.pop() | ||
| + | |||
| + | print(my_list[0]) | ||
| + | print(my_list[-1]) | ||
| + | |||
| + | length = len(my_list) | ||
| + | |||
| + | sublist = my_list[1: | ||
| + | sublist2 = my_list[: | ||
| + | sublist3 = my_list[2: | ||
| + | |||
| + | reversed_list = my_list[:: | ||
| + | |||
| + | combined_list = my_list + other_list | ||
| + | |||
| + | my_list.sort() | ||
| + | my_list.sort(reverse=True) | ||
| + | </ | ||
| + | |||
| + | Listenkomprehension ist eine elegante Möglichkeit, | ||
| + | |||
| + | <code python> | ||
| + | numbers = [1, 2, 3, 4, 5] | ||
| + | squared_numbers = [x ** 2 for x in numbers] | ||
| + | print(squared_numbers) | ||
| + | </ | ||
| + | |||
| + | =====Regular Expessions===== | ||
| + | |||
| + | Die grundlegende Funktion, um nach Mustern in einem String zu suchen, ist re.search(): | ||
| + | |||
| + | <code python> | ||
| + | import re | ||
| + | |||
| + | text = " | ||
| + | pattern = r"name is (\w+)" | ||
| + | |||
| + | match = re.search(pattern, | ||
| + | if match: | ||
| + | print(" | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | Du kannst re.sub() verwenden, um Muster in einem String zu ersetzen: | ||
| + | |||
| + | <code python> | ||
| + | import re | ||
| + | |||
| + | text = " | ||
| + | pattern = r" | ||
| + | replacement = " | ||
| + | |||
| + | new_text = re.sub(pattern, | ||
| + | print(new_text) | ||
| + | </ | ||
| + | |||
| + | |||
| + | Mit re.findall() kannst du alle Übereinstimmungen eines Musters in einem String extrahieren: | ||
| + | |||
| + | <code python> | ||
| + | import re | ||
| + | |||
| + | text = "Phone numbers: 123-456-7890, | ||
| + | pattern = r" | ||
| + | |||
| + | phone_numbers = re.findall(pattern, | ||
| + | print(phone_numbers) | ||
| + | </ | ||
| + | |||
| + | |||
| + | Du kannst Flags verwenden, um das Verhalten deiner regulären Ausdrücke zu ändern, z.B. für Groß-/ | ||
| + | |||
| + | <code python> | ||
| + | import re | ||
| + | |||
| + | text = " | ||
| + | pattern = r" | ||
| + | matches = re.findall(pattern, | ||
| + | print(matches) | ||
| + | </ | ||
| + | =====Sockets===== | ||
| + | |||
| + | Siehe [[: | ||
| + | |||
| + | <code python> | ||
| + | import socket | ||
| + | |||
| + | HOST = " | ||
| + | PORT = 65432 # Port to listen on (non-privileged ports are > 1023) | ||
| + | |||
| + | with socket.socket(socket.AF_INET, | ||
| + | s.bind((HOST, | ||
| + | s.listen() | ||
| + | conn, addr = s.accept() | ||
| + | with conn: | ||
| + | print(f" | ||
| + | while True: | ||
| + | data = conn.recv(1024) | ||
| + | if not data: | ||
| + | break | ||
| + | conn.sendall(data) | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | import socket | ||
| + | |||
| + | HOST = " | ||
| + | PORT = 65432 # The port used by the server | ||
| + | |||
| + | with socket.socket(socket.AF_INET, | ||
| + | s.connect((HOST, | ||
| + | s.sendall(b" | ||
| + | data = s.recv(1024) | ||
| + | |||
| + | print(f" | ||
| + | </ | ||
| + | |||
| + | =====Binary===== | ||
| + | |||
| + | <code python> | ||
| + | # convert a decimal ip address string into a binary ip address string | ||
| + | def test1(ip): | ||
| + | ipparts = ip.split(" | ||
| + | print( bin(int(ipparts[0]))[2: | ||
| + | |||
| + | # convert a decimal ip address string to Int32 | ||
| + | # x = 192.168.0.13 | ||
| + | # x = {13} + 2**0 * 0 + 2**8 * {0} + 2**16 * {168} + 2**24 * {192} | ||
| + | def test2(ip): | ||
| + | ipparts = ip.split(" | ||
| + | x = int(ipparts[3]) + 2**0 * 0 + 2**8 * int(ipparts[2]) + 2**16 * int(ipparts[1]) + 2**24 * int(ipparts[0]) | ||
| + | print( bin(x)[2: | ||
| + | |||
| + | | ||
| + | test1(" | ||
| + | test2(" | ||
| + | </ | ||
| + | =====Security===== | ||
| + | |||
| + | <code python> | ||
| + | python -c " | ||
| + | </ | ||
| + | |||
| + | =====JSON===== | ||
| + | |||
| + | <code python> | ||
| + | // [{" | ||
| + | import json | ||
| + | with open(" | ||
| + | data = json.load(file) | ||
| + | print(data[0].get(" | ||
| + | </ | ||
| + | |||
| + | =====PyGame===== | ||
| + | |||
| + | Pong | ||
| + | |||
| + | <code python> | ||
| + | import pygame | ||
| + | from pygame.locals import * | ||
| + | |||
| + | # Initialisierung von Pygame | ||
| + | pygame.init() | ||
| + | |||
| + | # Fenstergröße | ||
| + | WIDTH, HEIGHT = 640, 480 | ||
| + | WINDOW_SIZE = (WIDTH, HEIGHT) | ||
| + | |||
| + | # Farben | ||
| + | WHITE = (255, 255, 255) | ||
| + | BLACK = (0, 0, 0) | ||
| + | |||
| + | # Erstellung des Spielfensters | ||
| + | window = pygame.display.set_mode(WINDOW_SIZE) | ||
| + | pygame.display.set_caption(" | ||
| + | |||
| + | # Spieler | ||
| + | PLAYER_WIDTH, | ||
| + | player1_pos = pygame.Rect(50, | ||
| + | player2_pos = pygame.Rect(WIDTH - 50 - PLAYER_WIDTH, | ||
| + | player1_speed = 0 | ||
| + | player2_speed = 0 | ||
| + | |||
| + | # Ball | ||
| + | BALL_RADIUS = 10 | ||
| + | ball_pos = pygame.Rect(WIDTH/ | ||
| + | ball_speed_x = 3 | ||
| + | ball_speed_y = 3 | ||
| + | |||
| + | # Spiel-Loop | ||
| + | running = True | ||
| + | clock = pygame.time.Clock() | ||
| + | |||
| + | while running: | ||
| + | # Hintergrund zeichnen | ||
| + | window.fill(BLACK) | ||
| + | | ||
| + | for event in pygame.event.get(): | ||
| + | if event.type == QUIT: | ||
| + | running = False | ||
| + | elif event.type == KEYDOWN: | ||
| + | if event.key == K_w: | ||
| + | player1_speed = -3 | ||
| + | elif event.key == K_s: | ||
| + | player1_speed = 3 | ||
| + | elif event.key == K_UP: | ||
| + | player2_speed = -3 | ||
| + | elif event.key == K_DOWN: | ||
| + | player2_speed = 3 | ||
| + | elif event.type == KEYUP: | ||
| + | if event.key == K_w or event.key == K_s: | ||
| + | player1_speed = 0 | ||
| + | elif event.key == K_UP or event.key == K_DOWN: | ||
| + | player2_speed = 0 | ||
| + | | ||
| + | # Spieler bewegen | ||
| + | player1_pos.y += player1_speed | ||
| + | player2_pos.y += player2_speed | ||
| + | | ||
| + | # Kollision mit Fenster-Rändern überprüfen | ||
| + | if player1_pos.top <= 0: | ||
| + | player1_pos.top = 0 | ||
| + | if player1_pos.bottom >= HEIGHT: | ||
| + | player1_pos.bottom = HEIGHT | ||
| + | if player2_pos.top <= 0: | ||
| + | player2_pos.top = 0 | ||
| + | if player2_pos.bottom >= HEIGHT: | ||
| + | player2_pos.bottom = HEIGHT | ||
| + | | ||
| + | # Ball bewegen | ||
| + | ball_pos.x += ball_speed_x | ||
| + | ball_pos.y += ball_speed_y | ||
| + | | ||
| + | # Kollision mit Schlägern überprüfen | ||
| + | if ball_pos.colliderect(player1_pos) or ball_pos.colliderect(player2_pos): | ||
| + | ball_speed_x *= -1 | ||
| + | if ball_pos.top <= 0 or ball_pos.bottom >= HEIGHT: | ||
| + | ball_speed_y *= -1 | ||
| + | | ||
| + | # Ball-Ausrichtung umkehren, wenn das Spielfeld verlassen wird | ||
| + | if ball_pos.left <= 0 or ball_pos.right >= WIDTH: | ||
| + | ball_pos.x = WIDTH/2 - BALL_RADIUS/ | ||
| + | ball_pos.y = HEIGHT/2 - BALL_RADIUS/ | ||
| + | ball_speed_x *= -1 | ||
| + | | ||
| + | # Objekte zeichnen | ||
| + | pygame.draw.rect(window, | ||
| + | pygame.draw.rect(window, | ||
| + | pygame.draw.ellipse(window, | ||
| + | | ||
| + | # Fenster aktualisieren | ||
| + | pygame.display.flip() | ||
| + | clock.tick(60) | ||
| + | |||
| + | # Pygame beenden | ||
| + | pygame.quit() | ||
| + | </ | ||
| + | |||
| + | =====Tensorflow===== | ||
| + | |||
| + | Siehe [[: | ||
| + | |||
| + | <code python> | ||
| + | # -*- coding: utf-8 -*- | ||
| + | |||
| + | import tensorflow as tf | ||
| + | import numpy as np | ||
| + | from tensorflow.keras.models import Sequential | ||
| + | from tensorflow.keras.layers import Dense | ||
| + | from tensorflow.keras.optimizers import Adam | ||
| + | |||
| + | # Beispiel-Datensatz | ||
| + | sentences = [ | ||
| + | "Ich liebe diesen Film!", | ||
| + | "Das Essen war köstlich.", | ||
| + | "Der Service war schrecklich.", | ||
| + | "Der Film war enttäuschend.", | ||
| + | "Ich kann dieses Restaurant nicht empfehlen.", | ||
| + | "Ich gehe dort nie wieder hin", | ||
| + | "Das Konzert war fantastisch.", | ||
| + | "Der Urlaub war wunderbar.", | ||
| + | "Der Kundenservice war großartig." | ||
| + | ] | ||
| + | |||
| + | labels = np.array([1, | ||
| + | |||
| + | # Tokenisierung und Vektorisierung der Sätze | ||
| + | tokenizer = tf.keras.preprocessing.text.Tokenizer() | ||
| + | tokenizer.fit_on_texts(sentences) | ||
| + | sequences = tokenizer.texts_to_sequences(sentences) | ||
| + | word_index = tokenizer.word_index | ||
| + | max_length = max([len(seq) for seq in sequences]) | ||
| + | padded_sequences = tf.keras.preprocessing.sequence.pad_sequences(sequences, | ||
| + | |||
| + | # Modell erstellen | ||
| + | model = Sequential() | ||
| + | model.add(Dense(16, | ||
| + | model.add(Dense(1, | ||
| + | |||
| + | # Modell kompilieren | ||
| + | model.compile(loss=" | ||
| + | |||
| + | # Modell trainieren | ||
| + | model.fit(padded_sequences, | ||
| + | |||
| + | # Neue Sätze vorhersagen | ||
| + | new_sentences = [ | ||
| + | "Der Film hat mir gut gefallen.", | ||
| + | "Es war ein schrecklicher Abend.", | ||
| + | "Die sehen mich nie wieder", | ||
| + | "Das Konzert war erstaunlich gut." | ||
| + | ] | ||
| + | new_sequences = tokenizer.texts_to_sequences(new_sentences) | ||
| + | new_padded_sequences = tf.keras.preprocessing.sequence.pad_sequences(new_sequences, | ||
| + | predictions = model.predict(new_padded_sequences) | ||
| + | |||
| + | for sentence, prediction in zip(new_sentences, | ||
| + | sentiment = " | ||
| + | print(f" | ||
| + | print(f" | ||
| + | print() | ||
| + | </ | ||
| + | |||
| + | =====Torch===== | ||
| + | |||
| + | Siehe [[: | ||
| + | |||
| + | =====Transformers===== | ||
| + | |||
| + | Siehe [[: | ||
| + | =====Links===== | ||
| + | |||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||