You are on page 1of 8

2 marks :

1.Outline the logic to swap the values of two identifies without using third variable ?

Python Progam for swapping: OUTPUT:


Before Swapping x and y: 10 20
x=10 After Swapping x and y: 20 10
y=20
print("Before Swapping x and y:",x,y)
x,y=y,x
print("After Swapping x and y:",x,y)

2.What are keywords?Give examples.

 Keywords are the reserved words in Python.


• They are used defining the syntax and structure of the Python language.
• In Python, keywords are case sensitive.

Examples

• True, False, and, or, not, if, elif, else, for

3.Name four types of scalar objects in Python.

Scalar objects: Scalar objects are indivisible object that do not have internal structure.

The 4 types of scalar objects in Python are:

SCALAR OBJECTS EXAMPLE


bool x=True
y=False
int x=5

float (floating-point number) x=5.5

string name=”python”
name=’python’
4.State about logical operators available in python with examples.

• Logical Operators are used to perform logical operations like and, or and not.

16 marks :

1. Illustrate the various data types in Python with examples.

Data types of Python. They are

• Number (integer,complex number,float)


• Dictionary
• Boolean
• Set
• Sequence Data type. (String ,List , Tuple)

Number
• Numbers are created by numeric literals.
• Numeric objects are immutable, which means when an object is created its value cannot be
changed.
• Python will automatically convert a number from one type to another if it needs.
Python has three distinct numeric types:
1. Integers
• Boolean
2. Floating point numbers
3. Complex numbers.

Integers
• Integers represent negative and positive integers without fractional parts.

>>> type(2)
<class 'int'>

Boolean
• Boolean type is a subtype of plain integers.
• The simplest built-in type in Python is the bool type, it represents the truth
values,(True or False)

>>> type(True)
<class 'bool'>

>>> type(False)
<class 'bool'>

Floating point numbers

• Floating point numbers represents negative and positive numbers with fractional
Parts

>>> type(42.0)
<class 'float'>

Complex numbers

• A complex number is a number of the form A+Bi where i is the imaginary number.
Complex numbers have a real and imaginary part.
• Python supports complex numbers either by specifying the number in (real + imagj)
or (real + imagJ) form or using a built-in method complex(x, y).

>>> type(1+2j)
<class ‘complex’>
String
• A string type object is a sequence of characters.
• Strings start and end with single or double quotes.
• Python strings are immutable ie., once a string is generated, character within the string
cannot be modified.

>>> type('Python language')


31
<class 'str'>

List
• A list is a container which holds comma-separated values (items or elements)
between square brackets where all the items or elements need not to have the same type.
• A list without any element is called an empty list.
• Python lists are mutable.

>>>list1= [ 'computer', 2018 , 8.25, 'python' ]


>>> type(list1)
<class 'list'>

Tuple

• Tuple is an ordered sequence of elements same as list.


• The only difference is that tuples are immutable.
• Tuples once created cannot be modified.

>>>a=(1,'python')
>>>type(a)
<class 'tuple'>

Dictionary
• Dictionary is an unordered collection of items with key: value pairs.
• In a dictionary,elements can be of any type.

>>>d = {1: 'apple', 2: 'ball'}


>>>type(d)
<class 'dict'>
2. Explain in detail about operator precedence and associativity of operator precedence in
Python with relevant examples.

Precedence of Operators in Python: – PEMDAS

What is Operator Precedence???

• Operator Precedence in Python programming is a rule that describe which


operator is solved first in an expression.
• When an expression contains more than one operator, the order of evaluation
depends on the precedence of operators.

Python Operators Precedence Rule – PEMDAS:


• Operator precedence in python follows the PEMDAS rule for arithmetic

expressions.

• Firstly, parantheses will be evaluated, then exponentiation and so on.

◦ P – Parentheses

• Parentheses have the highest precedence and parentheses are evaluated first.
• Examples:
o 2 * (3-1) = 4
o (1+1)**(5-2) = 8.

◦ E – Exponentiation

• Exponentiation(**) has the next highest precedence.


• Example :
o 1 + 2**3 = 9
o 2 *3**2 =18

◦ M – Multiplication and D – Division

• Multiplication and Division have higher precedence than Addition and Subtraction.
• Examples:
o 2*3-1 = 5
o 6+4/2 = 8.

◦ For example, multiplication has higher precedence than subtraction.

◦ A – Addition and S – Subtraction

• Addition and Subtraction have lower precedence than Multiplication and Division

Operator Precedence Table:

• The following table shows the precedence of Python operators.

• The upper operator holds higher precedence than the lower operator.
Example Program for operator precedence:

# Precedence of and & or operators

colour = "red"

quantity = 0

if colour == "red" or colour == "green" and quantity >= 5:

print("Your parcel is dispatched")

else:

print("your parcel cannot be dispatched")

Output:

Your parcel is dispatched

ASSOCIATIVITY IN OPERATOR PRECEDENCE

• When two operators have the same precedence, associativity helps to determine the
order of operations.

• Associativity is the order in which an expression is evaluated, that has multiple

operator of same precedence.

• Almost all the operators have left to right associativity except exponentiation.

Example:

(i)Left to Right Associativity

• Mulitplication and Floor Division share the same precedence.

• Hence if both the operators were present in an expression the left one will be evaluated first.

print(10*2//3)

10*2 will be evaluated first.


ii) Right to Left Associativity

• Exponent operator ** has right-to-left associativity in Python.

You might also like