Python Data Types

We are going to be looking at the datatypes in Python

The data types in Python includes.

int, float, none, range, complex, bool, strings, list, set, touple, dict


Lets start with numbers.

we have the int, float, complex and bool

Example 1 Integers

the type() function is used to return the type of value of 11, which is an interger(int).

An int in Python is a whole number, it has no decimal point

  type(11)
  type(11 + 11)
  isinstance(11, int)

Example 2 Float

Here we check the type of 55.5 and it returns a float.

A float in python is a number that has a decimal point

  type(55.5)
  type(7/3) #can start with two ints and move to a float

Example 3 Complex Numbers

A complex number in Python is made up of a real part and an imaginary part.
complex imaginary numbers
imaginary unit i is represented by the letter “j” in Python.
6 + 2i
  type(6 + 2j)

Example 4 Booleans (True or False)

Here, True is a Boolean value in Python. Booleans represent truth values, True or False.

  type(True)
  type(33 < 11)

Example 5 None

None is similar to null.
It represents the absence of a value in Python. It is often used to indicate that a variable has no value yet.
Â
  type(None)

Example 6 range

range is seen a ton with for loops.
range creates a range object representing numbers from i.e 1 to 11 (11 is not included).
  type(range(1,11))

Example 7 Strings

‘koufax’ is a string (str), which means a sequence of characters(text) in Python

  name = 'koufax'
  type(name)
Substring.
Here we access the first character of the string ‘koufax’ using indexing.
In Python, string indices start at 0.
  name[0]

The type remains a str

  type(name[0])

This uses slicing to get characters from index ‘0’ up to (but not including) index 3 of the string ‘koufax’

  name[0:3]

This does same thing as the previous code.

When the start index is left out, Python starts from the beginning of the string.

  name[:3]
Strings can also have double quotes
  type("Sandy")

Strings can also have triple quotes

  type( """ Sandy Koufax """ )

Example 8 Lists

A list in Python is an ordered collection of items, which can be of any data type.

here we have a list called “list1” and it contains three integers.

List are muable, meaning you can change their contents.

They are written using square brackets.

  list1 = [5, 2, 3]
  type([5, 2, 3])
  #list with multiple data types
  type([5, 'Walter'])
#list with multiple lists

This is a nested list, meaning a list of lists.

The outer structure is still a list in Python

  multi_list = [[5, 2, 3], ['Walter', 'Johnson']] type(multi_list)
Accessing parts of a list
  print(list1)

This accesses the first item in the list. index 0 gives the first element.

  list1[0]

This slices the list to get elements from the start up to (but not including) index 2.

  list1[:2]

The append() method or function modifies the original list by adding one item at the end.

  list1.append(4)
  print(list1)

Example 9 tuples

A tuple is an ordered, immutable collection of items.

It is written with parenthesis ().

It cannot be chnaged after creation unlike lists.

It can contain diffferent data types.

  tuple1 = (1, 4, 5, 6)
  type(tuple1)

Example 10 sets

A set is an unordered collection of unique items.
Â
Duplicates are automatically removed.
Â
It is written using curly braces {}
  set_dup = {1,1, 2}
  print(set_dup)
  type(set_dup)
  sets1 = {1, 3, 4}
  sets2 = {11, 3, 5}

This prints the union of two sets.

the “|” operator combines all unique elements from both sets

  print(sets1|sets2)

Tis prints the intersection of two sets.

The “&” operator returns only the elements common to both sets.

  print(sets1 & sets2)

This prints the difference between the two sets.

It returns items that are in set1 but not in set2.

  print(sets1 - sets2)

This prints the symmetric difference of two sets.

The  “^” operator returns elements that are in either set, but not in both.

  print(sets1 ^ sets2)
#add to set

The add() method inserts a new element into the set if it’s not already there.

  sets1.add(5)
  print(sets1)

This removes the element “3” from the set sets1.

Note:

if 3 exists, it is removed.

if 3 does not exist, Python raises a KeyError

  sets1.remove(3)
  print(sets1)

Example 11 dictionaries

A dictionalry stores key-value pairs.Â

Int hsi example,

“cricketer” is the key,

“kohli” is the value

  cricketer = {'cricketer': 'Kohli'}
  type(cricketer)

.items() methid gives us a view of the dictionary’s key-value pairs as a list of tuples.

  cricketer.items()

.values() method gives a view of all the values in the dictionary, without the keys.

  cricketer.values()

.keys() method gives a view of all the keys in the dictionary.

  cricketer.keys()

This accessess the value associated woth the key “cricketer” in the dictionary.

  cricketer['cricketer']

The update() method chnages the value of the existing key “cricketer” from “Kohli” to “Dhoni”.

note:

If the key didn’t exist, it would be added instead.

  cricketer.update({'cricketer': 'Dhoni'})
  print(cricketer)

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Leave a Reply

Your email address will not be published. Required fields are marked *