Strings

Strings is a series of characters (numbers, letters, etc), one example of a string is your name or your id because strings can contain both numbers and letters.

The following are all examples of strings being used in code, in this case, within print functions.

print("hello world")
print('hello')
hello world
hello

Lists

Lists are sequences of elements with each element being a variable. An example of a list can be the names of the students in this classroom.

Features of Lists

  • The elements within the list can be accessed by index.
  • Can store various elements
  • The list is in order

Example of a list

thanksgivingList = ["cranberry pie", "casserole", "mashed potatoes", "turkey"]
casserole
casserole

List Index

An index is an element of a string. Indices typically start with 0, but Collegeboard has the index start at 1. Note that not all languages allow you to index from the end.

print(thanksgivingList[1]) #In this case, the index starts at 0, but in collegeboard, the index starts at 1
print(thanksgivingList[-3]) #Python can index from the end, in this case, "turkey" would be index -1 and 3. 
casserole
casserole

Questions

  • What is a list?
  • What is an element
  • What is an easy way to reference the elements in a list or string?
  • What is an example of a string?

Hacks

  • Create a list with indices
  • Index a part of the list that you created.
  • Try to index from the end