PYTHON

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

The try-except block in Python is used for exception handling, which allows

you to gracefully manage errors that might occur during the execution of
your code.

Here's how it works:


 Try block:
This block contains the code that might raise an exception. Python attempts to
execute the code within this block.
 Except block:
If an exception occurs within the try block, the execution immediately jumps to the
matching except block. The except block contains code to handle the specific
exception that was raised, such as logging an error message or attempting a
different action.
Purpose:
 Preventing crashes:
Without exception handling, an unhandled exception would cause your program to
terminate abruptly. try-except blocks allow you to catch and handle these
exceptions, preventing crashes and making your code more robust.
 Error recovery:
In some cases, you might be able to recover from an error. For example, if a file
you're trying to read is not found, you can prompt the user for a different file path.
 Custom error messages:
The default error messages in Python might not always be helpful to the end
user. try-except blocks allow you to provide more informative and user-friendly
error messages.

Different ways to use import statement in Python

The syntax of the import statement is straightforward. All you need


to do is type import followed by the name of the module you want to
import. In this code block, we first import the random module. Then,
we call the randint function from the random module to generate a
random number between 1 and 10.
Identify the meaning of variable and explain local and global variable with example?

Global variables are declared outside any function or class, while


local variables are declared within a function or block of code.
Global variables can be accessed from anywhere in the program,
whereas local variables are only accessible within the function or
block where they are defined.
1. 1. Global Variable:
 global_count is declared outside any function, making it a global variable.
 It can be accessed and modified from anywhere in the program.
 To modify a global variable inside a function, use the global keyword.
2. 2. Local Variable:
 local_count is declared inside the increment_local function, making it a local
variable.
 It is only accessible within that function and cannot be accessed from outside.

global_count = 0

def increment_global():
global global_count # Access the global variable
global_count += 1 # Modify the global variable

def print_global():
print("Global count:", global_count) # Access the global variable

# Local variable
def increment_local():
local_count = 0 # Local variable
local_count += 1
print("Local count:", local_count)

Utilize the Panda library through an example to read CSV file

Python language is one of the most trending programming languages as it


is dynamic than others. Python is a simple high-level and an open-source
language used for general-purpose programming. It has many open-
source libraries and Pandas is one of them. Pandas is a powerful, fast,
flexible open-source library used for data analysis and manipulations of
data frames/datasets. Pandas can be used to read and write data in a
dataset of different formats like CSV (comma separated values).
import pandas as pd
data = pd.read_csv("your downloaded dataset location ")
#data[start:end]

#start is inclusive whereas end is exclusive

print(data[10:21])

# it will print the rows from 10 to 20.

# you can also save it in a variable for further use in analysis

sliced_data=data[10:21]

print(sliced_data)

You might also like