How to Create Variables in Python For All Data Types

Since Python is an object-oriented programming language. You have to declare or create variables in python to store values. But what is a variable in Python and how you can assign a value?

There are different types of variables in Python. How you can create different types of variables in Python. The answer to all these questions is given in this tutorial.

In the end, you will also get section below which teaches you how to create a constant variable also in Python. So, keep reading this tutorial to the end.

What is Variable in Python

Variables are the memory locations used to store data or values of different data types. You have to give a unique name to a variable to identify them from other variables in Python.

If you are a Java or C++ developer, you have to first declare a variable to use it later. However, in Python, you don’t need to declare or create variables in Python before using it. You also don’t need to declare a type of variable.

The variables are stored in the memory locations based on its data type.

For a real-life example, suppose there is a class in a school(for example: 12th class). The class is a variable and student of that class are the objects of this class. You have to declare a name for this class. After declaration, you can store values or name of the student which you can change later through programming.

Create or Declare Variable and Assign Values

In Java or C++ programming language, you have to declare a data type to a variable. In java or C++, You can use the variable, after its type declaration.

However, in Python, you do not need to declare a variable type before using them in programming. When you assign a value to a variable, its data type automatically gets created.

Naming Convention to Declare or Create Variables in Python

Before creating a variable, you have to find a meaningful name of the variable. A meaningful variable makes it easy for programmers to identify and assign desired values.

To assign a value to a variable, you have to first create variables in python with the naming conventions. Use camelcase alphabets to declare a variable. The start letter of the variable should be a small letter.

Don’t use symbols like @,#,$ etc. Don’t start a variable with numbers.

Example: yourName, yourEmail, yourTotal.

Short Names For Variables:
You can also define short names for variables like x and y. It can be small or capital. After you create variables in python, you can start assigning values to them according to its data type.

Different Variable Data Types

There are 5 types of data in Python to declare a variable. Based on these data type, you can perform different operation on them based on its data type. The storage method and the reserved memory location are based on these data types.

  • Numbers
  • List
  • String
  • Tuple
  • Dictionary
  • Set

Number Variable in Python

You have to create a number variable to create objects with numeric values. There are four types of number variables: -integer, float, long and complex. The tutorial for a long and complex variable is not given here. You will learn explained examples for integer and float only.

Define Integer Variable
To define the integer variable, you have to use the below-given example. Change the example value and variable as per your requirement and create your own integer variable.

Output

18

The above example using the print statement to print the integer value. It also uses the Python single line comment to describe the purpose of the code.

Define Floating Point Variable
To create a floating point variable, you have to use the below-given syntax. You can assign a floating point value to create a floating point variable.

Output

10.12

The above example assigns the floating point number to the integer variable. The output prints the floating point number and gives the floating point value.

List Variable in Python

A list variable is a list of comma separated items enclosed within the square brackets. In the list variables, the string is added with single quotes and integers are added without quotes.

This is similar to the array we create in PHP, Java or C++. However one thing should note here, the items in the array can be of different data types.

To access the item of list variable in Python, you have to use the slice operator([]). The list variable starts with the indexed item 0. If you want to access more than one item or multiple items, you have to use another slice operator ([:]).

Output

Ram
Shyam
[‘Ram’]
[‘Shyam’, 10]
Feroz

The above example uses the Python print statement to print the items of list variable.

Concatenate Two List Variables
To concatenate two list variables in one, you have to use the plus(+) operator. See the example below to understand the concatenation of the list variables.

Output

[‘Ram’, ‘Shyam’, 10, ‘Bilal’, 13.2, ‘Feroz’, 12, ‘Kiran’]

The above example combines or concatenates the two Python list variables. The output prints the combined List of items of list variable after concatenation.

String Variable in Python

A string variable is a variable you can define using single and double quotes. Add multiple words to a single string embedded within single or double quotes.

Output

This is my string.

The above example prints the string using the Python print statement.

Tuple Variable in Python

A tuple is a sequence of comma separated values. It looks similar to the list variable of Python. However, the difference between the list variable and the tuple variable is the parenthesis.

The list variable is with square bracket while tuple variable is round bracket.

To access the item of Python tuple variable, use the slice operator([]). You have to tuple variable start with the indexed item 0. However, to access more than one tuple variable item or multiple items, you can use this slice operator ([:]).

Output

Ram
Shyam
[‘Ram’]
[‘Shyam’, 10]
Feroz

The above example contains the same element as of list variable. If you print the same sequence, you will get the same result as you get in list variable.

Dictionary Variable in Python

Dictionary variables are the variable that contains the element with keys. The values and keys of variables are enclosed within the curly brackets. Each element of the dictionary variables is paired with the keys and its value.

If you want to access the elements of the dictionary variable. You have to use the square bracket inside which you have to mention the keys. The mentioned key gives its paired value in the output.

Output

{‘one’: ‘Ram’, ‘two’: ‘Shyam’, ‘three’: 10, ‘fore’: ‘Bilal’, ‘five’: 13.2, ‘six’: ‘Feroz’}
Ram
Shyam

The above example prints the output as mentioned in the keys with values. The first line contains all the elements with keys and value pairs.

Set Variable in Python

Set variables are the variable that contains the comma-separated elements enclosed within the curly brackets. If the elements are string, they should be enclosed within double or single quotes. While, the integers are added without using the quotes. all the elements of Set variable are unique and the order is not defined.

If you want to access the Set elements, you have to use the for loop as given below:

Output

Ram
Bilal
10
13.2
Shyam
Feroz

The above example shows the for loop in Python and the elements printed in the output. The elements of Set are comma-separated and enclosed within the curly brackets.

Assign Multiple Values to Multiple Variables in Single Line

In addition to above all, you can also create variables in python with multiple value and variables. If you want to assign multiple values to the multiple variables in a single line. You have to create a number of variables in comma separation. After that include the same number of values as given in the example below.

The left-hand side of equals to operator contains the number of variables. The right-hand side of the equals to operator contains the same number of values.

To access the values of the multiple variables, you have to use the variable.

Output

Feroz
13.9
Saroj

The above example uses the print function and prints all the values as output.

What is a Constant Variable in Python

A constant variable of Python is the variable which you cannot change after assigning a value. A constant variable will remain constant once assigned a value to it. You can create a constant variable to use in all your application with only the same or single value.

If you think of a variable, you will find it dynamic to change. However, a constant variable is a static variable with static value once assigned.

Declare a Constant Value in Python

To create or declare a constant variable, you have to use capital letters. You can create constant with capital letter and letter separated with underscores.

Output

19

The above example contains the single constant variable. To print the constant variable, you have to use the print statement. It prints the constant variable in the output section.

You may also like to read how to comment in Python. Also, if you have no text editor to write and execute Python program. You must read our post based on Best IDE text editor for Python programming.

If you want to know different ways of executing Python code. You may like to read post on how to execute Python code.

have not installed Python yet, read post to download and install Python.

References

Hope you like this tutorial of create variables in Python for all data types.

Leave a comment

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.