Blog

Variables and Data Types in Python. Part 2 Python Course from Beginner to Advanced in 11 blog posts

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

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.

    • Type Conversion

      The type conversion is a process where you convert one datatype variable into another datatype variable.

    • int()

      This converts a number that is in string form or a float to integer value. The example is illustrated below with the code.

    a="6"
    b=6.5
    print(int(a),int(b))
    
    output:
    
    6 6
    
  • Note

    The int() can only convert numbers in string form to integers but not characters. If characters are used in int(). then it will give an error as illustrated below.

  • a="a"
    
    print(int(a))
    
    output:
    
    --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-128-d5a3b8380653> in <module> 1 a="a" 2 ----> 3 print(int(a)) ValueError: invalid literal for int() with base 10: 'a'
    
  • float()

    This is used for converting any real number in string form or any integer to float as illustrated in below code.

  • a="6.5"
    b=7
    print(float(a),float(b))
    
    output:
    
    6.5 7.0
    
  • Note

    The float() can only convert numbers in string form to float but not characters. If characters are used in float(). Then it will give an error as illustrated below.

  • a="a"
    
    print(float(a))
    
    output:
    
    --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-130-f48cb0b32023> in <module> 1 a="a" 2 ----> 3 print(float(a)) ValueError: could not convert string to float: 'a'
    
  • str()

    This function can convert any integer or float value to string form. The example is illustrated below.

  • a = 6
    b = 6.7
    c = True
    
    print(str(a), str(b), str(c))
    
    output:
    
    6 6.7 True
    
  • bool()

    This function can convert any integer, string, float value into a Boolean value.

  • Note

    If the values in integer or float are 0, then the bool() will give False. For strings, if the string is empty then False. The example is illustrated below.

  • a = 0
    b = 0
    c = ""
    
    print(bool(a), bool(b), bool(c))
    
    output: False False False

    Next Blog Glimpse

    In the next blog post we will be learning about Lists, Tuples, Sets and Dictionaries.

    You may also like our JavaScript Course from Beginner to Advanced.

    Author: Robert Whitney

    JavaScript expert and instructor who coaches IT departments. His main goal is to up-level team productivity by teaching others how to effectively cooperate while coding.

    Robert Whitney

    JavaScript expert and instructor who coaches IT departments. His main goal is to up-level team productivity by teaching others how to effectively cooperate while coding.

    Recent Posts

    Sales on Pinterest. How can it help with building your e-commerce business?

    Pinterest, which made its debut on the social media scene a decade ago, never gained…

    3 years ago

    How to promote a startup? Our ideas

    Thinking carefully on a question of how to promote a startup will allow you to…

    3 years ago

    Podcast in marketing: what a corporate podcast can give you

    A podcast in marketing still seems to be a little underrated. But it changes. It…

    3 years ago

    Video marketing for small business

    Video marketing for small business is an excellent strategy of internet marketing. The art of…

    3 years ago

    How to promote a startup business? Top 10 pages to upload a product

    Are you wondering how to promote a startup business? We present crowdfunding platforms and websites…

    3 years ago

    How to use social media to increase sales?

    How to use social media to increase sales? Well, let's start like that. Over 2.3…

    3 years ago