Python table example
import os y = [2, 3,4,5,6,7,8,9,10,11,12] x = [1, 2, 3,4,5,6,7,8,9,10,11,12] for a in y: for b in x: print(a," x ",b," = ",a*b) print()
Defining Functions:
def first2items(list1): return list1[0], list1[1] a, b = first2items(["Hello", "world", "hi", "universe"]) print a + " " + b
Calendar:
import calendar def display_calendar(year, month): cal = calendar.monthcalendar(year, month) # Header print(f"Calendar for {calendar.month_name[month]} {year}") print("Mo Tu We Th Fr Sa Su") # Display the calendar for week in cal: week_str = " ".join(str(day) if day != 0 else " " for day in week) print(week_str) # Example: Display the calendar for March 2024 display_calendar(2024, 3)
Calculator:
def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): if y != 0: return x / y else: return "Cannot divide by zero" def calculator(): while True: # Display menu print("\nCalculator Menu:") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") print("5. Exit") choice = input("Enter your choice (1-5): ") if choice == '5': print("Exiting the calculator. Goodbye!") break try: num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) except ValueError: print("Invalid input. Please enter valid numbers.") continue if choice == '1': result = add(num1, num2) print(f"{num1} + {num2} = {result}") elif choice == '2': result = subtract(num1, num2) print(f"{num1} - {num2} = {result}") elif choice == '3': result = multiply(num1, num2) print(f"{num1} * {num2} = {result}") elif choice == '4': result = divide(num1, num2) print(f"{num1} / {num2} = {result}") else: print("Invalid choice. Please enter a number between 1 and 5.") if __name__ == "__main__": calculator()
Display memory in hexa:
import struct def display_memory_in_hex(memory_block): # Create a memory view object memory_view = memoryview(memory_block) # Display hexadecimal representation print("Hexadecimal representation:") hex_output = " ".join(f"{byte:02X}" for byte in memory_view) print(hex_output) # Display ASCII representation print("\nASCII representation:") ascii_output = "".join(chr(byte) if 32 <= byte <= 126 else '.' for byte in memory_view) print(ascii_output) # Example: Create a bytes object (memory block) with some data data = b"Hello, Memory!" # Display the memory content in hexadecimal and ASCII formats display_memory_in_hex(data)
Encrypter/decrypter of mesage:
def encrypt(message, shift): encrypted_message = "" for char in message: if char.isalpha(): offset = ord('A') if char.isupper() else ord('a') encrypted_char = chr((ord(char) - offset + shift) % 26 + offset) encrypted_message += encrypted_char else: encrypted_message += char return encrypted_message def decrypt(encrypted_message, shift): return encrypt(encrypted_message, -shift) # Example usage: original_message = "Hello, World!" shift_amount = 3 # Encrypt the message encrypted_message = encrypt(original_message, shift_amount) print(f"Encrypted: {encrypted_message}") # Decrypt the message decrypted_message = decrypt(encrypted_message, shift_amount) print(f"Decrypted: {decrypted_message}")
FTP server (before: pip install pyftpdlib):
from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandler from pyftpdlib.servers import FTPServer def start_ftp_server(): # Create an authorizer for user authentication and permissions authorizer = DummyAuthorizer() authorizer.add_user("user", "password", "/your/folder", perm="elradfmw") # Create an FTP handler with the provided authorizer handler = FTPHandler handler.authorizer = authorizer # Create an FTP server with the provided handler and bind it to the specified address and port server = FTPServer(("127.0.0.1", 21), handler) print("FTP Server started on 127.0.0.1:21") # Start the FTP server server.serve_forever() if __name__ == "__main__": start_ftp_server()