MENU

Chapter 8 Getting Started with Python Solutions

Question - 21 : -
What is a list?

Answer - 21 : -

Lists are the most versatile of Python’s compound data types. A list contains items separated by commas and enclosed within square brackets ([ ]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.

Question - 22 : -
Explain String data type.

Answer - 22 : -

Strings in Python are identified as a contiguous set of characters in between quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.

Question - 23 : -
What is a Number data types ?

Answer - 23 : -

Number data types store numeric values. They are immutable data types, which means that changing the value of a number data type results in a newly allocated object.

Question - 24 : -
Write a list comprehension that builds a list of the even numbers from 1 to 10 (inclusive).

Answer - 24 : -

foo = [x for x in range(l, 11) if (x % 2) = = 0] print foo [2,4,6,8,10]

Question - 25 : -
When do you use list vs. tuple vs. dictionary vs. set?

Answer - 25 : -

‘List’ is like an array, individual element of list data can be accessed using indexing and can be manipulated. “Tuples” are similar to list, but there data can be changed once created through the execution of program. ‘Set’ stores unordered values and have no index. And unlike Tuples and lists, sets can have no duplicate data. “Dictionary” is similar to what their name is. It consist of pairs of keys and thier corresponding values.

Question - 26 : -
What is PEP 8?

Answer - 26 : -

PEP 8 is a coding convention(a set of recommendations) to write your Python code in order to make it more readable and useful for those after you.

Question - 27 : -
Explain how Python is interpreted.

Answer - 27 : -

Python program runs directly from the source code. Each type Python programs executed code is required. Python converts source code written by the programmer into intermediate language which is again translated into the native language/ machine language that is executed. So Python is an interpreted language.

Question - 28 : -
How do we share global variables across modules in Python?

Answer - 28 : -

We can create a config file & store the entire global variable to be shared across modules or script in it. By simply importing config, the entire global variable defined. It will be available for use in other modules.
For example we want a, b & c to share between modules.
config.py :
a = 0
b = 0
c = 0
modulel.py:
import config
config.a = 1
config.b = 2
config.c = 3
• print ” a, b & c are : ” , config.a, config.b, config.c

Question - 29 : -
What are the rules for local and global variables in Python?

Answer - 29 : -

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be a local. If a variable is ever assigned a new value inside the function, the variable is implicitly local, and you need to explicitly declare it as ‘global’. Though a bit surprising at first, a moment’s consideration explains this. On one hand, requiring global for assigned variables provides a bar against unintended side-effects. On the other hand, if global was required for all global references, you will be using global all the time. You have to declare as global every reference to a builtin function or to a component of an imported module. This clutter would defeat the usefulness of the global declaration for identifying side-effect.

Question - 30 : -
What does ‘immutable’ mean ? Which data type in Phython are immutable.

Answer - 30 : -

An immutable variable change of value will not happen in place. Modifying an immutable variable will rebuild the same variable. For example,
>>> x= 5
will create a value 5 referenced by
x x —> 5
>>> y = x
This statement will make y refer to 5 of x.
x
5
y
>>> x = x + y
As x being, immutable type, has been rebuild. In the statement expression of RHS will result into value 10 and when this is assigned to LHS (x), x will rebuild to 10. An Integer data type in python is immutable.

Free - Previous Years Question Papers
Any questions? Ask us!
×