Python Standard Data Type

Mutable and Immutable data type

Difference of Mutable and Immutable:
 Mutable Data type can be updated; Immutable Data type cannot be updated.

Types Data Type
Mutable List, dictionary, set
Immutable Numeral, String, Tuple

Examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
a = [1,2,3]
a[1] = 3
a

Output: [1, 3, 3]

a = (1,2,3)
a[1] = 3
a

Output:---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
C:\Users\ADMINI~1\AppData\Local\Temp/ipykernel_13720/700303818.py in <module>
1 a = (1,2,3)
----> 2 a[1] = 3
3 a

TypeError: 'tuple' object does not support item assignment

Numeral

Numeral data type includes: Integer, float
Some important operators:

  • % : called the modulo operator     69 % 12 == 9
  • // : called the floor division operator    69 // 12 == 5
    Some methods:
  • int( ) : return the integer part in int type, no round  int(1.6) == 1
  • float(): return the decimals in float type,   float(1) == 1.0

We can use these two function to convert string. And you will get error if the string does not contain numeric characters. What’s more you can’t use int to convert a string of decimals

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
>>> sval = '123'
>>> type(sval)
<class 'str'>
>>> print(sval + 1)
Error: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly

>>> ival = int(sval)
>>> type(ival)
<class 'int'>
>>> print(ival + 1)
124
>>> nsv = 'hello bob'
>>> niv = int(nsv)
Error: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int()
with base 10: 'x

int("3.2")
Error: ValueError: invalid literal for int() with base 10: '3.2'

Decimal computation problem

String

  • String has length
    fruit = "banana
    print(len(fruit)) Output: 6
    
  • Slicing Strings
    s = "Monty Python"
    print(s[0:4]) output: Mont
    

List

Dictionary

Tuple

Set