Short Tutorial: Variable and Data Types in Python

Python is a high-level programming language created by Guido Van Rossum in 1989. Python is very popular among programmers and testers as it is very easy to learn, use to automate any repetitive IT task very easily. In this article, we will cover variables, different types of operators and python lists.

Variables in Python

In python, no need to mention the type of variables unlike C, Java etc.  Here, the variable is just a reference to an object instead of a data type. In below example, same variable use for string and numerals.

 

The complex number can be assign by using variable a and can be check by using built-in isinstance() function as mentioned in below example.

 

To find the memory location of the variable, use either "id" function or "__repr__".

 

Different string variables examples as given below:

 

Unicode String

Unicode strings can be represented by small alphabet "u" in front of string while assigning.

 

String Operations

Python strings are immutable objects in python. You can't change it once it is created.

 

Now, you can't change the string by assigning  var1[3]  to some other character as mentioned below.

 

Strings are immutable. In below example, variable var2 assign with string 'AllAboutTesting' and variable an assign with the string 'InformationSecurity'. Now, again variable var2 assign with the value of variable a. Variable a reassign with string 'AllAboutTesting'. Now print variable a and var2. Variable var2 still contains the value of variable a which signifies strings are immutable.

 

Concatenation of strings

 

The repeated sequence in the string to use it as a buffer overflow. You can create those files by using * operator.

 

Convert an integer into a string and vice-versa by using int and str function.

 

String Methods

You can search string in variable by using below commands.

<variable_name>.find('<search_string>')

 

You can split string in variable by using below commands.

<variable_name>.split('<search_string>')

 

You can replace string in variable by using below commands.

<variable_name>.replace('<replace_string>', '<new_string>')

 

String Formatting

 

Numbers

Following operators can be used in python: addition (+), subtraction (-), multiplication (*), division (/), modulus(%) operations etc.

 

Exponent (x**y) operation

 

Comparison Operators: greater than (>), less than (<), greater than equal to (>=), less than equal to (<=), equal to (==) and not equal to (!=).

 

Lists in Python

It is defined as a collection of homogenous or heterogeneous objects. It is a collection of similar or different data types elements which is ordered and changeable.

 

The list can be altered and searched by different functions. Below are the examples showing built-in append(), replace(), pop() and reverse() functions.

 

Subscribe us to receive more such articles updates in your email.

If you have any questions, feel free to ask in the comments section below. Nothing gives me greater joy than helping my readers!

Disclaimer: This tutorial is for educational purpose only. Individual is solely responsible for any illegal act.

You may also like...

Leave a Reply

Your email address will not be published.