Python Notes
Python Notes
.
Here x is int type and y is of str type
• However, what if you want to loop through the cars and find a specific one? And
what if you had not 3 cars, but 300?
The solution is an array!
• An array can hold many values under a single name, and you can access the
values by referring to an index number.
Access the Elements of an Array
• You refer to an array element by referring to the index number.
The Length of an Array
• Use the len() method to return the length of an array (the number of
elements in an array).
You can use the pop() method to remove an element from the array. You can also
used remove () method for the same purpose.The list's remove() method only removes
the first occurrence of the specified value
Array Methods
• Python has a set of built-in methods that you can use
on lists/arrays.
List copy() Method
• The extend() method adds the specified list elements (or any iterable)
to the end of the current list.
If you try to call the function with 1 or 3 arguments, you will get an error.
Arbitrary Arguments, *args
• If you do not know how many arguments that will be passed into your
function, add * before the parameter name in the function definition.
Keyword Arguments
You can also send arguments with the key = value syntax. This way the order of the arguments
does not matter.
Arbitrary Keyword Arguments, **kwargs
• If you do not know how many keyword arguments that will be passed
into your function, add two asterisk: ** before the parameter name in
the function definition.
• Function definitions cannot be empty, but if you for some reason have
a function definition with no content, put in the pass statement to
avoid getting an error.
Python Lambda
• A lambda function is a small anonymous function.
• A lambda function can take any number of arguments, but can only have one
expression.
• Syntx:
lambda arguments : expression
Python Collections (Arrays)
• There are four collection data types in the Python programming
language:
• List is a collection which is ordered and changeable. Allows duplicate
members.
• Tuple is a collection which is ordered and unchangeable. Allows
duplicate members.
• Set is a collection which is unordered, unchangeable(add and remove
can be done), and unindexed. No duplicate members.
• Dictionary is a collection which is ordered(python 3.6 dictionary are
unordered) and changeable. No duplicate members.
Python Lists
type()
From Python's perspective, lists are defined as objects with the data type 'list'
The list() Constructor
Negative Indexing:
Negative indexing means start from the end
-1 refers to the last item, -2 refers to the second last item etc.
Range of Indexes:
You can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new list with the specified items.
Note:The search will start at index 2 (included) and end at index 5 (not included).
Contd..
We can access from starting by leaving out start value.The range will start at the first
item
By leaving out the end value, the range will go on to the end of the list:
Note: If you insert less items than you replace, the new items will be inserted where you
specified, and the remaining items will move accordingly.
Python - Add List Items
• Append Items
To add an item to the end of the list, use the append() method:
Insert Items
To insert a list item at a specified index, use the insert() method.The insert() method inserts an item at the
specified index.
Extend List
To append elements from another list to the current list, use the extend() method.
Python - Remove List Items
• Remove Specified Item:
The remove() method removes the specified item.
If you do not specify the index, the pop() method removes the last item.
del keyword:
The del keyword also removes the specified index
Contd..
• The del keyword can also delete the list completely.
Remove Items:
Tuples are unchangeable, so you cannot remove items from it, but you can use the
same workaround as we used for changing and adding tuple items:
Python - Join Tuples
Multiply Tuples
If you want to multiply the content of a tuple a given number of times, you can use the * operator
Python Sets
• A set is a collection which is unordered, unchangeable*, and unindexed.
• Sets are written with curly brackets.
• Note: Set items are unchangeable, but you can remove items and add new
items.Sets are unordered, so you cannot be sure in which order the items will
appear.
Add Sets:
To add items from another set into the current set, use the update() method.
Python - Remove Set Items
• Remove Item
• To remove an item in a set, use the remove(), or the discard() method.
Note:
If the item to remove does not exist, remove() will raise an error.
Note: If the item to remove does not exist, discard() will NOT raise an error.
Pop():
You can also use the pop() method to remove an item, but this method will remove a random item, so
you cannot be sure what item that gets removed.
Del() and clear() keyword in SETS
Python - Join Sets
• Join Two Sets
There are several ways to join two or more sets in Python.You can use the union() method that
returns a new set containing all items from both sets, or the update() method that inserts all the
items from one set into another:
The intersection_update() method will keep only the items that are present in both sets.
Accessing Items:
You can access the items of a dictionary by referring to its key name, inside square
brackets.
There is also a method called get() that will give you the same result
Contd..
• Get Keys
The keys() method will return a list of all the keys in the dictionary.
Get Values
The values() method will return a list of all the values in the dictionary.
Contd..
• Get Items:
The items() method will return each item in a dictionary, as tuples in a list.
• The del keyword removes the item with the specified key name.