Python Beginner Series

Ishwarya Lakshmi.S.S.
3 min readJun 1, 2021

What is a variable in Python and how to declare a variable in Python?

A Variable is a memory location used to store values of different data types. Variables are the basic tokens of any programming language. Variables are case-sensitive in Python. Variables are used to store data tokens that are fed for processing. Basic data types are String, Integer, Float, and Boolean.

Syntax to declare a variable in Python is very simple. We can create it by just mentioning Variable_name=Variable_Value.

Let us have a look at a sample for this syntax.

Declaring variable in Python

In this sample, we can see that the name variable is assigned a String data type within ‘ ‘.

Followed by age which is an Integer data type, height which is a Float data type, and marital_status which is a Boolean data type.Other than predefined variables python also enables us to create runtime variables which could be prompted to get user input.

How to create a runtime variable in Python?

To declare runtime variable input() function is used. Question or text to be prompted can be added within the function to enable user answer the prompt.Later the variable can be used to print or can be used anywhere in code where it is needed to display user information.

Syntax to create a runtime variable in Python

How to find the data type of a variable?

Python has an inbuilt function called type () which can be used to find out the data type of a variable.

Syntax to find out data type in Python

The string data type is used to represent text data. Numeric data is represented using Integer and Float data type. True or False kind of data is represented using Boolean data type. Other than these common data types there are also four advanced built-in Python data types like Tuple, List, Set, and Dictionary which would be covered in subsequent blogs.

Can we convert one data type from one type to another in Python?

Yes, we can convert one data type to another data type and this functionality is called type casting. Type casting comes in handy when you want to swap data types without wanting to change values in the variable. Though we can convert data types from one to another there is an exception in one case where we cannot convert String data type to Integer data type.

How to do type casting in Python?

We have two methods of typecasting in Python which is Implicit typecasting and Explicit type casting.

Implicit type casting happens when any operation is carried out with two different data types.

For example, if addition is carried out between an Integer data type and Float data type then the result will be of Float data type.

Implicit typecasting

Explicit type casting:

Explicit type casting can be done by using data type as a function.

I hope this blog would have helped you learn how to print statements in Python. For more such blogs do follow me on Medium.

--

--

Ishwarya Lakshmi.S.S.

Creating stories from complex datasets | BI solutions Developer @Visual BI Solutions|Data Analyst | Python,SQL,Tableau,Power BI,Machine Learning