
A python datatype is a core feature of the language, and a python variable is the name of the memory location where the value is stored while the program is running.
We will go through the python variable, variable name, and python datatypes in depth in the article Python Variable and Python Datatype.
Table of Contents
Python Variable
Python variable is the name of a memory region where the value is stored while the program is running. Variables in Python don’t need to be declared explicitly. When you assign a value to a variable, the declaration occurs automatically. When assigning values to variables, the equal(=) symbol is utilized.
Variables Naming
- The variable’s first character must be an alphabet or an underscore (_).
- Other than the first letter, the remaining characters can be an alphabet (a-z, A-Z), underscore, or number (0-9).
- There must be no whitespace or special characters in the name (!, @, #, %, ^, &, *).
- The name must not be identical to any of the language’s keywords.
- Case matters in names; for example, geekcer and GeekCer are not the same thing.
- Valid identifiers include x, _var, var20, var20t, _10, and etc.
- Invalid identifiers include 5n, %t, g c, 5_ etc.
Define Variables
x = 10
str = "Geek"
print(x);
print(str);
Display Single Variable
a = 5
print(a)
print((a))
Display Multiple Variable
a = 5
b = 6
# printing multiple variables
print(a,b)
# separate the variables by the comma
print(1, 2, 3, 4, 5, 6, 7, 8)
Multiple assignment in variable
Python allows you to assign a single value to many variables at the same time.
a = b = c = 20
a,b,c=10,20,35
Types of Variables in Python
In Python, there are two types of variables.
- Local Variable
- Global Variable
1) Local Variable
# Declaring a function
def add():
# Defining local variables. They has scope only within a function
a = 20
b = 30
c = a + b
print("The sum is:", c)
# Calling a function
add()
2) Global Variable
# Declare a variable and initialize it
x = 101
# Global variable in function
def mainFunction():
# printing a global variable
global x
print(x)
# modifying a global variable
x = 'Welcome To GeekCer'
print(x)
mainFunction()
print(x)
Methods for making variables more readable
There are a variety of approaches that may be used to make variables more readable.
- Camel Case (myVariableName)
- Pascal Case (MyVariableName)
- Snake Case (my_variable_name)
1) Camel Case
Except for the initial word, each word should begin with a capital letter.
myVariableName = "GeekCer"
2) Pascal Case
The first letter of each word should be capitalized.
MyVariableName = "GeekCer"
3) Snake Case
An underscore character should be used to separate each word.
my_variable_name = "GeekCer"
Python Datatype
A programming language’s datatype is a crucial part. Variables can hold data in a variety of formats. When you give a value to a variable in Python, the data type is automatically set.
Python also comes with a number of built-in data types, including dict, list, set and frozenset, and tuple. Unicode strings are stored in the str class, whereas binary data is stored in the bytes and bytearray classes.
A python datatype is a property of data that informs the compiler or interpreter how the programmer wants to use it.
The following are some of Python’s most significant datatypes:
- Numeric Types (Int, Float, Complex)
- Sequence Types (list, tuple, range )
- Mapping Type(dict)
- Text Type (str)
- Set Types (set, frozenset)
- Boolean Types (bool)
- Binary Types (bytes, bytearray, memoryview)
1) int datatype
The int datatype is used to hold any integer, such as 10, 20, 2, 50, and so on. In Python, the length of an integer is unrestricted.
x = 10
2) float datatype
The float datatype is used to hold floating-point numbers such as 10.02, -20.6, 2.65, and 50.5, among others.
x = 10.5
3) complex datatype
The complex datatype is used to hold data that is complex in nature. Ex: x + iy, where x represents actual data and y represents imaginary data.
x = 10 + 1j
4) String datatype
Strings, which may be represented in a variety of ways, can also be stored and manipulated in Python. Single quotes (‘…’) or double quotes (‘…’) can be used to enclose them (“…”).
str = "GeekCer"
str = 'GeekCer'
5) List datatype
The list is a very flexible Python datatype that may be expressed as a list of comma-separated values (items) enclosed in square brackets. Lists in Python are comparable to arrays in programming languages such as C and C++. Lists may contain items of many types, although they generally all belong to the same category.
squares = [1, 4, 9, 16, 25]
6) Dictionary (dict) datatype
The data is stored in a dictionary as a key-value pair.
x = {"name" : "Geek", "age" : 30}
7) Boolean (bool) datatype
true and false are the built-in values of the Boolean datatype.
x = True
8) Set datatype
Sets are used to hold numerous elements in a single, unordered, and unindexed unit. Curly brackets are used to write it. Duplicate items are not allowed in sets.
x = {"apple", "banana", "cherry"}
If you are unfamiliar with Python, you may learn more by clicking here.
Display the type of variable
The type() method may be used to determine the type of a variable.
x = 30.3
print(type(x))