Python data types – Int, Float, string, dict, lists with examples

Python variables and data-types

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

 

Python has following standard data types:

  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

Examples of declaring and printing number variables

The output will be:

An interger number: 10

Sum of two int variables =  30

A float number: 10.2

Python string

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

For example

a = “Phtyon”

strLesson = ‘This is 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 complete lesson.

Python dictionary data type

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

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

Values can be accessed using key rather index like in simple arrays.

This really helps in establishing a strong link between Array keys and values, as explained in employee/salary example below.

If you require to store salaries of employees in an array, a numeric indexed array can’t be best choice and might always be confusing.

Whereas using key – value pair in dictionary makes this task quite simple.

Syntax of dictionary

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 = []

–          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 sequence protocol and allows to add or remove items from 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

Among other compound data types available in Python, tuple is one of those. Tuples are sequence just like list but this type is immutable. That means its values cannot be changed. Once created, rest of the program it will stay same. Besides list use square brackets while tuples use parentheses ().

Go to tuple tutorial for full detail