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

exame book

Python is an interpreted, object-oriented scripting language known for its readability and compatibility across various platforms. Key features include simplicity, extensive libraries, and support for multiple programming paradigms. The document also outlines the program development life cycle, memory management, and various Python concepts such as data types, namespaces, and control statements.

Uploaded by

mamatha.pragada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

exame book

Python is an interpreted, object-oriented scripting language known for its readability and compatibility across various platforms. Key features include simplicity, extensive libraries, and support for multiple programming paradigms. The document also outlines the program development life cycle, memory management, and various Python concepts such as data types, namespaces, and control statements.

Uploaded by

mamatha.pragada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1. What is Python?

 Python is an interpreted scripting language that is known for its power, interactivity, and object-oriented
nature. It utilizes English keywords extensively and has a simpler syntax compared too many other
programming languages.
 Python is designed to be highly readable and compatible with different platforms such as Mac, Windows,
Linux, Raspberry Pi, etc.

2. Python is an interpreted language. Explain.

An interpreted programming language refers to any language that executes its instructions sequentially, one line
at a time. In the case of Python, programs are executed directly from the source code without the need for any
intermediate compilation process.

3. What distinguishes lists from tuples?


Lists Tuples
Lists are mutable, i.e., they can be edited Tuples possess immutability, denoting their incapability
of being modified like lists.
Lists are usually slower than tuples Tuples are faster than lists
Lists consume a lot of memory Tuples consume less memory when compared to lists
Lists have a higher likelihood of Tuples offer increased reliability due to their resistance to
experiencing unexpected changes, making unexpected modifications.
them less reliable in terms of errors.
Lists consist of many built-in functions. Tuples do not consist of any built-in functions.
Syntax: Syntax:

list_1 = [10, ‘Intellipaat’, 20] tup_1 = (10, ‘Intellipaat’ , 20)

4 . What are the Key features of Python?

Python's features include-


1. Simple and Easy-to-learn: Python has few keywords, simple structure, and a clearly defined
syntax. This allows a student to pick up the language quickly.
2. High library: Python's bulk of the library is very portable and cross- platform compatible on
UNIX, Windows, and Macintosh.
3. Interactive Mode: Python has support for an interactive mode, which allows interactive testing
and debugging of snippets of code.
4. Portable:
⮚ Python can run on a wide variety of hardware platforms and has the same interface on all
platforms.
⮚ The python program written on one operating system can be moved and executed on
other operating system without any modifications.
5. Extendable and extensible:
⮚ Being an interpreted language python programs/scripts takes more time for execution
compared to C/C++.
⮚ If we want to execute part of python very fastly and if you want to hide some part of code
then that part of code can be implemented by using other languages.
⮚ In the same way if we want add scripting capabilities to other languages then we can
place python code in that languages.
6. High level: Being a high level programming languages while writing programs in python we no need
to bother about the internal details like memory management etc.
7. GUI Programming: Python supports GUI applications that can be created and ported to many
system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X
Window system of Unix.
8. Scalable: Python provides a better structure and support for large programs than shell scripting.
Apart from the above-mentioned features, Python has a big list of good features. A few are listed below-
1. It supports functional and structured programming methods as well as OOP.
2. It can be used as a scripting language or can be compiled to byte-code for building large
applications.
3. It provides very high-level dynamic data types and supports dynamic type checking.
4. It supports automatic garbage collection.It can be easily integrated with C, C++, COM, ActiveX,
CORBA, and Java

5. Explain Program Development Life Cycle in python?


