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.

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

The output of the above statement will be

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.

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

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.

Output of the above code will be:

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.
Output of the above code will be:

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.

Output of the above code will be

Team RCV Academy

About Team RCV Academy

RCV Academy Team is a group of professionals working in various industries and contributing to tutorials on the website and other channels.

View all posts by Team RCV Academy