Python Basics

Posted on Aug 5th, 2019


Basics of the Python programming language.

String

A string in Python is represented by a series of characters enclosed by single or double quotes.

# declare a string
s = 'hello there'
t = "yap, it's another string"
# return a capitalized version of the string
>>> s = 'hello there'
>>> s.capitalize()
'Hello there'
# return a caseless (un-capitalized) version of the string
>>> s = 'HeLlo TheRe'
>>> s.casefold()
'hello there'

Dictionary

d = {'one': 1, 'two': 2}

Update a dictionary with the key/value pairs from another dictionary. This overwrites existing keys in the original dictionary.

params = {'d': 3.14, 'ep': 0.46, 'phi': 0.8}
case1 = {'d': 2.89, 'phi': 0.9}

params.update(case1)
# updates `params` to {'d': 2.89, 'ep': 0.46, 'phi': 0.9}

List

a = [1, 2, 3, 4]

Further Reading


Back