Python data types

LIST

list are ordered sequences ,which can hold variety of object types , it’s also mutable . They are represented by [],and ‘,’ is used to separate objects .It supports indexing and slicing .

my_list=['kaka',100,6.42]
1.len(my_list) = 3

2.my_list[0] = 'kaka'

3.my_list[1:] = [100,6.42]

4.my_list.append('element') = adds the element at the end of the list

5.my_list.pop() = removes the last element of the list by default . .pop('index of element') is used to remove an element form the list
SORTING IN LIST
There are two sort function's in python ,one is sort() and the other one is sorted(). list.sort() modifies the list in-place (and returns None to avoid confusion). Usually it’s less convenient than sorted() - but if you don’t need the original list, it’s slightly more efficient.

so
new_list=['a','e','b','c']
my_sortlist=new_list.sort()
it's gonna return NONE type
To index a nested list just add another set of bracket'[]'. 
for example :
[1,[2,3],4] ,to access 3 from the list use my_list[1][1]

DICTIONARIES

They are unordered mappings for storing object, they use the concept of key-value pairing. The key value allows the user to get objects without knowing the index of the objects. They use {} and ‘:‘ .Syntax = {“key1″:”value1″,”key2:”value2”}

When to choose a dictionary and when to choose a list??

  1. Dictionary: when you want to quickly grab info without knowings it’s index value. You can’t sort in dictionary.

Functions in keys

  1. keys() = list’s all the keys available in the dictionary.
  2. values() = list’s all the values available in the dictionary.
  3. items() = list’s all the tuples available in the dictionary.

Dictionaries do not retain order , if you want order as well check orderddict.

TUPLES

Quite similar to list accept the fact that they are immutable.We use “()”. The 2 main functions are

  1. count() = count the number of time an element occurs
  2. index() = send the index of the first occurrence of the element

SETS

They are unordered collection of unique elements.i.e only one representation of an object.

my_set= set(). O/P is denoted by {}.

BOOLEANS

The value is either True(1) or false(0). Important with control flow and logic. Capitalize the first character of True and False.

To define an object with any value we can use None operator , for example – a=None

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s