Sei sulla pagina 1di 3

List, tuples and Dict

a. Differences btw list tuple and dict : 2 key diff


Lists:- are what they seem - a list of values. Each one of them is numbered,
starting from zero - the first one is numbered zero, the second 1, the third 2,
etc.
You can remove values from the list, and add new values to the end.
Example: Your many cats' names.
Tuples:- are just like lists, but you can't change their values. The values
that you give it first up, are the values that you are stuck with for the rest of
the program. Again,
each value is numbered starting from zero, for easy reference.
Example: the names of the months of the year.
Dictionaries:- are similar to what their name suggests - a dictionary. In a
dictionary, you have an 'index' of words, and for each of them a definition. In
python, the word is
called a 'key', and the definition a 'value'. The values in a dictionary
aren't numbered - tare similar to what their name suggests - a dictionary.
In a dictionary, you have an 'index' of words, and for each of them a
definition. In python, the word is called a 'key', and the definition a 'value'.
The values in a dictionary aren't numbered - they aren't in any specific
order, either - the key does the same thing. You can add, remove, and modify the
values in dictionaries. Example: telephone book.
1) Lists and tuples maintain order. Dictionaries and sets are unordered
2) Lists and tuples allow duplication. Dictionary and set items are unique.
3) List and tuple items may be accessed by index. Dictionary items are accessed
by key. Set items cannot be accessed by index

b. Creating a database, which one to be used above.


Python data structures include lists, dictionaries, tuples, and sets.

c. Can i be able to use dict inside list and vice versa.


d. How to declare an empty dict and list
>>> d = {} # Empty dictionary
>>> l = [] # Empty list
>>> ms = set() # Empty set
>>> s = '' # Empty string
>>> t = () # Empty tuple

e. How to get number of items in list.


len(MyList)=> This also works for strings, tuples, dict objects.
f. How find a value of key in dict or to check if key is present or not ....
The method get() returns a value for the given key. If key is not available then
returns default value None.
dict = {'Name': 'Zabra', 'Age': 7}
print "Value : %s" % dict.get('Age')
print "Value : %s" % dict.get('Education', "Never")

When we run above program, it produces following result -

Value : 7
Value : Never

g. How to check the type of variables.

dict = {'Name': 'Zara', 'Age': 7};


print "Variable Type : %s" % type (dict)

h. List = ['a',1,'b', 2]. Is it ok to declare.


want to convert to string with - a-1-b-2?

>>> mylist = ['spam', 'ham', 'eggs']


>>> print ', '.join(mylist)
spam, ham, eggs

i. Str = 'My Name is abcd'


how to check adcd is present.?
change abcd to Kumar. ?
We search for a specific character or characters in a string with the .find()
method.

>>> s = "I intend to live forever, or die trying."


>>> s.replace("to", "three")
'I intend three live forever, or die trying.'
>>> s.replace("fore", "five")
'I intend to live fivever, or die trying.'

Functions, variables and returns


a. how to define functions in python
b. def funcret2(a,b) .. want to change the values and return back b to a, vice
versa
c. print "Hello, %s , From My Function!, I wish you %s"%(username, greeting)
d. Command line arguments ? sys.argv
e. How to define a main function

Modules and packages


a. Difference
b. How to implement
c. _all_ significance
d. want to get particular funtion from module.

Classes
a.create a class : two var a,b
method set and get
initailize a= 0 b=0
instance
b. global and local variables

A data structure is a particular way of organizing data in a computer so that it


can be used efficiently.
Data structures can be used to organize the storage and retrieval of information
stored in both main memory and secondary memory.
The array and record data structures are based on computing the addresses of data
items with arithmetic operations; while the linked data structures are based on
storing addresses of data items within the structure itself.
An array is a number of elements in a specific order, typically all of the same
type. Elements are accessed using an integer index to specify which element is
required. Arrays may be fixed-length or resizable.
A linked list is a linear collection of data elements of any type, called nodes,
where each node has itself a value, and points to the next node in the linked list.
A record (also called tuple or struct) is an aggregate data structure. A record is
a value that contains other values, typically in fixed number and sequence and
typically indexed by names. The elements of records are usually called fields or
members.
A class is a data structure that contains data fields, like a record, as well as
various methods which operate on the contents of the record.
Python data structures include lists, dictionaries, tuples, and sets.
Python lists are implemented internally as variable-length arrays, rather than
linked lists.
Python dictionaries are implemented internally as resizable hash tables.
Python tuples are records.
Python sets are implemented internally as dictionaries (hash tables) with keys and
no values.
The data types for a list, dictionary, tuple, and set, are 'list', 'dict', 'tuple',
and 'set', respectively.
The list(), dict(), tuple(), and set() functions may be used to create list,
dictionary, tuple, and set objects, respectively.
List and dictionary items are mutable. Tuple and set items are immutable.
Lists and tuples maintain order. Dictionaries and sets are unordered.
Lists and tuples allow duplication. Dictionary and set items are unique.
Sets may be used to remove duplication found in other data structures.
List, dictionary, tuple, and set items may be accessed using a for loop.
List and tuple items may be accessed by index. Dictionary items are accessed by
key. Set items cannot be accessed by index.
Python supports having a tuple on the left side of an assignment statement. This
allows you to assign more than one variable at a time.
Sets support logical set operations, including membership, subset, superset, union,
intersection, and difference.

Potrebbero piacerti anche