Worksheet 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Computer science

Holiday homework

Worksheet

1) Why is Python interpreted?


2) Who developed Python?
3) Is Python a compiler language or an interpreter language?
4) What is the difference between interactive mode and script mode in Python?
5) How are keywords different from identifiers?
6) Differentiate between mutable and immutable objects in Python language with example.
7) What are literals in Python? How many types of literals are allowed in Python?
8) How many ways are there in Python to represent an integer literal?
9) What are data types? What are Python's built-in core data types?
10) What is a statement ? What is the significance of an empty statement?
11) If you are asked to label the Python loops as determinable or non-determinable, which label
would you give to which loop? Justify your answer.
12) There are two types of else clauses in Python. What are these two types of else clauses?
13) What do you understand by the term Iteration?
14) What is indexing in context to Python strings? Why is it also called two-way indexing?
15) What is a string slice? How is it useful?
16) Write a program that reads a string and checks whether it is a palindrome string or not.
17) How are lists different from strings when both are sequences?
18) How are tuples different from lists when both are sequences?
19) When are dictionaries more useful than lists
20) Can sequence operations such as slicing and concatenation be applied to dictionaries? Why?

Answers

1) Python is interpreted because the program is processed at runtime by the


interpreter and we do not need to compile the program before executing it.
2) Python was developed by Guido van Rossum in the early nineties at the
National Research Institute for Mathematics in the Netherlands
3) The normal execution of Python program is interpreted. However, subsets
of the language can be compiled.
4) In interactive mode, instructions are given in front of Python prompt (>>>)
in Python Shell, Python carries out the given instructions and shows the
result there itself.

In script mode, Python instructions are stored in a file, generally with.py


extension, and executed together in one go as a unit. The saved
instructions are known as Python script or Python program.
5) keywords have special meaning in python while identifier are define by
user.
6) Every variable in Python holds an instance of an object. There are two types
of objects in Python, i.e.,
Mutable and Immutable objects. Whenever an object is instantiated, it is
assigned a unique object id. The type of the object is defined at the
runtime and it can't be changed afterwards.

However, its state can be changed if it is a mutable object.

For example, int, float, bool, string, unicode, tuple are immutable objects in
Python. In simple words, an immutable object can't be changed after it is
created. Lists, dictionaries and sets are mutable types.
7) Literals mean constants i.e., the data items that never change their value
during a program run. Python allows five types of literals:

(i) String literals


(ii) Numeric literals
(iii) Boolean literals
(iv) Special Literal None
(v) Literal Collections like tuples, lists etc.
8) Python allows three types of integer literals:

(a) Decimal (base 10) integer literals


(b) Octal (base 8) integer literals
(c) Hexadecimal (base 16) integer literals
(a) Decimal Integer Literals:- An integer literal consisting of a sequence of
digits is taken to be decimal integer literal unless it begins with 0 (digit
zero).
For instance, 1234, 41, +97, -17 are decimal integer literals.

(b) Octal Integer Literals:- A sequence of digits starting with 0 (digit zero)
is taken to be an octal integer.
For instance, decimal integer 8 will be written as 010 is octal integer. (810 =
108,) and decimal integer 12 will be written as 014 as octal integer (1210 =
148).

(c) Hexadecimal Integer Literals:- A sequence of digits preceded by 0x or


0X is taken to be an hexadecimal integer.
For instance, decimal 12 will be written as 0XC as hexadecimal integer.
Thus number 12 will be written either as 12 (as decimal), 014 (as octal) and
0XC (as hexadecimal).
9) The real life data is of many types. So to represent various types of real-life
data, programming languages provide ways and facilities to handle these,
which are known as data types.

Python's built-in core data types belong To:

(i) Numbers (integer, floating-point, complex numbers, Boolean's)


(ii) String
(iii) List
(iv) Tuple
(v) Dictionary
10) A statement is an instruction given to the computer to perform any kind of
action.

An empty statement is useful in situations where the code requires a


statement but logic does not. To fill these two requirements
simultaneously, empty statement is used.
Python offers pass statement as an empty statement.
11) The 'for loop' can be labelled as determinable loop as number of its
iterations can be determined beforehand as the size of the sequence, it is
operating upon.

The 'while loop' can be labelled as non-determinable loop, as its number


of iterations cannot be determined beforehand. Its iterations depend upon
the result of a test-condition, which cannot be determined beforehand.
12) The two types of Python else clauses are:

(a) else in an if statement :- The else clause of an if statement is executed


when the condition of the if statement results into false.

(b) else in a loop statement :- The else clause of a loop is executed when
the loop is terminating normally i.e., when its test-condition has gone false
for a while loop or when the for loop has executed for the last value in
sequence.
13) Iteration refers to repetition of a set of statements for a sequence of values
or as long as a condition is true.
14) In Python strings, each individual character is given a location number,
called index and this process is called indexing. Python allocates indices in
two directions:

* in forward direction, the indexes are numbered as 0, 1, 2 ... length-1.


* in backward direction, the indexes are numbered as -1, -2, -3.... length.

This is known as two-way indexing.


15) A sub-part or a slice of a string, say s, can be obtained using s [n: m]
where n and m are integers.

Python returns all the characters at indices n, n+1, n + 2 ... m - 1

For example :-
>>> 'Path Walla' [1:4]
>>> 'ath'
16)

string = input("Enter a string :")


length = len(string)
mid = int(length / 2)
rev = -1
for a in range (mid) :
if string[a] == string[rev] :
a += 1
rev -= 1
else :
print(string, "is not a palindrome")
break
else :
print(string, "is a palindrome
17) The lists and strings are different in following ways:

(i) The lists are mutable sequences while strings are immutable.
(ii) In consecutive locations, a string stores the individual characters while
a list stores the references of its elements.
(iii) Strings store single type of elements - all characters while lists can
store elements belonging to different types.
18) The tuples and lists are different in following ways:

• The tuples are immutable sequences while lists are mutable.


• Lists can grow or shrink while tuples cannot.
19) Dictionaries can be much more useful than lists. For example, suppose we
wanted to store all our friends' cell-phone numbers. We could create a list
of pairs (name of friend, phone number), but once this list becomes long
enough searching this list for a specific phone number will get time-
consuming. Better would be if we could index the list by our friend's name.
This is precisely what a dictionary does.
20) No, sequence operations like slicing and concatenation cannot be applied
on dictionaries. The reason being, a dictionary is not a sequence. Because
it is not maintained in any specific order, operations that depend on a
specific order cannot be used.

You might also like