



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















1. Define Algorithm Algorithm : It is a sequence of instructions designed in such a way that if the instructions are executed in the specified sequence, the desired results will be obtained. The instructions in an algorithm should not be repeated infinitely. The algorithm should be written in sequence. 2. What are the properties of algorithm? It is written in simple English. Each step of an algorithm is unique and should be self explanatory. An algorithm must have at least one input. An algorithm must have at least one output. An algorithm has finite number of steps. 3. What are the building block of algorithm? The three building block of algorithm are : Sequence Selection Iteration 4. What is meant by selection, iteration and sequence controlstructures? Sequence: A sequence is one of the basic logic structures in computer programming. In a sequence structure, an action, or event, leads to the next ordered action in a predetermined order. Selection: A selection (also called a decision) is also one of the basic logic structures in computer programming. In a selection structure, a question is asked, and depending on the answer, the program takes one of two courses of action, after which the program moves on to the next event. Selection: A selection (also called a decision) is also one of the basic logic structures in computer programming. In a selection structure, a question is asked, and depending on the answer, the program takes one of two courses of action, after which the program moves on to the next event. 5. Define Flowchart It is a pictorial representation of an algorithm. The flowchart uses different shape symbols to denote the different appropriate instructions and these instructions can be written within the boxes using clear statements.
6. Write the Characteristics of Pseudo code. Named variables represent data and identifiers denote higher level functions. Composed of a sequence of statements or steps. Statements are often numbered sequentially. Operational (Imperative) statements include assignment, input, and outpu Control structures provide iterative and conditional execution. Indentations used for grouping b 8. What is need for flowchart symbol? The need for flowchart symbols because each symbols of different shapes denotes different types of instructions. The program logic through flowcharts is made easier through the use of symbol that has standardized planning. **9. Write some rules for drawing a flowchart.
1. Define python Python is an object-oriented, high level language, interpreted, dynamic and multipurpose programming language. 2. Give the features of python.
^ ^ Easy to Use: ^ ^ Expressive Language ^ Interpreted Language ^ Cross-platform language ^ ^ Free and Open Source ^ ^ Object-Oriented language Extensible
3. What is python interpreter? The engine that translates and runs Python is called the Python Interpreter: There are two ways to use it: immediate mode and script mode. The >>> is called the Python prompt. The interpreter uses the prompt to indicate that it is ready for instructions. 4. What is the difference between intermediate mode and script mode?
In immediate mode, you type Python expressions into the Python Interpreter window, and the interpreter immediately shows the result. Alternatively, you can write a program in a file and use the interpreter to execute the contents of the file. Such a file is called a script. Scripts have the advantage that they can be saved to disk, printed, and so on.
4. What is meant by value in python? A value is one of the fundamental things—like a letter or a number—that a program manipulates. 5. List the standard data types in python. Python has five standard data Types: Numbers Strings List, Tuples Dictionary
9. What is tuple? What is the difference between list and tuple? A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists. Eg:
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
10. Give the features of python dictionaries Python's dictionaries are kind of hash table type. They work like associative arrays and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]). For example − dict = {}
dict['one'] = "This is one"
11. What is a variable? One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value. The assignment statement gives a value to a variable. Eg:
n = 17 pi = 3. 12. What are the rules for naming a variable? Variable names can be arbitrarily long. They can contain both letters and digits, but they have to begin with a letter or an underscore. Although it is legal to use uppercase letters, by convention we don‘t. If you do, remember that case matters. Bruce and bruce are different variables. The underscore character ( _) can appear in a name. Eg: my_name 13. What are keywords? Keywords are the reserved words in Python. We cannot use a keyword as variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language In Python, keywords are case sensitive. There are 33 keywords in Python. Eg: False, class, finally, return
14. What are the rules for writing an identifier? Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (). Names like myClass, var and print_this_to_screen, all are valid example. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine. Keywords cannot be used as identifiers. We cannot use special symbols like !, @, #, $, % etc. in our identifier. Identifier can be of any length.
15.what are expressions? An expression is a combination of values, variables, operators, and calls to functions. If you type an expression at the Python prompt, the interpreter evaluates it and displays the result:
1 + 1=
16. What is a statement? A statement is an instruction that the Python interpreter can execute. When you type a statement on the command line, Python executes it. Statements don‘t produce any result. For example, a = 1 is an assignment statement. if statement, for statement, while statement etc. are other kinds of statements. 17. What is multiline statement? In Python, end of a statement is marked by a newline character. But we can make a statement extend over multiple lines with the line continuation character (). For example: a = 1 + 2 + 3 +
4 + 5 + 6 +
7 + 8 + 9 18. What is docstring? Doc string is short for documentation string. It is a string that occurs as the first statement in a module, function, class, or method definition. It is used to explain in brief, what a function does. 19. What is a function? Mention the type of function and use. A Function can be called as a section of a program that is written once and can be executed whenever required in the program, thus making code reusability. There are two types of Functions. a) Built-in Functions: Functions that are predefined. We have used many predefined functions in Python. b) User- Defined: Functions that are created according to the requirements. 20. Mention the types of arguments in python 1.python default arguments. 2.python keyword argument
32. Give the characteristics of membership operator? in and not in are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).In a dictionary we can only test for presence of key, not the value.
Operator Meaning Example in True if value/variable is found in the sequence 5 in x not in True if value/variable is not found in the sequence 5 not in x
1. What is Boolean value? A Boolean value is either true or false. In Python, the two Boolean values are True and False (the capitalization must be exactly asshown), and the Python type is bool.
type( True )
2.What is Boolean Expression? A Boolean expression is an expression that evaluates to produce a result which is a Booleanvalue. For example, the operator == tests if two values are equal. It
produces (or yields) aBoolean value: ^ ^ >>>^ 5 == (3 + 2) # Is five equal 5 to the result of 3 + 2? True 3.What is meant by conditional If? The if statement in python is used to test a condition. If condition is true, statement of if block is executed otherwise it is skipped. Syntax of python if statement: if(condition): statements
4.What is chained conditional statement? To check for multiple conditions elif Statement is used.This statement is like executing a if statement inside a else statement. Syntax: If statement:
statements elif statement: statements else: statements 5.Write the syntax and usage of for loop For Loop is used to iterate a variable over a sequence(i.e., list or string) in the order that they appear. Syntax:
for
6.Write the syntax and usage of while loop While Loop is used to execute number of statements or body till the condition passed in while is true. Once the condition is false, the control will come out of the loop.
‘V‘ in ‗VRB‘ True ‘S‘ in ‗VRB‘ False The not in operator returns the logical opposite results of in operator. ‘x‘ not in ‗VRB‘ True
13.What is the use of str.upper() and str.lower() functions in string? The functions str.upper() and str.lower() will return a string with all the letters of original string converted to upper or lower case letters.
ss=‘VRB Publishers‘ print(ss. upper()) VRB PUBLISHE RS print(ss .lower()) Vrb publishers
14.Explain string comparison with an example. The comparison operator works on string to check if two strings are equal.
word=‘VRB Publishers‘ if word==‘VRB Publishers‘ Print(‗Both are Equal‘) Both are Equal
15.How to split strings and what function is used to perform that operation? The str.split() method is used to split strings up.
book=‘Problem Solving and Python Programming‘ print(book.split()) [‗Problem‘, ‗Solving‘, ‗and‘, ‗Python‘, ‗Programing‘] PART B
Write a program to check whether entered string is palindrome or not. (8 marks) 5.Answer the following questions. What is the output produced by this program given below? (8 marks) words = ‗Hello World‘ print words.title() print words.replace(―World‖,‘Osw al‘) print words.upper()
print words* Assuming num=125, determine the value of each of the following Python expressions. (8 marks) (i) num/ (ii)num% (iii)(num==2 1)&(2<3)
(iv)not((num<45.9)&(6*2<=13))
6. How lists are updated in Python? The append() method is used to add elements to a list. Syntax: list.append(obj) List=[123,‘VR B‘] List.append(2017)
Print(― Updat ed List:‖, List) Output: Updated List: [123,‘VRB‘,2017]
7. Write a few methods that are used in Python Lists. a) append()- add an element to end of list b) insert()- insert an item at the defined index c) remove()- removes an item from the list d) clear()- removes all items from the list e) reverse()- reverse the order of items in the list 8. What are the advantages of Tuple over List? Tuple is used for heterogeneous data types and list is used for
homogeneous data types. ^ ^ Since tuple are immutable, iterating through tuple is faster than with list. ^ ^ Tuples that contain immutable elements can be used as key for dictionary.
^ Implementing data that doesn‘t change^ as a tuple remains write-protected.
9. What is indexing and negative indexing in Tuple? The index operator is used to access an item in a tuple where index starts from 0. Python also allows negative indexing where the index of -1 refers to the last item, -2 to the second last item and so on.
my_tuple=(‗p‘,‘y‘,‘t‘,‘h‘,‘o‘,‘n‘) print(my_tuple[5] ) n print(m y_tuple[- 6]) p 10. What is the output of print tuple[1:3] if tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )? In the given command, tuple[1:3] is accessing the items in tuple using indexing. It will print elements starting from 2nd till 3rd. Output will be (786, 2.23). 11. What are the methods that are used in Python Tuple? Methods that add items or remove items are not available with tuple. Only the following two methods are available:
a) count(x)- returns the number of items that is equal to x b) index(x)- returns index of first item that is equal to x
12. Is tuple comparison possible? Explain how with example. The standard comparisons (‗<‘,‘>‘,‘<=‘,‘>=‘,‘==‘) work exactly the same among tuple objects. The tuple objects are compared element by element.
a=(1,2,3,4,5) b=(9,8,7,6,5)
a< b True
13. What are the built-in functions that are used in Tuple?
^ ^ all()- returns true if all elements of the tuple are true or if tuple is empty ^ ^ any()- returns true if any element of tuple is true ^ ^ len()- returns the length in the tuple ^ ^ max()- returns the largest item in tuple ^ ^ min()- returns the smallest item in tuple sum()- returns the sum of all elements in tuple
14. What is the output of print tuple + tinytuple if tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) and tinytuple = (123, 'john')? It will print concatenated tuples. Output will be ('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john'). 15. Explain what is dictionary and how it is created in Python? Dictionaries are similar to other compound types except that they can use any immutable type as an index. One way to create a dictionary is to start with the empty dictionary and add elements. The empty dictionary is denoted {}:
eng2sp = {} eng2sp[‘one‘] = ‘uno‘ eng2sp[‘two‘] = ‘dos‘ 16. What is meant by key-value pairs in a dictionary? The elements of a dictionary appear in a comma-separated list. Each entry contains an index and a value separated by a colon. In a dictionary, the indices are called keys, so the elements are called key-value pairs.>>> print eng2sp {‘one‘: ‘uno‘, ‘two‘: ‘dos‘} 17. How does del operation work on dictionaries? Give an example. The del statement removes a key-value pair from a dictionary. For example, the following dictionary contains the names of various fruits and the number of each fruit in stock: inventory = {‘apples‘: 430, ‘bananas‘: 312, ‘oranges‘: 525, ‘pears‘: 217} print inventory {‘oranges‘: 525, ‘apples‘: 430, ‘pears‘: 217, ‘bananas‘: 312}
Write short note on Functional Programming Approach. (4 marks) What is the difference between Lists and Tuples? Give an example for their usage. (4 marks) Explain the purpose of loop structure in a programming language. Describe the syntax and semantics of any two loop structures provided by Python. ( marks)
1.What is module and package in Python? In Python, module is the way to structure program. Each Python program file is a module, which imports other modules like objects and attributes.
2. Explain how can you access a module written in Python from C? We can access a module written in Python from C by following method,Module = =PyImport_ImportModule(―
print f<open file ‗test.dat‘, mode ‗w‘ at fe820> The open function takes two arguments. The first is the name of the file, and the second is the mode. Mode "w" means that we are opening the file for writing. 5. Explain how the write method works on a file.
f.write("Now is the time") f.write("to close the file") Closing the file tells the system that we are done writing and makes the file available for reading: f.close()
6. Which method is used to read the contents of a file which is already created?
The read method reads data from the file. With no arguments, it reads the entire contents of the file:
text = f.read() print text Now is the timeto close the file