Python


General

Significant Whitespace

  • Python uses whitespace to demarcate code blocks
  • by convention, 4 spaces are used per depth
  • end blocks with blank lines

Significant Whitespace Rules

  1. Prefer four spaces
  2. NEVER mix spaces and tabs
  3. Be consistent on consecutive lines
  4. Only deviate to improve readability

"Programming as Guido intended indented it"

Python Culture

Python Enhancement Proposals (pep)

Pep 20: The Zen of Python

import this

The Python Standard Library

  • often referred to as "Batteries Included"
  • gain access to standard library modules using import keyword
  • import module_name
import math

math.sqrt(81)
# => 9.0
math.factorial(5)
# => 120

n = 5
k = 3

math.factorial(n) / (math.factorial(k) * math.factorial(n - k))
10.0
  • can use help in REPL environment to retrieve any embedded documentation from objects
    • for example: help(math), help(math.factorial)

Typical uses of import

n = 5
k = 3

# simplest import
import math

math.factorial(n) / (math.factorial(k) * math.factorial(n - k))
# => 10.0


# can import specific function from module into namespace
from math import factorial

factorial(n) / (factorial(k) * factorial(n - k))
# => 10.0


# can rename imported function as well
from math import factorial as fac

fac(n) / (fac(k) * fac(n - k))
# => 10.0

NOTE: can return integer using Python's integer division operator

from math import factorial as fac

n = 5
k = 3

fac(n) // (fac(k) * fac(n - k))
# => 10

NOTE: Python can compute arbitrarily large numbers, limited only by the memory in your computer

Relational Operators

  • ==: value equality/equivalence
  • !=: value inequality/inequivalence
  • <: less-than
  • >: greater-than
  • <=: less-than or equal
  • >=: greater-than or equal

Indexing and Common Operations

# Define a list and a string
my_list = [1, 2, 3, 4, 5]
my_string = "Hello"

# Slicing: my_list[start:end], `start` inclusive, `end` exclusive
slice_list = my_list[2:4] # [3, 4]
slice_string = my_string[1:3] # "el"

# Concatenation: my_list + another_list
concatenate_list = my_list + [6, 7, 8] # [1, 2, 3, 4, 5, 6, 7, 8]
concatenate_string = my_string + ", world!" # "Hello, world!"

# Repetition: my_list * n #
repeat_list = my_list * 2 # [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
repeat_string = my_string * 2 # "HelloHello"

# Finding the first occurrence of an element in a list or a string
# Note that if the element is not found, `index` throws an exception
# So we should initially check the existence by the operator `in`,
# Then use the `index` method if it exists using the `if-else` construction.
# If the element is not found, the indices are assigned to `-1`
found_1_in_list = 1 in my_list # Returns: True
found_8_in_list = 8 in my_list # Returns: False
found_in_string = 'l' in my_string.lower() # Returns True
index_1_in_list = my_list.index(1) if found_1_in_list else -1 # Returns: 0
index_8_in_list = my_list.index(8) if found_8_in_list else -1 # Returns: -1
index_in_string = my_string.lower().index('l') if found_in_string else -1 # Returns: 2

# Sorting items in a list
sorted_list = sorted(my_list, reverse=True) # [5, 4, 3, 2, 1]
Made with Gatsby G Logo