Py Unit 1
Py Unit 1
UNIT I
A compiler and an interpreter are two types of language translators that convert
human-readable source code into machine-readable code that can be executed by a
computer.
A compiler is a program that takes the entire source code as input, analyzes it, and
generates an executable machine code file. In this process, the compiler translates the
source code into machine code without executing it. The resulting executable file can
then be executed multiple times without needing to recompile the source code.
An interpreter, on the other hand, is a program that reads the source code line by line
and executes it immediately. The interpreter converts each line of code into machine
code and executes it before moving on to the next line. Unlike a compiler, an
interpreter doesn't produce a separate executable file, and the source code must be
interpreted every time it is run.
The main advantage of a compiler is that the resulting executable code is generally
faster and more efficient because it has already been fully optimized during the
compilation process. Interpreters are generally slower because they need to interpret
the code at runtime, but they are often easier to use and more flexible, as they allow
for more dynamic behavior during execution.
Both compilers and interpreters have their own strengths and weaknesses, and
choosing between them depends on the specific needs of the project or task at hand.
Easy to Learn: Python is designed to be easy to learn and use, with a clean and
straightforward syntax that makes it an ideal language for beginners.
Open Source: Python is open-source software, meaning that it is freely available and
can be used, modified, and distributed by anyone.
1
Linux, and macOS, making it a highly portable language.
Interpreted: Python is an interpreted language, which means that it does not need to
be compiled before execution. This makes it quick and easy to write and test code.
Dynamic Typing: Python uses dynamic typing, meaning that variable types are
determined at runtime rather than during compilation. This allows for more flexible and
efficient code.
In Python, there are mainly three types of errors: syntax errors, runtime errors, and
logical errors.
Syntax Errors:
Syntax errors occur when the code is not written correctly and does not conform to the
syntax rules of the Python language. This type of error is usually detected by the Python
interpreter before the program is executed. Common syntax errors include missing
colons, parentheses, quotation marks, or other characters, as well as misspelled
keywords or variable names.
Runtime Errors:
Runtime errors occur during the execution of a program when something unexpected
happens, such as trying to divide by zero or accessing an index that is out of range.
These errors are not detected by the Python interpreter until the program is executed.
Logical Errors:
Logical errors occur when the program runs without generating any error messages, but
the output is not what is expected. These types of errors are also called bugs, and they
occur when the logic of the program is incorrect.
⦁ What are the differences between Brackets, Parentheses and Braces? Explain.
2
⦁ What is the role of comment in a program? How many ways to use comment in
a python program? Explain.
⦁ What are the Arithmetic operators in python? Give suitable example to explain
each operator.
In Python, the for loop is a control flow statement that allows you to iterate over a
sequence of values, such as a list, tuple, or string. One of the most commonly used
functions with a for loop in Python is the range() function.
The range() function generates a sequence of numbers that can be used to iterate over
using a for loop. The basic syntax of range() is as follows:
Here, start is the starting number of the sequence (inclusive), stop is the ending number
of the sequence (exclusive), and step is the difference between each number in the
sequence. If start is not specified, it defaults to 0. If step is not specified, it defaults to 1.
Here's an example of using a for loop with range() to print the numbers 0 to 4:
python
for i in range(5):
print(i)
Output:
3
4
You can also specify the starting number and step value when using range():
python
print(i)
Output:
In this example, the range(1, 10, 2) function generates a sequence of numbers from 1 to
9 (inclusive) with a step value of 2. The for loop iterates over this sequence, and the
loop variable i takes on each value in the sequence. The print() function is called to print
the value of i.
break Statement:
The break statement is used to terminate a loop prematurely. When a break statement
is encountered within a loop, the loop is immediately terminated, and control is
transferred to the next statement following the loop.
Here's an example of using break in a while loop to terminate the loop when a certain
4
condition is met:
python
i=0
if i == 5:
break
print(i)
i += 1
Output:
In this example, we use a while loop to print the values of i from 0 to 9. However, we
also use an if statement to check if i is equal to 5. If it is, we use the break statement to
terminate the loop prematurely. As a result, the loop only prints the values of i up to 4.
continue Statement:
The continue statement is used to skip the current iteration of a loop and continue with
the next iteration. When a continue statement is encountered within a loop, the
current iteration is immediately terminated, and control is transferred to the next
iteration.
python
for i in range(10):
5
if i % 2 == 0:
continue
print(i)
Output:
In this example, we use a for loop to print the values of i from 0 to 9. However, we also
use an if statement to check if i is even. If it is, we use the continue statement to skip
the current iteration and move on to the next iteration. As a result, the loop only prints
the odd numbers from 0 to 9.
code:
sum = 0
sum += i
output:
6
num2 = float(input("Enter second number: "))
else: