Python Data Types
Python has the following built-in data types.
Numeric : int, float, complexSequence Type : list, tuple, range
Boolean : bool
Set : set, frozonset
Binary : bytes, bytearray
Python Data types at a glance.
Python Data Types |
Getting the Data Type
Setting the Data Type
In Python the data type is set automatically when you assign a value to a variableYou can check the data type of following variables by yourself.
lst = ["apple", "banana", "cherry"] ==> Data type list
tup = ("apple", "banana", "cherry") ==> Data type tuple
rng = range(6)==> Data Type range
dict =
{"name" : "John", "age" : 36} ==>Data type dict
s1 =
{"apple", "banana", "cherry"} ==> Data type set
f =
frozenset({"apple", "banana", "cherry"}) ==>Data type frozenset
b = True==>Data type bool
by =
b"Hello" ==>Data type bytes
by1 =
bytearray(5)==> Data type bytearray
Numeric Data Type
To determine the type of data type type() function is used in python.
Sequence Data Type in Python
- String
- List
- Tuple
String Creation in Python Using Single Quote |
String Creation in Python Using Double Quotes |
Example 3: Creating string using triple quotes
String Creation in Python Using Triple Quotes |
List
Creating and printing a empty list in python |
Syntax to create list with multiple values
Creating List with multiple values. |
Accessing element from list |
Negative indexing of the list |
Tuple
Tuple is also an ordered collection of
Python objects. Tuples are immutable means tuples cannot be modified like list once it is
created. It is represented by tuple class.
Creating an empty tuple |
Creating tuple with multiple values |
Tuples items can also be accessed with their index number
Access tuple elements |
Negative indexing of tuple
Access tuple elements from last |
Mapping Data Type in Python
key:value is provided in the dictionary to
make it more optimized. Each key:value pair in a dictionary is separated by a colon:
whereas each key is separated by a comma,.
Creating Empty Dictionary |
Syntax to create dictionary with keys
Creating Dictionary with keys |
# accessing an element from a Dictionary Creating a Dictionary
Dict = {'name': 'Tom', 'Roll': 5, 'salary': 2000}
# accessing dictionary element using key
print("Accessing an element using key:")
print(Dict['name'])
print(Dict['salary'])
#Accessing an element from Dictionary using get() method
print("Accessing an element using get method:"
print(Dict.get('name'))
Output:
Accessing an element using get method:
Tom
Boolean Data Type in Python
The Boolean data type have one of the two built-in values True or False. It is denoted by class.
Example :
#python script to explain bool data type
print(type(True))
print(type(False))
Output
<class 'bool'>
<class 'bool'>
Note : First Character of "True" and "False" always should in capital else it will throw an error.
Set Data Type in Python
In python Set is an unordered collection of data type that is mutable, iterable and has no duplicate values. Set elements are undefined but it may consist of various elements.
Creating Set : Sets can be created with following ways :
1. By using built-in set() function with iterable objects
2. A sequence by placing the sequence inside middle bracket({}) separated by comma(,).
Example1 : Create and print blank set
# Creating a Set
set1 = set()
print("blank Set: ")
print(set1)
Example2: Creating set with the string
# Creating a Set with the String
set1 = set("thisisaboy")
print("\nSet with the String: ")
print(set1)
Output :
Set with the String:
{'y', 'o', 'b', 't', 'h', 'a', 's', 'i'}
Click below link to get your Dream Job
https://optimhire.com?ref_code=jk125735