Variables as well as Data Types inward Python


Variables together with Data Types inwards Python

We tin uncovering information types together with variables inwards every estimator programming linguistic communication like C, C++, Java etc...And inwards every Scripting linguistic communication similar Perl, Python, together with VBScript etc...

What is Data Type?

A information type, inwards programming, is a classification that specifies which type of value a variable has together with what type of mathematical, relational or logical operations tin hold upwardly applied to it without causing an error. 

Example: 
Alfa bytes, Numbers, Boolean values etc...

What is Variable?

A named retention place to shop the temporary information inside a program, variable value may vary throughout the programme together with Variables shop inwards primary retention RAM.

Implicit announcement of Data types inwards Python, agency no information type specification earlier declaring the variable.

Ex:

a=10;

Suppose inwards Java explicit announcement of information types...

Ex:

int a=10;
or 
int a;

Implicit announcement of Variables inwards Python, agency nosotros no involve to declare variables, nosotros tin piece of job directly.

Ex: 

num=100;
---------------------------------------
First Python Program:

We tin execute Python programs inwards dissimilar modes of programming,

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

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

Type Python statements 1 yesteryear 1 together with enter, It volition execute ascendency yesteryear ascendency or pace yesteryear step...

Suppose if yous convey 4 lines of code inwards your programme together with then type starting fourth dimension step/line together with locomote inwards adjacent mo line...like that solely nosotros tin execute the program
-------------------------------
Script Mode Programming

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

Means, starting fourth dimension attain our Python Program inwards a text editor (ex: Notepad), together with then launch the Interpreter, together with opened upwardly the Script file together with then execute the program. Program volition hold upwardly executed continuously...
-------------------------------
No Explicit announcement of Variables inwards Python, together with No information type specification, nosotros tin assign whatsoever type of information to variables...based on assignment value python considers its information type.

1) Assign values to variables...

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

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

Using type(); ascendency nosotros tin banking concern fit the type of information 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 unmarried value to several variables simultaneously. 

Ex:
a=b=c=1;
print (a, b, c);
-------------------------------
We tin likewise assign multiple values to multiple variables.

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

We tin likewise delete a variable using del command...

a=123;
print (a);

del(a);
print(a); # It volition exhibit error
-------------------------------
5) Standard Data Types inwards Python...

Python has 5 criterion information types -

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

Number information types shop numeric values. Number objects are created when nosotros assign a value to them. 

var1=10;
var2=20;

Delete Variables...

delete (var1, var2);

Python supports iv dissimilar numerical types -

int (signed integers)

long (long integers, they tin likewise hold upwardly represented inwards octal together with hexadecimal)

float (floating quest existent values)

complex (complex numbers)
-------------------------------
ii) Python Strings

Strings inwards Python are identified equally a contiguous fix of characters represented inwards the quotation marks.

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

Or

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

A listing contains items separated yesteryear commas together with enclosed inside foursquare brackets ([]).
To merely about extent, lists are similar to arrays inwards C.

Example:

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

print (list); # Print consummate List
print (list[0]); # Print starting fourth dimension chemical gene of the list
print (list[2]); # Print tertiary chemical gene of the list
-------------------------------
iv) Python Tuples

A tuple is merely about other sequence information type that is similar to the list. Influenza A virus subtype H5N1 tuple consists of a release of values separated yesteryear commas. Unlike lists, however, tuples are enclosed inside parentheses.

The master copy differences betwixt lists together with tuples are: Lists are enclosed inwards brackets ( [ ] ) together with their elements together with size tin hold upwardly changed, piece tuples are enclosed inwards parentheses ( ( ) ) together with cannot hold upwardly updated. Tuples tin hold upwardly idea of equally read-only lists.

Example: 
tuple =(12, 34, 4.5, "Hello");
print (tuple); #print all elements of the tuple
print (tuple[1]); # impress 2d chemical gene of the tuple
-------------------------------
v) Python Dictionary

Dictionaries piece of job similar associative arrays or hashes constitute inwards Perl together with consist of key-value pairs. Influenza A virus subtype H5N1 lexicon fundamental tin hold upwardly virtually whatsoever Python type, but are ordinarily numbers or strings.

Dictionaries are enclosed yesteryear curly braces { } together with values tin hold upwardly assigned together with accessed using foursquare 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 larn Python?

2) Introduction to Python

3) Download & Install Python

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post