More on Variables – Python Self-Paced Video Training at Lammle.com
It is time for us to dig deeper into the critical concept of variables in Python. In this post, we test my code and provide the output using Python in interactive mode.
When we do this, we are not building a script (script mode) but typing the commands into a terminal and getting results immediately. So, to enter interactive mode, launch Python and do not specify a script to execute:
C:\Users\greg\OneDrive\Blog\Python>python
How Variables Work in Python
- Unlike other programming languages, you do not need to declare a variable before placing a value in it. So in your Python code – you can immediately state something like name = ‘Greg’, and you do not need to worry about declaring the name variable first! In JavaScript, for example, you need to declare your variable first like this var name.
- Notice also in our name = ‘Greg’ example, Python automatically sets the variable type for us based on the value we placed in the variable. Here it is a variable of type string.
- Variable are strongly typed in Python. What does this mean? It means if I try this code print name + 2, I will get an error that Python cannot combine a string and an integer value.
Note: Observe how you can use “ or ‘ with your string values. This is very useful if you want that “ to be part of your string. For example:
>>> myvar = ‘”Hello World!”‘
>>> print myvar
“Hello World!”
So what are the standard data types for our variable that exist in Python? Here they are:
Number – that’s right, of course, we can work with numeric values in Python. There are four different types supported:
- int (signed integers)
- long (long integers; can also be represented in octal and hexadecimal)
- float (floating point real values)
- complex (complex numbers)
Example of a float type:
myvar = -12.357
String – here you define a list of characters inside your quotation marks.
An example of a string type is:
myvar = “I love Python!”
List – contains items separated by commas and enclosed within square brackets ([]). All the items belonging to a list can be of a different data type. The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list.
Example:
>> myvar = [“Greg” , 123 , 15.4]
>>> print myvar[0]
Greg
>>> print myvar[2]
15.4
Tuple – A tuple is similar to the list data type. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses. The main differences between lists and tuples are that tuples are enclosed in parentheses and cannot be updated. Tuples can be thought of as read-only lists.
Example:
>> myvar = (“Greg” , 123, 15.4)
>>> print myvar[0]
Greg
>>> print myvar[2]
15.4
Dictionary – Dictionaries are also similar to lists. A dictionary consists of a collection of key-value pairs. Each key-value pairing maps the key to its associated value. They differ from lists primarily in how elements are accessed. Notice that you access list elements by their position in the list via indexing. Dictionary elements are accessed via keys.
Example:
>> myvar = {“Associate” : “CCNA”, “Professional” : “CCNP”, “Expert” : “CCIE”}
>>> print myvar[“Expert”]
CCIE
Fun Tips on Variables
You can save time (and space) by declaring multiple values on a single line! For example:
>> myvar , myvar2 , myvar3 = 1 , 2 , 3
>>> print myvar3
3
You can also assign one variable to another. For example:
>>> myvar1 = 10
>>> myvar2 = 20
>>> myvar1 = myvar2
>>> print myvar1
20
You can embed variables inside your strings using string formatting. We use %s for this inside of our string. To represent an integer, we use %d. For example:
>>> myname = “Robert Bachman”
>>> myage = 49
>>> print “My correct name is %s and my age is: %d” % (myname, myage)
My correct name is Robert Bachman and my age is: 49
Thanks for reading, and I hope you look forward to our next post!