Data Types and Variable Naming in Python

Data types in Python

We store data in different formats and therefore, it becomes important for us to know the type of our data. It helps us in performing different kinds of operations and avoid preventable errors.

In Python, we store our data using variables. These variables have different data types. Since Python is a dynamically typed language we do not need to explicitly declare the type of the variable while declaring it.  The interpreter implicitly understands its type with the help of the value provided. However, if we wish to check the type of the variable, Python permits us to do so with the help of type ( ) function. The type () function displays the type of the variable in the output.

The Standard Data Types in Python are:

  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary

Numbers

This type comes into picture when we assign numeric values to the variable. There are four subcategories in this data type.

  • int ( for signed integers like -2,10,9,etc.)
  • long ( for higher range integer values )
  • float ( for floating point numbers like 1.5, 2.354, etc.)
  • complex ( for complex numbers like 3.14j, 1.0 + 10.1j, etc.)
#Numbers
i = 1
print(type(i))
class 'int' #Output 

f = 3.14
print(type(f))

class 'float' #Output

c = 1+2.14j
print(type(c))

class 'complex' #Output

String

A string is a sequence of characters. We assign a string to a variable using single quotes or double-quotes.

#String
a = "This is a string"
print(type(a))

class 'str' #Output 

List

List is like a sequence of values where the values can be of any type. The values in the list are called elements or items.  The elements of the list are enclosed within square brackets [] and separated by a comma (,). A single list can contain integers, strings, and objects also.

#List
a = [10,20,30,40,50]
print(a)
print(type(a))

[10, 20, 30, 40, 50] #Output
class 'list' #Output 

b = ['string', -2,4.555] #containing different data types
print(b)
print(type(b))

['string', -2, 4.555] #Output
class 'list' #Output

Tuple

A tuple is similar to a list in many ways. The items of the tuple are enclosed in parentheses () and separated with a comma (,). Tuples can also contain items of different data types. The difference between a list and a tuple is that lists are mutable i.e. it can be changed, whereas, tuples are immutable.

#Tuple
t = (10,20,30,40,50)
print(t)
print(type(t))

(10, 20, 30, 40, 50) #Output
class 'tuple' #Output

b = ('string', -2,4.555) #containing different data types
print(b)
print(type(b))


('string', -2, 4.555) #Output
class 'tuple' #Output

Dictionary

A Dictionary is a set of key-value pairs. It is used to store data values like a map since we don’t have a single value as an element but we have a pair of a key and its value.  The items in the dictionary are enclosed within curly braces {} and separated by a comma (,).  The key-value pair within the dictionary is written using the following syntax – key: value.

#Dictionary
d = {1:'This',2:'is',3:'a',4:'dictionary'}
print(d)
print(type(d))

{1: 'This', 2: 'is', 3: 'a', 4: 'dictionary'} #Output
class 'dict' #Output

Variable Naming in Python

Since we use variables extensively, it becomes important for us to know about variable naming. We generally choose names that are meaningful and help us understand our code in a better manner.  However, there are a few rules which we need to consider while naming our variables. Let’s look into it.

  • Variable names can contain both letters and numbers, but they cannot start with a number.
  • The underscore character ( _ ) can be used and we can also start the name using that.
  • The variable can be named using both Upper case and lower case letters.
  • No other special character or white space is allowed while naming the variable.
  • Variables cannot be named using any of the Python keywords. The keywords are as follows :

Hence, we need to keep in mind the above-mentioned rules while naming any variable in Python.

About the Author

Jinal Shah

Graduate in Applied Statistics & Analytics (NMIMS) and Aspiring Actuary

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.