Short Tutorial: Variable and Data Types in Python
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/cover-2.jpg?resize=720%2C340&ssl=1)
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 and 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 the below example, the same variable is used for string and numerals.
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/variable.jpg)
The complex number can be assigned by using variable a and can be checked by using the built-in isinstance() function as mentioned in the below example.
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/chk-complex.jpg)
To find the memory location of the variable, use either "id" function or "__repr__".
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/variableory-location.jpg)
Different string variables examples as given below:
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/string-variable.jpg)
Unicode String
Unicode strings can be represented by a small alphabet "u" in front of the string while assigning.
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/unicode.jpg)
String Operations
Python strings are immutable objects in python. You can't change it once it is created.
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/stringope1.jpg)
Now, you can't change the string by assigning var1[3] to some other character as mentioned below.
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/stringope2.jpg)
Strings are immutable. In the below example, variable var2 is assigned with the string 'AllAboutTesting', and variable an assigned with the string 'InformationSecurity'. Now, again variable var2 assign with the value of variable a. Variable a reassign with the string 'AllAboutTesting'. Now print variables a and var2. Variable var2 still contains the value of variable a which signifies strings are immutable.
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/stringope3.jpg)
Concatenation of strings
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/concatenate.jpg)
The repeated sequence in the string to use as a buffer overflow. You can create those files by using the * operator.
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/buffer.jpg)
Convert an integer into a string and vice-versa by using the int and str functions.
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/int2str.jpg)
String Methods
You can search string in a variable by using the below commands.
<variable_name>.find('<search_string>')
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/find.jpg)
You can split strings in variables by using the below commands.
<variable_name>.split('<search_string>')
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/split.jpg)
You can replace the string in the variable by using the below commands.
<variable_name>.replace('<replace_string>', '<new_string>')
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/replace.jpg)
String Formatting
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/stringformat.jpg)
Numbers
The following operators can be used in python: addition (+), subtraction (-), multiplication (*), division (/), modulus(%) operations, etc.
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/operators.jpg)
Exponent (x**y) operation
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/power.jpg)
Comparison Operators
greater than (>), less than (<), greater than equal to (>=), less than equal to (<=), equal to (==), and not equal to (!=).
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/operators-less-than.jpg)
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.
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/list.jpg)
The list can be altered and searched by different functions. Below are the examples showing built-in append(), replace(), pop(), and reverse() functions.
![](https://i0.wp.com/allabouttesting.org/wp-content/uploads/2018/10/list-operations.jpg)
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.