0% found this document useful (0 votes)
4 views3 pages

Python Method Cs

This cheat sheet outlines various Python data types including strings, integers, floats, lists, sets, tuples, and dictionaries, along with their methods and common operations. It highlights key features such as immutability of certain types, order preservation in lists and dictionaries, and the use of comprehensions. Additionally, it provides examples of operations like concatenation, membership checks, and arithmetic operations for each data type.

Uploaded by

peaky29blinders
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Python Method Cs

This cheat sheet outlines various Python data types including strings, integers, floats, lists, sets, tuples, and dictionaries, along with their methods and common operations. It highlights key features such as immutability of certain types, order preservation in lists and dictionaries, and the use of comprehensions. Additionally, it provides examples of operations like concatenation, membership checks, and arithmetic operations for each data type.

Uploaded by

peaky29blinders
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Data Types: Operations & Methods Cheat Sheet

📘 String (str)
════════════════
Methods:
lower(), upper(), strip(), replace(), split(), find(), count(),
startswith(), endswith(), join(), title(), splitlines(), lstrip(),
rstrip(), isalpha(), isdigit(), isalnum(), partition(), format(), encode()

Operations:
Concatenation: 'Hi' + '!' → 'Hi!'
Repetition: '-' * 10 → '----------'
Length: len('Python') → 6
Membership: 'a' in 'apple' → True
Slicing: text[2:5] → substring
Formatting:
f"Value: {x}" (f-strings)
"{}".format(var) (format method)

🔢 Integer (int)
════════════════
Methods:
bit_length(), to_bytes(), from_bytes(), as_integer_ratio()

Operations:
Arithmetic:
7 + 3 → 10 (Addition)
7 - 3 → 4 (Subtraction)
7 * 3 → 21 (Multiplication)
7 // 3 → 2 (Floor division)
7 % 3 → 1 (Modulus)
7 ** 3 → 343 (Exponentiation)
Bitwise:
5 & 3 → 1 (AND)
5 | 3 → 7 (OR)
5 ^ 3 → 6 (XOR)
5 << 1 → 10 (Left shift)
5 >> 1 → 2 (Right shift)
~5 → -6 (NOT)

🔢 Float
════════
Methods:
is_integer(), hex(), fromhex(), as_integer_ratio()

Operations:
2.5 + 1.5 → 4.0
5.0 / 2 → 2.5
round(3.14159, 2) → 3.14
float.is_integer(5.0) → True
Comparisons: 3.0 > 2.9 → True

📜 List
════════
Methods:
append(), extend(), insert(), remove(), pop(), clear(), sort(),
reverse(), copy(), count(), index()

Operations:
Concatenation: [1,2] + [3] → [1,2,3]
Repetition: [0] * 3 → [0,0,0]
Length: len([1,2,3]) → 3
Membership: 3 in [1,2,3] → True
Slicing: lst[1:3] → sublist
Sum: sum([1,2,3]) → 6
Min/Max: min([1,2,3]) → 1, max([1,2,3]) → 3
List Comprehension: [x*2 for x in lst]
Unpacking: a, b, c = [1,2,3]

📦 Set
═══════
Methods:
add(), remove(), discard(), pop(), clear(), union(), intersection(),
difference(), symmetric_difference(), update(), isdisjoint(), issubset(),
issuperset()

Operations:
Union: {1,2} | {2,3} → {1,2,3}
Intersection: {1,2} & {2,3} → {2}
Difference: {1,2} - {2,3} → {1}
Symmetric Difference: {1,2} ^ {2,3} → {1,3}
Membership: 3 in {1,2,3} → True
Subset: {1} <= {1,2} → True
Set Comprehension: {x**2 for x in s}

🧱 Tuple
═════════
Methods:
count(), index()

Operations:
Concatenation: (1,2) + (3,) → (1,2,3)
Repetition: ('Hi',) * 2 → ('Hi','Hi')
Length: len((1,2,3)) → 3
Membership: 2 in (1,2,3) → True
Slicing: tup[1:] → (2,3)
Unpacking: x, y, z = (10,20,30)
Sum: sum((1,2,3)) → 6

🔑 Dictionary (dict)
════════════════════
Methods:
get(), keys(), values(), items(), pop(), popitem(), clear(),
copy(), update(), setdefault(), fromkeys()

Operations:
Value Access: d['key'] → value
Key Check: 'key' in d → True
Length: len(d) → number of keys
Key Deletion: del d['key']
Merge (Python 3.9+): d1 | d2
Dictionary Comprehension: {k:v*2 for k,v in d.items()}
Safe Access: d.get('missing', default) → default value

Key Notes:
══════════
• Immutability: Strings, tuples, integers, floats are immutable
• Order Preservation: Lists, tuples, and dicts (Python 3.7+) maintain insertion
order
• Comprehensions: Available for lists, sets, and dictionaries
• Unpacking: Works with lists, tuples, and dictionaries (for keys)

You might also like