Recommended Books

Python Data types



We’ll be going through a brief explanation all the main data types in Python, followed by an explanation on how to deal with and convert data types.

Numeric Data types:

  • Integer: Positive or Negative Number without a fractional part. e.g: 5
  • Float: A number with a fractional part. e.g: 3.25
  • Complex: A number with a real and imaginary part. e.g: 3 + 2i

Collection Data types:

Data types with a collection of values.

  • List: An ordered collection of one or more items, separated by commas. These items may have different data types.
    e.g: List1 = ["Hello", 5, 3.2]
  • Tuple: An ordered collection of one or more items, separated by commas. These items may have different data types.
    e.g: Tuple1 = ("Hello", 5, 3.2)
  • Dictionary: An un-ordered collection of one or more items in a key:value pair form.
    e.g: Dict1 = {Name: Amanda, Age:35, Gender: F}

String:

A string is a data type used in programming, such as an integer and floating point unit, but is used to represent text rather than numbers. In Python, strings are kept within double quotes. More information in Python Strings.
e.g: String1 = "Hello world"

Boolean:

A Boolean is a data type with only two possible values, True and False. Remember that Python is case sensitive, and the “T” in True, and the “F” in False must be Capitol.

Set:

A datatype commonly used in Statistics, Sets. In python, a set is a unordered collection which is unindexed. This means that the order of the items in the set is random and unpredictable. Used when position of object in a collection of objects is not important.
e.g: Set = {"Bob", "Sam", "Charles"}

Arrays:

Arrays are not natively supported in python, the closest replacement for it being lists. If you’re coming from another programming language however, you will most likely want to use Arrays. We can create Arrays using a python library numpy. See the Article for details.


Handling Python Data types:

As explained in the Python Syntax, data is stored in variables. Once these variables are assigned a value for the first time, they take on the data types of that value. But what if we want to change the data type? What if we took a String of numbers as an input from the user and now need it to use it in calculations. You can’t use a string to run calculations, as it’s not a numeric value. What you do instead is convert the string to a integer/float first.

To find out the type of a variable, use the type() function.

Python Data Types

Python has the inbuilt functions of int() and str() that are used to convert data types. The int() will convert anything placed within its parameters to an integer values. Likewise, the str() function converts any value within it’s parameters into a string.

There are some restrictions to this however. For instance, the int() can only convert certain types of data. The string “5” has the integer equivalent of 5. However, the string “Bob”, has no integer equivalent. Similarly, you can only convert single values. You cannot place a list or dictionary within the parameters of the int() function. Similar restrictions apply to the str() function.

Python Data types Conversion

Mutable and Immutable Data Types:

When a variable of a data type is created, it’s then saved into the memory of the computer. There are some Data types which can be changed during the execution of a program called Mutable Data Types. In contrast to these are some data types whose values can’t be changed once they have been assigned a value. These data types are called Immutable Data types.

A list and a tuple are almost identical except for the fact that a list is a mutable data type and a tuple is immutable.


This marks the end of the Python Data types Article. Any suggestions or contributions for Coderslegacy are more than welcome.

Leave a Reply

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