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:
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
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
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
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
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
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 “
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 valueprint(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
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'
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
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'
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
This function can convert any integer, string, float value into a Boolean value.
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.
Python Course From Beginner to Advanced in 11 blog posts:
- Introduction to Python Course. Part 1 Python Course from Beginner to Advanced in 11 blog posts
- Variables and Data Types in Python. Part 2 Python Course from Beginner to Advanced in 11 blog posts
- Python tuples, lists, sets and dictionaries. Part 3 Python Course from Beginner to Advanced in 11 blog posts
- Python sets and dictionaries. Part 4 Python Course from Beginner to Advanced in 11 blog posts
- Conditional statements in Python. Part 5 Python Course from Beginner to Advanced in 11 blog posts
- Loops in Python. Part 6 Python Course from Beginner to Advanced in 11 blog posts
- Python functions. Part 7 Python Course from Beginner to Advanced in 11 blog posts
- Advanced functions in Python. Part 8 Python Course from Beginner to Advanced in 11 blog posts
- Python classes and objects. Part 9 Python Course from Beginner to Advanced in 11 blog posts
- Files in Python. Part 10 Python Course from Beginner to Advanced in 11 blog posts
- Python applications in practice. Part 11 Python Course from Beginner to Advanced in 11 blog posts