Data Types

HI. So, I'm back after a very long time. Sorry for that. I was in a dilemma, of whether I should continue to write this blog, considering there are already so many resources available. But I thought, most of the other resources just tell you what python does, but don't teach you important software engineering principles. So, I am back with this blog.
At the end of this tutorial you'll have basic understanding of Python datatypes.


Numbers
Numbers are just like the numbers you know in real life. They are a sequence of Digits. We can use them to represent many things like age, distance etc. Of, course a number in itself is not so useful, unless we use it with a variable. Numbers can be Integers or Decimals (floats).
>>> age = 20
 
>>> pi = 3.14
>>> age = 20

>>> pi = 3.14


Variables
A variable is a lowercase word in Python. It is basically a name for some data. So I can write age = 20. What this does is, assigns the value 20 to the variable age. So whenever I say age, python knows that I mean 20. This process is called assignment. So, I am assigning the value 20 to the variable age. A variable can be visualized to be a Black Box. When I typed age = 20, what I did was created an Empty Black Box, put a label on it that read 'age' and put the number 20 inside that box.
>>> age = 20     # Here, 'age' is a variable. 
>>> age          # So, whenever I type 'age' in the interpreter, I'll get 20
20
 
>>> age = 20     # Here, 'age' is a variable. 
>>> age          # So, whenever I type 'age' in the interpreter, I'll get 20
20
 


Strings
A string is just a sequence of characters, surrounded by quotes. It's just some text. 'string' is just another fancy computer science word.
So if I can write food = 'spam and eggs'. You can use either Single quotes or Double quotes, but you shouldn't mix them.
Now, you must be going
"Wait, what If I want to use an apostrophe(') in my string ?"
>>> food = 'spam and eggs'

>>> say = "Wait, what If I want to use an apostrophe(') in my string ?"
>>>  #  I used Double Quotes to surround the string
 
>>> food = 'spam and eggs'

>>> say = "Wait, what If I want to use an apostrophe(') in my string ?"
>>>  #  I used Double Quotes to surround the string
 
See what I did there? Because I wanted to use a single quote in the string, I used Double Quotes to surround the string. If you want to use Double quotes inside a string, then use single quotes to surround the string.


Lists
Remember, I said, strings are an ordered collection of characters.
Similarly, Lists are ordered collections of Objects.  In case I forgot, strings and numbers are Objects in python.
Lists are surrounded by Square brackets, and the contents are separated by commas(,).
You can access the contents of a List by specifying an index.
Say if you want to access the 1st object in a list, you should type
foods[0]
>>> food = ['spam', 'eggs', 'bacon', 'cheese']
>>> food[0]
'spam'
>>> food[2]
'bacon'
 
>>> food = ['spam', 'eggs', 'bacon', 'cheese']
>>> food[0]
'spam'
>>> food[2]
'bacon'
 
Notice, the 1st object has the index 0
Similarly, the 2nd object has index 1


Tuples
Tuples, are almost the same as Lists, except that they are surrounded by Parenthesis.
>>> food = ('spam', 'eggs', 'bacon', 'cheese')
>>> food[0]
'spam'
>>> food[2]
'bacon'
 
>>> food = ('spam', 'eggs', 'bacon', 'cheese')
>>> food[0]
'spam'
>>> food[2]
'bacon'
 
They can be accessed the same way, Lists are accessed.
Sounds like a List, right?
Wrong.
Lists are Mutable, while Tuples are Immutable.
These again, are some fancy computer science words.  Here's a simple explanation.
>>> food_list = ['spam', 'eggs', 'bacon', 'cheese']
>>> food_list[2] = 'juice'  # Trying to change the 3rd element to 'cheese'
>>> food_list  # Now the list contains 'juice' instead of 'bacon'
['spam', 'eggs', 'juice', 'cheese']

>>> # Now what happens when I try to do it in a Tuple
>>> food_tuple = ('spam', 'eggs', 'bacon', 'cheese')
>>> food_tuple[2] = 'juice'  # Trying to change the 3rd element to 'cheese'
Traceback (most recent call last):
  File "", line 1, in 
    food_
NameError: name 'food_' is not defined
 
>>> food_list = ['spam', 'eggs', 'bacon', 'cheese']
>>> food_list[2] = 'juice'  # Trying to change the 3rd element to 'juice'
>>> food_list  # Now the list contains 'juice' instead of 'bacon'
['spam', 'eggs', 'juice', 'cheese']

>>> # Now what happens when I try to do it in a Tuple
>>> food_tuple = ('spam', 'eggs', 'bacon', 'cheese')
>>> food_tuple[2] = 'juice'  # Trying to change the 3rd element to 'juice'
Traceback (most recent call last):
  File "", line 1, in 
    food_
NameError: name 'food_' is not defined
 

Dictionaries
A dictionary is an unordered collection of Key:Value Pairs.
They are known as Hashes, or Hash-Tables in some other Programming Languages. Dictionaries are surrounded by Hashes. They look like this:

Think of a Dictionary, like a Phonebook in your cell-phone.
>>> phone_numbers = {'Bob':12345, 'John':99120} 
>>> # Here 'Bob' and 'John' are keys, and their respective phone numbers are their values.
>>> phone_numbers['Bob']  # Accessing the value of 'Bob'
12345
 
>>> phone_numbers = {'Bob':12345, 'John':99120} 
>>> # Here 'Bob' and 'John' are keys, and their respective phone numbers are their values.
>>> phone_numbers['Bob']  # Accessing the value of 'Bob'
12345
 
If you want to find the phone number of your friend 'Bob', you open your address book, and look for 'Bob'.
So, you're accessing Bob's phone number, by his name.


Comments
Ok, you must be going, "WTF, comment is not a data-type".
I know. But, I think I should just touch upon this topic now. You must have noticed the funny lines starting with '#' in the code above. They are comments.
>>> # <- Yeah, this is a comment.
 
>>> # <- Yeah, this is a comment.
 
Comments are Notes, that accompany the code. They are ignored by the python interpreter.

Comments are insanely useful in documentation. Notes don't explain the code itself. So don't start writing Essays using comments.


I think that's  it for today. Next time I will discuss, Control Structures.




No comments:

Post a Comment