This article will help the reader understand about the basic data types in Python, variables, some commonly used functions with respect to data types and some basic applications in real world. We will be using Visual Studio Code as our code editor. If you have not installed Visual Studio Code, the instructions is given in the previous blog post.

Variables and Data Types in Python – table of contents:

  1. Introduction to Python
  2. Variables in Python
  3. Data types in Python
  4. Next Blog Glimpse

Introduction to Python

As we have learned in the previous blog post that Python is a high-level, interpreted, dynamically typed, and object-oriented language. Due to its high-level nature, the language is very easy to learn, and syntax is also simple. There are a variety of applications of python in real-world like for machine learning, data science, game development, web applications, and many more.

In the previous blog post, we learned on how to print text in Python. We used to print (“your desired text”) as the syntax. Let’s start with what are variables and why do we use variables.

Variables in Python

A variable is an entity that stores a value. The value may be a number, integer, real number, text, or a character. Let’s see some examples of how you can use a variable to store values in Python.

# variables
x = 1  # storing integer
y = 2.5  # storing real number
z = "string"  # storing string or text
n = "a"  # storing a character
b = True  # storing a boolean value
print(x,y,z,n,b)
Output:
1 2.5 string a True

We have seen how to store variables, now let’s see how to print their values. You already know the answer, which is to use print(), which we used in the first blog to print the desired text. Also, see that we are using the variables without using any double quotes or single quotes as opposed to before. This is because a variable is recognized by print directly as it is stored in the memory when it is declared. Now, let’s print the variables.

We can see that the variables are printed as highlighted in the above image. As we can see Python supports most of the different data types in Python like integer, float (real numbers), string (text or characters) and Boolean (True or False).

Data types in Python

Strings

data_types_in_python

What operations can be performed using strings?

  • title()

    This function can be used to capitalize the starting letter of each word in the string as seen below the output is highlighted.

  • text="this blog is awesome"
    print(text.title())
    
    Output:
    This Blog Is Awesome
    
  • upper()

    This function can be used to capitalize the whole words in the string. The example is illustrated in the below image.

  • text="this blog is awesome"
    print(text.upper())
    
    output:
    THIS BLOG IS AWESOME
    
  • lower()

    This function can be used to convert the whole words in the string into lowercase alphabets. The example is illustrated in the below image.

  • text = "this blog is awesome"
    print(text.lower())
    
    Output:
    
    this blog is awesome
    
  • Concatenation of strings

    To combine two different strings “+” can be used. The example is illustrated in the below image.

  • text = "this blog is awesome"
    text2="for beginners"
    print(text+text2)
    
    Output:
    
    this blog is awesomefor beginners
    
    
  • White Spaces

    There are times when you don’t want to print text in a single line but to have multiple lines and sometimes you want text to be having tab space. This can be done in Python by using “\n”(new line) and “\t”(tab space). The example is illustrated below.

  • print("this blog is \nawesome")
    print("\tthis blog is awesome")
    
    Output:
    
    this blog is 
    awesome
      this blog is awesome
    
  • Strips functions

    This is a function in Python that removes any white space in the string. Using strip both left and right white spaces can be removed. But sometimes for the specific requirements for removing white space in left “lstrip()” can be used and for right “rstrip()” can be used. The example with code is illustrated below.

  • z=" hello "
    print(z)
    print(z.strip())
    print(z.rstrip())
    print(z.lstrip())
    Output:
    “ hello “
    “hello”
    “ hello”
    “hello “
    
  • String Length

    By using len() function, a string length can be determined. The example with code is illustrated below. You can see for string “hello”, the output is 5.

    Z="awesome"
    Print(len(Z))
    
    Output:
    5
    

    Integers

    The integers data types in Python are used only when whole numbers are to be stored. There are several operations, which can be performed on integers. Let’s learn about type() function here. The type() function tells you about the variable’s datatype. The example for the type() function with code is illustrated below.

  • a=1
    
    type(a)
    
    output:
    
    int
    

    Floats

    In integer data type variables only, whole numbers can be stored but to include real numbers or to store real numbers we use float. In essence float is used for decimals.

a=1.6

type(a)
output:

float

Operations on Floats and Integers

In our basic mathematics during our high school, we have learned several operations which can be performed on numbers like Addition, Subtraction, Multiplication, Division and many more. We can perform all those operations on floats and integers as illustrated below with code.

# variables

x = 1  # storing integer
y = 2.5  # storing real number
z = "string"  # storing string or text
n = "a"  # storing a character
x = True  # sprint(x,y,z,n,b)toring a boolean value
print(type(x),type(y),type(z),type(n),type(b)) [/code]
output:

<class 'bool'> <class 'float'> <class 'str'> <class 'str'> <class 'bool'>

Boolean

In Python there are times where a developer needs to know if a statement is true or false. Mostly when using conditions, the Boolean are used. Boolean consists of True and False. Not that Python is case sensitive when using Booleans, hence they need to be in the True and False format only.

As we have learned about data types in Python and variables in Python, let’s code a simple program and test our knowledge.