Like any other programming language, a variable in python stores data and variable name is used to identify the value.
As we know python is a dynamically typed language, we do not have to specify the data type.
Declare a variable:
id = 1 name = "John" is_success = True
To check the type of a variable :
print(type(id)) print(type(name)) print(type(is_success))
<class 'int'> <class 'str'> <class 'bool'>
The variable names in python are case sensitive, which means variable “empid” is not the same as “EMPID”.
The same variable can hold data like below:
id = "hello world"
This behavior is called dynamically typed behavior because the type of data the variable hold can change dynamically whereas statically typed language will not allow this kind of behavior.
One downside of the dynamically typed language, it requires more testing as the run time error shows up when the application finally runs.
Global & Local variable
Let’s understand the concept of local and global variables. Here examples for global and local variable contains some concepts of function/method. If you are not familiar with functions, then please spend some time in the function tutorial here and come back to this page.
Variables in python are of two types.
- Local variable
- Global variable
Local variables are generally defined inside a function or block where the scope is limited only to that function or block. Other functions cannot access the variable.
def local_variable(input1, input2): sum_number = input1 + input2 print(sum_number) print(sum_number) ##trying to access variable outside method
output: NameError: name 'sum_number' is not defined.
Here as the ‘sum_number’ is defined inside the method, User can not access the variable outside. Hence error is saying ‘NameError:name not defined.’
Global variables can be accessed and modified anywhere inside the class. Follow the below example of a global variable.
input1 = 20 input2 = 10 def test_global_variable(): sum_number = input1 + input2 print(sum_number) test_global_variable()
Here ‘input1’ and ‘input2’ are defined outside the method which can be accessed inside the ‘test_global_variable’ method.
To define a variable as global from the method, you can use the ‘global’ keyword. This will help you make the variable as a global variable and can be accessed anywhere.
The below example illustrates the same.
input1 = 20 input2 = 10 def test_global_variable(): global sum_number sum_number = input1 + input2 test_global_variable() print("Global variable value is :",sum_number)
output: Global variable value is : 30
In this section, you will find about the operators in python.
Python operators are mainly.
- Arithmetic operator
- Logical Operator
- Comparison operator
- Assignment operator
- Bitwise Operator
‘Arithmetic’ operators are used to perform operations like addition, subtraction, multiplication, division, etc.
1. addition: ‘addition’ operator (+) allows users to add two or more numbers.
number1 = 10 number2 = 20 print("Sum is: ",number1 + number2)
Sum is: 30
2. subtraction: ‘subtraction’ operator allows subtracting the number from left to right.
print("Output: ",number2 - number1)
3. division: division operator (/) used to divide the left by right expression.
print("Output: ",number2 / number1)
4. multiplication: multiplication(*) operator used to multiply two operands.
print("Output: ",number2 * number1)
Comparison operator or relational operator used to compare two expressions and return True if comparison matches and False else wise.
1. Equal to Operator: ‘Equal to ‘ operator (==) used to compare two expressions. Returns True if two expression matches otherwise return False
number2 = 20 number1 = 10 number3 = 10 print("Output1: ",number2 == number1) print("Output2: ",number1 == number3)
Output1: False Output2: True
2. Greater than: Greater than (>) operator checks if the left expression is greater than the right expression.
3. Greater than or equal: Greater than or equal (>=) operator checks if the left expression greater or equal to right expression.
print("Output: ",number1 >= number3)
4. Less than: Less than (<) operator checks if the left expression is less than the right expression. [code lang="python"] print("Output: ",number1 < number2) [/code]
5. Less than or equal: Less than or equal (<=) operator compares if the left expression equals or less than the right expression. [code lang="python"] print("Output: ",number1 <= number2) [/code]
6. Not equals to: ‘Not equals’ to (!=) element compares and returns True if both the expression are not equal and returns False if both the expression is equal.
print("Output: ",number1 != number2)
Logical operations can be performed using logical operators. Below is the list of a few logical operators.
1. and: Returns True if both the expression are True and returns False if any of the expression is False.
boolean_1 = True boolean_2 = False print("Output: ",boolean_1 and boolean_2)
2. or: Returns True if any of the expression contains True else returns False.
print("Output: ",boolean_1 or boolean_2)
3. not: not operator inverts the Boolean value.
Assignment operators used to assign values to variables. In some cases, the assignment operator first evaluates an expression and then assigns the value to a variable.
1. Equal: equal (=)operator is used to assign the value to a variable.
var = 10
2. Add and assign: Add and assign ( +=) operator first adds the value and assign the value.
x = 1 x+=10 print(x)
3. Subtract and assign: Similar to the above operation, instead of adding this operator subtracts and then assigns.
x = 1 x -= 10 print(x)
4. Multiply and assign: First multiply the value and assign the value.
x = 2 x *= 10 print(x)