Python Data Types and Variables

Python is one of the most wanted programming languages today. Developers and programmers want to focus on the implementation part of an application rather than spending time writing the complex code. This is where python comes to use as it has great ease of access and readability. For any programming language in this world, fundamental concepts and theories are its foundation. Hence, in this article, we will learn about python’s data types and variables.

What are the variables in Python?

As the name suggests, variables and data types are the values that vary or change in python. In any programming language, a variable is a memory location where a user stores a value. The value that is stored can change according to the needs and specifications. For example, let us take Name = ‘Sakshi’. Here, Name is the variable and Sakshi is the value stored in the variable. A variable is created as soon as a value is assigned to it in python. We don’t need to give some additional commands for declaring a variable in python. However, there are several rules and regulations that a programmer needs to follow while writing a variable. Let us have a look at them in the next section.

Variable definition and declarations

The rules that we need to keep in mind while declaring a variable are as follows:

  1. The variables are case sensitive.
  2. Python doesn’t allow special characters in a variable.
  3. The variable can only contain underscores and alphanumeric characters.
  4. The name of the variable can’t start with a number. It should only start with an underscore or a character.

There are a few data types in python. Each value that the programmer declares in python has a data type. In short, data types are the classes and variables are the instances of these classes.

Data types in Python

There are mainly 6 data types in python programming language according to the properties that it possesses. However, there is one more data type range in python. We use it while working in loops in python. Now that we have known that the data stored in a variable can be of many types, we will understand this by an example. Let us take Sarah. Her age will be stored as a numeric value whereas her address will be stored as alphanumeric characters. Data types define operations that are possible on them and each one of them has a storage method.

Numerical Data Types

The data types which hold a numerical value are known as numerical data types. This data type has further four subtypes that are:

  • Integers – It represents whole number values. For example, x = 24, y = 500.
  • Float – It represents decimal point values. For example, x = 12.5, y = 70.8.
  • Complex numbers – These numbers represent imaginary values that are denoted with ‘j’ at the end of the number. For example, x = 20 = 8j.
  • Boolean – Boolean is for the categorical output. The output of Boolean will be either true or false, 1 or 0. For example –

Num = 6<10, when we will type ‘print(num)’,it will print true.

Strings

In python, strings are used to represent Unicode character values. There is no character data type in python that is why a single character is also considered as a string. We always denote the string values inside single or double-quotes. For accessing the values in a string, we use indexes and square brackets. Strings are immutable. You cannot change them once replaced. For example, take the code snippet –

Name = ‘sakshi’

Name[2] – this will give output as ‘k’ according to indexes.

Lists

Lists are a part of collection data types in python. When we choose a collection type, we need to understand the limitations and functionality of the collection. Tuples, dictionary, and sets are other collection data types. A list is changeable and ordered. We can also add duplicate values to it. To declare a list, we use square brackets. Moreover, lists can store any data types whether it be numbers, strings, or anything else. Example – mylist = [10,20,30,40,50].

Tuples

Tuples are other collection data types that are immutable or unchangeable. It is an ordered data type and we can access its values by using index values. A tuple can include duplicate values. For the declaration of tuples, we use round brackets. Tuples are unchangeable that’s why there are very few operations that one can perform on it. However, it has an advantage. You can store values in a tuple that you don’t want to be changed. You can always access the values but there won’t be any changes to be made. Example – mytuple = (10,30,50,70).

Sets

The set is an unordered collection data type. It doesn’t have any indexes. For declaring it, we use curly brackets. Example – myset = {10,60,44,76}. It doesn’t contain any duplicate values. Even if the user adds them, it will not show any error and the output will have distinct values as usual. For accessing the values of a set, we can loop through the set or can use a membership operator.

Dictionary

A dictionary is another normal collection data type but it has key-value pairs. It is changeable and not in order. We always use certain keys to access items from a dictionary. For the declaration of a dictionary, we use curly brackets. We use the keys to access items thus there can’t be any duplicate value. However, the values can always have a duplicate item. Example- mydictionary = {‘someone’, ‘anyone’, ‘no one’}.

Range

We use the range when we are using a loop. Let us understand this with an example –

For x in range(5):

Print(x)

This will print the numbers from 0-5.

Leave a Comment