• When we want to develop a program using any programming language, we follow a sequence of steps.
• These steps are called phases in program development.
• The program development life cycle is a set of steps or phases that are used to develop a program in any
programming language.
• Generally, the program development life cycle contains 6 phases, they are as follows….
• Problem Definition
• Problem Analysis
• Algorithm Development
• Coding & Documentation
• Testing & Debugging
• Maintenance
1. Problem Definition
• In this phase, we define the problem statement and we decide the boundaries of the problem.
• In this phase we need to understand the problem statement, what is our requirement, what should
be the output of the problem solution.
• These are defined in this first phase of the program development life cycle.
2. Problem Analysis
• In phase 2, we determine the requirements like variables, functions, etc. to solve the problem.
• That means we gather the required resources to solve the problem defined in the problem
definition phase.
• We also determine the bounds of the solution.
3. Algorithm Development
• During this phase, we develop a step by step procedure to solve the problem using the
specification given in the previous phase.
• This phase is very important for program development. That means we write the solution in step
by step statements.
4. Coding & Documentation
• This phase uses a programming language to write or implement the actual programming
instructions for the steps defined in the previous phase.
• In this phase, we construct the actual program.
• That means we write the program to solve the given problem using programming languages like
C, C++, Java, etc.,
5. Testing & Debugging
• During this phase, we check whether the code written in the previous step is solving the specified
problem or not.
• That means we test the program whether it is solving the problem for various input data values or
not. We also test whether it is providing the desired output or not.
6. Maintenance
• During this phase, the program is actively used by the users.
• If any enhancements found in this phase, all the phases are to be repeated to make the
enhancements.
• That means in this phase, the solution (program) is used by the end-user.
• If the user encounters any problem or wants any enhancement, then we need to repeat all the
phases from the starting, so that the encountered problem is solved or enhancement is added

6. Write any four Differences between C, C++, JAVA and python

7. What is meant by IDE and mention any two IDE’s?


IDE is an integrated development environment

8. Write any two lines how is Memory managed in Python?

 Python makes use of automatic memory management through garbage collection.


 The garbage collector keeps track of objects and frees memory when they are no longer in use.
 Python uses reference counting to manage memory, incrementing and decrementing reference counts as
needed.
 A cyclic garbage collector handles objects with circular references.
 Python also provides tools like context managers and the “with” statement to release resources
automatically.
 Python’s memory management simplifies coding by handling memory allocation and deallocation
automatically.

9. What are Python Modules?

Files containing Python codes are referred to as Python modules.

This code can be of different types like classes, functions, or variables. This saves the programmer’s time by
providing predefined functionalities when needed. It is a file with “.py” extension containing an executable code.

10. What are the common built-in data types in Python?

Python supports the below-mentioned built-in data types:

Immutable data types:

 Number
 String
 Tuple

11. What are python namespaces?

 A Python namespace ensures that the names assigned to objects within a program are unique and can be
used without conflict. In Python, namespaces are implemented as dictionaries where the object’s name
serves as the key and the object itself serves as the value.

12. What is scope ?

 In Python, a scope defines the region of code where an object remains valid and accessible. Every object
in Python operates within its designated scope

13. What is __init__ in Python?

 In Python classes, the reserved method init serves a similar purpose as constructors in object-oriented
programming (OOP) terminology. When a new object is created, the init method is automatically called,
initializing the object and allocating memory for it. This method can also be utilized to set initial values
for variables.

Below is an example:
class Human:
def __init__(self, age):
self.age = age
def say(self):
print('Hello, my age is', self.age)
h = Human(22)
h.say()

Output:
Hello, my age is 22

14. What are local variables and global variables in Python?

A local variable is a variable that is defined within a specific function and is only accessible within that
function. It cannot be accessed by other functions within the program.

A global variable is a variable that is declared outside of any function, allowing it to be accessed by all
functions .

Example:-

g=4 #global variable

def func_multiply():

l=5 #local variable

m=g*l

return m

func_multiply()

Output: 20

15. What is type conversion in Python?

Python offers a valuable feature that allows for the conversion of data types as needed. This process is referred to
as type conversion in Python.

Type conversion can be divided into two types:

Implicit Type Conversion: This type of conversion is automatically performed by the Python interpreter without
requiring any user intervention.

Explicit Type Conversion: This type of conversion involves the user explicitly changing the data type to the
desired type.

16. What is a dictionary in Python?

Python supports various data types, including dictionaries. A dictionary in Python is a collection of elements
that are stored as key-value pairs. It is an unordered data structure, and the indexing is done based on the keys
assigned to each element.

example: we have a dictionary named ‘dict’ with two keys, ‘Country’ and ‘Capital’, which have corresponding
values ‘India’ and ‘New Delhi’, respectively.

Syntax:
dict={‘Country’:’India’,’Capital’:’New Delhi’, }

17. Is python case sensitive?

Yes,

Python is a case sensitive language. In Python, it is important to note that “Function” and “function” are distinct
entities, similar to how SQL and Pascal handle them differently.
18. Is indentation required in Python?

Indentation is an essential aspect of Python syntax, ensuring proper code structure.

It is a method used by programming languages to determine the scope and extent of code blocks. In Python,
indentation serves this purpose.

