# CodingSplash # Day04-Variable and Data Types

## What is Variable?

Variable - is like a container that holds data.

Example:

``````a = 1234453734563455
print(a)
b = "Harry"
print(b)
``````

Output:

``````1234453734563455
"Harry"
``````

## Data Type:

Data types specify the type of data that can be stored inside a variable. It is required in programming to perform 'operations'.

 Data Types Classes Description Numeric int, float, complex holds numeric values String str holds sequence of characters Sequence list, tuple, range holds collection of items Mapping dict holds data in key-value pair form Boolean bool holds either True or False Set set, frozenset hold collection of unique items Binary bytes, bytearray, memoryview includes bytes, bytearray, and memoryview None NoneType represents the absence of a value

## Built-in Data Types:

### Numeric Data Type - int, float, complex:

• int - It contains positive or negative whole numbers.

• float - It is a real number with a floating-point representation. It is specified by a decimal point.

• complex - holds complex numbers. It is specified as (real part) + (imaginary part)j. For example - 2+3j

Example:

``````a = 3
print(a, 'is of type', type(a))

b = 7.38
print(b, 'is of type', type(b))

c = complex(8,2) #or c = 8+2j
print(c, 'is of type', type(c))
``````

Output:

``````Python String Data Type:3 is of type <class 'int'>
7.38 is of type <class 'float'>
(8+2j) is of type <class 'complex'>
``````

### Text Data Type - string:

A string (str) is a collection of one or more characters put in a single quote, double-quote, or triple-quote.

Example:

``````String1 = 'Hello World' #using single quote
string2 = "Welcome to Python Language course" #using double quote
string3 = '''Day-03'''  #using triple quote
print("Using single quote: ", String1)
print("Using double quote: ", string2)
print("Using triple quote: ", string3)
``````

Output:

``````Using single quote:  Hello World
Using double quote:  Welcome to Python Language course
Using triple quote:  Day-03
``````

### Sequence Data Type - list, tuple, range:

The sequence Data Type in Python is the ordered collection of similar or different data types.

• List - It is an ordered collection of similar or different types of items separated by commas and enclosed within square brackets [ ]. Lists are mutable and can be modified after creation.

• Tuple - It is an ordered sequence of items same as a list. The element in tuple are separated by comma and enclosed within round brackets ( ) .once created cannot be modified.

• Range - python range is a function that returns a sequence of numbers.

Example:

``````a = ["apple", "banana", "cherry"] #list
print(a)
print(type(a))

b = ("apple", "banana", "cherry") #tuple
print(b)
print(type(b))

c = range(6)  #range
print(c)
print(type(c))
``````

Output:

``````['apple', 'banana', 'cherry']
<class 'list'>

('apple', 'banana', 'cherry')
<class 'tuple'>

range(0, 6)
<class 'range'>
``````

### Mapping Data Type - dict:

A dictionary is an unordered collection of data containing a key:value pair. The key:value pairs are enclosed within curly brackets {key:value}.

Example:

``````x = {"name" : "John", "age" : 36}
print(x)
print(type(x))
``````

Output:

``````{'name': 'John', 'age': 36}
<class 'dict'>
``````

### Boolean Data Type - bool:

The boolean (bool) in Python represents logical values True and False.

Example:

``````a = True
print(a)
print(type(a))
``````

Output:

``````True
<class 'bool'>
``````

### Set Data Type: set, frozenset:

The set data type allow you to store a collection of unique items.

• Set - It is an unordered collection of data types that is iterable, mutable and has no duplicate elements.

• Frozen set - It is just an immutable version of a Python set object. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation.

Example:

``````a = {"apple", "banana", "cherry"}   #set
print(a)
print(type(a))

b = frozenset({"apple", "banana", "cherry"})   #frozenset
print(b)
print(type(b))
``````

Output:

``````{'banana', 'cherry', 'apple'}
<class 'set'>

frozenset({'banana', 'cherry', 'apple'})
<class 'frozenset'>
``````

### Binary Data Type - bytes, bytearray, memoryview:

• Bytes - It is used to represent a group of 8 binary digits. It is a built-in data type. It can hold any value from 0 to 255.

• Bytearray - It is a mutable sequence of bytes. It is a built-in data type.

• Memory View - It allows you to view the memory of an object without creating a copy of it, making it useful for working with large data sets.

Example:

``````a = b"Hello"  #here b = bytes
print(a)
print(type(a))

b = bytearray(5)    #bytesarray
print(b)
print(type(b))

c = memoryview(bytes(5))  #memoryview
print(c)
print(type(c))
``````

Output:

``````b'Hello'
<class 'bytes'>

bytearray(b'\x00\x00\x00\x00\x00')
<class 'bytearray'>

<memory at 0x013D8FA0>
<class 'memoryview'>
``````

### None Data Type - none:

The NoneType data type represents the absence of a value. It is a built-in type that has only one value: None.

Example:

``````x = None
print(x)
print(type(x))
``````

Output:

``````None
<class 'NoneType'>
``````

REMEMBER:

• String can be written in: single, double or triple quote.

• List are mutable while Tuples are immuatble.

• Dictionary are key value pairs.

<< Previously -------------------------------------------------- Next Lesson >>