Python data types – Int, Float and compound types with examples

Python variables and data-types

Variables are the way to store values that are changeable. Variable has data types that define what kind of data to store in that variable. For example numbers, strings etc.

Similarly, Python supports different data types like Python int, float, string etc. to store different types of data. See detail in the following section.

Python has the following standard datatypes:

  1. Numbers (int, float etc.)
  2. String
  3. Dictionary
  4. Tuple
  5. List

Python Numeric data types

Python has three distinct numeric types:

  • int (e.g. 1, 100, -100)
  • float (1.234, -1.234)
  • complex

Python Float and int variables example

The following example shows how to declare and use Python int and float variables. The values of int and float variables are then displayed by using the print function.

The output will be:

An integer number: 10

The Sum of two int variables =  30

A float number: 10.2

Python string

Strings are the sequence of characters. Strings are enclosed in single or double quotes in Python. Python will treat single or double quoted strings alike.

For example:

a = “Python”

strLesson = ‘This is Python strings lesson’

Triple quotes are also used to enclose strings in Python. This is used when strings are enclosed in multi-lines:

StrLesson = “””This

Is

Python lesson”””

A whole chapter is dedicated to string data type, click here for the complete lesson.

Python dictionary data type

Python provides another data type dictionary which is just like the associative arrays in some other languages. Dictionary data types work with key – value pairs.

A key can be of any type of the object like a number, string or a list.

Values can be accessed using the keys rather than the index like in simple arrays.

Syntax

dict = {key:value, key:value, …..}

For example

dict = {‘Mike’: 1233456,’John’: 567890}

For more, go to dictionary data type tutorial.

Python List data type

Among other compound data types available in Python, List data type is the most versatile.  

  • List items (values) can be written as comma separated values in square [] brackets.

list_name = [1,2,3]

  • A List can store different types of values or items like numbers and strings.

e.g. Py_list = [1, 2, ‘Python’, ‘lists’ ]

  • The list data type implements the sequence protocol and allows to add or remove items from the list.

A few examples of lists

list_number = [1,2,3,4,5]

list_strings = [‘this’, ‘is’, ‘list’, ‘lesson’]

list_mixed = [1, ‘this’, 2, ‘is’, 3, ‘list’, 4, ‘chapter’]

For more go to list data type tutorial.

Python Tuple data type

Tuples are the sequence just like the list, but this type is immutable. That means its values cannot be changed. Once created, the rest of the program it will stay the same.

Go to tuple tutorial for full detail.

Was this article helpful?

Related Articles

Leave A Comment?