Datatypes in Python

Datatypes in Python

Namaste Coders!

In this article, we will learn about datatypes in Python. At last we need to work on data.

Datatypes tell us about the type of data that is stored in python variable. The data stored in memory can be of many types. Python is dynamically typed language that doesn't restrict a variable to store a particular type of variable.

type()function used to know the type of data.

Following are all the datatypes available in python.

Primitive Datatypes

  • int: to store any integer value.
age = 15
print(age)  #15
print(type(age))  #<class 'int'>
  • float: to store any real(decimal) value.

    price= 15.23
    print(price)  #15.23
    print(type(price))  #<class 'float'>
    
  • complex: Python supports complex (real num +/- imag num) numbers. j represents imaginary number in complex number.

    c= 15 + 3j
    print(c)  #15 + 3j
    print(type(c))  #<class 'complex'>
    
  • bool: It stores two values either True or False.

    is_valid = True
    print(is_valid)  #True
    print(type(is_valid))  #<class 'bool'>
    
  • str: String in Python that is a immutable. Anything defined using single quote(‘ ‘ ) or double quote(“ “) is a string.

s = "we are learning"
print(s)
print(type(s))

m = "let's start " \
 "playing"
print(m)
print(type(m))

msg = '''
It is a 
multiline 
string
'''

print(msg)

There are two separate ways because if we want to print (‘) inside string than we will wrap inside double quotation marks(“ “) and vice versa.

1.png

  • None: In Python, None means nothing or no value. It reserves the space in memory but there in None assigned to it. It is similar to null in other programming languages. ``` temp = None

print(temp) #None print(type(temp)) #


# Derived Datatypes

> Derived datatypes allows us to store multiple values under a single variable name. It contains a sequence of various Python objects. 

- **list:** A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between **square brackets [ ].**

List -> [ ]

marks = [19, 15, 14, 19, 16, 15] print(marks) #[19, 15, 14, 19, 16, 15] print(type(marks)) #


- **tuple:** A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use **parentheses()**, whereas lists use square brackets[].

tuple-> ( )

marks = (19, 15, 14, 19, 16, 15] print(marks) #(19, 15, 14, 19, 16, 15) print(type(marks)) #


.
- **dict:** In Dictionary each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just **two curly braces, like {}.**

dictionary -> {key : value}

user = { "first_name" : "John", "second_name" : "doe", "age" : 15, "courses" : ["Maths" , "Sci"] }

print(user) print(type(user))



![2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664245108406/B1XnmuWuA.png align="left")

- **set:** Set stores unique Python objects. It is mutable but does not maintain specific order of elements. It is written within curly braces {} .

set-> { }

marks = {19, 15, 14, 19, 16, 15} print(marks) #{16, 19, 14, 15} print(type(marks)) # ```

The End

I hope you enjoyed the article and had a good learning experience.

Follow for more articles and keep sharing👩

Keep coding

Linkedin

hashnode blogs

Did you find this article valuable?

Support akshita garg by becoming a sponsor. Any amount is appreciated!