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

Python Unit 2 (2marks)

Uploaded by

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

Python Unit 2 (2marks)

Uploaded by

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

124

Problem Solving and Python Programming


Enter choicc(1/2/3/4):1
Enter first number: 45
Fnter second number: 7
45 + 7 =52

QUESTIONS WITH
ANNA UNIVERSITY 2 MARK
ANSWERS

1. What is slicing operator in python? Give an example.


(AU Nov/ Dec 2022, R2021)

The slice operator selects a range of elements.


>>>'a','e', 'i','o','u')
>>>[1:3]
('e', i)
this statements using
2. ython variables do not have snecific types. Justify
(AUNov/ Dec 2022, R2021)
example.
Cxplicitly define
Python is a type-inferred language, so we don't have to
declare variables before using them
the variable type. That is. we do not need to
declare their type. A variable is created the moment we first assign a value to
or
A Python variable is a name given to amemory location. It is the basic unit of
I1.
storage in a program.

Example:
>>>n=10
>>>name-Jovita"
>>>P[=3.14

3. List any four built in data types in Python. (AUApr/ May 2022, R2021)
Point type, and
The built in data types are, Numbers (Integer type, Floating
Complex type), String, List, Tuple, Set, Boolean and None types.
2022, R2021)
4. How do you assign a value to a tuple in Python? (AUApr/ May
Data Types, Expressions, Satements 125

Tuple assignment is an ass1gnment with a sequence on the right side and a


of variables on the left. The right side is tuple
evaluated and then its elements are
assigned to the variables on the left.
>>>T1,T2=T2,T1 #swap TI and T2
6. Are
comments executable statements in a Python program? How
comments are included in a Python Program? (AU January 2022, R2021)
1 Comment in Pythoi statements are
non-executable statements so they are
ignored during program execution.
2. In Python, a comment starts with #
(hash sign). Everything following the #
till the cnd of that line is treated as a
comment and the interpreter simply
ignores it while executing the statement. They have no effect
results. on the program

6 Are comments executable statements in a Python program? How


are included in a python program? comments
(AU January 2022, R2021)
Refer Page No.: 112

1. ldentify the operand (s) and operator (s) in the


sum = a + b.
following expression:
(AU January 2022, R202 1)
sum, a, b are operands
-,+ are the operators

8. Compare interpreter and compiler. What type of Translator is


used for
Python? (AUNov/Dec 2019, R2017)
Python uses interpreter as a translator.
SI.
no Compiler Interpreter

1.
The compiler takes a program as a Interpreter translates program
whole and translates it. statement by statement.

2 Intermediate code or target code is Interpreter doesn't create intermediate


generated in case of a compiler. code.
Python Programming
126 Problem Solving and

Acompiler is comparatively faster


than Interpreters compile each line of code
Interpreter as the compiler take the after the other.
whole program at one go.
the When an error takes place in the
In compiler when an error occurs in
interpreter, it prevents its translatio.
Program, it stops its translation and
program is and after removing the
4.
after removing eror whole
translation resumes.
error.
translated again
In acompiler. the process requires two Interpreter is a one step process in
steps in which firstly source code is which Source code is compiled and
translated to target program then executed at the same time.
executed
in languages
6. The compiler is used in programming Interpreter is employed
languages like C, C++. C#, Scala, etc. like PHP, Ruby, Python, etc.

9. What are Keywords? Give Examples. (AUDec/Jan 2019, R2017)

Keywords are reserved words that have predefined meanings in python. They
cannot be used as ordinary identifiers and must be spelled exactly as they are
written python3 has 33 keywords.
Keywords of Python
False class finally return

None continue for lambda try


True def from noniocal while
and delglobal not with
elif if yield
assort etse import pass
break except raise

10. State the reasons to divide programs into functions.


(AUDec/Jan 2019, R2017)
The length of the source program can be reduced by dividing it into smaller
functions.
By using functions it is very casy to locate and debug an error.
The user-defined function can be used in many other source programs
wheneveT necessary.
Functions avoid coding of repcated programming of the similar instructions.
DataTypes, Expressions, Statements 127

11. Name the four types of scalar objects Python has.


(AUJan 2018, R2017)
The commonly used scalar types in Python are:
Integers (type int), tloating point numbers (type float),
strings (type str).
Booleans (type bool) and lists are a set of scalar objects in Python.
12. What is a Tuple? How literals of type
tuple are written? Give example.
(AU Jan 2018, R2017)
Atuple may be defined as a finite, static list
of numbers or string. It
immutable sequence of values separated by commas. The values can becontains of any
type, and they are indexed by integers. Literals of type tuple are
parentheses ().
enclosed within

Example:

>>>t=(*a', 'b', 'c', 'd', 'e')


>>>max(5,8,9)

13. What is interpreter mode in Python?

The interpreter mode is the mode where the scripted and finished -py files are
run in the Python interpreter.

14. What is interactive mode in Python?

The interactive mode is a command line shell which gives immediate feedback
for each statement, fed statements in active memory. As new lines are fed into
the interpreter. the fed program is evaluated both in part and in whole.
15. Name five primitive data types in Python.

Five primitive data types in Python are: Numbers, String, List, Tuple and
Dictionary.
l6. What does immutable' mean: which data type in python are immutable?
Progranmming
128 Problem Solving and Python said to be
in place is
An obiect whose state or value cannot be changed one that cannot
Sequence is
immutable type. So, a sequence that is immutabl
change. For example. Strings and Tuples ae immutable.
escape sequence in Python.
17. What are escape sequences? Write any four
consists of backslash
followed
Escape sequences are non printable characters. It
by acharacter both are enclosed within single quotes.
Backslash(\)
Single quote(')
Double quote(")
\a ASCII Bell (BEL)
18. Define Value.

A value is the basic units of data. like a number or a string tnat a program
manipulates.

19. What are the types used in Python?


1ypes is acategory of values. Integers (type int), floating point numbers (type
tloat), strings (type str), Booleans (type bool) and lists are a set of predefined
data types in python. They are called as built-in-types.
20. What is boolean type in Python?

ABoolean type represents special values True and False. The most common
way to produce a Boolean value iswith a relational operator
21. How can you create a complex literal in Python?
Complex literals can be created by using the notationx + yj where x is the real
component and y is the imaginary component. For example, >>> 1j * 1jwhich
produces the result as: (-1+0j).
22. What is a variable?
Avariable is an identifier, which holds a value. In programming, we assign a
Value to a variable. Technically, a varjable is a reference to a computer
memory, where the value is stored.
Data Types, Expressions, Statements 129

23. List out the rules to be followed in naming


variable.
Variable names can be at any length.
They can contain both letters and
number. numbers, but they can't begin with a
Both uppercase letters and lower case
letters can be used.
The underscore () character can
appear in a name. It is often used in
with multiple words. names
A variable name cannot be any
one of the keywords.
24. What isexpression?
An expression is a combination of values, variables, and
itself is considered as an expression. operators. A value by
Expressions, most commonly, consist of a
combination of operators and operands.
Example: 5 + (8 * k)
25. Give example for simultaneous
assignment statements.
Simultaneous Assignment Statement
Python permits any number of variables to appear on the left side
commas. The same number of expressions must then appear on separated by
the right side,
again separated by commas.
Example:
>>>x=]10
>>>y=5
>>>sum,dif,prod=xyx-yx
>>>sum
15
>>>diff
5
>>>pr
50
26. What is a tuple? Give example.
A tuple is a immutable sequence of values. They are comma separated list
values. The value can be any type. and they are indexed by integer.
130 Problem Solving and Python Programming
Example:

>>t='a', 'b', 'c', 'd','e'


>>>=('a', 'b', 'c', d', 'e')
27. How to create a tuple?
Creating a tuple:
comma is to be included.
i) To create atuple with a single element, final
>>>t| = 'a',
built-in function tuple. With
l1) Another way to create a tuple is the no

argument,it creates empty tuple:


>>>t= tuple()
>>>t

i) If the argument is a sequence (string, list or tuple), the result is a tuple wth
the elements of the sequence:
>>>t=tuple("Jovita')

(J,o, 'v', i", t, 'a')


28. How can we declare variables in Python?
Python variables do not have to be explicitly declared to reserve memory space.
The declaration happens automatically when you assign a value to avariable.
The equal sign (=) is used to assign values to variables.
29. How bracket operator used in tuple?
The bracket operator indexes an element.

>>>(a',e','",o,'u')
'a'

>>>|3]
Data Types, Expressions, Statements 131

Ine slice operator selects a range of elements.

>>>[1:3]
('e', i)
30. Give the precedence of operators in python.
Parentheses have the highest
precedence and can be used to force an
expression to evaluate in the order.
Example: 5*(9 - 3) = 30 since
expressions in
first. parentheses are evaluated
" Exponentiation has the next highest precedence.
Example: I +2**3 =9, not 27 and
2 * 3**2 =18, not 36.

Multiplication and Division have higher precedence than Addition and


Subtraction.
Example: 2*3-1 5, not 4and
6-42 =8, not 5

ANNA UNIVERSITY 16 MARK QUESTIONS WITH


ANSWERS

1. Write a program to find the roots of a quadratic equation gives the


coefficients a.
b, c. (AU Nov/Dec 2022, R2021)
Refer Page No.:206
2. Describe the shift and logical operators used in python with example.
(AU Nov/Dec 2022, R2021)
Refer Page No.:139

3. Elaborate the membership, identityand bitwise operators of python with suitabl


examples. (AU Nov/Dec 2022, R2021
Refer Page No.: 141

4. Write a program to print the d1git at one's place of a number.


(AUNov/Dec 2022, R202

You might also like