What are Python’s data types?

1. Numbers

  • int: whole numbers with +/- signs (ex) 10, -23, -0x260
  • long: integers with bigger capacity than int, representable in octal/hexadecimal. When using, make sure to add the letter “L” to the end. You can use lowercase, but it is better to use the uppercase by convention, to avoid confusion with the number one. (ex) 51923812L, 122L, -319237198237L
  • float: decimal numbers (ex) 0.0, 123.21, -90., 23.1-E12, 31.4+e18
  • complex: for complex numbers consisting of an pair of floats, denoted by a+bj, where a+b are real and j is imaginary (ex) 3.55j, -.123j, 2e+12J

2. String

  • denoted by single or double quotation marks
  • use * for repetition
  • use + to concatenate
  • (ex) “Hello World”, ”       “, “12345”

3. List

  • a compound data type
  • holds items, separated by commas contained within square brackets
  • index starts at 0, not 1
  • use * for repetition
  • (ex) [1, “Hello”, 423.2, -1, “abc]

4. Tuple

  • a sequence data type similar to list
  • holds values, separated by commas contained within round brackets
  • unlike lists, tuples cannot be updated and are a read-only version of lists
  • (ex) (1, “Hello”, 423.2, -1, “abc)

5. Dictionary

  • associative array consisting of key-value pairs, enclosed with {}
  • can contain any data type, but are usually numbers/strings
  • values assigned/accessed using []
  • (ex) address = {‘John’:1442, ‘Tom’:1443, ‘Jill’:1444, ‘Shirley’:1445}
Advertisements