Variables and Data Types in Python

Variables and Data Types in Python

We can find data types and variables in every computer programming language like C, C++, Java etc...And in every Scripting language like Perl, Python, and VBScript etc...

What is Data Type?

A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error. 

Alfa bytes, Numbers, Boolean values etc...

What is Variable?

A named memory location to store the temporary data within a program, variable value may vary throughout the program and Variables store in primary memory RAM.

Implicit declaration of Data types in Python, means no data type specification before declaring the variable.



Suppose in Java explicit declaration of data types...


int a=10;
int a;

Implicit declaration of Variables in Python, means we no need to declare variables, we can use directly.


First Python Program:

We can execute Python programs in different modes of programming,

1) Interactive Mode programming
2) Script Mode Programming
Interactive Mode programming:

Invoking the interpreter without passing a script file as a parameter,

Type Python statements one by one and enter, It will execute command by command or step by step...

Suppose if you have 4 lines of code in your program then type first step/line and enter next second that only we can execute the program
Script Mode Programming

Invoking the interpreter with a script parameter begins execution of the script and continues until the script is finished.

Means, first prepare our Python Program in a text editor (ex: Notepad), then launch the Interpreter, and open the Script file then execute the program. Program will be executed continuously...
No Explicit declaration of Variables in Python, and No data type specification, we can assign any type of data to variables...based on assignment value python considers its data type.

1) Assign values to variables...

counter=10;     # An Integer Assignment
distance=20.45; # A Floating Point
name= "Ramu";   # A String

print (counter);
print (distance);
print (name);
2) Verify Data Type:

Using type(); command we can check the type of data that a variable hold.

It returns int...

It returns float...

It returns str...
3) Multiple Assignment

Python allows us to assign a single value to several variables simultaneously. 

print (a, b, c);
We can also assign multiple values to multiple variables.

a, b, c=1, 2.2, "John";
print (a);
print (b);
print (c);
4) Delete a Variable

We can also delete a variable using del command...

print (a);

print(a); # It will show error
5) Standard Data Types in Python...

Python has five standard data types -

i) Python Numbers

Number data types store numeric values. Number objects are created when we assign a value to them. 


Delete Variables...

delete (var1, var2);

Python supports four different numerical types -

int (signed integers)

long (long integers, they can also be represented in octal and hexadecimal)

float (floating point real values)

complex (complex numbers)
ii) Python Strings

Strings in Python are identified as a contiguous set of characters represented in the quotation marks.

str="Hello World";
print (str);


str='Hello World';
print (str);
iii) Python Lists

A list contains items separated by commas and enclosed within square brackets ([]).
To some extent, lists are similar to arrays in C.


list = [12, 23, "Hello", 1.23];

print (list); # Print complete List
print (list[0]); # Print first element of the list
print (list[2]); # Print third element of the list
iv) Python Tuples

A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

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. Tuples can be thought of as read-only lists.

tuple =(12, 34, 4.5, "Hello");
print (tuple); #print all elements of the tuple
print (tuple[1]); # print 2nd element of the tuple
v) Python Dictionary

Dictionaries work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings.

Dictionaries are enclosed by curly braces { } and values can be assigned and accessed using square braces [].

dict = {};
dict ['UserId'] ="abcd123";
dict ['Password'] ="hyderabad123";

print (dict['UserId']); # Print value for 'userId' key
print (dict.keys()); #Print all keys
print (dict.values()); # Print all values
Also Read:

1) In Which sequence should We learn Python?

2) Introduction to Python

3) Download & Install Python


Post a Comment