:::: Python Cheat Sheet ::::

 Python Cheat Sheet – QA + Interview Edition

A Python data types and functions cheat sheet provides a concise summary of fundamental concepts for quick reference. These resources typically cover:
Python Data Types:
  • Numeric Types: int (integers), float (floating-point numbers), complex (complex numbers).
  • Sequence Types:
    • str (strings): Immutable sequences of characters.
    • list: Mutable ordered sequences of items.
    • tuple: Immutable ordered sequences of items.
  • Mapping Type: dict (dictionaries): Mutable collections of key-value pairs.
  • Set Types: set (mutable unordered collections of unique items), frozenset (immutable version of set).
  • Boolean Type: bool (Boolean values: True or False).
Key Functions and Operations:
  • Type Conversion Functions: int()float()str()list()tuple()dict()set().
  • Built-in Functions: print()len()type()input()range()sum()min()max()abs().
  • String Methods: .upper().lower().strip().split().join().replace().
  • List Methods: .append().extend().insert().remove().pop().sort().reverse().
  • Dictionary Methods: .keys().values().items().get().
  • Defining Functions: Syntax for creating custom functions using def.
Control Flow:
  • Conditional Statements: ifelifelse.
  • Loops: for loops (iterating over sequences), while loops (repeating based on a condition).


�� Python Data Types

· - int, float, str, bool – Basic scalar types

· - list – Ordered, mutable

· - tuple – Ordered, immutable

· - set – Unordered, no duplicates

· - dict – Key-value pairs

⚙️ Python Basics

· - def greet(name): return f"Hello, {name}"

· - *args – Variable positional arguments

· - **kwargs – Variable keyword arguments

· - Lambda: lambda x: x ** 2

· - List comprehension: [x*x for x in range(5)]

�� Loops & Conditionals

· - for item in items:
    if item % 2 == 0:
        print("Even")
    else:
        continue

�� String Handling

· - text = " Hello "
text.strip(), text.upper(), '-'.join(["a", "b", "c"])

��️ File Handling

· - with open('data.txt', 'r') as f:
    lines = f.readlines()

· - with open('out.txt', 'w') as f:
    f.write("Sample text")

�� Unit Testing with Pytest

· - def add(x, y): return x + y

def test_add():
    assert add(2, 3) == 5

· - Run with: pytest test_file.py

�� Networking Automation

· - SSH with Paramiko:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("192.168.1.1", username="admin", password="admin")
stdin, stdout, stderr = ssh.exec_command("show ip route")
print(stdout.read().decode())

· - Run CLI with subprocess:
import subprocess
subprocess.run(['ping', '-c', '4', '8.8.8.8'])

· - API testing with requests:
import requests
response = requests.get("https://api.example.com/data")
print(response.json())

�� Regex Snippets

· - import re

· - MAC: re.findall(r'(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}', text)

· - IP: re.findall(r'\b(?:\d{1,3}\.){3}\d{1,3}\b', log)

· - Email: re.match(r'[\w.-]+@[\w.-]+\.\w+', 'test@email.com')

�� Useful Libraries for QA

· - pytest, robotframework, requests, re, paramiko, subprocess, os, json, xml, logging, sys

Comments

Popular posts from this blog

TCL Interview Programs

Python Interview Programs

-: Networking interview questions :-