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

Selfstudys Com File

The document is a Python revision guide covering various topics such as string manipulation, indexing, keywords, identifiers, literals, escape sequences, and operators. It includes questions and answers that explain concepts like negative indexing, tokens, and handling undefined variables. Additionally, it provides examples and corrections for common coding errors in Python.

Uploaded by

Gourav Kundral
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Selfstudys Com File

The document is a Python revision guide covering various topics such as string manipulation, indexing, keywords, identifiers, literals, escape sequences, and operators. It includes questions and answers that explain concepts like negative indexing, tokens, and handling undefined variables. Additionally, it provides examples and corrections for common coding errors in Python.

Uploaded by

Gourav Kundral
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PYTHON REVISION TOUR - I

1 How will you convert a string to all lowercase?

Ans: To convert a string to lowercase, lower() function can be used.

Example:

1 stg='ABCD'
2 print(stg.lower())
Output: abcd

2 What are negative indexes and why are they used?

Ans: The sequences in Python are indexed and it consists of the positive as well as
negative numbers. The numbers that are positive uses ‘0’ that is uses as first index
and ‘1’ as the second index and the process goes on like that.

The index for the negative number starts from ‘-1’ that represents the last index in
the sequence and ‘-2’ as the penultimate index and the sequence carries forward
like the positive number.

The negative index is used to remove any new-line spaces from the string and allow
the string to except the last character that is given as S[:-1]. The negative index is
also used to show the index to represent the string in correct order.

3 Special meaning words of Pythons, fixed for specific functionality are called ..........
.

1. Identifiers
2. functions
3. Keywords ✓
4. literals

4 Names given to different parts of a Python program are .......... .

1. Identifiers ✓
2. functions
3. Keywords
4. literals
5 Data items having fixed value are called .......... .

1. Identifiers
2. functions
3. Keywords
4. literals ✓

6 Which of the following is/are correct ways of creating strings ?

1. name = Jiya
2. name = 'Jiya' ✓
3. name = "Jiya" ✓
4. name = (Jiya)

7 Which of the following are valid identifiers ?

1. my name
2. _myname ✓
3. myname ✓
4. my-name

8 Which of the following are literals ?

1. myname
2. "Radha" ✓
3. 24.5 ✓
4. 24A

9 Escape sequences are treated as .......... .

1. strings
2. characters ✓
3. integers
4. none of these

10 Value 17.25 is equivalent to

1. 0.1725E-2
2. 0.1725E+2 ✓
3. 1725E2
4. 0.1725E2 ✓

12 Select the reserved keyword in Python.

1. else
2. import
3. print
4. all of these ✓

13 To convert the read value through input( ) into integer type, ..........( ) is used.

1. floating
2. float
3. int ✓
4. integer

14 What are tokens in Python ? How many types of tokens are allowed in Python ?
Examplify your answer.

Answer
The smallest individual unit in a program is known as a Token. Python has
following tokens:

1. Keywords — Examples are import, for, in, while, etc.


2. Identifiers — Examples are MyFile, _DS, DATE_9_7_77, etc.
3. Literals — Examples are "abc", 5, 28.5, etc.
4. Operators — Examples are +, -, >, or, etc.
5. Punctuators — ' " # () etc.

15 Can nongraphic characters be used in Python ? How ? Give examples to support


your answer.

Answer
Yes, nongraphic characters can be used in Python with the help of escape
sequences. For example, backspace is represented as \b, tab is represented as \t,
carriage return is represented as \r.

16 Which of these is not a legal numeric type in Python ? (a) int (b) float (c) decimal.

Answer
decimal is not a legal numeric type in Python.

17 What are operators ? What is their function ? Give examples of some unary and
binary operators.

Answer
Operators are tokens that trigger some computation/action when applied to
variables and other objects in an expression. Unary plus (+), Unary minus (-),
Bitwise complement (~), Logical negation (not) are a few examples of unary
operators. Examples of binary operators are Addition (+), Subtraction (-),
Multiplication (*), Division (/).
18 What do you understand by undefined variable in Python ?

Answer
In Python, a variable is not created until some value is assigned to it. A variable is
created when a value is assigned to it for the first time. If we try to use a variable
before assigning a value to it then it will result in an undefined variable. For
example:

print(x) #This statement will cause an error for undefined variable x


x = 20
print(x)

The first line of the above code snippet will cause an undefined variable error as
we are trying to use x before assigning a value to it.

19 What is the error in following code : X, Y = 7 ?

Answer

The error in the above code is that we have mentioned two variables X, Y as
Lvalues but only give a single numeric literal 7 as the Rvalue. We need to specify
one more value like this to correct the error:

X, Y = 7, 8

20 Predict the output

a, b, c = 10, 20, 30
p, q, r = c - 5, a + 3, b - 4
print ('a, b, c :', a, b, c, end = '')
print ('p, q, r :', p, q, r)
Output

a, b, c : 10 20 30p, q, r : 25 13 16

21 Find the errors in following code fragment :

c = input( "Enter your class" )


print ("Last year you were in class") c - 1
Answer

There are two errors in this code fragment:

1. c - 1 is outside the parenthesis of print function. It should be specified as


one of the arguments of print function.
2. c is a string as input function returns a string. With c - 1, we are trying to
subtract a integer from a string which is an invalid operation in Python.
The corrected program is like this:

c = int(input( "Enter your class" ))


print ("Last year you were in class", c - 1)

22 Write a program that displays a joke. But display the punchline only when the user
presses enter key.
(Hint. You may use input( ))

print("Why is 6 afraid of 7?")


input("Press Enter")
print("Because 7 8(ate) 9 :-)")

You might also like