Python Complete Series – 05

Learn Python Complete Series.

Lecture 5: Outline

  • Python datatypes
  • Built-in types
  • Get datatype using get function
  • Setting the data types with examples
  • Setting specific datatype with examples
  • Check knowledge, short quiz

Python DataTypes;

In “programming” approach, data types are the essential part and significant concept.

Every programming language has its own data types and syntax to declare the type variable.

Python has also provided the data types to distinguish between code data. Variable(s) can store records of various types, and different ‘datatypes’ can perform multiple different things.

Built-in Python DataTypes

The followings are the default built-in datatypes and in categorized form.

A list of default built-in data types that Python provides is given below. The datatypes are categorized in multiple different types. See below to check built-in data types:

  1. For ‘Textual’ Type;
str
  1. For ‘Number’ Type(s);
Int | float | complex
  1. For ‘Sequence(s)’ Type(s);
List | tuple | range
  1. For ‘Map or mapping’ Type;
dict
  1. For ‘Set’ Type(s);
set | frozenset
  1. For ‘Boolean’ Type;
bool
  1. For ‘Binary’ Type(s);
Bytes | bytearray | memoryview

We will briefly discuss each datatype in upcoming lectures. Right now, understand the types, their usage, and the objective.

Getting the Data Type

How to get datatype?

For getting the type of any variable, simply use type() function.

In this way, you can get type in return.

Example

Show the DataType of “x”:

learn python series

Var has a value of 5. It’s a numeric value and of ‘int’ type recognized by Python.

Click here to learn Python Series Complete

NOTE:  As we already know, the python didn’t allow us to write the name of datatype before the variable declaration like other languages. It automatically detects the type of variable.

Setting up the Python Data Type

Following are the constructor functions by using these, you can specify the python datatype:

Example Data Type

a = "Hello Hello" str

a = 2 int

a = 2.5 float

a = 2j complex

a = ["apples", "bananas", "cherries"] list

a = ("apple", "banana", "cherry", "cherry" tuple

a = range(5) range

a = {"name" : "jerry", "age" : 39} dict

a = {"apple", "banana", "cherry", "cherry", "cherry"} set

a = frozenset({"apple", "banana", "cherry"}) frozenset

a = True bool

a = b"Hello" bytes

a = bytearray(5) bytearray

a = memoryview(bytes(5)) memoryview

 

Setting Specific Pyhton DataType

Following are the constructor functions by using these, you can specify the Pythaon datatype:

Example Data Type

a = str("Hello World") str

a = int(50) int

a = float(80.5) float

a = complex(1j) complex

a = list(("apple", "banana", "cherry")) list

a = tuple(("apple", "banana", "cherry")) tuple

a = range(6) range

a = dict(name="John", age=36) dict

a = set(("apple", "banana", "cherry")) set

a = frozenset(("apple", "banana", "cherry")) frozenset

a = bool(5) bool

a = bytes(5) bytes

a = bytearray(5) bytearray

a = memoryview(bytes(5)) memoryview

 

Let’s have a short quiz:

The following code example would print the Python dataType of a, what dataType would that be?

a = 45.5

Print(type(a))

What should be the answer?

The answer is FLOAT because it’s in point value, 45.5 not 45. 45 is integer value while 45.5 is float.

Also, visit the official Python site here.

Exit mobile version