Python viva
Python viva
STRING:
Sequence of characters enclosed in single, double, or triple quotes
Immutable
LIST:
Ordered collection of data values
Represented by [ ]
Mutable
Created using the list() function
Example: [1, 2, 3, "hello"]
data.append(89) # Add an item
data.remove(45) # Remove an item
TUPLE:
Ordered collection of data values
Represented by ( )
Immutable
Created using the tuple() function.
SET:
Unordered collection of unique elements
Represented by { }
Mutable
Created using the set() function
Example: {1, 2, 3, 4, 5}
set_example = {1, 2, 3, 3, 4} # Duplicates are removed: {1, 2, 3, 4}
DICTIONARY:
A collection of key-value pairs
Keys must be unique
Represented by { }
Mutable
Created using the dict() function
'id': 1,
'name': 'John Doe',
'email': '[email protected]',
'age': 30
}
5//2 = 2
5/2 = 2.5
EXCEPTIONAL HANDLING:
3 main keywords i.e. try, except, and finally
Exceptions:
Error in the code
10 * (1/0)
Exception handling
try:
result = 10 * (1 / 0)
print("Result:", result)
except ZeroDivisionError as e:
print(f"Error: {e}. Division by zero is not allowed.")
Final output
LAMBDA:
A lambda function is an anonymous function. It can have any number of parameters but one
statement.
Example:
a = lambda x, y : x*y
print(a(7, 19))
NEGATIVE INDEX:
Negative numbers mean that you count from the right instead of the left Example: list[-1]
refers to the last element
USER-DEFINED FUNCTIONS:
def keyword is used to declare user-defined functions
Syntax:
def function_name():
statements
.
.
Example:
def fun():
print("Inside function")
fun()
CLASS:
Blueprint for creating objects
CONSTRUCTOR:
__init__()
All classes have a function called __init__(), which is always executed when the class is being
initiated.
It is used to initialize objects of a class.
It is also called a constructor.
SELF:
Represents the instance of class
Allows you to access variables, attributes, and methods of a defined class
OBJECTS:
Variables that contain data and functions that can be used to manipulate the data
FUNCTION:
Set of statements that take inputs, do some specific computation, and produce output
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 call the function.
Functions that readily come with Python are called built-in functions.
Eg: print()
strip() - remove the whitespace or the characters that are passed in the argument.
CONTROL STRUCTURES
Selection - This structure is used for making decisions by checking conditions and
branching.
Only if
if-else
The nested if
The complete if-elif-else
Repetition – Used to repeat a certain set of statements.
The for loop
The while loop
DJANGO
Python framework that makes it easier to create web sites using Python.
Django models act as the interface between the database and the server code.
Django is a high-level Python web framework that enables rapid development of secure and
maintainable websites.
Manage.py
Running the development server. Doing database migrations.
URLs
URLs are used to route users to the appropriate content or functionality when they visit a
web application.
Views
Functions or classes in Django that handle HTTP requests and determine what data or
content should be sent back to the client.
models.py
Defines the structure of the application's database.
PANDAS
Series
It is a one-dimensional array holding data of any type.
DataFrame
Like a 2-dimensional array, or a table with rows and columns.
Three main components: the data, the index, and the columns.
NumPy
Numerical Python, is an open-source Python library that provides support for scientific
computing and mathematical tasks
MATPLOTLIB
Low-level graph plotting library in python that serves as a visualization utility.
Open source
CLASSIFICATION
Assigning data into predefined categories based on specific attributes.
CLUSTERING
Groups data into clusters based on similarities without predefined labels