Skip to content
TheChatArchiveThe Chat Archive
o4-mini-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

Python programming language cheat sheet.

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro’s answer.
  1. User

    Python programming language cheat sheet.

  2. Assistant

    🐍 Python Cheat Sheet


    1. Variables & Data Types

    TypeLiteral ExampleNotes
    Integerx = 42Arbitrary precision
    Floaty = 3.14Double precision
    Strings = "hello"Unicode
    Booleanflag = TrueTrue / False
    Listlst = [1,2,3]Mutable, ordered
    Tuplet = (1,2,3)Immutable, ordered
    Setst = {1,2,3}Mutable, unordered, unique
    Dictd = {"a":1,"b":2}Mutable, key→value mapping
    a, b, c = 1, 2.0, "three"      # multiple assignment
    

    2. Basic Operators

    CategoryOperatorExample
    Arithmetic+ - * / // % **5**2 → 25
    Comparison== != < > <= >=3 < 5 → True
    Logicand or not
    Membershipin, not in"a" in "cat"

    3. Control Flow

    # If-elif-else
    if x > 0:
        print("Positive")
    elif x == 0:
        print("Zero")
    else:
        print("Negative")
    
    # For loop
    for i in range(5):    # 0,1,2,3,4
        print(i)
    
    # While loop
    while cond:
        # …
        break
    

    4. Functions & Lambdas

    def greet(name, greeting="Hi") -> str:
        """Return a greeting message."""
        return f"{greeting}, {name}!"
    
    square = lambda x: x*x
    

    5. List / Dict Comprehensions

    # List of squares
    squares = [x*x for x in range(10)]
    
    # Filter evens
    evens = [x for x in range(20) if x%2==0]
    
    # Dict mapping
    d = {x: x*x for x in range(5)}  # {0:0,1:1,…}
    

    6. Classes & OOP

    class Person:
        def __init__(self, name, age):
            self.name = name
            self.age  = age
    
        def __str__(self):
            return f"{self.name}, {self.age} yrs"
    
    # Usage
    p = Person("Alice", 30)
    print(p)   # Alice, 30 yrs
    

    7. Modules & Imports

    import math
    import os, sys
    from datetime import datetime
    import numpy as np
    

    8. File I/O

    # Read
    with open("in.txt","r") as f:
        text = f.read()
    
    # Write
    with open("out.txt","w") as f:
        f.write("Hello\n")
    

    9. Exception Handling

    try:
        1/0
    except ZeroDivisionError as e:
        print("Error:", e)
    finally:
        print("Cleanup")
    

    10. Common Built-ins & Methods

    Built-inPurpose
    len(x)length
    type(x)type of x
    sum(iter)sum of numbers
    min/max(iter)min or max
    sorted(iter)returns sorted list
    enumerate(x)(index, value) pairs
    zip(a,b,…)aggregate iterables

    String Methods

    • .lower(), .upper(), .split(), .join(), .strip(), .replace()

    List Methods

    • .append(), .extend(), .insert(idx,val), .pop([idx]), .remove(val), .sort(), .reverse()

    Dict Methods

    • .keys(), .values(), .items(), .get(key,default), .update(other_dict)

    ✨ Keep this cheat sheet handy for rapid reference! Happy coding! 🚀

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.