In this post we are going to explore what are variables, how to create them, how to assign values to variables, what values and types of values exists.
Example 1 Investigate a value type for following values 3, 15, 4, 6, "Hello World!", and "This is my first Python program."
Solution: To investigate value type the Python has built-in function "type" so we will use this function for each value.
Example 2 Investigate the value type of 3.5, 6.3, 13.8, '178.4', 30, and '30'.
Solution: As in previous example we will use built-in type function to investigate value type and print function the print the result.
Example 4 Assign value of 1 to variable \(a,b,c\) and asign values 50, 600, "This is the end" to variables \(d, g\), and \(h\).
Solution:
Example 5 Create following variables 100miles, this@, and del and assign to them some values.
The video tutorial on how to name variables in Python is shown below.
What Values and type of values exist in Python ?
The value is one of the basic things a program works with. Some values that you have seen in previous examples so far are: 3, 15, 4, 6 and "Hello World!", "This is my first Python program.". The previously given value examples belong to different types. The numbers 3, 15, 4, and 6 are integers. "Hello, World!", and "This is my first Python program." are strings. The strings are enclosed by quotation marks.Example 1 Investigate a value type for following values 3, 15, 4, 6, "Hello World!", and "This is my first Python program."
Solution: To investigate value type the Python has built-in function "type" so we will use this function for each value.
print("type(3) = ", type(3))The previous lines code are pretty simple. All of them start with the built in function print followed by the command type() in string type and then actual type command. The only novelty in the previous code block is string with three ". The """ """ are usually used when we want to create multiline strings or there are another pair of " " inside the strings. Since in the string "type("Hello World!")" we have one pair of " " inside the string we have to use """ """. The output of the previous block of code is shown below.
print("type(15) = ", type(15))
print("type(3) = ", type(3))
print("type(6) = ", type(6))
print("""type("Hello World!") = """, type("Hello World!"))
print("""type("This is my first Python program.") = """, type("This is my first Python program."))
type(3) = <class 'int'>As seen from previous output the four numbers 3, 15, 4 and 6 are integers (class 'int') while "Hello World!" and "This is my first Python program." are strings (class 'str'). On the other hand the numbers with decimal point are of float type. However, if we type '17' or '3.2' although they look like numbers they are strings.
type(15) = <class 'int'>
type(4) = <class 'int'>
type(6) = <class 'int'>
type("Hello World!") = <class 'str'>
type("This is my first Python program.") = <class 'str'>
Example 2 Investigate the value type of 3.5, 6.3, 13.8, '178.4', 30, and '30'.
Solution: As in previous example we will use built-in type function to investigate value type and print function the print the result.
print("type(3.5) = ",type(3.5))The result of previous code is shown below.
print("type(6.3) = ",type(6.3))
print("type(13.8) = ",type(13.8))
print("""type('178.4') = """,type('178.4'))
print("type(30) = ", type(30))
print("""type('30') = """, type('30'))
type(3.5) = <class 'float'>As seen from previous output the numbers 3.5, 6.3 and 13.8 are floats, the '178.4' and '30' are strings, and 30 is integer. The video showing solutions to Example 1 and 2 is given below.
type(6.3) = <class 'float'>
type(13.8) = <class 'float'>
type('178.4') = <class 'str'>
type(30) = <class 'int'>
type('30') = <class 'str'>
How to define variables and assign values to them?
Variable manipulations are one of the most powerful features of any programming language. A variable is a name that refers to a value. An assignment statement creates new variables and gives them values. In general form the assignment statement can be written as:variable_name = valueExample 3 Create new variables for following values: 56.124, "This is just an info.", 40, and 50. Display their values using print function and check the type of each variable.
a = 56.124In this example we have created four assignments. In first assignment the float number 56.124 is assigned to variable name \(a\). In second assignment the string "This is just an info." is assigned to variable name \(info\). In the third assignment the integer number 40 is assigned to variable named \(b\). In the fourth and last assignment the integer number 50 is assigned to variable named \(c\). Each variable will be displayed using print function, so type in the following lines of code.
info = "This is just an info."
b = 40
c = 50
print("a = ", a)After runing the entire script the following ouput is obtained.
print("info = ",info)
print("b = ",b)
print("c = ", c)
a = 56.124To find out the type of each variable we will use the type function and the print function.
info = This is just an info.
b = 40
c = 50
print("type(a) = ",type(a))The output of the previous block of code is given below.
print("type(info) = ",type(info))
print("type(b) = ",type(b))
print("type(c) = ",type(c))
type(a) = <class 'float'>All previous information about variable definition is shown in following video.
type(info) = <class 'str'>
type(b) = <class 'int'>
type(c) = <class 'int'>
How to perform multiple assignment?
In Python you can assign single value to a several variables simultaneously and you can assign multiple objects to multiple variables. The general form of assinging multiple variables to same value can be written as:variable_name_1 = variable_name_2 = variable_name_3 = valueIn this case all three variables i.e. variable_name_1, variable_name_2, and variable_name_3 all have same value value. The general form of assigning multiple variables to multiple values can be written as:
variable_name_1, variable_name_2, variable_name_3 = value_1, value_2, value_3In this case the value_1 is assigned to variable_name_1 the value_2 is assigned to variable_name_2 and value_3 is assigned to variable_name_3.
Example 4 Assign value of 1 to variable \(a,b,c\) and asign values 50, 600, "This is the end" to variables \(d, g\), and \(h\).
Solution:
a = b = c = 1In first code line the integer object is created with the value of 1 and all three variables are assigned to the same memory location. In second line of code two integer objects with values 50 and 600 are asigned to the variables \(d\) and \(g\), and one string object with the value "This is the end" is assigned to the variable \(h\). To show the value of each variable we will use print function.
d,g,h = 50, 600, "This is the end"
print("a = ",a)Output:
print("b = ",b)
print("c = ",c)
print("d = ",d)
print("g = ",g)
print("h = ", h)
a = 1The video on how to perform multiple variables assingment in Python is given below.
b = 1
c = 1
d = 50
g = 600
h = This is the end.
How to name variables in Python?
When naming variables you should choose names that are meaningful and document what the variable is used for. The name of the variable can be arbitrarly long, it can be created using uppercase letters, it can contain underscore character (_) , it can contain letters and numbers. However, the variable cannot start with a number.Example 5 Create following variables 100miles, this@, and del and assign to them some values.
100miles = "Big Truck"The problem with previous block of code that each line will generate SyntaxError: invalid syntax. The 100miles is illegal because the variable name should not begin with the number. The this@ is illegal since it contains character, and del is illegal since is one of Python's keywords. The Python interpreter uses keywords to recognize program structure so they cannot be used as a variable names. The Python 31 keywords that should not be used as variable names are given in Table 1.
this@ = 47
del = "The Big Bang"
and | del | from | not | while |
as | elif | global | or | with |
assert | else | if | pass | yield |
break | except | import | class | |
exec | in | raise | continue | finally |
is | return | def | for | lambda |
try |