Variables in Python - Practical Examples of Python Variable

Variables In Python

In Python programming, a variable is a container (storage area) to hold data. Let’s See For example,

number = 10

Above, number is the variable storing the value 10.

Assigning values to Python Variables

As like we can see from the upon example, we can use the assignment operator = to assign a value to a variable.

# assign value to site_name variable
site_name = 'devsenv.pro'

print(site_name)

# Output: devsenv.pro

Now in the upon example, we assigned the value'devsenv.pro' to the site_name variable. After, we printed out the value assigned to site_name.

Changing the Value of a Variable in Python Programming

site_name = 'devsenv.pro'
print(site_name)

# assigning a new value to site_name
site_name = 'akash.com'

print(site_name)

Output :

devsenv.pro'
akash.com

Above, the value of site_name is changed from 'devsenv.pro' to 'akash.com'.

Example: Assigning multiple values to multiple variables In python

a, b, c = 5, 3.2, 'Hello'

print(a)  # prints 5
print(b)  # prints 3.2
print(c)  # prints Hello 

If you want to assign the same value to multiple variables at once again, we can do this as below-

site1 = site2  = 'programiz.com'

print(site1)  # prints devsenv.com
print(site2)  # prints devsenv.com

Below, you have assigned the same string value 'devsenv.com' to both the variables site1 & site2.

Rules for Naming Variables In Python

  • Constant and variable names should have a abbreviation of letters in lowercase (a to z) or uppercase** (A to Z) **or digits (0 to 9) or an underscore (_). For example Below:
snake_case
MACRO_CASE
camelCase
CapWords
  • Create a name that be aware. For example, vowel makes more be aware than v.
  • Suppose you want to create a variable name having two words, use underscore to several them. For example below:
my_name
current_salary
  • Python is a case-sensitive. So num and Num are several variables. For example below,
var num = 5 
var Num = 55
print(num) # 5
print(Num) # 55
  • Eliminate using keywords like if, True, class, etc. as like variable names.

Constants In python

A certain is a special type of variable whose value cannot be changed.

In Python Program, certain are generally declared and assigned in a module (a new file containing variables, functions, etc which is imported to the main file).

Now let’s see how we declare constants in several file and use it in the main file.

Create a constant.py:

# declare constants 
PI = 3.14
GRAVITY = 9.8

Create a main.py:

# import constant file we created above
import constant

print(constant.PI) # prints 3.14
print(constant.GRAVITY) # prints 9.8

In the upon example, we maked the constant.py module file. After, we assigned the constant value to PI and GRAVITY.

After this, we make the main.py file and import the constant module. Finally, you printed the constant value.

Literals In Python

Literals are deputation of fixed values in a program. Which can be numbers, characters, or strings, etc. For example, 'Hello, World!', 12, 23.0, ’C', etc.

Literals are often to used to assign values to variables or constants. For example below-

site_name = 'devsenv.com'

In the upon example, site_name is a variable, and 'devsenv.com' is a literal.

Numeric Literals In python

Numeric Literals are unchangeable). Numeric literals can belong to 3 several numerical types: Integer, Float, and Complex.

Screenshot-2024-02-09-003253

Boolean Literals In python

We see there are two boolean literals: True and False.

Let’s see for example-

result1 = True  

Above, True is a boolean literal assigned to result1.

String and Character Literals in Python Program

Character literals are unicode characters surrounded in a quote. For example-

some_character = 'S'

Above, S is a character literal assigned to some_character.

Likewise, String literals are series of Characters enclosed in quotation marks.

For Example-

some_string = 'Python is fun' 

Above, 'Python is fun' is a string literal assigned to some_string.

Special Literal in Python Program

Python takes on one special literal None. We can use it to specify a null variable. For example below-

value = None

print(value)

# Output: None

Above, we will show None as an output as the value variable has no value assigned to it.

Literal Collections In python

There are four several literal collections List literals, Tuple literals, Dict literals, and Set literals.

# list literal
fruits = ["apple", "mango", "orange"] 
print(fruits)

# tuple literal
numbers = (1, 2, 3) 
print(numbers)

# dictionary literal
alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} 
print(alphabets)

# set literal
vowels = {'a', 'e', 'i' , 'o', 'u'} 
print(vowels)

Output :

['apple', 'mango', 'orange']
(1, 2, 3)
{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'e', 'a', 'o', 'i', 'u'}

In the upon example, we maked a list of fruits, a tuple of numbers, a dictionary of alphabets having values with keys designated to every value and a set of vowels.

Previous
Hello World In Python - First Programming Example in Python
Next
Python Functions Mastery Your Complete Guide for Success