q Python Variable: A Comprehensive Guide to Python Variable
codingstreets
Search
Close this search box.
python-variable-a-comprehensive-guide-to-python-variable

Python Variable: A Comprehensive Guide to Python Variable

This article will help you learn about Python variables, what they are, how to use them to manipulate data, different data type variables, and rules to define their names in Python. We will also put light on some basic operations on numbers and strings.

Table of Contents

Variable

A Variable is like a Python container used to store values of different types, such as numbers, strings, lists, or even complex objects. In Python, a Variable is created when we assign a value to it. 

Unlike other languages, Python has no command to declare the variable. Python variables are dynamically typed, meaning that their type can change during runtime. 

Following are the rules to declare the variable –

  1. The letter can be upper case (A-Z) and lower case (a-z). It should be noted that the variable must begin with letters. It can also have an underscore (_).
  2. Value can be string, number (int and float), and Boolean value (True and False).

Note: Python variable is case-sensitive. Be careful regarding it. Python reserves some variables named or reserved words, so we can’t use them.

				
					Syntax

<variable> = <expression>

				
			

Creating Variables

				
					Name = "codingstreets"
int = 1
Float = 1.1 
_boolean = True
print(Name)
print(int)
print(Float)
print(_boolean)

#output:

codingstreets
1
1.1
True
				
			

Note: Equal sign (=) is used to assign (right-side) the value to the variable (left-side ).

In Python, A variable can have a short name like a and b and a long or descriptive name such as mission, curriculum, and my_name, etc. In a nutshell, a variable name must be started with a letter (a,b) or the underscore character (my_name), and it contains only alphanumeric characters and underscores (A-z, 0-9, and _ ).

Example:

				
					myname = "codingstreets"
Myname = "codingstreets"
my_name = "codingstreets"

print(myname)
print(Myname)
print(my_name) 

#output:

codingstreets
codingstreets
codingstreets
				
			

Here are some examples based on variables that can be used to assign a value to a variable name.

Name 

Example

Uppercase()

Codingstreets

Lowercase()

codingstreets

Uppercase with underscore(_)

Coding_streets

Lowercase with underscore(_)

coding_streets

String, Integer, or Float (generally in mathematical terms, it is referred to as a decimal number)

“CS” , “Codingstreets”, “codingstreets”, 123, 12.3, “coding_streets”, etc.

Single or Double Quotes

In Python, the variable can be written in both single quote (”) and double quote (“), and the value of the variable will be the same in both quotes.

				
					Str = "codingstreets"
# meaning of the variable will be the same
Str = 'codingstreets'
print(Str) 

#output:
codingstreets
				
			

Get Types

In Python, a variable type can get by using the type() function.

				
					Int = 2
Str = "2"
print(type(Int))
print(type(Str))

#output:
<class 'int'>
<class 'str'>

				
			

As Python considers a variable’s latest value, a variable with the same name’s value gets changed or updated automatically. In other words, changing a variable data type into another data type.

				
					Int_number = 1 # Int_number is int.
Int_number = "codingstreets" # Int_number is string.
print(Int_number) 

#output:
codingstreets
				
			

Casting

Specifying a data type of variable is known as Casting.

				
					Float = float(1)
Str = str(3)

print(Float)
print(Str)


#output:
1.0
3
				
			

Case-Sensitive

In Python, the variable’s name is case-sensitive.

				
					x = "codingstreets"
print(X)

#output:
NameError: name 'X' is not defined
				
			

Note: Be careful! Python Variable case is sensitive. If the lowercase variable (add) changes into an uppercase variable (Add) and capitalization (ADD), then Python will give us an error.

Assign Value to Multiple Variables

In Python, you can assign values to multiple variables in one line.

				
					x,y,z = "Alex", "Sara", "Thomas"
print (x)
print (y)
print (z) 

#output:
Alex
Sara
Thomas
				
			

Variable combination

In Python, you can use the (+) operator to combine variables.

				
					x = "programmer"
print("I am a " + x )

#output:
I am a programmer
				
			

(+) the operator also can be used to add variables.

Example:

				
					x = "I like to read articles"
z = "on codingstreets.com" 

print(x+z)

#output:
I like to read articles on codingstreets.com
				
			

Example:

				
					Example:

x = 20
z = 5

print(x+z)

#output:
25
				
			

Note: Python will give us an error if you try to add a string and number.

Example:

				
					name = "Alex"
age = 20

print(name+age) 

#output:
TypeError: can only concatenate str (not "int") to str
				
			

Global Variables

Variables which is created outside the function. It’s called global variables. Like in the above, all examples variable was created outside the function.

Global variables can be used both inside and outside of functions.

Example:

Create a variable outside a function, and use it inside it.

				
					name = "I am Alex."
def myfunc():
     print(name)
myfunc()

#output:
I am Alex.
				
			

If you create a variable with the same name inside a function, then the variable will be local and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.

Example:

Create a variable inside a function with the same name as the global or outside variable.

				
					name = "I am Alex."      #global variable
def myname():
     name = "I am Elisa."    #local variable`
     print(name)
myname()
print(name) 


#output:
I am Elisa.
I am Alex.
				
			

The global Keyword – Normally, when a variable is created inside a function, it works as a local variable and can be used only inside the function. Use the global keyword to use a variable as a global inside function.

Example:

Use the global keyword; the variable belongs to the global scope:

				
					def myname():
     global name
     name = "I am Elisa."
myname()
print(name) 

#output:
I am Elisa.
				
			

Also, use the global keyword to change a global variable inside a function.

				
					name = "I am Alex."
def myfunc():
     global name
     name = "I am Elisa."
myfunc()
print(name) 

#output:
I am Elisa.
				
			

Conclusion

Python variables are powerful tools for storing and manipulating data in a flexible and dynamic manner. Understanding their usage and scope is essential for writing effective and readable Python code.

It’s important to choose meaningful variable names that accurately describe the data they store. This enhances code readability and makes it easier for others (including yourself) to understand and maintain the code.

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on