In programming we store values in variables. The values could be string, number, boolean, etc. Simply a variable holds value in a memory location and give a name to it. That value can later be access with the variable name and used in the program.
>>> variable_name = value
>>> var = 10 # numeric variable >>> string_variable = "bumble bee" # string variable >>> is_true = False # boolean variable
Here, var, string_variable, is_true all are valid variable names.
Rules of creating variables:
The rules for creating variable is similar to creating identifier. We can use letters, numbers and underscore. We can not use reserved keywords as variables. For example. a, x30, num1, var_one, count_1, remainder_of_value, etc all are valid variable names.
Constant as the name suggest is an item that holds values but its value can not change. It is similar to variable but it’s value is constant and does not change. We generally make constant in separate file and import them in our program. By convention the constant are created with all upper case, which signifies that it’s a constant.
CONSTANT_NAME = value
Create a python file with name constantvalue.py and write the following code.
PI = 3.14 GRAVITY = 9.8
Let’s use this constant in our program
from constantvalue import PI, GRAVITY # Calcuate the area of a circle area_of_circle = PI * 5 * 5 print("Area:", area_of_circle) # 1000 kg astroid collision force,F= M*G astroid_collision_force = 1000* GRAVITY print("Force:", astroid_collision_force)
Note: we will see the import statement in later exercise.
Output of the program is
Area: 78.5 Force: 9800.0
Literal is the notation or representation of a fixed value in a programming language. In python we have various literals like numeric literals, string literals, boolean literals and special literals,.
Numeric literals are of binary, decimal, octal, hexadecimal, Float, and Complex type.
# Numeric Literals b = 0b1110 # binary literal( binary literals start with '0b') number = 10 # Decimal literal(any number value) octal_literal= 0o250 # octal literal(it start with '0o') hex_value = 0x1F # hexadecimal literal(start with '0x') print(b) print(number) print(octal_literal) print(hex_value) # Float literals a = 3.5 # Float literals b = 99.008 #Float literals b1 = 1.5e3 print("a=",a) print("b=",b) print("b1=",b1) # Complex literal c = 3+4j # complex literal( has syntax RealPart+(ImaginaryPart)j) d = 3.5+ 9j # complex literal print("c=",c) print("d=",d) # String literal e = "a" f = "PythonSpark.com" multiple_string= """This is multi line string.""" print("e=",e) print("f=",f) print("multiple_string=",multiple_string) # boolean literal g = True h = False i = (1==True) print(g,h,i) # special literal j = None # special literal print("j=",j)
Note: None is considered as special literal it represents no value.
The output of above program is
14 10 168 31 a= 3.5 b= 99.008 b1= 1500.0 c= (3+4j) d= (3.5+9j) e= a f= PythonSpark.com multiple_string= This is multi line string. True False True j= None