Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
163 views

Python QA

Tokens are the smallest units in Python like keywords, identifiers, literals, operators, and punctuators. Indentation is important in Python for code blocks. Comments are used to explain code. Python allows dynamic typing where a variable can change types. Errors may occur from undefined variables, incorrect operators, invalid syntax like using ; instead of ,, or using keywords as variables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views

Python QA

Tokens are the smallest units in Python like keywords, identifiers, literals, operators, and punctuators. Indentation is important in Python for code blocks. Comments are used to explain code. Python allows dynamic typing where a variable can change types. Errors may occur from undefined variables, incorrect operators, invalid syntax like using ; instead of ,, or using keywords as variables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Fundamentals

1. What are tokens in python? How many types of tokens are allowed in python?
Exemplify your answer.

Tokens are smallest individual unit in a program.

Types of tokens,

a. Keywords: False, True, for, while


b. Identifiers: a, A, lst, dic (Name of the user defined variables)
c. Literals: “python”, 5,9,’program’
d. Operator: +, -, /, *, **, %, //
e. Punctuators: &, ^,?,#,@,!
2. What would the following code do:

X=Y=7?

X is a variable and their value is 7 and Y is also variable and their value is 7.

3. What is the error in following code:


X,Y=7?
In variable we cannot use special symbols, but here comma is a special symbol that’s
why code creates error.
4. Following variable definition is creating problem X=0281, find reasons.
It is creating problem because decimal number/ decimal literal cannot be start with
zero in python.
5. Comments are useful and easy way to enhance readability and understandability of a
program”. Elaborate with example.
Comments are generally ignored by compilers and interpreters and they are added
with the purpose of making the source code easier for human to understand.
6. Which of these is not a legal numeric type in python? a) int b)float c) decimal.

c) decimal – is not a legal numeric type in python.

7. What is the role of indentation in python.

Indentation is one of the most important features of python, indentation is use to mark
blocks of code. It is required for indicating what block of code a statement belongs to.

8. Can nongraphic characters be used in python? How? Give examples to support your
answer.

Yes, non graphic character can be used in python. in python we can use by type in
side of double quote or single quote. Eg. print(“\\”)

9. Which argument of print() would you set for:


a. Changing the default separator(space)?
b. Printing the following line in current line?

Ans:

a. By using sep argument of print()


b. By using end argument of print()
10. What is dynamic typing feature of python?
In python, can make a variable point to a value of different type by reassigning it to a
value of that type, this is called dynamic typing.
Eg:
Var1=23
Var1 =’Mercedes’ # Reassignment to a different variable type.
11.  What will be the output of following code ?
p=3j
q=p+(1+1.5j)
print(p)
print(q)

Output:
3j
1+4.5j

12. Find the errors in the following code fragment

a) y=x+5
print(x,y)
# x is not defined
b) print(x = y = 5)
# Variables are being assigned at the same time as being printed, this is invalid.
c) a= input(“value”)
b=a/2
print(a,b)
# a is a string type because all inputs are taken as string in python and hence a
can’t be divided.

13. From the following, find out which assignment statement will produce an error. State
reason(s) too.

The statements which will produce an error are:


y = 037 # Decimal Integer literals can’t start with a 0
z= 0o98 # Digit 9 can’t be used in octal system
56thnumber = 3300 # variable name can’t start with a digit
!Taylor = ‘Instant # variable name can’t start with a !
this variable = 87.E02 # variable name can’t have a space ( ‘ ‘ ) in between
float = .17E-03 # float is a keyword

14. Find out the error(s) in following code fragments:


i) temperature = 90
print temperature # print() is a function and parentheses must be used in python3

ii) a = 30
b = a + b # b is not defined
print(a And b) # And should be written as and
iii) a, b, c = 2, 8, 9
print(a, b, c)
c, b, a = a, b, c
print(a;b;c) # ',' should be used instead of ';'
iv) X = 24
4 = X # Incorrect assignment, Variables are Rvalues
v) print("X="X) # A ',' should be used between the string and variable
vi) else = 21-5 # else is a keyword and can't be used a variable

15. What will be returned by Python as result of following statements?


a)
>>>type(0)
# output
int

b)
>>>type(int(0))
# output
int

c)
>>>type(int('0'))
# output
int

d)
>>>type('0')
# output
str

e)
>>>type(1.0)
# output
float

f)
>>>type(int(1.0))
# output
int

g)
>>>type(float(0))
# output
float

h)
>>>type(float(1.0))
# output
float

i)
>>>type(3/2)
# output
float

16. Find out the error(s) in following code fragments :


17. i)
18. temperature = 90
19. print temperature # print() is a function and parentheses must be used in
python3
20. ii)
21. a = 30
22. b = a + b # b is not defined
23. print(a And b) # And should be written as and
24. iii)
25. a, b, c = 2, 8, 9
26. print(a, b, c)
27. c, b, a = a, b, c
28. print(a;b;c) # ',' should be used instead of ';'
29. iv)
30. X = 24
31. 4 = X # Incorrect assignment, Variables are Rvalues
32. v)
33. print("X="X) # A ',' should be used between the string and variable
34. vi)
35. else = 21-5 # else is a keyword and can't be used a variable

You might also like