0% found this document useful (0 votes)
4 views6 pages

Chapter 7

Chapter 7 covers Python fundamentals including the None literal, common errors in code, the difference between keywords and identifiers, and the role of operators and tokens. It explains the importance of indentation, types of strings, and provides examples of syntactically correct strings. Additionally, it includes programming exercises related to temperature conversion, date handling, and area calculations.

Uploaded by

kingsss99909
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Chapter 7

Chapter 7 covers Python fundamentals including the None literal, common errors in code, the difference between keywords and identifiers, and the role of operators and tokens. It explains the importance of indentation, types of strings, and provides examples of syntactically correct strings. Additionally, it includes programming exercises related to temperature conversion, date handling, and area calculations.

Uploaded by

kingsss99909
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Chapter 7: PYTHON FUNDAMENTALS

Q1. What is None literal in Python?


Ans: Python has one special literal, which is None. The None literal is used to indicate
absence of value. It is also used to indicate the end of lists in Python. It means
―There is nothing here‖.
Q2. What is the error in following code: x, y =7 ?
Ans: The following error comes - 'int' object is not iterable. Which means an integer
object i.e. cannot be repeated for x and y. one more integer object is required
after 7.
Q3. What will the following code do: a=b=18 ?
Ans: This code will assign 18 to a and b both.

Q4. Find the error in the following code:

(a) temp=90 (b) a=12 (c) print(“x=”x)


Print temp b = a + b print( a And b)

(d) a, b, c=2, 8, 4 (e) x = 23 (f) else = 21-4


print(a, b, c) 4=x
c, b, a = a, b, c
print(a; b; c)
Ans: (a) Missing parentheses in call to 'print'.
(b) Name ‗b‘ is not defined.
(c) Invalid Syntax.
(d) Invalid Syntax in second print statement.
(e) can't assign to literal in second line.
(f) Invalid Syntax.

Q5. Find the error in the following code:


(a) y = x +5 (b) a=input(“Value: “) (c) print(x = y = 5)
print(x,y) b = a/2 print( a, b)
Ans: (a) Name 'x' is not defined.
(b) Unsupported operand type(s) for /: 'str' and 'int'.
(c) Invalid Syntax.

Q6. What is the difference between a keyword and an identifier?


Ans: Difference between Keyword and Identifier:

IDENTIFIER
KEYWORD

1 Keywords are predefined word that gets Identifiers are the values used to define

Prepared by Pulak Sir………….


IDENTIFIER
KEYWORD

different programming items such as


reserved for working programs that have variables, integers, structures, unions and
special meaning and cannot get used others and mostly have an alphabetic
anywhere else. character.

An identifier can be in upper case or lower


2 A keyword should be in lower case. case.

A keyword contains only alphabetical An identifier can consist of alphabetical


3 characters. characters, digits and underscores.

No punctuation or special symbol except


4 No special symbol, punctuation is used. ‘underscore’ is used.

Examples of keywords are: int, char, if, Examples of identifiers are: Test, count1,
5 while, do, class etc. high_speed, etc.

Q7. What are literals in Python? How many types of Literals allowed in Python?
Ans: Literals: Python comes with some built-in objects. Some are used so often that Python
has a quick way to make these objects, called literals.
The literals include the string, Unicode string, integer, float, long, list, tuple and
dictionary types.
Q8. How many types of sequences are supported in
Python?
Ans: Three Types of Sequences are supported in
python:
(i) String
(ii) List
(iii) Tuple
Q9. What factors guide the choice of identifiers in program?
Ans: (i) An identifier must start with a letter or underscore followed by any number
of digits and/or letters.
(ii) No reserved word or standard identifier should be used.
(iii) No special character (Other than underscore) should be included in the identifier.
Q10. What is the difference between an expression and a statement in Python?
Ans: A statement is an instruction that the Python interpreter can execute. We have only seen
the assignment statement so far. Some other kinds of statements that we‘ll see
shortly are while statements, forstatements, if statements, and import statements.
(There are other kinds too!)

Prepared by Pulak Sir………….


An expression is a combination of values, variables, operators, and calls to functions.
Expressions need to be evaluated. If you ask Python to print an expression, the
interpreter evaluates the expression and displays the result.
Q11. What are tokens in Python? How many types of tokens allowed in
Python?
Ans: Tokens are the smallest unit of the program. There are following tokens
in Python:
 Reserved words or Keywords
 Identifiers
 Literals
 Operators
 Punctuators

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

Ans: “Operators are those symbols used with operands, which tells compiler which operation is
to be done on operands, in other words – ―operators are tokens that
trigger some computation/action when applied to variables and other objects in an
expression.‖
Operators are of following types:
Unary operators like (+) Unary Plus, (-) Unary Minus, not etc.

Binary Operators like (+) addition, (*) multiplication, and etc.

Q13. What is block/code block/suit in Python?


Ans: Sometimes a group of statements is part of another statement of function. Such a
group of one or more statements is called block or code-block or suit in python. e.g.

Q14. What is the role of indentation in Python?


Ans: Indentation plays a very important role in Python. Python uses indentation to create blocks
of code. Statements at same indentation level are part of same block/suit. You cannot
unnecessarily indent a statement; python will raise an error for that.
Q15. How many types of strings are supported
by Python?
Ans: Python supports two types of strings:
(i) Single-line string That terminates in single line.
(ii) Multi-line String That stores multiple lines of text.

Prepared by Pulak Sir………….


Q16. How can you create multi-line strings in Python?
Ans: We can create multi-line string by putting a backslash (\) at the end of line which allows
us to continue typing in next line in same string.

Q17. Which of the following are syntactically correct strings? State reason.
(a) ”Python is nice Language”
(b) „He called me “Friend!” when he came‟
(c) “Very Good‟
(d) „This is a good book‟
(e) “Namaste
(f) “I liked „Harry Potter‟ very much”
Ans: (a) Correct (b) Incorrect (c) Correct (d) Correct (e) Incorrect (f) Correct

Q18. What is the error in following Python program with one statement?
print(“My name is : “, name)

suggest a solution

Ans: Error is : ―name 'name' is not defined‖. And the solution is to declare the variable-name
before this statement.

Q19. Predict the output of the following:

Ans: Output: 17 5

Q20. What will be the output of the following code:

Ans: Output: Hari , you are 18 now but You will be 19 next year

Prepared by Pulak Sir………….


Q21. Write a Program to obtain temperature in Celsius and convert it into Fahrenheit
using formula – C X 9/5 + 32 = F

Ans:

Q22. WAP to read todays date (only date Part) from user. Then display how many days
are left in the current month.
Ans:

Q23. WAP to print the area of circle when radius of the circle is given by
user.
Ans:

Q24. WAP to print the volume of a cylinder when radius and height of the cylinder is
given by user.
Ans:

Q25. WAP that asks your height in centimeters and converts it into foot and
inches.

Ans:

Q26. WAP to find area of a triangle.

Prepared by Pulak Sir………….


Ans:

Q27. WAP to calculate simple interest.


Ans:

Q27. WAP to read a number in n and prints n2, n3, n4


Ans:

Q28. Predict output:

Ans: Output: 4 6 8

Prepared by Pulak Sir………….

You might also like