Last time when we have declared variables, we have only used simple data. But programming isn't for doing simple things; it is for making things simple.
So today, we are going to tell you different data types in python and how to leverage them.
Python data types:
A variable can store 5 types of data, and these are:
Python Numbers
Number data types store numeric values. We have already 2 numeric data type variables, initialBudget, and wagePerDay.
# Declaring an integer | |
initialBudget = 1400 | |
# Declaring a decimal or float | |
wagePerDay = 14.5 | |
print('InitialBudget is', initialBudget) | |
print('wagePerDay is', wagePerDay) |
But there is a difference between the two values initialBudget is an integer and wagePerDay is a decimal value also called as float. And Python can store both the values and perform all arithmetic operations on them.
Python Strings:
Group of characters inside single quote (‘), double quote (“) and even triple quote (‘’’) is a string for Python. Single quote and double quotes are used for a single line and triple quote is used for multiple lines.
# declaring String variables using single quote (') | |
companyName = 'New Company Private Limited' | |
# declaring String variables using double quote (") | |
tutorialName = "Variable and data types" | |
# declaring String variables using triple quote (''') | |
companyAddress = ''' 342-345, first floor, | |
Building number 5, | |
Road 7, North Carolina, | |
USA | |
''' | |
# printing the value | |
print('company Name is', companyName) | |
print("tutorial Name is", tutorialName) | |
print('''company address is''', companyAddress) |
The beauty of triple quotes it keeps the formatting, you might use it when giving structure to multiple lines.
But, what will happen if we want to have a single quote or double quote? In that case, you have to use a backslash (\) followed by the quote.
# printing single quote | |
print('This is an \'important\' feature') | |
# printing double quote | |
print("This is \"important\", let me note it.") |
Remember the input function it always gives you string and the print function that prints the string on the computer screen.
Python Lists:
Until now we have declared one variable for a single value. But, there will be the cases when you have to declare multiple values for a single variable.
This is where Lists come into the picture. It is what it sounds like a list of anything. And you can declare a list by using square brackets ([]).
# Declaring Lists of office items as strings | |
officeItems = ['pens', 'sticky notes', 'white board'] | |
# declaring prime no till 10 | |
primeNumber = [2,3,5,7] | |
print('Office item list is', officeItems) | |
print('Prime number upto 10 list is', primeNumber) |
Python Tuples
The geographic location of any place is never going to change. So we want to store values like that in a variable that once declared does not change. This is why we are going to use Tuples.
# Declaring tuples | |
geographicalLocation = (12.234, 34.945) | |
print("Geographical Location is", geographicalLocation) |
Tuples are identified by parenthesis (). And they can contain any value, be it number, string, lists, other tuples. The only rule is that it cannot be changed once created.
Python Dictionary
In all the above data type we have serial indexing, but we don’t always want to search by serial number. We like to store our contact info by the name of the person. For that requirement, we are going to use a dictionary.
A dictionary is enclosed in curly braces ({}) and has 2 parts, key, and value. Key is separated by value by using colon (:) and keys can be separated by a comma (,).
# Declaring Dictionary with name as key and occupation as value | |
companyBook = { "Daniel": "Owner", "Stacy": "Receptionist", "Smith": "Manager", "Dev":"IT"} | |
print("My company Book is", companyBook) |
Name is the key, and the occupation is the value. Key has to be unique for a dictionary, but it can have multiple values.
And dictionaries are very fast. You will be surprised to know that Google search is big dictionary also called as a hash table.
Weak Type checking:
Python is not a strict type checking language. It does not check any variable for data type but allocates the memory automatically.
So what will happen if you do the following?
# Declaring a variable and assigning value to it | |
variable = 12 | |
variable = 23.45 | |
variable = "garbage value" | |
variable = [1,2,3] | |
print("value of variable is", variable) |
It will behave normally, and you will never get an error. The flexibility of not declaring data types comes at a price.
Word of caution:
You might forget sometime what variable you have declared, and for which purpose. Most developers struggle with this problem and create bugs.
So, we repeat what we had said earlier, "declare proper variable names." This is thumbs rule that you must follow. This is a simple solution to use the flexibility of python without getting those bugs.
Multiple Assignments:
Till now we have only assign single value in a single line. But, we can also assign multiple values in a single line.
# Assigning multiple values at a single time | |
project1Budget = project2Budget = project3Budget = 1400 | |
# 3 variables are created with same value | |
print("project 1 budget is", project1Budget) | |
print("project 2 budget is", project2Budget) | |
print("project 3 budget is", project3Budget) | |
# You can assign multiple value in multiple places | |
name, gender, age = "Ron", "Male", 25 | |
# 3 variables are created with 3 different values | |
print("name is", name) | |
print("gender is", gender) | |
print("age is", age) |
This is fast way, but we recommend to use it once you have enough hands-on experience.
Deleting a variable
What will happen if we do not want to use a particular variable anymore? For security purpose and to save memory, it is better to remove them.
That brings us to our first keyword, i.e., del.
del is used to delete a variable or delete its reference.
# deleting variable | |
customerName = "David" | |
customerAge = 28 | |
customerInterest = ["BasketBall", "Programming"] | |
# deleting the variable | |
del customerName, customerAge, customerInterest |
Variables are deleted, and if you try to print their values, you will get an error.
Key Takeaways:
- There are 5 types of data types that are used for specific purpose.
- Numbers are integers and decimals.
- Strings are a group of characters.
- Lists can declare a list of values for a single variable.
- Tuples are constants that we declare.
- You can index with keys if you use Dictionary.
- Declare variable name with camel case nomenclature to avoid bugs.
- You can delete a variable by using the del keyword.
Programming exercises:
These concepts need immediate practice. Please download the file and start practicing.
- Create a string variable firstName using single quotes (') and assign your first name. Create a string variable lastName using double quotes (") and assign your last name. Create a string variable myAddress using triple quotes (''') and assign your address, with formatting. Print the value of variable firstName, lastName, myAddress
- Create a variable bucketList, it should be your bucket list, things that you want to do. Assign atleast 5 values to it. Print the value of variable bucketList.
- Create a variable myGeoLocation, it should be a tuple. Search google for your geoLocation as latitude and longitude. Assign the values in myGeoLocation. Print the value of variable myGeoLocation.
- Create a variable myDictionary, it should be a dictionary. Find any 5 words as keys and find their meanings as values. Assign key and values to the dictionary. Print the value of myDictionary
If you have any doubt, download the solution file here.