Chereads / PYTHON / Chapter 6 - 3.

Chapter 6 - 3.

(2 marks)

1. Define the identity Operator python with example

2. Describe different modes of python

3. State basic list operation in python with example

(4 marks)

4. Write program to calulate sum of n natural number using while loops

5. What is python path, how to add pyton in windows

6.Wrote simple program display welcome message

7. List operators in details

8. explain set and how to access, update, delete value in sets

9. Define dictionary, how to create dictionary write any program of dictionary

1.Define the identity Operator python with example

-operators compare the memory locations of objects

-There are two Identity operators as below

-is :

Evaluates to true if the variables on either side of the operator point to the same object and false otherwise

Eg:

x=5

If(type(x) is int):

print("true")

Else:

print("false")

Output: true

is not:

Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.

Eg:

X=5.2

if (type(x) is not int):

print("true")

else:

print("false")

Output : true

2. Describe different modes of python

-

Interactive Mode

Batch Mode

Script mode

Interactive Mode :

Interactive mode is a command line shell.

-mode allows us to write codes in Python command prompt (>>>)

-mode is used to test the features of the python, or to run a smaller script that may not be reusable.

Batch Mode :

Batch mode is mainly used to develop business applications

Script mode:

script mode programs can be written and stored as separate file with the extension .py and executed.

-Script mode is used to create and edit python source

3. State basic list operation in python with example

1.append()

The append() method is used to add elements at the end of the list.

-This method can only add a single element at a time.

2. extend()

The extend() method is used to add more than one element at the end of the list.

3. insert()

The insert() method can add an element at a given position in the list

-This method takes two arguments. The first argument specifies the position, and the second argument specifies the element to be inserted.

4. remove()

The remove() method is used to remove an element from the list.

5. pop()

The method pop() can remove an element from any position in the list.

6. slice

The slice operation is used to print a section of the list.

7. reverse()

The reverse() operation is used to reverse the elements of the list.

8. len()

The len() method returns the length of the list,

9. min() & max()

-min() method returns the minimum value in the list.

-max() method returns the maximum value in the list.

10. count()

The function count() returns the number of occurrences of a given element in the list.

11. concatenate

The concatenate operation is used to merge two lists and return a single list.

12. multiply

Python also allows multiplying the list n times.

13. index()

The index() method returns the position of the first occurrence of the given element.

14. sort()

The sort method sorts the list in ascending order.

Code:

15. clear()

This function erases all the elements from the list

4. Write program to calulate sum of n natural number using while loops

-

Source Code

num = 16

if num < 0:

print("Enter a positive number")

else:

sum = 0

while(num > 0):

sum += num

num -= 1

print("The sum is", sum)

Output

The sum is 136

5. What is python path, how to add pyton in windows

-

1.

In the Windows search bar, type in python.exe, but don't click on it in the menu. Instead, right-click on it, and select Open file location

2

Find where Python is installed

3

A window will open up with some files and folders: this should be where Python is installed. Right-click on the address bar at the top and select Copy address as text.

4

Copy Python location

5

From the main Windows menu, open the Control Panel:

6

Open control panel

7

In the search box on the top right, type in environment, and in the search results, click on Edit environment variables for your account

8

If there is already a variable listed called Path, click on it click next

9

If the the command prompt window is still open, close it and re-open it.

10

Once again, type in python at the command prompt and press the Enter key. You should now see Python start successfully.

6. Wrote simple program display welcome message

Print ("welcome")

Output: welcome

7.List operators in details

-

Arithmetic operators

Relational

Assign

Logical

Membership

Identity

Bitwise

Arithmetic Operators:

arithmetic operators for different mathematical operations. They are:

+ (Addition)

– (Subtraction)

* (Multiplication)

/ (Division)

** (Exponentiation)

// (Floor division)

% (Modulus)

Relational Operators:

They are also called comparison operators and they compare values.

> (Greater than)

< (Less than)

== (Equal to)

!= (Not equal to)

>= (Greater than or equal to)

<= (Less than or equal to)

Assignment Operators:

They perform an operation and assign a value.

= (Assign)

+= (Add and assign)

-= (Subtract and assign)

*= (Multiply and assign)

/= (Divide and assign)

%= (Modulus and assign)

**= (Exponentiation and assign)

//= (Floor-divide and assign

Logical Operators:

They can combine conditions.

and (Logical and)

or (Logical or)

not (Logical not)

Membership Operators:

check whether a value is in another. Python has 2 membership operators:

in

not in

Identity Operators:

check whether two values are identical. Python has 2 identity operators as well:

is

is not

a. is operator

The is operator returns True if the first value is the same as the second. Otherwise, it returns False.

Bitwise Operators:

They operate on values bit by bit.

& (Bitwise and)

(Bitwise or)

^ (Bitwise xor)

~ (Bitwise 1's complement)

<< (Bitwise left-shift)

>> (Bitwise right-shift)

a. Bitwise and

8. explain set and how to access, update, delete value in sets

Set:Sets are used to store multiple items in a single variable.

Delete

#removing 'German' from language

language.remove('German')

Update

# Updated language set

print('Updated language set:', language)

9. Define dictionary, how to create dictionary write any program of dictionary

Dictionary

unordered and mutable Python container that stores mappings of unique keys to values.

create dictionary

placing a sequence of elements within curly {} braces, separated by 'comma

# set of mixed datatypes

my_set = {1.0, "Hello", (1, 2, 3)}

print(my_set)

Output

{1, 2, 3}

{1.0, (1, 2, 3), 'Hello'}