0% found this document useful (0 votes)
3 views6 pages

Variable

Python variables are dynamic containers that store values without the need for prior declaration or type specification. They follow specific naming rules and can be assigned values in various ways, including multiple assignments and type changes. Additionally, Python distinguishes between local and global variables, and it supports various built-in data types such as numeric, text, sequence, boolean, set, and dictionary.

Uploaded by

rj0110865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Variable

Python variables are dynamic containers that store values without the need for prior declaration or type specification. They follow specific naming rules and can be assigned values in various ways, including multiple assignments and type changes. Additionally, Python distinguishes between local and global variables, and it supports various built-in data types such as numeric, text, sequence, boolean, set, and dictionary.

Uploaded by

rj0110865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Variable

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.
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.

x, y, z = "Orange", "Banana", "Cherry"

print(x)
print(y)
print(z)
Variables Assignment in Python
# An integer assignment
age = 45

# A floating point
salary = 1456.8
# A string
name = "John"

print(age)
print(salary)
print(name)

Declaration and Initialization of Variables


Let’s see how to declare a variable and how to define a variable and
print the variable.
Python
# declaring the var
Number = 100

# display
print( Number)

Python Assign Values to Multiple Variables


Also, Python allows assigning a single value to several variables
simultaneously with “=” operators.
For example:
Python
a = b = c = 10

print(a)
print(b)
print(c)
Assigning different values to multiple variables
Python allows adding different values in a single line with “,”
operators.
Python
a, b, c = 1, 20.2, "Alankar PG Girls College"

print(a)
print(b)
print(c)
Can We Use the Same Name for Different Types?
If we use the same name, the variable starts referring to a new value
and type.
a = 10
a = "BCAIII"

print(a)
How does + operator work with variables?
The Python plus operator + provides a convenient way to add a value
if it is a number and concatenate if it is a string. If a variable is
already created it assigns the new value back to the same variable.
Python
a = 10
b = 20
print(a+b)

a = "Alankar PG"
b = "Girls College"
print(a+b)
Can we use + for different Datatypes also?
No use for different types would produce an error.
Python
a = 10
b = "students"
print(a+b)

Global and Local Python Variables


Local variables in Python are the ones that are defined and declared
inside a function. We can not call this variable outside the function.
Python
# This function uses local variable s
def f():
s = "Welcome "
print(s)
f()
Global variables in Python are the ones that are defined and declared
outside a function, and we need to use them inside a function.
Python
# This function has a variable with
# name same as s
def f():
print(s)
# Global scope
s = "I like youtube "
f()
Rules of global keyword
 If a variable is assigned a value anywhere within the function’s
body, it’s assumed to be local unless explicitly declared as
global.
 Variables that are only referenced inside a function are implicitly
global.
 We use a global in Python to use a global variable inside a
function.
 There is no need to use a global keyword in Python outside a
function.

Example
Create a variable outside of a function, and use it inside the function
x = "awesome"

def myfunc():
print("Python is " + x)

myfunc()

Example
 Create a variable inside a function, with the same name as the
global variable
 x = "awesome"

def myfun():
x = "fantastic"
print("Python is " + x)
myfun()

print("Python is " + x)

Variable Types 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 (object) of these classes.
Built-in Python Data types are:
 Numeric
 Text Type
 Sequence Type (Python list, Python tuple, Python range)
 Boolean
 Set
 Dictionary

You might also like