Thursday 21 September 2017

4)Python Variables and Data Types

Python Variables and Data Types:

Python Variables:
A variable is a location in memory used to store data(value).

Note:

  • We don't need to declare a variable before using it. In Python, we simply assign a value to a variable and it will exist. 
  • Also don't even have to declare the type of the variable. This is handled internally according to the type of value we assign to the variable.


Single Variable assignment
Example:
x=10
Multiple assignments

  • Multiple assignments with different values

x,y,z=10,'A',10.20

  • Multiple assignments with same values

x=y=z=10

Python Data types:

Every value in Python has a datatype. Since everything is an object in Python programming, data types are classes and variables are instance (object) of these classes.

Let's go through with some important data types:

1)Python Numbers

Integers, floating point and complex numbers falls under Python numbers category

In Python repository, there are int, float and complex classes

Note:

  • type() function to know which class a variable or a value belongs to
  • isinstance() function to check if an object belongs to a particular class.
Example:



Note:
Floating numbers can hold up to 15 decimal places, if exceeds, other parts will be truncated


2)Python List:
List is an ordered sequence of items.

Note:
All the items in a list do not need to be of the same type.

Declaration:
Separated by commas are enclosed within brackets [ ].

Slicing operator [ ]:

To extract an item or a range of items from a list. Index starts form 0 in Python.


3)Python Tuple:
Tuple is an ordered sequence of items same like list.

Note:

  • The only difference is that tuples are immutable. Tuples once created cannot be modified, it returns an exception
  • Tuples are used to write and protect data, are usually faster than list as it cannot change dynamically.


Declaration:
Separated by commas within parenthesis or braces,

Slicing operator [ ]:
For tuples, using slicing operator, only retrieving the values can be allowed, but not allow the value change


Example:


4)Python Strings:
String is sequence of Unicode characters.

Declaration:

  • single line strings are with single quotes or double quotes 
  • Multi-line strings can be denoted using triple quotes, ''' or """.


Slicing operator [ ]:
For String, using slicing operator, only retriving the values can be allowed, but not allow the value change

Example:


5)Python Set:
Set is an unordered collection of unique items.

Declaration:
Set is defined by values separated by comma inside curly braces { }. 

Note:

  • Items in a set are not ordered and not indexed so slicing operator wont work with sets
  • We can perform set operations like union, intersection on two sets. Set have unique values. They eliminate duplicates.


Example:


6)Python Dictionary:

Dictionary is an unordered collection of key-value pairs.

Declaration:
In Python, dictionaries are defined within braces {} with each item being a pair in the form key:value. Key and value can be of any type.

Note:
To retrieve value of dictionary can be done through only the key




Conversion between data types:

Below functions are useful to convert datatypes ,

int(), float(), str(),set(),tuple(),list() ,dict() etc.

Example:
>>> float(5)
5.0
>>> int(10.6)
10
>>> str(25)
'25'
>>> set([1,2,3])
{1, 2, 3}
>>> tuple({5,6,7})
(5, 6, 7)
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> dict([[1,2],[3,4]])
{1: 2, 3: 4}
>>> dict([(3,26),(4,44)])
{3: 26, 4: 44}

Previous page                                                                                                       Next page

1 comment:

  1. The information which you have provided in this blog is really useful to everyone. Thanks for sharing.
    Python Training in Hyderabad

    Python Training

    Python Online Training

    ReplyDelete

Fundamentals of Python programming

Fundamentals of Python programming: Following below are the fundamental constructs of Python programming: Python Data types Python...