Learning Python – Lesson 4: More on Variables

Python Variables

Here we will dig deeper into the important concept of variables in Python. In this lesson, I will test my code and provide the output using Python in what is called interactive mode. Here I am not building a script (script mode), but I am typing my commands into my terminal and getting results back immediately. To enter interactive mode, I just launch Python and I do not specify a script to execute:

C:\Users\terry\OneDrive\Blog\Python>python
Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

How Variables Work in Python

Here are some important things to consider:

  • Unlike other programming languages, you do not need to declare a variable before you place a value in it. So in your Python code – you can just immediately state something like name = ‘Anthony’ and you do not need to worry about declaring the name variable first! In JavaScript, you need to declare your variable first like this var name.
  • Notice also in our name = ‘Anthony’ example, Python is also automatically setting 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. This 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: Notice 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.

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 = ["Anthony" , 123 , 15.4]
>>> print myvar[0]
Anthony
>>> 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 = ("Anthony" , 123, 15.4)
>>> print myvar[0]
Anthony
>>> 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 pair maps the key to its associated value. They differ from lists primarily in how elements are accessed. List elements are accessed 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 = "Anthony Sequeira"
>>> myage = 49
>>> print "My correct name is %s and my age is: %d" % (myname, myage)
My correct name is Anthony Sequeira and my age is: 49

Thanks for reading and I hope you look forward to Lesson 5!

Leave a Reply

Your email address will not be published. Required fields are marked *