0% found this document useful (0 votes)
2 views2 pages

Python Cheatsheet

This document is a quick reference guide for Python, summarizing essential syntax, data types, control flow, built-in functions, string methods, and list comprehensions. It covers numeric types, sequences, key-value pairs, and unique values, along with control flow statements and useful built-in functions. Additionally, it provides examples of string manipulation and comprehension techniques.

Uploaded by

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

Python Cheatsheet

This document is a quick reference guide for Python, summarizing essential syntax, data types, control flow, built-in functions, string methods, and list comprehensions. It covers numeric types, sequences, key-value pairs, and unique values, along with control flow statements and useful built-in functions. Additionally, it provides examples of string manipulation and comprehension techniques.

Uploaded by

Akhilesh Desai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Quick Reference

Essential syntax and built-in functions at a glance

Data Types
• int, float, complex – numeric types
• str – immutable text: "hello", 'world'
• list – mutable sequence: [1, 2, 3]
• tuple – immutable sequence: (1, 2, 3)
• dict – key-value pairs: {"a": 1}
• set – unique values: {1, 2, 3}
• bool – True / False

Control Flow
• if x > 0: / elif x == 0: / else:
• for item in iterable:
• while condition:
• break – exit loop early
• continue – skip to next iteration
• pass – do nothing placeholder

Useful Built-ins
• len(x) – length of a sequence
• range(start, stop, step) – integer range
• enumerate(iterable) – index + value pairs
• zip(a, b) – pair two iterables
• sorted(iterable, reverse=False)
• map(func, iterable) / filter(func, iterable)
• any(iterable) / all(iterable)

String Methods
• .strip() .lstrip() .rstrip() – trim whitespace
• .split(sep) / [Link](list)
• .upper() / .lower() / .title()
• .replace(old, new)
• .startswith(s) / .endswith(s)
• f"Hello {name}" – f-string formatting

List Comprehensions
• [x*2 for x in range(10)]
• [x for x in nums if x > 0]
• {k: v for k, v in [Link]()} – dict comp
• {x for x in lst} – set comp

You might also like