variables in python

Python Variables | Syntax | examples

Python Variables Definition

Variables in simple terms are names you give to computer memory locations that are helpful to store values in programming or A variable is a name that refers to a value.

Create Python Variable

unlike Java or any other programming language Python has no special commands for declaring variables. You can create a variable with a name, equal sign (the symbol = ), and value.

variable_name = value_here

Even you do not need to mention the type of value in python. It has two parts, the variable(name) and its value.

These values belong to different types, for example, 4 is an integer, and “Hello, World!” is a string, so-called because it contains a “string” of letters. The interpreter can identify strings due to quotation marks.

Here are some examples of Python Variables

# Python Variables
   message = “Hello, World!”
   num = 20
   pi = 3.1415926535897931
# here the 'pi' is the name of the variable and 3.1415926535897931 is its value.

If you are not sure what type a value has, You can use the type() method. Pass variable as a parameter.

Because it creates confusion, for example, what about values like “20” and “3.14”? They look like numbers, but they are in quotation marks like strings, so they are strings in python.

type('Hello, World!')
# 'str'

type(20)
# 'int'

type(3.14)
# 'float'

type('20')
# 'str'

type('3.14')
# 'str'

How does Python handle large integer values?

When you type a large integer, you might be tempted to use commas between
groups of three digits, as in 1,000,000.

# Example 
# Python variables
   large_int = 2,000,00
   print(large_int)
   #Output: (2, 0, 0)

# Actually print large number
  large_num = 2_000_00
  print(large_num)
  # Output: 200000

Python interprets 2,000,000 as a comma-separated sequence of integers, that is printed with spaces between.

Note: You can write the strings with single or double quotes but make sure that is case-sensitive.

Rules for Python variables

  • A variable name must start with a letter or the underscore character
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • A variable name cannot start with a number
  • Variable names are case-sensitive (name, Name, and NAME are not variables)
#Example
#legal variable names in python
   varname = "John doe"
   var_name = "John doe"
   _var_name = "John doe"
   varName = "John doe"
   VARNAME = "John doe"
   var_name1 = "John doe"

#illegal variable names in Python
   1vaename = "John doe"
   var-name = "John doe"
   var name = "John doe"
###

Assign Multiple Values to Multiple Variables

Python allows you to assign values to multiple variables in a single line. consider the simple example.

#Assign Multiple Values to Multiple Variables
    name, age, location = "John Doe", "31", "US"
    print(name)
    #Output: 'John Doe'

Global Variables in Python

Global variables in Python are defined outside a function, usually on top of the program.

#Python Global Variables
    name = "Jhon Doe"
    def getName():
      print("My name is " + name)
    getName()
    #Output: My name is Jhon Doe

The above example name = "Jhon Doe" is a Global variable. It has a global scope and is accessible inside the getName() method.

Local variables in python

The local variables in Python are defined inside of a function. they have local scope.

we will discuss the local and global scope later here in this chapter.

Consider the following example of local variables.

#Local variables in python 
    def getName():
      name = "Jhon"
      print("My name is " + name)
    getName()
    #Output: My name is Jhon

What is a local and global variable?

A variable’s scope is the range of the script where it is accessible. Variables have either global or local scope. A global variable is accessible in every function. A local variable, however, has a limited scope and can be accessed only within the block that it is declared in.

The global Keyword

When you create a variable inside a function, that variable is local, has local scope, and can only be available inside that function.

Use global keyword, this will make the scope global even if the variables are defined inside of the function.

#The global scope in python
    
    def myName():
        global name
        name = "Jhon Doe"
    myName()
    print("My name is " + name)

#Output: My name is Jhon Doe

Add comment