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
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)}");
Output:
Value:15 Type:<class 'int'>
Value:500 Type:<class 'int'>
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)}");
Output:
Value:100.256 Type:<class 'float'>
Value:-78.23 Type:<class 'float'>
Complex:
- Complex is used to store the value with imaginary digits.
Ex:
num=3j+1;
print(f"Value:{num} , Type:{type(num)} ");
Output:
Value:(1+3j) , Type:<class 'complex'>
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);
Output:
false
True
<class 'bool'>
True
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)} ");
Output:
Value:Raksha , Type:<class 'str'>
Value:Parrot , Type:<class 'str'>
Value:Apple , Type:<class 'str'>
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));
Output:
[10, 20, 'Raksha', 10, 'Honey Bee', 2.36, 10]
Raksha
<class 'list'>
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));
Output:
('Hello', 'Byee', 1, 'Hii')
Byee
<class 'tuple'>
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));
Output:
{'name': 'Cat', 'place': 'Chennai', 'age': 3}
<class 'dict'>

Top comments (0)