0% found this document useful (0 votes)
32 views

Pythhon Exam

The type() function returns the type of an object and the is operator compares the types of two objects and returns True if they are of the same type. The type() function can be used to check the type of a variable and return the corresponding type object. The is operator is used to check if two variables refer to the same object or not.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Pythhon Exam

The type() function returns the type of an object and the is operator compares the types of two objects and returns True if they are of the same type. The type() function can be used to check the type of a variable and return the corresponding type object. The is operator is used to check if two variables refer to the same object or not.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Explain the built in function?

Built-in functions are pre-defined functions that are available for use
without requiring import or installation of additional libraries.
following are some important Built-in functions.
1. abs()

The abs() function returns the absolute value of a number,

which is the distance from zero to that number. It always

returns a positive value or zero.

Example:

>>>abs(3)

>>>abs(-3)

Output:

2. min()
The min() function returns the smallest of two or more
arguments.

Example:

>>> min(10, 2, 7, 8, 5)

Output: 2

3. max()

The max() function returns the largest of two or more


arguments.

Example:

>>> max(10, 2, 7, 8, 5)

Output:

10

4. divmod()
The divmod() function is used to perform
both division and modulus (remainder) operations
simultaneously on two numbers. It returns a tuple containing
the quotient and the remainder when one number is divided
by another.

Example:

>>> divmod(5, 2) # 5/2 ---> quotient is 2, remainder is 1.

Output:

(2, 1)

5. pow()

The pow() function in Python is used to calculate the power


of a number. The pow(x, y) function returns x to the power y.

Example:

>>> pow(3, 2)

Output:
9

5. len()

The len() function returns the length or the number of


items in an object.

Example:

>>> len("Python")

Output: 6

explain the continue and break statement with example?

In Python, the continue and break statements are control flow statements used
within loops to alter their behavior.

continue Statement:
The continue statement is used to skip the rest of the code inside a loop for
the current iteration and move to the next iteration.

Example: Using continue in a for loop to skip even numbers.

for i in range(1, 11):

if i % 2 == 0:

# Skip even numbers


continue

print(i)

In this example, the continue statement is used to skip the print(i) statement
for even numbers, and it moves to the next iteration of the loop.

break Statement:
The break statement is used to exit a loop prematurely, stopping further
iterations even if the loop condition is not fully satisfied.

Example: Using break in a while loop to find the first occurrence of a


number greater than 5.

i=1

while i <= 10:

if i > 5:

# Stop the loop when the first number greater than 5 is found

break

print(i)

i += 1
Output: 1 2 3 4 5

In this example, the break statement is used to exit the while loop prematurely
when i becomes greater than 5, even though the loop condition ( i <= 10) is
still satisfied.

Both continue and break statements provide a way to control the flow of a loop
based on specific conditions, allowing for more flexibility in loop execution.

write a program to check the enter number is even or odd?


num = int (input ("Enter any number : "))
if (num % 2) == 0:

print ("The number is even")

else:

print ("The number is odd")

Command line Argument?


In Python, command-line arguments are parameters that you can pass to a
Python script when running it from the command line or terminal.

Python provides a module called sys that allows you to access command-
line arguments through the sys.argv list. The sys.argv list contains all
the arguments passed to the script, including the script’s name itself as the
first element (at index 0).
Example: import sys

print(sys.argv[0]) #returns the name with path of current script.

Output: C:\Users\Admin\AppData\Local\Programs\Python\Python311\Example.py

Write short note on keyword argument?

Keyword arguments in Python allow you to pass arguments

to a function using their parameter names. In the calling

function, you can explicitly specify the argument

name along with their value in the form kwarg = value.

Example:

#Function Definition def person_detail(name, city):

print(name,'is leave in', city) #Function Calling

person_detail(name="Devika", city="Aurangabad")

person_detail(city="Jaipur", name="Raj")

Output:
Devika is leave in Aurangabad

Raj is leave in Jaipur

write short note on del statement?

The del statement in Python is used to delete


objects, such as variables, elements from a list, or
attributes from an object. It allows you to remove
references to objects, freeing up memory and
allowing the garbage collector to reclaim the space
occupied by the deleted objects.

Here are a few use cases of the del statement:


1. Deleting Variables:

x = 10

del x
# Now, the variable x is no longer defined

2.Deleting Elements from a List:

my_list = [1, 2, 3, 4, 5]

del my_list[2]

# Now, my_list is [1, 2, 4, 5]

3.Deleting Entire Variables or Objects:

my_variable = [1, 2, 3]

del my_variable

my_object = SomeClass()

del my_object
# The entire object instance my_object is deleted

