Loading...
Loading...
00:00:00

Identifiers / Variables in Python3

What Is Variable And How To Declare In Python?

In python, There is no keyword for creating a variable. Variables are container that can store any kinds of data. python variable has no need to declare data type. It data type define automatically when we assign value.

In Python 3, an identifier is a name used to identify a variable, function, class, module, or other object.

 

Rule For Creating Variables

Here are some rules for creating variables in Python 3:

  1. Variable names can only contain letters, numbers, and underscores (_). They cannot start with a number.

  2. Variable names cannot contain spaces or special characters, except for the underscore (_).

  3. Variable names should not be the same as Python keywords, such as for, while, if, elif, etc.

  4. Variable names should be descriptive and meaningful, making it easier to understand the code. For example, student_name instead of sn.

  5. Variable names are case-sensitive, so student_name and student_Name are treated as two different variables.

  6. Python has some conventions for naming variables, such as using lowercase letters and separating words with an underscore.

  7. Variables can be assigned values using the = operator. Here are some examples:

Declare And Assign Value In  Variables

# creating variables and assign the value in it.
var1 = "hello World"
var2 = 100
var3 = 35.97
var4 = 'python programming'  
print(var1, var2, var3, var4) # print all variables values

This Is Not Allowed In Python

4var = 200   # error
$x = 78  # error 
first name = "jack"   # error

Operations With Variables

we can reassign variable in python and variable will be automatically convert their type according the value.

# assign multiple value in multiple variables at once
a, b , c = 10, 20, 30  # this is valid syntax
var1 = "hello World"
var2 = 100
# reassign the value
var1 = 34
var2 = "this is string"
print(var1, type(var1))
print(var2, type(var2))
print(a, b, c)

Type Of Variables

We can check the type of variable in python using type() function it retuns the type of variable class

var1 = "hello World"
var2 = 100
var3 = 35.97
var4 = 'python programming'  
dict1 = {"python": 3000, "java": 3050, "C++": 2300}
list1 = [12, 35, 92, 82,-123, "python"]
tuple1 = (23.3, 92, -56, "hello", )
print(type(var1)) # <class 'str'>
print(type(var2)) #  <class 'int'>
print(type(var3)) # <class 'float'>
print(type(var4)) # <class 'str'>
print(dict1, type(dict1))  # <class 'dict'>
print(list1, type(list1))  # <class 'list'>
print(tuple1, type(tuple1))  # <class 'tuple'>

In Python 3, there is no need to specify the type of the variable, as it is a dynamically-typed language, meaning that the type of the variable is determined at runtime based on the value assigned to it.

In Python 3, there are two types of variables:

  1. Global variables: These are variables declared outside of any function or class and are accessible from anywhere in the code. They can be declared and modified anywhere in the code, including within functions and classes.

  2. Local variables: These are variables declared within a function or a class and are only accessible within that function or class. They are created when the function or class is executed and destroyed when it terminates.

Additionally, there are two more variable types:

  1. Instance variables: These are variables that are specific to an instance of a class and are declared within the class but outside of any method.

  2. Class variables: These are variables that are shared among all instances of a class and are declared outside of any method and within the class, but before any instance variables.

It is important to understand the scope of variables in Python and how they can be accessed within different parts of your code. Understanding the different types of variables and their scopes will help you write cleaner, more organized, and more efficient code.