Python Notes (Code With Harry)
Python Notes (Code With Harry)
Pip:
Pip is a package manager for Python i.e., pip command can be used to download any external
module in Python. It is something that helps us to get code written by someone else from
somewhere.
We can install a module in our system by using pip command:
Open cmd or PowerShell in your system.
And then, type pip install module name and press enter.
Once you do that, the module will start downloading and will install automatically on your
computer.
After installing any module in Python, you can import it into your program or your Python
projects. For example, to use flask, I will type "import flask" at the top of my Python
program.
import flask
There are two types of modules in Python:
Built-in Modules:
Built-in modules are the modules that are pre-installed in Python i.e., there is no need to
download them before using. These modules come with python interpreter itself.
Example – random, os, etc.
To get a complete list of built-in modules of python head to the following page of the official
documentation - https://docs.python.org/3/py-modindex.html.
External Modules:
These are the modules that are not pre-installed in Python i.e., we need to download them
before using them in our program.
Example – Flask, Pandas, TensorFlow, etc.
Escape
Description
Sequences
abc4=int(abc2)
print(abc+abc4) # Output: 50
print(abc+int(abc2)) # Output: 50
String Slicing:
As we know the meaning of the word ‘slice’ is ‘a part of’. I am sure you have sliced paneer
cubes at home! Just like paneer slice refers to the part of the paneer cube; In Python, the term
‘string slice’ refers to a part of the string, where strings are sliced using a range of indices.
To do string slicing we just need to put the name of the string followed by [n:m]. It means ‘n’
denotes the index from which slicing should start and ‘m’ denotes the index at which slicing
should terminate or complete. Let's look into an example!
In Python, string slicing s[n:m] for a string s is done as characters of s from n to m-1. It
means characters are taken from the first index to second index-1.
For E.g., abc="Demo" then abc[0:3] will give ‘Dem’ and will not give ‘Demo’ coz index
number of ‘D’ is 0, ‘e’ is 1, ‘m’ is 2, and ‘o’ is 3. So it will give a range from n to m-1 i.e. 0
to 3-1=2. That’s why we got output ‘Dem’.
In string slicing, we sometimes need to give a skip value i.e. string[n:m:skip_value]. This
simply takes every skip_valueth character. By default, the skip value is 1 but if we want to
l1.sort()
print(l1) #l1 after sorting
l1.reverse()
print(l1) #l1 after reversing all elements
List Slicing:
List slices, like string slices, returns a part of a list extracted out of it. Let me explain, you can
use indices to get elements and create list slices as per the following format:
seq = list1[start_index: stop_index]
Just like we saw in strings, slicing will go from a start index to stop_index-1. It means the seq
list which is a slice of list1 contains elements from the specified start_index to specified
(stop_index – 1).
List Methods:
There are a lot of list methods that make our life easy while using lists in python. Lets have a
look at few of them below:
Sets in Python
In Mathematics:
“A set is a collection of well-defined objects and non-repetitive elements that is - a set with
1,2,3,4,3,4,5,2, 2, and 3 as its elements can be written as {1,2,3,4,5}”
No repetition of elements is allowed in sets.
In Python programming, sets are more or less the same. Let us look at the Python
programming definition of sets:
“A set is a data structure, having unordered, unique, and unindexed elements.”
Restrictions:
Everything has a limit to its functionality, there are some limitations on working with sets
too.
Once a set is created, you cannot change any of its items, although you can add new items or
remove previous but updating an already existing item is not possible.
There is no indexing in sets, so accessing an item in order or through a key is not possible,
although we can ask the program if the specific keyword, we are looking for is present in the
set by using “in” keyword or by looping through the set by using a for loop.
Despite these restrictions, sets play a very important role in the life of a python programmer.
In most cases, these restrictions are never a problem for the programmer given he knows
which data type to use when. And this skill is something you will learn with time after
writing a lot of python programs
Set Methods:
There are already a lot of built-in methods that you can use for your ease and they are easily
accessible through the internet. You might want to peep into python's official documentation
at times as well to check for some updates they might push down the line. Some of the
methods you can use with sets include union(), discard(), add(), isdisjoint(), etc. and their
functionality is the same as in the sets in mathematics. Also, the purpose of these functions
can easily be understood by their names.
# list1 = [5, 7, 3]
# print(15 not in list1)
# if 15 not in list1:
# print("No its not in the list")