The mandatory requirement of indentation in Python not only enforces consistent code formatting but also
enhances code readability, which is likely the reason behind its inclusion.

19. How does break, continue, and pass work?

The following statements assist in altering the course of execution from the regular flow, which categorizes them
as loop control statements.

 Python break: This statement aids in discontinuing the loop or the statement and transferring control to the
subsequent statement.
 Python continue: This statement enforces the execution of the subsequent iteration when a particular
condition is met, instead of terminating it.
 Python pass: This statement allows the syntactical writing of code while intending to bypass its execution.
It is also recognized as a null operation, as no action is taken when the pass statement is executed.

20. What type of language is python? Programming or scripting?


 Python is a versatile programming language that excels not only as a general-purpose language but also
as a scripting tool.

21. What are negative indexes and why are they used?

 To retrieve an item from a sequential collection, we can simply utilize its index, which represents the
position of that specific item. Conventionally, the index commences at 0, implying that the initial element
has an index of 0, the second element has an index of 1, and so forth.
 When employing reverse indexing, we access elements from the opposite end of the sequence. In this
case, the indexing initiates from the last element, denoted by the index number ‘-1’. The second-to-last
element is assigned an index of ‘-2’, and so forth. These negative indexes employed in reverse indexing
are specifically referred to as negative indexes.

22. What is the difference between append() and extend() methods?

 Both append() and extend() methods are methods used to add elements at the end of a list.
 The primary differentiation between the append() and extend() methods in Python is that append() is used
to add a single element to the end of a list. In contrast, open () is used to append multiple aspects, such as
another list or an iterable, to the end of a list.

22. What is the lambda function in Python?

 A lambda function is an anonymous function (a function that does not have a name) in Python. To define
anonymous functions, we use the ‘lambda’ keyword instead of the ‘def’ keyword, hence the name
‘lambda function’. Lambda functions can have any number of arguments but only one statement.

For example:
l = lambda x,y : x*y
print(a(5, 6))

Output:30

23. What is slicing in Python?


Slicing is a technique employed to extract a specific range of elements from sequential data types, such as
lists, strings, and tuples. Slicing is beneficial and easy to extract out elements. It requires a : (colon) which
separates the start index and end index of the field. All the data sequence types, list or tuple, allows users to
use slicing to get the needed elements. Although we can get elements by specifying an index, we get only a
single element. Whereas, using slicing, we can get a group or appropriate range of needed elements.

Syntax:
List_name[start:stop]

24. What is self in Python?


Self is an object or an instance of a class. This is explicitly included as the first parameter in Python. On the other
hand, in Java it is optional. It helps differentiate between the methods and attributes of a class with local
variables.
The self variable in the init method refers to the newly created object, while in other methods, it refers to the
object whose method was called.

Syntax:
Class A:
def func(self):
print("Hi")

25. Why would you use NumPy arrays instead of lists in Python?

NumPy arrays provide users with three main advantages as shown below:

 NumPy arrays consume a lot less memory, thereby making the code more efficient.
 NumPy arrays execute faster and do not add heavy processing to the runtime.
 NumPy has a highly readable syntax, making it easy and convenient for programmers.

26. What is polymorphism in Python?


Polymorphism is the ability of the code to take multiple forms. Let’s say, if the parent class has a method named
XYZ then the child class can also have a method with the same name XYZ having its own variables and
parameters.

27. How will you remove the last object from a list in Python?
my_list = [1, 2, 3, 4, 5]
my_list.pop()

Here, −1 represents the last element of the list. Hence, the pop() function removes the last object (obj) from the
list.

28. How can you generate random numbers in Python?


This is achieved by importing the random module. It is the module that is used to generate random numbers.
Syntax:
import random
random.random # returns the floating point random number between the range of [0,1].

29. How will you convert a string to all lowercase?


To convert a string to all lowercase in Python, you can use the built-in lower() method. The lower() method is
available for strings and returns a new string with all characters converted to lowercase.
For Example:
demo_string='ROSES'
print(demo_string.lower())

30 . What would be the output if I run the following code block?


list1 = [2, 33, 222, 14, 25]
print(list1[-2])
Ans. output:14

31 . How can the ternary operators be used in python?


The ternary operator is the operator that is used to show conditional statements in Python. This consists of the
boolean true or false values with a statement that has to be checked.

Syntax:
x , y=10,20
count = x if x<y else y

32. How to add values to a python array?


