MENU

Chapter 8 Getting Started with Python Solutions

Question - 31 : -
Does Python support data type conversion ?

Answer - 31 : -

Yes, Python supportdata type conversion. To convert between built-in types, programmer simply usethe type name as a function. There are several built-in functions to performconversion from one data type to another. These functions return a new objectrepresting the converted value.

Function

Description

int (x [, base])

Converts x to an integer, base specifies the base if x is a string.

long [x [, base])

Converts x to a long integer, base specifies the base if x is a string.

float (x)

Converts x to a floating­point number.

complex (real, [ing])

Creates a complex number.

str (x)

Converts object x to a string representation.

repr (x)

Converts object x to an ex­pression string.

tuple (x)

Converts x to a tuple.

list (x)

Converts x to a list.

chr (x)

Convets an integer to a character.

unichr (x)

Converts an integer to a Unicode character.

diet (x)

Creates a disetionary, x must be a sequence of tuples.

set (x)

Converts x to a set

 

Question - 32 : -
How do you make an array in Python?

Answer - 32 : -

Use a list: [“this”, 1, “is”, “an”, “array”] Lists are equivalent to C or Pascal arrays in their time complexity. The primary difference is that a Python list can contain objects of many different types. The array module also provides methods for creating arrays of fixed types with compact representations, but they are slower to index than lists. Also note that the numeric extensions and others define array-like structures with various characteristics as well.

Question - 33 : -
How do you make conversion between tuples and lists ?

Answer - 33 : -

The function tuple (seq) converts any sequence (actually, any.iterable) into a tuple with the same items in the same order.
For example, tuple([1, 2, 3]) yields (1, 2, 3) and tuple (‘abc’) yields (‘a’, ‘b’, ‘c’). If the argument if a tuple, it does not make a copy but returns the same object, so it is cheap to call tuple( ) when you aren’t sure that an object is already a tuple.
The function list(seq) converts any sequence or iterable into a list with the same items in the same order. For example, list((1, 2, 3)) yields [1, 2, 3] and list (‘abc’) yields [‘a’, ‘b’, ‘c’j. If the argument is a list, if makes a copy just like se[:j would.

Question - 34 : -
What is the difference between list and tuple ? Give an example.

Answer - 34 : -

Lists are Python’s general purpose container, often used for collections of similar objects. Lists are mutable objects that can contain any Python data. Example :
>my list = [ ] # make an Empty list
>my list = [1,1.0+3j, “aperitivo”, true] # make a list containing four entities.
A Tuple is an immutable value (can’t be changed) that can contain any Python data. They are generally used for small collectioin of data.
Example :
>mytuple = [1, 2, 3, 4) # create a four element tuple.
>mytuple[2] = 4 # Error – tuple can’t be modified. > print mytuple[2], len(mytuple)

Question - 35 : -
Is there a tool help to find bugs or perform static analysis ?

Answer - 35 : -

Yes, pychecker is a static analysis tool that finds bugs in Python source case and warns about code complexity and style.
Pylint is another tool that checks if a module satisfies a coding standard, and also make it possible to write plug-ins to add a custom feature.

Question - 36 : -
Name four Python’s basic data types. Why are they called so ?

Answer - 36 : -

The four basic data types are :
1. Number
2. Sequences
3. Sets
4. Maps.
These are called so because :
1. Number data type stores numerical values and is immutable i.e., value of its object cannot be changed.
These are of 3 types :
1. Integer and Long
2. Float/Floatingpoint
3. Complex
2. Sequence is an ordered collection of items, indexed by positive integers, It is a combination of mutable and non-mutable data types. Three types of sequence data type available in Phyton are strings, lists and tuples.
3. Sets in an unordered collection of values of any type, with no duplicate entry. Sets are immutable. Example S = Set ([1,2,3,4])
4. Mapping data types are unordered and mutable. Dictonaries falls under mapping.
Example
d = {1 : ‘a’, 2 : Tb’, 3 : ‘c’

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