Table of Contents
Python Variables Definition
Variables in simple terms are names you give to computer memory locations that are used 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. A variable is created just with a name, equal sign (the symbol = ), and value.
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 because they are enclosed in 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.
# Example type('Hello, World!') #Output: <class 'str'> type(20) #Output: <class 'int'> type(3.14) #<class 'float'> type('20') #Output: <class 'str'> type('3.14') #Output: <class '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 ###
Python interprets 2,000,000 as a comma-separated sequence of integers, that is printed with spaces between.
Note: Strings can be written with single or double quotes but make sure that are 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 ###
In the above example,
name = "Jhon Doe" is a Global variable. It has a global scope and is accessible inside the
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 used inside that function.
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