Instructor Saqib Daud Python Functions • A block of related statements designed to perform a computational, logical, or evaluative task. • The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over and over again. Syntax (functions) Function and its calling in Python Python Function with arguments Default Argument Keyword Arguments (caller does not need to remember the order of parameters. Python3
) Length finding Through list List Code Example Compound data types
• List
• Tuples
• Sets
• Strings
• Dictionaries List
• Sequence of items in an order
• Not necessary to have the same type
Creating list Length of list Accessing list Negative Indexing Slicing of the list Change items of the lists • Pretty dynamic to add , modify and delete items from the lists
• Changeable objects Mutable
More items to change in one go In key word
• To check whether the item is in the list or not
Iteration in list Adding items in list Removing items from the lists Copy Tuples(Ordered but Immutable) Sets • A set is an unordered collection of items. Every set element is unique (no duplicates) and must be immutable (cannot be changed). Don’t have indexes as well
• However, a set itself is mutable. We can add or remove items
from it.
• Sets can also be used to perform mathematical set operations
like union, intersection, symmetric difference, etc. Creation of Sets Sets with duplicate items Empty Set List to set Conversion Adding Items in Set Remove Items from sets Checking some thing present in set Dictionaries • Python dictionary is an unordered collection of items. Each item of a dictionary has a key/value pair.
• Dictionaries are optimized to retrieve values
when the key is known. Creation of dictionary Accessing items in dictionary