Object and Data Structure Basics

  1. Integers int - Whole numbers such as 3, 200, 300

  2. Floating point float - Numbers with a decimal point: 2.3, 4.6, 100.0

  3. Strings str - Ordered sequence of characters: "hello", 'Sammy', "2000"

  4. Lists list - Ordered sequence of objects: [10, "hello", 200.3]

  5. Dictionaries dict - Unordered Key:Value pairs: {"mykey" : "value", "name" : "Frankie}

  6. Tuples tup - Ordered immutable sequence of objects: (10, "hello", 200.3)

  7. Sets set - Unordered collection of unique objects: {"a", "b"}

  8. Boolean bool - Logical values indicating True or False

Python Numbers

  • Addition: 2 + 1

  • Subtraction: 2 - 1

  • Multiplication: 2 * 1

  • Division: 2 / 1

  • Modulo: 2 % 1

  • Exponent: 2 ** 1

Variable Assignments

We can assign a value to a variable.

Rules for variable names:

  1. Cannot start with a number

  2. Can't have spaces

  3. Can't use :"',<>/?|\()!@#$%^&*~-+

Best practice is that names are lowercase. Also avoid built-in keywords used by Python in names.

Python uses dynamic typing. You can assign and reassign with no issue.

Use type() to get the type of whatever is stored in a variable.

Strings

Use single or double quotes if needed.

Strings are ordered sequences so we can index and slice them.

Indexing and Slicing Strings

  • Every character in a string has an index assigned to it beginning at 0.

  • We can use square brackets [] to select a character by index.

name = "Victor"
print(name[1])

Here a string was assigned to variable name and then the character in the 1 position was printed (i).

  • Slicing allows us to grab a subsection of multiple characters.

  • The syntax is [start:stop:step].

  • The stop is not inclusive

print(name[0:2])

Here the string Vi will be printed.

  • We can use use negative numbers to slice or index "backwards"

  • For example, putting the start position as -1 will mean that the slice begins at the last character

  • Use [start:] to slice from the starting position until the end of the string

  • [:stop] will slice from the beginning until the character before the stopping position (remember, it's non-inclusive)

  • [::2] will provide every second character

Fast way to reverse string: mystring[::-1]

Escape Sequences

  • \n for newline

  • \t for tab

String Properties and Methods

  • You can concatenate strings together with +

  • You can also use * to multiply a string a certain number of times

  • Error will occur when concatenating a string and a number

  • Useful methods

    • mystring.upper() to make string uppercase (not in place, doesn't change the original string)

    • .lower() to lowercase

    • .split() to split a string into a list based on whitespace or whatever character you pass in

  • There are a few methods to format strings for printing variables in them

  • This is called string interpolation

.format() method

Example:

print('String here {} then also {}'.format('something1', 'something2'))
  • You can also select which variable goes where based on the index position: 'String two: {1}. String one: {0}'.format('fox', 'brown')

  • Float formatting {value:width.percision f}:

    result = 0.1287001287001287
    print("The result was {r:1.3f}".format(r=result))
    >>> The result was 0.129

String literal (Python 3.6+)

Example:

name = "Jose"
print(f'Hello, his name is {name}')
  • Also called "f-strings"

Lists

  • Ordered sequences that can hold a variety of objects

  • Use brackets and commas

  • Support indexing and slicing

  • Can be nested

  • Use len(my_list) to get length of list

  • Lists are mutable

  • Use .append() to add item to end of list (in-place, changes original)

  • Use .pop() to remove last item (in-place)

    • Pass index position to function to remove a specific item

  • Use .sort() to sort the list (in-place)

    • Doesn't return anything

  • Use .reverse() to reverse the list (in-place)

    • Doesn't return anything

Dictionaries

  • Unordered mappings for storing objects

  • Uses key-value pairs

  • This key-value pairing makes it possible to quickly grab an object without knowing the index

  • Can't be sorted

  • Get value with key like this: my_dict['key1']

  • Can hold lists or even other dictionaries

  • Can add more key-value pairs: my_dict['key3'] = 'value3'

  • Same for reassignment

  • Use my_dict.keys() to see all keys

  • Use my_dict.values() to see all values

  • Use my_dict.items() to see all pairs

Tuples

  • Similar to lists but tuples are immutable

  • Once an element is inside a tuple, it cannot be reassigned

  • Uses parenthesis

  • my_tuple.count('a') to count how many times the string 'a' appears in the tuple

  • my_tuple.index('a') to get the first occurrence of the string 'a' in the tuple

Sets

  • Unordered collections of unique elements (not more than one of the same object)

  • Use set() to create a set

  • my_set.add(1) to add to the set