4.Deleting Slices from a List:

my_list = [1, 2, 3, 4, 5]

del my_list[1:3]

# Now, my_list is [1, 4, 5]

2. Read and write to files in Python

Python offers various methods to read and write to files


where each functions behaves differently. One important
thing to note is the file operations mode. To read a file,
you need to open the file in the read or write mode. While
to write to a file in Python, you need the file to be open in
write mode.
Here are some of the functions in Python that allow
you to read and write to files:
 read() : This function reads the entire file and returns a string
 readline() : This function reads lines from that file and returns as
a string. It fetch the line n, if it is been called nth time.
 readlines() : This function returns a list where each element is
single line of that file.
 write() : This function writes a fixed sequence of characters to a
file.
 writelines() : This function writes a list of string.
 append() : This function append string to the file instead of
overwriting the file.

Q :write a program to declare a variable and initialize it with ant three


digit number and display the addition of digit in python?

# Declare and initialize a variable with a three-digit number


number = 345

# Extract individual digits


digit_1 = number // 100
digit_2 = (number % 100) // 10
digit_3 = number % 10

# Calculate the sum of digits


digit_sum = digit_1 + digit_2 + digit_3

# Display the result


print(f"The number is: {number}")
print(f"Sum of digits: {digit_sum}")
explain loop and its type?
In programming, a loop is a control flow structure that allows a
set of instructions to be executed repeatedly. It's a way to
efficiently perform repetitive tasks without having to write the
same code over and over again. In Python, there are several types
of loops, each serving different purposes. The two main types are:

1. for Loop:
The for loop is used to iterate over a sequence (such as a list,
tuple, string, or range) and execute a block of code for each
element in the sequence.

Syntax: for variable in sequence:

while Loop:
The while loop is used to repeatedly execute a
block of code as long as a specified condition is
true.
write short note on range () function in python?
The range() function in Python is a built-in function that is used to

generate a sequence of numbers within a specified range. It's

often used with loops, especially in for loops, to iterate over a

sequence of numbers.

Syntax:

range(stop)
range(start, stop)

range(start, stop, step)

 start: The starting value of the range (default is


0).
 stop: The ending value of the range (exclusive).
 step: The step or increment between numbers
(default is 1).
The range() function returns an immutable sequence type ( range
object), which is iterable.

Examples:

1. Using range(stop) to generate numbers from 0 to (stop - 1):


2. for i in range(5):
3. print(i)
4. # Output:
5. # 0
6. # 1
7. # 2
8. # 3 #4

Using range(start, stop) to specify a starting value:

for i in range(1, 10, 2):

print(i)

# Output:

#1

#3

#5

#7

#9

Creating a list using list() with range():

my_list = list(range(3, 10, 2))

print(my_list)

# Output: [3, 5, 7, 9]

the range() function is memory-efficient because it doesn't


create the entire sequence of numbers at once. It generates the
values on-the-fly as you iterate over it, making it particularly
useful for large ranges. Additionally, it is commonly used in
conjunction with loops for iterating a specific number of times or
over a range of indices.

Explain the return statement and void function ?

1. Void Function

A void function is a function that doesn’t return a value.

Its main purpose is to perform a certain task or operation,

without producing any output that needs to be used

elsewhere in the program.

Example: In this example, the greet function takes a

name as a parameter and prints a greeting message. It

doesn’t return any value; it simply performs an action.

#Function Definition

def add(num1,num2):
print(num1+num2)

#Function Calling

Output: 30

add(10,20)

2. Return Statement:

A return statement is used in a function to send a value back to the

caller.

Example: In this example, the add function takes two parameters and

returns their sum. The value returned by the function is assigned to the

variable result and then printed.

#Function Definition
def add(num1,num2):
return num1+num2
#Function Calling
result = add(10,20)
print(result)
Output: 30
Explain the type function and Is operator with
example?

Definition
 Python type() is a built-in function that returns the type of the
objects/data elements stored in any data type or returns a new type
object depending on the arguments passed to the function.
 The Python type() function prints what type of data structures are
used to store the data elements in a program.

Python Type()
In Python, we do not explicitly specify the data type of the variable for
storing data elements. Hence, if we want to find out what type of data is
stored in a variable we use Python’s built-in type() function. Python
type() is a built-in function that is used to print the type of function
parameters based on the arguments passed to it. There are 2 different
forms of type() functions i.e. there are 2 different methods to pass the
arguments into the type() function.

a = 10
b = 10.5
c = True
d = 1 + 5j

print(type(a))
print(type(b))
print(type(c))
print(type(d))
output:
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'complex'>

You might also like