Python is an object-oriented programming language ideally designed for rapid prototyping of complex applications. Like Perl, Python source code is also available under the GNU General Public License (GPL).

In this article we well see, How to declare and use a variable in Python. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc.

In python one does not need to put "variable type" before declaring a variable, the declared variable be interpreted as per the value assigned to it (in the example below - 'number' and 'string' are name of variables):
number = 1;
string = 'string';

print number;
print string;
Output: Output will be something like this:

1
string


1) Numbers

In python, when a numeric value is assigned to a variable, Number objects are created, as shown below:

intNumber = 1;
longNumber = 675l;
floatNumber = 12.34;

print intNumber;
print longNumber;
print floatNumber;
Output: Output will be something like this:

1
675
12.34


2) String

In python, when a string value(sequence of characters) is assigned to a variable, String objects are created, string value can be assigned using both single('') and double quotes(""), as shown below:

singleQuoteString = 'single quote';
doubleQuoteString = "double quote";

print singleQuoteString;
print doubleQuoteString;
Output: Output will be something like this:

single quote
double quote


Python substring are very flexible and can be used as shown below:
simpleString = 'tbNext for Python';


print simpleString          # Prints complete string
print simpleString[0]       # Prints first character of the string
print simpleString[2:5]     # Prints characters starting from 3rd to 5th
print simpleString[2:]      # Prints string starting from 3rd character
print simpleString * 2      # Prints string two times
print simpleString + "TEST" # Prints concatenated string
Output: Output will be something like this:

tbNext for Python
T
chb
chburps for Python
tbNext for PythontbNext for Python
tbNext for PythonTEST


3) Lists

Python lists are extremely versatile, even a list can have values of different data types as shown below:
list = [ 'tech', 786 , 2.23, 'burps', 70.2 ]
print list
Output: Output will be something like this:

['tech', 786, 2.23, 'burps', 70.2]


4) Dictionary

Python Dictionary are hashtable like, key value pair data storage:
direct = {};
direct['key'] = "value";
print direct['key'];
Output: Output will be something like this:

value


5) Tuples

The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated.
tuple = ( 'tbNext', 786 , 2.23, 'Python', 70.2  )
print tuple
Output: Output will be something like this:

('tbNext', 786, 2.23, 'Python', 70.2)


In this article we have seen, How to declare and use a variable in Python and different data types available. In upcoming articles we will see more complex usecases and tricks of python.
  • By Techburps.com
  • Sep 30, 2017
  • Python