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. 

Example: 
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.

Ex:

a=10;

Suppose in Java explicit declaration of data types...

Ex:

int a=10;
or 
int a;

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

Ex: 

num=100;
---------------------------------------
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 line...like 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.

a=1;
type(a);
It returns int...

b=2.2;
type(b);
It returns float...

c="abcd";
It returns str...
-------------------------------
3) Multiple Assignment

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

Ex:
a=b=c=1;
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...

a=123;
print (a);

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

Python has five standard data types -

Numbers
String
List
Tuple
Dictionary
-------------------------------
i) Python Numbers

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

var1=10;
var2=20;

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);

Or

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.

Example:

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.

Example: 
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";
dict[1]=100;

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

0 comments:

Post a Comment