Python Data Types
PYTHON DATA TYPES
Python Programming datatypes is important concept. Python have 5 data types. We will learn it.
1. 1. Numeric
· Integer
· Float
· Complex
2. 2.Dictionary
3. 3.Set
4. 4.Boolean
5. 5.Sequence Type
· Strings
· Lists
· Tuples
Python has classified into two category Mutable and Immutable
data types. Python have various data types. We will learn
main
datatypes in python. There are
1. Numbers
2. Strings
3. Tuples
4. Lists
5. Dictionaries
6. Sets
We will learn Boolean and Built-in-data types in python later.
Now, we will learn above 6 datatypes in python. Let's start.
Numbers:
In python Numbers are Integers, Float and Complex.
1. Integers: Integers
have whole values. Example: 1,2,3,4, etc.
2. Float: Float
values have decimal value. Example: 1.7, 5/2=2.5
3. Complex: Example:
3+2j, (4+4j) +(3+3j) = 7+7j
In above examples, I hope you understand it.
Let’s move on Strings
Strings:
Strings are sequences of characters strings.
Example 1:
‘Python’ and “HRS INTERSTING FACTS”
You can also use multi-line strings by using triple quotes (“”” or’’’)
Triple quotes we already discuss about Triple quotes. If you not
see
it. Link has given below.
Example 2:
“”” Hello guys,
Welcome to HRS INTERSTING FACTS
Python Programming Tutorial.”””
Example Program:
st1='hrs'
st2="interesting"
st3=""" Hello guys,
Welcome to HRS INTERSTING FACTS
Python Programming Tutorial."""
st4="let's Start"
print(st1)
print(st2)
print(st3)
print(st4)
Output:
hrs
interesting
Hello guys,
Welcome to HRS INTERSTING FACTS
Python Programming Tutorial.
let's Start
In above example st4="let's Start" is valid.
If you type st4=’let's Start’ is invalid. Because single quote started
and ended again single quote start but not end so, by using double quotes can solve
it.
We’ll learn sequence string operations in upcoming posts.
Tuple:
A tuple is sequence of immutable python object like floating
number, string literal etc.
The tuple can’t be changed unlike lists.
Tuples are define using parenthesis ().
Example:
my_tuple=('a',"Welcome",2)
print(my_tuple)
Output:
('a', 'Welcome', 2)
We’ll learn tuple sequence operations in upcoming posts.
List:
A list is a sequence of mutable python object like float, string etc.
It defined using Square braces[].
Example:
my_list=['python', 2.7,5]
print(my_list)
Output:
['python', 2.7, 5]
Dictionaries
A python dictionary is a collection of unordered, changeable and \
indexed.
It has defined by curly braces {}
It have keys and values.
Example:
My_dict={1:’python’,2:’programming’}
↓ ↓
Key Value
Dictionary is not allows duplicate keys.
My_dict={} #empty
My_dict={1:’apple’, 2:’ball’} #integer keys
My_dict={‘name:’rangan’, 1:[2,4,6]} # mixed keys
Example Program
# Dictionary Mydict={1:'python',2:'programming'} print("Mydict=",Mydict,"This Integer keys") Mydict1={'First name': 'Hemarangan', 'Last name':"S"} print(Mydict1) Mydict2={'Welcome':'HRS',2:[2,4,6]} print("Mixed Keys",Mydict2) print("Dictionary not allows duplicate keys") # Dictionary not allows duplicate keys print("Before") Mydict={1:'python',2:'programming'} print(Mydict) print("After") Mydict={1:'python',2:'programming',2:"hello"} print(Mydict) |
Output:
Mydict= {1: 'python', 2: 'programming'} This Integer keys
{'First name': 'Hemarangan', 'Last name': 'S'}
Mixed Keys {'Welcome': 'HRS', 2: [2, 4, 6]}
Dictionary not allows duplicate keys
Before
{1: 'python', 2: 'programming'}
After
{1: 'python', 2: 'hello'}
Sets:
A set is an unordered collection of items.
Every element is unique (no duplicates).
It must be immutable which cannot be changed.
My_set= {1,2,3}
Example:
myset={1,2,3,5,3,6,7}
print(myset)
Output:
{1,2,3,5,6,7}
In above example program, myset={1,2,3,5,3,6,7} then print myset it
shows the result {1,2,3,5,6,7}. Because 3 value two time repeated so sets is
not allowed duplicate values.
We will learn briefly all data types in python in upcoming posts.
I hope you guys, you will understand it. If you have any queries about
python data types, please comment or contact me.
If this article helpful you click like and leave a comment.
Thank guys.
Upcoming: Sequence operations in Python
Strings.