Skip to content

Data Types

Rajesh Khadka edited this page Nov 15, 2019 · 1 revision

Integer

# declare and initialize an integer
a = 5

float

# declare and initialize float
b = 5.4

boolean

# declare and initialize boolean
result = True

type(object)

# check type of variable
type(a)

String

a = 'hello' can use '' or " "

type conversion

a = str(a)
print(a)
print(type(a))

a = float(a)
print(a)
print(type(a))

List

# declare list
data = [4, 5, 6]
OR
# declare list
data = list()

data.append(4)
data.remove(5)
data.insert(1, 3)

data.sort()

# pop element of index 0
data.pop(0)

# slice
a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

a[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

Dictionary

   
   user = dict()
   # OR
   user = {}

   user['name'] = 'Bob'
   user['address'] = 'USA'
   user['profession'] = 'Engineer'

   print(user)

   user = {'name': 'Bob',
        'address': 'USA',
        'profession': 'Engineer'}

   print(user.keys())
   print(user.get('name'))
   print(user.values())
   print(user.items())

   for key, value in user.items():
       print(F'{key} : {value}')


   # nested dictionary

   address = {
    'latitude': 1234,
    'longitude': 23456
    }

   # add address of user
   user['address'] = address

   # update latitude value to 500
   user['address']['latitude'] = 500

Tuple

   token = tuple()
   token = (1)
   token = tuple(1, 'Jhon', 3.5)

   print(type(token))

   token = (1,)

   print(type(token))

   key, value = [2, 3]

   # swap number
   a = 5
   b = 6
   a, b = b, a
   print(a, b)

   # right side of tuple can be of any type string, list, tuple
   name, domain = 'bob@google.com'.split('@')

   # tuple comparision
   if (5, 6, 7) > (1, 2, 4):
    print(True)

   # access tuple's  element by index
   values = (5, 6, 7)
   print(values[1])

Set

   set_a = set()
   set_b = set()

   set_a.add(1)
   set_a.add(2)

   set_b.add(3)
   set_b.add(4)

   set_a.union(set_b)

   # can perform all functions of set

References

Clone this wiki locally