Python Variables and Data Types

Python Variables and Data Types, Creating Python Variables, Assigning Values to Variables, Variable Types in Python, Global and Local Variables in Python.

Python Variables and Data Types

1. What is a Variable?
2. Variable naming rules
3. Creating Variables
4. Assigning a single value to multiple variables
5. Assigning different values to multiple variables
6. Get the Data Type
7. Global and Local Variables in Python
8. Delete a Variable
9. Getting the Data Type in Python

1. What is a Variable?

• Variable is a named memory location to store the temporary data within a program.

• We have two types of memory in a computer environment (Temporary Memory- RAM and Permanent Memory – ROM), variables store in the Temporary memory (RAM).

• In Python, We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it.

• A variable is a name given to a memory location. It is the basic unit of storage in a program.

2. Variable naming rules

• A variable name must start with a letter or the underscore character.

• A variable name cannot start with a number.

• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).

• Variable names are case-sensitive.

• The reserved words(keywords) cannot be used to name the variable.

3. Creating Variables

Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

Example:

# declaring the variables
x = 123
y = “G C Rddy”
z=10.234

# Print variables
print(x)
print(y)
print(z)

Output:
123
G C Reddy
10.234

4. Assigning a single value to multiple variables

Example:

a = b = c = 10

print(a)
print(b)
print(c)

Output:
10
10
10

5. Assigning different values to multiple variables

Example:

a, b, c = 1, 20.2, “GCReddy Info”

print(a)
print(b)
print(c)

Output:
1
20.2
GCReddy Info

6. Get the Data Type

We can get the data type of a variable with the type() built-in function.

Example:

x = 100
y = “India”
z= 10.234

print(type(x))
print(type(y))
print(type(z))

Output:
<class ‘int’>
<class ‘str’>
<class ‘float’>

7. Global and Local Variables in Python

Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.

This means that local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions.

Example:

x = 100 # Global variable

def func():
y=200 # Local variable

print(x+y)

func()

Output:
300

8. Delete a Variable

We can also delete a variable using del command.

a=123;
print (a); #123

del(a);
print(a); # It will show error

Variable type in Python:

Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes and variables are instances (objects) of these classes.

Python has five standard data types −

1. Numbers
2. String
3. List
4. Tuple
5. Dictionary

9. Getting the Data Type in Python

a=100
b=10.23
c=”India”
d=False
e=12j

f= [“India”, “China”, 10, 20.34, False]
g=(“India”, “China”, 10, 20.34, False)
h={“India”, “China”, 10, 20.34, False}
x= {“Name”: “Latha”, “Age”: 20, “Qualification”: “BTech”}
print(len(x))
print(len(f))
print(“”)
print (type(a)) # int
print (type(b)) # float
print (type(c)) # str
print (type(d)) # bool
print (type(e)) # complex”’
print(“”)
print (type(f))#list
print (type(g))#tuple
print (type(h)) #set
print (type(x)) #dict


Python Step by Step Tutorial

1) In Which sequence should We learn Python?

2) Introduction to Python

3) Download & Install Python

4) Variables and Data Types in Python

5) Python Operators

6) Python Conditional Statements

7) Python Loops…

8) String Handling in Python

9) Python Lists

Follow me on social media: