BUILT-IN TYPES IN PYTHON

Here is the list of most commonly used built-in types that Python supports:

Immutable Built-in Datatypes

NUMBERS :

Python supports integers, floats and complex numbers.

An integer is a number without decimal point for example 5, 6, 10 etc.

float is a number with decimal point for example 6.7, 6.0, 10.99 etc.

complex number has a real and imaginary part for example 7+8j, 8+11j etc.

#int

num1= 10

num2=100

print(num1+num2)

#float

a = 10.5

b = 8.9

print(a-b)

#complex numbers

x = 3 + 4j

y = 9 + 8j

print(y-x)

STRINGS:

Strings in python are contiguous series of characters delimited by single or double quotes. Python doesn’t have any separate data type for characters so they are represented as a single character string.

name = “Janani” # a string
char = ‘a’ # a character

You can also use the following syntax to create strings.

a = str() # this will create empty string object
b = str(“Janani”) # string object containing ‘Janani’

TUPLES:

tuple is an immutable list of Python objects which means it can not be changed in any way once it has been created.

t = (“pie”,”marshmello”,”kitkat”)

print(t)

Mutable Built-in Datatypes

LISTS:

A list is a data type that allows you to store various types data in it. List is a compound data type which means you can have different-2 data types under a list, for example we can have integer, float and string items in a same list.

#list of floats

list1=[11.22,9.9,34.7,54.6]

# list of int, float and strings

list2 = [1.13, 2, 5, “Janani”, 100, “hi”]

# an empty list

list3 = []

SETS:

A set is a data structure that stores unordered items. The set items are also unindexed. Like a list, a set allows the addition and removal of elements. However, there are a few unique characteristics that define a set and separate it from other data structures:

  • A set does not hold duplicate items.
  • The elements of the set are immutable, that is, they cannot be changed, but the set itself is mutable, that is, it can be changed.
  • Since set items are not indexed, sets don’t support any slicing or indexing operations.

set1 = {1, 2, 2 , 3, 4, 5, 6}

print(num_set) //Ouput will be {1,2,3,4,5,6}

DICTIONARIES:

Dictionaries are Python’s implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value.

d = {
    <key>: <value>,
    <key>: <value>,
      .
      .
      .
    <key>: <value>
}
d = {
    Rama: 32,
    Seetha: 87,
      .
      .
      .
    Jan: 56
}

Hope you enjoyed this!! Will meet you in the next blog.

Leave a comment

Design a site like this with WordPress.com
Get started