Header Ads

Python Data Types Explained with Examples - Python Tutorials

This article explains you about Python Data Types with examples.

In the previous articles, we have explained you the below topics that will help you to understand this articles much better from basics.

Python Data Types Explained with Examples - Python Tutorials

Python Data Types Explained with Examples - Python Tutorials

What is Python Data Types?

Data types are nothing but the type of the data stored in memory for a variables.

For example,
Name = "Barry"
Age = "30"

Where "name" and "age" are variables and value of each are different, one is in alphabetic charactors(Strings) and age is in numbers.
This is what the data types.

Types of Python Data Types

So Python has five standard data types,

1. Numbers
2. String
3. List
4. Tuple
5. Dictionary

Lets go through each one of these with examples.

Python Data Types - Part 1 (Numbers and Strings)

Python Data Types - Part 2 (Lists, Tuples and Dictionaries)

Number Data Types are nothing but data of a variable store in number format.
>>> Apples = 20
>>> Grapes = 10

As per the example, you have 20 apples and 10 grapes. 20 and 10 are in numeric values, hence these are in number data types.

Number data types may have some of these types,
1. Integer (Pure Numbers : 10, -123)
2. long (Mixed numbers : 51924361L, -0x19323L)
3. float (0.45, -21.9, 32.3+e18) and
4. complex numbers (3.14j, 3e+26J or 4.53e-7j)

Along with numbers, We use some operators to perform mathemtic functions, like addition, subsctraction, multiplication, divisions and modulars.
For example, lets do addition for these apples and grapes.

>>> Total = Apples + Grapes
>>> Total
>>> Apples - Grapes
>>> Apples*Grapes
>>> Apples / Grapes

Strings are nothing but, characters enclosed within single quotes or double quotes.

For example,
>>> Firstname = "Bob"
>>> Lastname = "Henry"

Where "Bob" and "Henry" are the value of "Firstname" and "Lastname" which are in strings data types.

Here too we can use addition operators to merge these two variables as below if you wanted to get print something like "Fullname".

>>> Fullname = Firstname + Lastname
>>> Fullname

If you wanted to leave some space inbetween firstname and lastname, then use in this format.

>>> Fullname = Firstname + " " + Lastname
>>> Fullname
'Bob Henry'

Additionally, we have options to get the sub strings of strings using slice operators.

For example:
print Fullname          # Prints complete string
print Fullname[0]       # Prints first character of the string

If you wanted to only print "Henry" from the variable "Fullname", you can also use the slice operators.

Subsets of strings starts with indexes 0 from the beginning of the string.

In python, starting index should give in machine reading format ie from '0123456....' and for the end index, we should give in the human reading format ie count from '1234567....'.
>>> Fullname
'Bob Henry'
print Fullname[2:5]     # Prints characters starting from 3rd to 5th
print Fullname[2:]      # Prints string starting from 3rd character
print Fullname * 2      # Prints string two times
'Bob HenryBob Henry'
print Fullname + "TEST" # Prints concatenated string
'Bob HenryTest'

This is how we declare string data types and we use slice operators.

Lists are nothing but, list of items can be declared for a variable separated by commas and enclosed within square brackets.

For example,
Laptops = ["ibm","mac","acer","hp"]

Here too you can use slice operators to search and slice the list.

print Laptops           # Prints complete list
['ibm', 'mac', 'acer', 'hp']
print Laptops[0]       # Prints first element of the list
print Laptops[1:3]     # Prints elements starting from 2nd till 3rd
['mac', 'acer']
print Laptops[2:]       # Prints elements starting from 3rd element
['ibm', 'mac']
print Laptops * 2   # Prints list two times
['ibm', 'mac', 'acer', 'hp', 'ibm', 'mac', 'acer', 'hp']

If you wanted to update a value of a list items, then use this format.

>>> Laptops[3] = "Lenova"
>>> print Laptops
['ibm', 'mac', 'acer', 'Lenova']

lists are similar to arrays in C. But in Lists, we can have multiple data types (Numbers, strings or other data types) not in C arrays.

A tuple is also a data type which is similar to the list. The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and values can be changed, But tuples are enclosed in parentheses ( ( ) ) and values cannot be updated. So tuples can be considered as read only.

Lets me show you the example.

tuple1 = ( 'abcd', 786 , 2.23, 'john', 70.2  )

If you try to update the value, you will get an error as not supported.

>>> tuple1[0] = "test"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

Its purely depend on the requirement to use Lists or Tuples.

Dictionaries are like a table format type with heading and rows. Dictionaries are enclosed by curly braces ({ }).

Lets see an example.
dict = {'name': 'john','code':6734, 'dept': 'sales'}

Here too, you can use slice operators,

>>> print dict   # Prints complete dictionary
{'dept': 'sales', 'code': 6734, 'name': 'john'}
>>> print dict['dept']       # Prints value for 'dept' key
>>> print dict.values() # Prints all the keys
['sales', 6734, 'john']
>>> print dict.keys() # Prints all the values
['dept', 'code', 'name']

Dictionaries have no concept of order among elements. It is incorrect to say that the elements are "out of order"; they are simply unordered.
Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.

YouTube | Facebook | Twitter | Pinterest | Telegrampython

Powered by Blogger.