DEV Community

Rakshambika
Rakshambika

Posted on

Datatypes in Python

Data types in Python define the type of value stored in a variable and determine the operations that can be performed on that data.


Numeric:

  • It is used to store Numeric values.

  • There are three numeric types in python

          *int
          *float
          *complex
Enter fullscreen mode Exit fullscreen mode

Int:

-int is used to store positive , negative numbers without decimals.

Ex:

number1=15;
number2=500;
print(f"Value:{number1} Type:{type(number1)}");
print(f"Value:{number2} Type:{type(number2)}");
Enter fullscreen mode Exit fullscreen mode

Output:

Value:15 Type:<class 'int'>
Value:500 Type:<class 'int'>
Enter fullscreen mode Exit fullscreen mode

Float:

  • Float is used to store the positive or negative values with decimals.

Ex:

num1=100.256;
num2=-78.23;


print(f"Value:{num1} Type:{type(num1)}");
print(f"Value:{num2} Type:{type(num2)}");
Enter fullscreen mode Exit fullscreen mode

Output:

Value:100.256 Type:<class 'float'>
Value:-78.23 Type:<class 'float'>
Enter fullscreen mode Exit fullscreen mode

Complex:

  • Complex is used to store the value with imaginary digits.

Ex:

num=3j+1;
print(f"Value:{num} , Type:{type(num)} ");
Enter fullscreen mode Exit fullscreen mode

Output:

Value:(1+3j) , Type:<class 'complex'>
Enter fullscreen mode Exit fullscreen mode

Boolean:

  • Booleans represent one of two values: True or False.

Ex:

num1=0;
num2=1;

print(num1>num2);
print(10==10);
print(type(10=="10"));
print(3!=4);
Enter fullscreen mode Exit fullscreen mode

Output:

false
True
<class 'bool'>
True
Enter fullscreen mode Exit fullscreen mode

Truthy Values:

  • String with any values, without empty string.
  • Any number is true except 0 (zero).
  • Any list, tuple, set, and dictionary are True, except empty ones.

Falsy values:

  • Empty String , Set , Tuple , Dictionary.
  • number 0
  • None

Sequence Datatype:

  • A sequence is an ordered collection of items, which can be of similar or different data types. Elements in a sequence can be accessed using indexing.

Types:

  • String
  • List
  • Tuple

String:

  • String is used to store single or set of Characters.
  • String is created by single, double or triple quotes.

Ex:

name="Raksha";
bird='Parrot';
fruit="""Apple""";

print(f"Value:{name} , Type:{type(name)} ");
print(f"Value:{bird} , Type:{type(bird)} ");
print(f"Value:{fruit} , Type:{type(fruit)} ");

Enter fullscreen mode Exit fullscreen mode

Output:

Value:Raksha , Type:<class 'str'>
Value:Parrot , Type:<class 'str'>
Value:Apple , Type:<class 'str'>
Enter fullscreen mode Exit fullscreen mode

List:

  • Lists are used to store multiple items in a single variable.

  • Lists are created using square brackets[] .

  • Lists allow duplicates.

  • Lists are mutable.

Ex:

list_items=[10,20,"Raksha" ,10 , "Honey Bee" , 2.36 , 10];

print(list_items);
print(list_items[2]);
print(type(list_items));

Enter fullscreen mode Exit fullscreen mode

Output:

[10, 20, 'Raksha', 10, 'Honey Bee', 2.36, 10]
Raksha
<class 'list'>
Enter fullscreen mode Exit fullscreen mode

Tuple:

  • Tuple is used to store multiple items in a single variable.

  • Tuple is ordered.

  • Tuple is immutable.

  • Tuples are written with round bracket().

Ex:

tuple_item=("Hello" , "Byee" , 1 , "Hii");
print(tuple_item);
print(tuple_item[1]);
print(type(tuple_item));
Enter fullscreen mode Exit fullscreen mode

Output:

('Hello', 'Byee', 1, 'Hii')
Byee
<class 'tuple'>
Enter fullscreen mode Exit fullscreen mode

Dictionary:

  • Dictionaries are used to store data values in key:value pairs.

  • Dictionary is mutable.

  • Dictionary does not allow Duplicates.

  • Dictionary items are ordered.

Ex:

'name':"Cat",
'place':"Chennai",
'age':3
}

print(dict_items);
print(type(dict_items));

Enter fullscreen mode Exit fullscreen mode

Output:

{'name': 'Cat', 'place': 'Chennai', 'age': 3}
<class 'dict'>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)