Python Variables and Data types | Built in Operators

Python Variables and Data types | Built in Operators

In this tutorial we will explain about Python variables and data types – Python variable is a location in memory to store the value you assign to that variable.

Separate declaration of variable is not required, Python internally takes care of variable declaration internally.

Python Variables

A variable is a location in memory used to store some value. Unique names are given to differentiate between different memory locations. A variable name can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).

In Python, we don’t have to declare a variable separately. Simply assign a value to a name and Python will take care of it. We don’t even have to declare type of the variable. Python handles it internally according to the value assigned to it.

Note– Choose your variable names according to intended use. final_result, counter, flag_check are some examples.

Variable assignment

We use assignment operator (=) to assign values to a variable. In Python, any type of value can be assigned in to a valid variable name.

final_result = 10
academy_name = 'RCV Academy'
floating_value = 10.5

Now, if you write print command to print value of variable, it will print the value assigned to that variable.

print(academy_name)

The output of the above statement will be

RCV Academy

Python will decide the type of variable according to the value assigned to it. For example, final_result will be an integer variable as integer value is assigned to it.

Python data types

Basic data type

We will discuss Python data types in two parts. First let’s look on some basic data types.

a. Numbers

Integers, Float and complex numbers falls under the category of Python numbers.

int_variable=10
float_variable=10.5
complex_variable=1+2j

print(type(int_variable))
print(type(float_variable))
print(type(complex_variable))

Here, we have defined three variables and assigned different type of numbers to them. To check that python has correctly interpreted these variables we can use a built-in function called type. It will show what kind of variable it is.

Output of the above code will be

<class 'int'>
<class 'float'>
<class 'complex'>

Note – Everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes. We will discuss object-oriented programming in detail later.

b. Strings

A sequence of characters is called string. We can use single quote or double quote to represent strings.

string_variable_1="I am a string"
string_variable_2='I am also a string'
print(string_variable_1)
print(string_variable_2)

Output of the above code will be:

I am a string
I am also a string

Some built-in operators for strings

There are four main methods

  1. len() – returns the length of the string.
  2. lower() – replace all capital letters to lower letters.
  3. upper() – just opposite to above.
  4. str() – The str()method turns non-strings into strings.
test_string="rcvacademy"
test_integer=10
convert_to_capital=test_string.upper()
check_length=len(test_string)
convert_to_string=str(test_integer)

print(convert_to_capital)
print(check_length)
print(type(convert_to_string))

Output of the above code will be:

RCVACADEMY
10
<class 'str'>

c. Bool

This is a special data type provided by python. It only supports two values “True” and “False” as input.  This is useful to set some flag condition.

bool_variable=True
print(bool_variable)
bool_variable=False
print(bool_variable)

Output of the above code will be

True
False