In python, adding elements in an array can be easily done with the help
of extend(),append() and insert() functions.
Consider the following example:
x=arr.array('d', [11.1 , 2.1 ,3.1] )
x.append(10.1)
print(x) #[11.1,2.1,3.1,10.1]
x.extend([8.3,1.3,5.3])
print(x) #[11.1,2.1,3.1,10.1,8.3,1.3,5.3]
x.insert(2,6.2)
print(x) # [11.1,2.1,6.2,3.1,10.1,8.3,1.3,5.3]

33. How to remove values to a python array?


Elements can be removed from a python array by using pop() or remove() methods.
pop(): This function will return the removed element .
remove():It will not return the removed element.
Consider the below example :
x=arr.array('d', [8.1, 2.4, 6.8, 1.1, 7.7, 1.2, 3.6])
print(x.pop())
print(x.pop(3))
x.remove(8.1)
print(x)

Output:
3.6
1.1 # element popped at 3 rd index
array('d', [ 2.4, 6.8, 7.7, 1.2])

34. Write a code to sort a numerical list in Python?

The following code can be used to sort a numerical list in Python:


numbers = ["2", "5", "7", "8", "1"]
numbers = [int(i) for i in numbers]
numbers.sort()
print(numbers)

Output:- 1 2 5 7 8

35. Which of the following is an invalid statement?


1. xyz = 1,000,000
2. x y z = 1000 2000 3000
3. x,y,z = 1000, 2000, 3000
4. x_y_z = 1,000,000

Ans. 2 statement is invalid.

36. Write a command to open the file c:\hello.txt for writing?

Command:
f= open(“hello.txt”, “wt”)

37. What does len() do?

len() is an inbuilt function used to calculate the length of sequences like list, python string, and array.
my_list = [1, 2, 3, 4, 5]
length = len(my_list)
print(length)

38. What does *args and **kwargs mean in Python?

 .*args: It is used to pass multiple arguments in a function.


 **kwargs: It is used to pass multiple keyworded arguments in a function in Python.

39. What are python iterators?

Python iterators are objects that allow you to access elements of a collection one at a time. They use the __iter__()
and __next__() methods to retrieve the next element until there are no more. Iterators are commonly used in for
loops and can be created for custom objects. They promote efficient memory usage and enable lazy evaluation of
elements. In summary, iterators provide a convenient way to iterate over data structures in a controlled and
efficient manner.

40. Do we need to declare variables with respective data types in Python?

No. Python is a dynamically typed language, i.e., the Python Interpreter automatically identifies the data type of a
variable based on the type of value assigned.

41. What are Dict and List comprehensions?

Python comprehensions are like decorators that help to build altered and filtered lists, dictionaries, or sets from a
given list, dictionary, or a set. Comprehension is a powerful feature in Python that offers a convenient way to
create lists, dictionaries, and sets with concise expressions. It eliminates the need for explicit loops, which can
help reduce code size and save time during development.

Comprehensions are beneficial in the following scenarios:

 Performing mathematical operations on the entire list


 Performing conditional filtering operations on the entire list
 Combining multiple lists into one
 Flattening a multi-dimensional list

For example:
my_list = [2, 3, 5, 7, 11]
squared_list = [x**2 for x in my_list] # list comprehension

# output => [4 , 9 , 25 , 49 , 121]


squared_dict = {x:x**2 for x in my_list} # dict comprehension

# output => {11: 121, 2: 4 , 3: 9 , 5: 25 , 7: 49}

42. How will you remove duplicate elements from a list?

To remove duplicate elements from the list we use the set() function.

Consider the below example:


demo_list = [5, 4, 4, 6, 8, 12, 12, 1, 5]
unique_list = list(set(demo_list))
output = [1, 5, 6, 8, 12]

43.Write a Python program to count the total number of lines in a text file?

Refer the code below to count the total number of lines in a text file-
def file_count(fname):
with open(fname) as f:
for i, _ in enumerate(f):
pass
return i + 1

print("Total number of lines in the text file:",


file_count("file.txt"))

44. Is Python fully object oriented?

Python does follow an object-oriented programming paradigm and has all the basic OOPs concepts such as
inheritance, polymorphism, and more, with the exception of access specifiers. Python doesn’t support strong
encapsulation (adding a private keyword before data members). Although, it has a convention that can be used for
data hiding, i.e., prefixing a data member with two underscores.

You might also like