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

07 Python Quick Reference

This document is a quick reference guide for Python, covering core syntax, data structures, and idioms. It includes sections on variables, strings, collections, control flow, functions, comprehensions, file handling, and handy idioms. The guide emphasizes Python's readability and provides concise examples for each topic.

Uploaded by

fapepay841
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)
3 views2 pages

07 Python Quick Reference

This document is a quick reference guide for Python, covering core syntax, data structures, and idioms. It includes sections on variables, strings, collections, control flow, functions, comprehensions, file handling, and handy idioms. The guide emphasizes Python's readability and provides concise examples for each topic.

Uploaded by

fapepay841
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

Core syntax, data structures and idioms

Python is valued for readable syntax and a large standard library. This reference gathers the language
features that come up constantly, so the right construct can be found quickly while writing or reading code.

1. Variables and Basic Types


Python is dynamically typed: a variable simply refers to an object, and its type is determined at runtime.
The core built-in types cover most needs.
n = 42 # int
pi = 3.14 # float
name = "Ada" # str
ok = True # bool
nothing = None # the null value
type(n) # <class 'int'>

2. Strings
s = "hello world"
[Link]() # 'HELLO WORLD'
[Link]() # ['hello', 'world']
[Link]('l', 'L') # 'heLLo worLd'
len(s) # 11
f"value is {n}" # f-string interpolation
s[0:5] # slicing -> 'hello'

3. Collections
Type Syntax Notes

list [1, 2, 3] Ordered, mutable

tuple (1, 2, 3) Ordered, immutable

dict {'a': 1} Key to value mapping

set {1, 2, 3} Unique, unordered

nums = [3, 1, 2]
[Link](4) # [3, 1, 2, 4]
[Link]() # [1, 2, 3, 4]

user = {"name": "Ada", "age": 36}


user["name"] # 'Ada'
[Link]("city", "?") # safe lookup with default

Python Quick Reference Page 1


4. Control Flow
if n > 10:
print("big")
elif n == 10:
print("ten")
else:
print("small")

for item in nums:


print(item)

while n > 0:
n -= 1

5. Functions
Functions are defined with def and may take positional arguments, keyword arguments and defaults. A
function returns None unless an explicit return is given.
def greet(name, greeting="Hi"):
return f"{greeting}, {name}!"

greet("Ada") # 'Hi, Ada!'


greet("Ada", greeting="Yo") # 'Yo, Ada!'

square = lambda x: x * x # small anonymous function

6. Comprehensions
Comprehensions build a collection in a single readable expression, often replacing a short loop. They exist
for lists, dictionaries and sets.
squares = [x*x for x in range(5)] # [0,1,4,9,16]
evens = [x for x in range(10) if x%2==0] # filter
lookup = {w: len(w) for w in ["a", "bb"]} # dict comp

7. Files and Exceptions


with open("[Link]") as f:
text = [Link]() # file closes automatically

try:
value = int(user_input)
except ValueError:
print("not a number")
finally:
print("done")

8. Handy Idioms
• Use enumerate(seq) to loop with an index.
• Use zip(a, b) to iterate over two sequences in step.
• Use 'in' to test membership: if x in collection.
• Swap values in one line: a, b = b, a.
• Guard a script's entry point with if __name__ == '__main__'.

Python Quick Reference Page 2

You might also like