Data types

Every variable has a data type, determining what sort of data it can hold. Python has a bazillion data types, but we only use a few in this course.

String

A string can contain any sequence of characters you can type, like Doggos rule! and ╟Φ╟ (kinda looks a Tie fighter).

String constants are delimited by quotes. E.g.,

  • 'This is the song that never ends'
  • "There's a hole in the bucket, dear Liza, dear Liza"

Integer

A whole number, like 17 or -3332.

Float

A number that can have a decimal component, like 3.14159, -3929.4, or 32.0.

Boolean

A value that's either True or False. Often used to track whether something has happened or not.

List

A list is a sequence of individual values. The values can be strings, floats, dictionaries, other lists, anything.

Here's a list of strings, Australian state names.

  • states = ['Queensland', 'New South Wales', 'Victoria',
  •           'Tasmania', 'South Australia', 'Western Australia']

Brackets [] denote lists.

Add items to a list: thelist.append(something)

Dictionary

A dictionary is a set of key/value pairs. The keys are usually strings. The values can be anything. Add as many key/value pairs as you like.

For example,

  • best_pokemon = {
  •     'name': 'Snorlax',
  •     'generation': 1,
  •     'pokedex number': 143
  • }

Access values using keys:

  • best_pokemon['weight'] = 'A lot!'
  • print(best_pokemon['weight'])