Thursday, September 27, 2018

Data types in Python contd....

Strings

A string is simply a series of characters. A Python string is a contiguous sequence of Unicode characters. Anything inside quotes is considered a string in Python, and you can use single or double quotes around your strings.

To declare a string, you can either use -

variableName = 'initial value' (single quotes) or

variableName = "initial value" (double quotes)

Example: user_name = 'Python user1'
                 emp_name = "veevaeck"
                 emp_age = '41'

what if we write emp_age = 41? (hint: no quotes )

Refer to this screenshot and try this on your editor.



Python includes a number of built-in functions to manipulate strings which we'll cover in a new post dedicated only towards strings.

List

List in Python refers to a collection of data which are normally related. Instead of storing these
data as separate variables, we can store them as a list. A list can contain heterogeneous values such as integers, floats, strings, tuples, lists, and dictionaries. Moreover, Python lists are mutable; yes, they can change.

For example: We need to store usernames of programmers in your development team. One option is to create user specific usernames and assign names to them.

user1 = "Bob", user2 = "Ted", user3 = "Joey", user4 = "Chris", user5 = "Mac"

Other option is to create a list and store these names in that list. To declare a list, you write listName = [initial values]. Note that we use square brackets [ ] when declaring a list. Multiple values are separated by a comma.

users = ["Bob","Ted", "Joey","Chris","Mac"]

The following program list_demo.py shows the lists :



You must wandering if you can create an empty list for future usage, something like emails = [] where in you can store email Ids of the team members, yes you can. We shall cover this while exploring Lists in a separate post.


Tuples

Python tuple is a sequence, which can store heterogeneous data types such as integers, floats, strings, lists, and dictionaries. Like strings, tuple is immutable.So Tuples are just like lists, but you cannot modify their values. The initial values are the values that will stay for the rest of the program.

To declare a tuple, you write tupleName = (initial values). Notice that we use parentheses ( ) when declaring a tuple however we used [] to declare list. Multiple values are separated by a comma.

If we know the value of our datatype will not change in future we can use tuple instead of a list. For example days of week.

days_of_week = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')

or months_in_year = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')

I hope the basic idea regarding tuple is clear and how it differs from a list. See the tuple_demo.py program for better understanding-




Dictionary

In Python, a dictionary is a built-in data type containing sequence of key-value, or item, pairs separated by commas. To declare a dictionary, you write dictionaryName = {dictionary key : value},
with the requirement that dictionary keys must be unique (within one dictionary). Multiple pairs are separated by commas.

For example:

programmers = {'Bob':'Java', "Ted":'C', "Joey":'C++', "Chris":'Python',"Mac":'R'}

To retrieve an item's value we write  dictionaryName [dictionary key ]

So in our program if wanna see what's Bob's programming language we will simply write-

programmers['Bob']

see  dictionary_demo.py program below



The key-value pair is called an item. The key and value are separated by a colon (:), and each item is separated by a comma (,). The items are enclosed by curly braces ({ }). An empty dictionary can be created just by using curly braces ({ }). Key features of the dictionary are:
  • The key of the dictionary can not be changed
  • A string, int, or float can be used as a key
  • A tuple that does not contain any list can be used as a key
  • Keys are unique
  • Values can be anything, for example, list, string, int, and so on
  • Values can be repeated
  • Values can be changed
  • A dictionary is an unordered collection, which means that the order in which you have entered the items in a dictionary may not be retained and you may get the items in a different order.

As this was just an introduction to dictionary data type we will limit our discussion to accessing item value only but later on we'll see in what ways we can use a dictionary.

Here we have seen the basic data types used in Python. A separate post will cover these data types in detail but before that we'll see about type casting and some more operators which are supported by Python which will be the focus of my next posts.





Share:

0 comments:

Post a Comment