tutorials questions & answers
by AlexB

Understanding Built-in data types in Python

TAGS:
  • python
  • linux
  • coding
  • data types
  • conversion

Introduction

Data types in python programming differes a bit from other languages. Python utilizes dynamic typing, that means. Basically verification of type safety happens at runtime. Python correlates data type with an object, and when a specific action is performed, check is that operation is valid for that object. There are several classes of data types, and in this guide i will describe them, and teach how to convert from one type to another.

Prerequisites:

Linux system ( windows system with python is also OK ) python installed basic programming knowledge

Data types in Python

Numeric: int, float, long, complex

1) int: integer, plain integers have 32bit precision 2) long: long integers, have unlimited precision 3) complex : complex numbers, have a real and imaginary part, where each is a floating point 4) float: floating point numbers, usually implemented using double in C

Sequences: str, list, bytes, byte array, tuple 1) str: string, it is a sequence of Unicode symbols in python 3.x and sequence of 8-bit symbold in python 2.* 2) list: this is a list of elements 3) bytes: python 3.x only, sequence of integers, 0-255 4) byte array: python 3.x, mutable bytes 5) tuple: this is a tuple of elements

Sets: 1) set: exist, since python 2.6+, a collection of objects with no order 2) frozen set: exist since python 2.6+, basically an immutable set

Mappings: 1) dict: python dictionaries, unordered collections of random objects with a key-based access. Sometimes called hashmaps or associative arrays.

Boolean: 1) boolean: True or False, interchangeable with 0 and 1

Creation of objects

1) decimal numbers: directly

2) hexadecimal numbers: require added 0x For example 0xff, which is 255

3) octals: in python 2.x - added 0, in python 3.x - added 0o

For example 0111 is 111 octal in python 2.x, and 0o111 is 111 in python 3.x 4) floating points: directly

5) long ints: directly or by added L in the end For example: 1L is 1 long int

6) complex: real number + imaginary with a j For example 6+7j. j is not itself an imaginary, you still need to write 1j.

7) strings: single or triple quoted words For example 'word' or '''word''' . Double quote works as well, "word"

8) list: to define a list you need to use square brackets. For example, ['abc', 1,2,3]

9) tuples: parentheses, separated by commas. For example: (8, 'some words')

10) dicts: curly braces, and inside a list of key + value, key is separated from value by a colon, elements are separated by commas For example: { 'name': Alex', 'occupation': 'python developer' }

Difference between tuple and list: First of all tuple is immutable, and list is mutable. Tuples are heterogenous, and lists are homogenous, and we also can say that tuples have structure, and lists have order. It may be hard to comprehend, so here are practical examples: Defining:

Tuple = (1,2)
List  = [1,2] 

Size

x = tuple(range(1000))
y = list(range(1000))

x.__sizeof__() # 8024
y.__sizeof__() # 9088

Permitted operation:

x = [1,2]   
y[0] = 3 

y = (1,2)
a[0] = 3  // produces an error

Also you can't delete a tuple element, or sort it. However you can add a new element to both tuple and list:

x = (1,2)
y = [1,2]  
x += (3,)    # (1, 2, 3)
y += [3]     # [1, 2, 3]

Usage: Tuple is immutable, so you can use it as a key in a dictionary, while you can use list:

x = (1,2)
y = [1,2] 

z = {x: 1}   // OK
c = {y: 1}  // error

Conversion of data types

Elements also allow conversion of one type into another. Is is done using some simple mechanics: Converting to integer:

x = int(1.1)
x = int("1")
x = int (1, 11) // 1 base 11

Converting to floating:

x = float(1) // 1.0
x = float(1.1) / 1.1
x = float(1.1E-1) 
x = float(False) // False is treated as 0
x = float(True) // True is treated as 1

strings:

x = str(1.1) // "1.1"
x = str([1, 2, 3]) // "[1, 2, 3]"

booleans:

x = bool(0) // False
x = bool(1) // True
x = bool([]) // empty list is False
x = bool("") // empty string is False

sets:

x = set([1, 2])

lists:

x = set([1, 2]) // we define a set
y = list(x) // we convert set to list

You can also convert types indirectly: for example:

INTx = 1 // variable is integer 1
FLOATx = INTx + 1.1 // we convert integer to floating by adding 1.1 to an integer
STRx = "string is:" + str(INTx) // we convert int into a string by adding a integer to a string
INTx2 = 1 + False // boolean False is 0, so it is implicitly converted to 0 and summed with 1

Conclusion

Now you now what main data types python support, how to operate them, how to create certain variables. You can also convert from one variable type to another. This is a first step in mastering python coding, but can make your starting experience much easier.

comments (0)

    Start now for as low as $2,99 a month!

     
    Open livechat