You are on page 1of 2

Dictionary and Sets

1. Dictionary is a collection of key-value pairs, where each key must be unique. They work
like real world dictionary where the people look up word, which is key, to find their
definition, which is value.
Set is the unordered collection of unique elements. It is similar to a list, but it does not
allow duplicate items.
2.

Here to create the set we used ‘{}’ this type of brackets which indicate that the following
group of data appear to be the set. After, we used the ‘x**2’ which mean that we square
the number. ‘for x in range(1,11)’ gives the values for x form 1 to 10 inclusive.
3.

Here the to indicate the dictionary used the “{}” these types of brackets and the
dictionary was written in the following form:
dict_name = {
key : value
}
Which appear to be the syntax of dictionary in python. The ‘try except’ construction was
used to give an error message in case of the key value which is not in list.
Testing, Debugging, Exceptions, Assertions
1. Syntax Errors:
a. Syntax errors in Python occur when the interpreter face code that violate the rules
of Python language. These errors prevent the code from being executed. Most of
the errors are typically because of mistakes in the structure of the code, such as
incorrect usage of keywords, missing or misplaced punctuation, or other
violations of the language syntax rules.
b. On of the example of syntax error is the missing colon:
Which can be fixed just by adding the colon after the if statement:

2. Runtime errors:
a. Runtime errors are the type errors that occur during the execution of program.
Unlike syntax errors, which are detected by the interpreter before execution,
runtime errors occur while program is running.
b. Runtime errors can be handled through the usage of ‘try’ – ‘except’ block where
in try part the code will run while in except part when runtime occurs will run
code for the runtime error handling.
3. Debugging tools:
a. The main purpose of debugging is to ensure that program runs correctly and
produces correct output.
b. Breakpoints are a powerful debugging tools that allows developer to pause the
execution of a code at a specific line or condition, giving them chance to inspect
the state of the program analyze the behavior of program.
4. Testing methods:
a. While unit testing tests individual portions of code in isolation, integration testing
ensure that different portions of code work together as a whole system when
regression testing verifies that new changes do not break the existing
functionality.
5. Assertions:
a. Assertions in Python are statements used to check if a given expression is true.
They are often used for debugging and testing to catch and handle situations. In
testing, assertions are useful for validating that the program behaves as expected
during development and to catch unexpected issues.
6. Debugging techniques:
a. Debugging with print statement involves strategically inserting the print
statements in your code to output variables and messages. BY running the
program and analyzing the printed output there can be identified the and isolated
issues in the code. This method should be iterated till the problem pinpointed,
then it can be removed or commented out.
b. Debugging techniques offer valuable benefits like code understanding, early issue
detection, and collaborative problem solving. They help developers efficiently
identify and address issues, which improve the quality of code. However,
debugging can be time consuming, especially for complex programs, and reliance
on print statement may clutter code. Debugging also relies on developer skills.
Nevertheless, leaving debugging tool in production code poses security risks.
7. Exception handling:
a. Exception handling in Python helps developers anticipate, catch and manage
errors during runtime, preventing program termination and providing informative
messages for early issue detection. This approach, fostering robust and user-
friendly software, allows for graceful degradation, centralized error handling, and
streamlined debugging processes, enhancing overall code reliability.

You might also like