Python Basics

Python.org

Python scripts are stored in *.py files

  • print(“add text here”)
  • # to comment out the code
  • ** exponentials
  • % modulo

Variables

  • are case-sensitive
  • type() to check the type
  • string – use ” or ‘
  • float – real number
  • int – integer
  • boolean – True or False
  • = is assignment, not equality
  • Convert Types
    • str(), int(), float(), bool()

Lists

  • Used to give a single name to a collection of values
  • car = [a, b, c]
  • Can contain mixed data types as well as lists
  • fam = [[“Joe”, 180], [“Jennifer”, 135]]
  • to get information from the list:
    • fam [2] => c
    • fam [-1] => c – first item from the end of the list
    • list [strt : end ] => list slicing

Functions

  • help(<add any function name>) – to get information about a specific function
  • sorted (iterable, key, reverse) – key default = None, reverse default = False

Methods

These are functions that belong to objects: strings, floats, integers, lists, etc.

  • index()
  • count()
  • append()
  • remove()
  • reverse()
  • upper()
  • capitalize()

Packages

Packages are sets of python scripts saved together. Each script represents a module.

To install a package, use pip – package maintenance systems for Python. Additional information: https://pip.pypa.io/en/stable/installation/