VIVA notes
What is an Algorithm?
-To write a logical step-by-step method to solve the identified problem is called algorithm, in
other words, an algorithm is a procedure for solving problems.
What is a flowchart?
-A flowchart is the graphical or pictorial representation of an algorithm with the help of
different symbols, shapes and arrows in order to demonstrate a process or a program.
What is a programming language?
-A programming language is a vocabulary and set of grammatical rules for instructing a
computer to perform specific tasks.
What is a program?
-A computer program is a collection of instructions that perform a specific task when executed
by a computer.
Why is Python a good programming language?
1. easy to learn, read and maintain
2. a broad standard library- built in functions to solve a variety of problems
3. interactive mode- allows easy debugging of code
4. portability and compatibility
5. databases and scalable
IDLE- Integrated Development Environment
Interactive mode- easy to code and verify small snippets of code
when one command is entered, python immediately executes it
Script mode- write the whole program before execution
allows to create and edit python source file
Python statement- Instructions written in the source code for execution are called statements.
There are different types of statements in the Python programming language like Assignment
statement, Conditional statement, Looping statements etc.
Python comments- A comment is text that doesn't affect the outcome of a code, it is just a
piece of text to let someone know what you have done in a program or what is being done in a
block of code.
In Python, we use the hash (#) symbol to start writing a comment
Keywords:
Words that are set for a specific function in python
e.g. false, true, if, else, import, break
identifiers:
names we give to variables etc
e.g. count…
can be used like count=10
Variables:
A variable is a named location used to store data in the memory.
Data types in python:
1. Int- integers
2. Float- decimals
3. Complex- None
This is special data type with single value. It is used to signify the absence of value/false
in a situation.
string- ordered sequence of letters/characters (like a sentence)
4. Lists- sequence of values of any type
- are editable
5. 5. tuples- similar to lists
but cannot be edited (immutable)
6. 6. sets- unordered lists
7. Mapping- dictionary
Dictionary is an unordered collection of key:value pairs. It is generally used when we
have a huge amount of data. Dictionaries are optimized for retrieving data. We must
know the key to retrieve the value.
Python operators
Operators are special symbols which represent computation
Arithmetic operators
+ addition
- subtraction
* multiplication
/ division
// integer division (ignores the decimal and rounds off the ans to whole number, e.g.
25/10=2)
% remainder
** raised to power
comparison operators:
> greater than (true or false)
< lesser than (true or false)
== equal to (t or f)
!= not equal to (t or f)
>= greater than or equal to (t or f)
<= lesser than or equal to (t or f)
logical operators:
and true and true (true)
or true or false (true/false)
not not false (true)
Assignment operator
= x=5
Implicit data type- python (or a program) decides the type for you, also called typecasting
automatically.
Explicit data type- you tell python exactly what type you want, done using functions like int(),
float(), str().
LISTS:
List is a sequence of values of any type. Values in the list are called elements / items. List is
enclosed in square brackets.
a list is created by placing all the items (elements) inside a square bracket [ ], separated by
commas. It can have any number of items and they may be of different types
A list can also have another list as an item. This is called nested lists.
# nested list
student marks = ["Aditya", "10-A", [ "english",75]]
How to access elements of a list?
- list index: A list index is the position at which any element is present in the list. Index in the list
starts from 0
- negative indexing: The index of -1 refers to the last item, -2 to the second last item and so on.
How to add elements to the list
-append() : adds ONE item at the end
puts it at the end of the list
e.g. nums = [1, 2, 3]
[Link](4)
print(nums)
-insert(): adds ONE item at a specific position
syntax- insert(index, element)
{indexing starts from left most from 0}
-extend(): adds MULTIPLE items to the end
used to merge lists, elements get added one by one
Removing elements from a list
-remove(): is used to remove the value (u know the exact value which needs to be removed)
Default behavior- removes the first matching value
-pop(): is used to remove whatever value is present at the index which needs to be removed (u
know the position of the value which needs to be removed)
Default behavior- removes the last index item if not specified.
Slicing of a list-
Slice operation is performed on Lists with the use of colon(:). List[ : ]
- To print elements from beginning to a range use [:Index]
- to print elements from end use [:-Index]
- to print elements from specific Index till the end use [Index:]
- to print elements within a range, use [Start Index: End Index]
- to print whole List with the use of slicing operation, use [:].
- to print whole List in reverse order, use [::-1].
Other operators possible in lists:
clear() - Removes all items from the list
index() - Returns the index of the first matched item
count() - Returns the count of number of items
sort() - Sort items in a list in ascending order
reverse() - Reverse the order of items in the list
copy() - Returns a shallow copy of the list
what are tuples- tuple is a collection of Python objects which is ordered and unchangeable.
Deleting a tuple- del()
If statement- The if statement is used to check a condition: if the condition is true, we run a
block of statements (called the if-block).
If-else statement- The if..else statement evaluates test expression and will execute body of if
only when test condition is True. If the condition is False, body of else is executed. Indentation is
used to separate the blocks.
If-elif-else statement- The elif is short for else if. It allows us to check for multiple expressions. If
the condition for if is False, it checks the condition of the next elif block and so on. If all the
conditions are False, body of else is executed.
The if block can have only one else block. But it can have multiple elif blocks
Nested if statements- we can have an if...elif...else statement inside another if...elif...else
statement.
Indentation is the only way to figure out the level of nesting.
For loop- The for..in statement is another looping statement which iterates over a sequence of
objects.
- runs a fixed number of times or over a sequence
- usually used with lists, strings, ranges
e.g for i in range(5):
print(i)
while loop- The while statement allows you to repeatedly execute a block of statements as long
as a condition is true.
- runs as long as a condition is True
- used when you don’t know exactly how many times you need
- e.g.
i=0
while i < 5:
print(i)
i += 1
PACKAGES
- a package is nothing but a space where you can find codes or functions or modules of
similar type.
1) NumPy
- A package created to work around numerical arrays in python.
- Handy when it comes to working with large numerical databases and calculations
around it.
2) OpenCV
- An image processing package which can explicitly work around images and can be used
for
- image manipulation and processing like cropping, resizing, editing, etc.
3) Matplotlib
- A package which helps in plotting the analytical (numerical) data in graphical form.
- It helps the user in visualizing the data thereby helping them in understanding it better.
4) NLTK
- NLTK stands for Natural Language Tool Kit and it helps in tasks related to textual data.
- It is one of the most commonly used package for Natural Language Processing.
5) Pandas
- A package which helps in handling 2-dimensional data tables in python.
- It is useful when we need to work with data from excel sheets and other databases.
import numpy to import the numpy file into the python
import numpy as np import numpy and refer to it as np wherever in the code
from numpy import array import array(one functionality) from whole numpy package
from numpy import array as arr import only on functionality from the whole package (array)
and refer to it as arr.
What is NumPy?
Stands for numerical python, package for mathematical and logical operations on arrays in
Python.
-array - a set of multiple values which are of same datatype
Numpy array lists
- only numbers (int, float, etc.) - can have anything. Numbers, strings,
even other lists
Speed: super fast for calculations because Speed: slow for heavy math.
they are made for math
math applies to all elements at once you can’t do math directly on the whole list.
import numpy A = [1,2,3,4,5,6,7,8,9,0]
A=[Link]([1,2,3,4,5,6,7,8,9,0])
array() → make a numpy array from a list
zeros() → all zeros
random() → random numbers
full() → same number everywhere
arange() → sequence of numbers
u can js give a variable to the [Link]
for e.g.
[Link]=arr
arr+5
for example will add 5 to every item present in the array.
This makes computation very easy.
Properties of an array mathematical functions of an array
Function What it does Function What it does
type(ARR) tells you if it’s a numpy array or not biggest number in the whole
[Link]()
array
[Link] number of dimensions (1D, 2D, etc.)
[Link](axis=1) biggest number row-wise
rows × columns (or length of each
[Link]
dimension) [Link](axis=0) smallest number column-wise
[Link] total number of elements [Link]() adds up all numbers in the array
[Link] type of elements inside (int, float, etc.)