Apply for Zend Framework Certification Training

Python





Variables

Python Variable is containers that store values. Python is not “statically typed”. 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 Python variable is a name given to a memory location. It is the basic unit of storage in a program. In this article, we will see how to define a variable in Python.

Example of Variable in Python

An Example of a Variable in Python is a representational name that serves as a pointer to an object. Once an object is assigned to a variable, it can be referred to by that name. In layman’s terms, we can say that Variable in Python is containers that store values.

Here we have stored "webphplearn"  in a variable var, and when we call its name the stored information will get printed.

Rules for Python variables

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

A Python variable name cannot start with a number.

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

Variable in Python names are case-sensitive (name, Name, and NAME are three different variables).

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

 

Output

Rajesh

35

78.67

Rajesh 35 78.67

 

Assigning single value to multiple variables

name = "Rajesh" ; age=35 ; marks=78.67

print(name)

print(age)

print(marks)

 

print(name,age,marks);

#Assigning single value to multiple variables

x=y=z=151;

print(y)

# Assigning multiple values to multiple variables

p,q,r = 10,20,30;

print(p);

# Local Variables

def displayvaribles():

    d=20;

    e=30;

    f= d+e;

    print("The sum of D and E is ",f)

 

displayvaribles()

 

#print(d)

#Global Variables

p=121;

def displayvaribles2():

    global p;

    print(p)

    p="Hello World"

    print(p)

 

displayvaribles2();

#Delete a variable

q1= 20;

print(q1)

del q1;

print(q1)

 

< What is python Operators in python >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top